Browse Source

Worked at audio firmware

Thomas Buck 12 years ago
parent
commit
2f76d52359
6 changed files with 222 additions and 7 deletions
  1. 60
    0
      AudioFirmware/adc.c
  2. 28
    0
      AudioFirmware/adc.h
  3. 66
    0
      AudioFirmware/eq.c
  4. 28
    0
      AudioFirmware/eq.h
  5. 37
    4
      AudioFirmware/main.c
  6. 3
    3
      AudioFirmware/makefile

+ 60
- 0
AudioFirmware/adc.c View File

@@ -0,0 +1,60 @@
1
+/*
2
+ * adc.c
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+#include <avr/io.h>
24
+#include <stdint.h>
25
+
26
+#include "adc.h"
27
+
28
+void adcInit(void) {
29
+	uint16_t tmp;
30
+	
31
+	DDRC &= ~(3);
32
+	ADMUX = 0;
33
+	ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
34
+	ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
35
+	ADCSRA |= (1 << ADEN); // Enable adc
36
+	adcStartConversion(0);
37
+	while (adcIsFinished() == 0);
38
+	tmp = ADCW;
39
+}
40
+
41
+void adcStartConversion(uint8_t channel) {
42
+	ADMUX &= 0xF0; // Clear channel selection bits
43
+	ADMUX |= (channel & 0x0F); // Set channel
44
+	ADCSRA |= (1 << ADSC); // start conversion
45
+}
46
+
47
+uint8_t adcIsFinished(void) {
48
+	return (ADCSRA & (1 << ADSC)); // Return start bit
49
+}
50
+
51
+uint16_t adcGetResult(void) {
52
+	while (adcIsFinished() == 0);
53
+	return ADCW;
54
+}
55
+
56
+uint8_t adcGetByte(void) {
57
+	uint16_t tmp = adcGetResult();
58
+	tmp = tmp >> 2;
59
+	return tmp;
60
+}

+ 28
- 0
AudioFirmware/adc.h View File

@@ -0,0 +1,28 @@
1
+/*
2
+ * adc.h
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+
24
+void adcInit(void);
25
+void adcStartConversion(uint8_t channel);
26
+uint8_t adcIsFinished(void);
27
+uint16_t adcGetResult(void);
28
+uint8_t adcGetByte(void);

+ 66
- 0
AudioFirmware/eq.c View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * eq.h
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+#include <avr/io.h>
24
+#include <stdint.h>
25
+#include <stdlib.h>
26
+#include <util/delay.h>
27
+
28
+#include "eq.h"
29
+#include "adc.h"
30
+
31
+#define RESETDELAY 1 /* in µs */
32
+#define RESETSTROBEDELAY 72 /* in µs */
33
+#define STROBEDELAY 18 /* in µs */
34
+#define READDELAY 36 /* in µs */
35
+
36
+#if ((STROBEDELAY * 2) + READDELAY) < 72
37
+#error Strobe to Strobe Delay too short
38
+#endif
39
+
40
+void equalizerInit(void) {
41
+	DDRC |= 12; // Strobe: PC2
42
+				// Reset: PC3
43
+				// Out: ADC0
44
+	PORTC |= (1 << PC3); // Reset enabled
45
+	_delay_us(RESETDELAY);
46
+
47
+}
48
+
49
+uint8_t *equalizerGet(void) {
50
+	uint8_t *result = (uint8_t *)malloc(7); // Has to work... If not, were screwed anyway :)
51
+	uint8_t i;
52
+
53
+	PORTC &= ~(1 << PC3); // Disable reset
54
+	_delay_us(RESETSTROBEDELAY); // Wait trs
55
+
56
+	for (i = 0; i < 7; i++) {
57
+		PORTC |= (1 << PC2); // Strobe '1'
58
+		_delay_us(STROBEDELAY); // create minimal pulse width
59
+		PORTC &= ~(1 << PC2);
60
+		adcStartConversion(0);
61
+		_delay_us(READDELAY); // Wait for result to appear
62
+		result[i] = adcGetByte(); // Blocks for adc
63
+		_delay_us(STROBEDELAY);
64
+	}
65
+	return result;
66
+}

+ 28
- 0
AudioFirmware/eq.h View File

@@ -0,0 +1,28 @@
1
+/*
2
+ * eq.h
3
+ *
4
+ * Copyright 2011 Thomas Buck <xythobuz@me.com>
5
+ * Copyright 2011 Max Nuding <max.nuding@gmail.com>
6
+ * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
7
+ *
8
+ * This file is part of LED-Cube.
9
+ *
10
+ * LED-Cube is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * LED-Cube is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+ * GNU General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with LED-Cube.  If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+
24
+void equalizerInit(void);
25
+
26
+// 7 elements in byte array
27
+// lowest to highest frequency
28
+uint8_t *equalizerGet(void);

+ 37
- 4
AudioFirmware/main.c View File

@@ -25,19 +25,52 @@
25 25
 #include <avr/io.h>
26 26
 #include <util/delay.h>
27 27
 
28
+#include "adc.h"
29
+#include "eq.h"
30
+
28 31
 #ifndef F_CPU
29 32
 #define F_CPU 16000000L
30 33
 #endif
31 34
 
35
+void blinkStatus(void) {
36
+	PORTB |= (1 << PB2);
37
+	_delay_ms(250);
38
+	PORTB &= ~(1 << PB2);
39
+	_delay_ms(250);
40
+}
41
+
32 42
 int main(void) {
43
+	uint8_t *music;
33 44
 	uint8_t i;
45
+	uint16_t val;
46
+
47
+	DDRB = 0x3F;
48
+	DDRC = 0x0C;
49
+	DDRD = 0xFF;
50
+	
51
+	adcInit();
52
+	// equalizerInit();
53
+
54
+	blinkStatus();
55
+	blinkStatus();
34 56
 
35 57
 	// Blink led :)
36 58
 	while (1) {
37
-		PORTB |= (1 << PB2);
38
-		_delay_ms(1000);
39
-		PORTB &= ~(1 << PB2);
40
-		_delay_ms(1000);
59
+		/* music = equalizerGet();
60
+		val = 0;
61
+		for (i = 0; i < 7; i++) {
62
+			val += music[i];
63
+		}
64
+		val /= 7; */
65
+
66
+		adcStartConversion(0x01); // 0x0E -> 1,3 V Ref.
67
+		val = adcGetResult();
68
+
69
+		if (val >= 512) {
70
+			PORTB |= (1 << PB2);
71
+		} else {
72
+			PORTB &= ~(1 << PB2);
73
+		}
41 74
 	}
42 75
 
43 76
 	return 0;

+ 3
- 3
AudioFirmware/makefile View File

@@ -36,7 +36,8 @@ TARGET = main
36 36
 
37 37
 # List C source files here. (C dependencies are automatically generated.)
38 38
 SRC = $(TARGET).c
39
-#SRC += uart.c # Additional Source-File
39
+SRC += adc.c # Additional Source-File
40
+SRC += eq.c
40 41
 
41 42
 # List Assembler source files here.
42 43
 # Make them always end in a capital .S.  Files ending in a lowercase .s
@@ -256,7 +257,7 @@ ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
256 257
 
257 258
 
258 259
 # Default target.
259
-all: begin gccversion sizebefore build sizeafter finished end
260
+all: begin gccversion sizebefore build sizeafter finished end clean
260 261
 
261 262
 build: elf hex eep lss sym
262 263
 
@@ -389,7 +390,6 @@ clean: begin clean_list finished end
389 390
 clean_list :
390 391
 	@echo
391 392
 	@echo $(MSG_CLEANING)
392
-	$(REMOVE) $(TARGET).hex
393 393
 	$(REMOVE) $(TARGET).eep
394 394
 	$(REMOVE) $(TARGET).obj
395 395
 	$(REMOVE) $(TARGET).cof

Loading…
Cancel
Save