Browse Source

Audio detection working

Thomas Buck 12 years ago
parent
commit
baf026dbd2
6 changed files with 79 additions and 30 deletions
  1. 8
    5
      AudioFirmware/adc.c
  2. 2
    2
      AudioFirmware/eq.c
  3. 41
    20
      AudioFirmware/main.c
  4. 2
    2
      AudioFirmware/makefile
  5. 26
    1
      Hardware/LED Cube.sch
  6. BIN
      Hardware/Schematic.png

+ 8
- 5
AudioFirmware/adc.c View File

26
 #include "adc.h"
26
 #include "adc.h"
27
 
27
 
28
 void adcInit(void) {
28
 void adcInit(void) {
29
-	uint16_t tmp;
30
-	
31
 	DDRC &= ~(3);
29
 	DDRC &= ~(3);
32
 	ADMUX = 0;
30
 	ADMUX = 0;
33
 	ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
31
 	ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
34
 	ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
32
 	ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
35
 	ADCSRA |= (1 << ADEN); // Enable adc
33
 	ADCSRA |= (1 << ADEN); // Enable adc
36
 	adcStartConversion(0);
34
 	adcStartConversion(0);
37
-	while (adcIsFinished() == 0);
38
-	tmp = ADCW;
35
+	adcGetResult();
39
 }
36
 }
40
 
37
 
41
 void adcStartConversion(uint8_t channel) {
38
 void adcStartConversion(uint8_t channel) {
45
 }
42
 }
46
 
43
 
47
 uint8_t adcIsFinished(void) {
44
 uint8_t adcIsFinished(void) {
48
-	return (ADCSRA & (1 << ADSC)); // Return start bit
45
+	// Return 1 if ADSC is 0
46
+	if (ADCSRA & (1 << ADSC)) {
47
+		return 0;
48
+	} else {
49
+		return 1;
50
+	}
49
 }
51
 }
50
 
52
 
51
 uint16_t adcGetResult(void) {
53
 uint16_t adcGetResult(void) {
52
 	while (adcIsFinished() == 0);
54
 	while (adcIsFinished() == 0);
55
+	ADCSRA &= ~(1 << ADSC);
53
 	return ADCW;
56
 	return ADCW;
54
 }
57
 }
55
 
58
 

+ 2
- 2
AudioFirmware/eq.c View File

57
 		PORTC |= (1 << PC2); // Strobe '1'
57
 		PORTC |= (1 << PC2); // Strobe '1'
58
 		_delay_us(STROBEDELAY); // create minimal pulse width
58
 		_delay_us(STROBEDELAY); // create minimal pulse width
59
 		PORTC &= ~(1 << PC2);
59
 		PORTC &= ~(1 << PC2);
60
-		adcStartConversion(0);
60
+		adcStartConversion(0x00);
61
 		_delay_us(READDELAY); // Wait for result to appear
61
 		_delay_us(READDELAY); // Wait for result to appear
62
-		result[i] = adcGetByte(); // Blocks for adc
62
+		result[i] = adcGetByte(); // This line hangs
63
 		_delay_us(STROBEDELAY);
63
 		_delay_us(STROBEDELAY);
64
 	}
64
 	}
65
 	return result;
65
 	return result;

+ 41
- 20
AudioFirmware/main.c View File

34
 
34
 
35
 void blinkStatus(void) {
35
 void blinkStatus(void) {
36
 	PORTB |= (1 << PB2);
36
 	PORTB |= (1 << PB2);
37
+	PORTB &= ~(1 << PB1);
37
 	_delay_ms(250);
38
 	_delay_ms(250);
38
 	PORTB &= ~(1 << PB2);
39
 	PORTB &= ~(1 << PB2);
40
+	PORTB |= (1 << PB1);
39
 	_delay_ms(250);
41
 	_delay_ms(250);
40
 }
42
 }
41
 
43
 
42
-int main(void) {
44
+void showMusicOnLED(void) {
43
 	uint8_t *music;
45
 	uint8_t *music;
44
-	uint8_t i;
45
-	uint16_t val;
46
+	uint8_t i, val;
47
+
48
+	music = equalizerGet();
49
+	val = 0;
50
+	for (i = 0; i < 7; i++) {
51
+		val += music[i];
52
+	}
53
+	val /= 7;
54
+
55
+	free(music);
56
+
57
+	if (val >= 20) {
58
+		PORTB |= (1 << PB2);
59
+	} else {
60
+		PORTB &= ~(1 << PB2);
61
+	}
62
+}
46
 
63
 
64
+void showPotOnLED(void) {
65
+	uint8_t val = 0;
66
+	adcStartConversion(0x01); // 0x0E -> 1,3 V Ref.
67
+	val = adcGetByte();
68
+
69
+	if (val >= 127) {
70
+		PORTB |= (1 << PB1);
71
+	} else {
72
+		PORTB &= ~(1 << PB1);
73
+	}
74
+}
75
+
76
+int main(void) {
47
 	DDRB = 0x3F;
77
 	DDRB = 0x3F;
48
 	DDRC = 0x0C;
78
 	DDRC = 0x0C;
49
 	DDRD = 0xFF;
79
 	DDRD = 0xFF;
50
 	
80
 	
51
 	adcInit();
81
 	adcInit();
52
-	// equalizerInit();
82
+	equalizerInit();
53
 
83
 
54
 	blinkStatus();
84
 	blinkStatus();
55
 	blinkStatus();
85
 	blinkStatus();
56
 
86
 
57
 	// Blink led :)
87
 	// Blink led :)
58
-	while (1) {
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
-		}
88
+	while (1) {		
89
+		//showPotOnLED();
90
+		showMusicOnLED();
91
+		
92
+		// Heartbeat
93
+		PORTB ^= (1 << PB1);
94
+		_delay_ms(1);
74
 	}
95
 	}
75
 
96
 
76
 	return 0;
97
 	return 0;

+ 2
- 2
AudioFirmware/makefile View File

87
 #  -Wall...:     warning level
87
 #  -Wall...:     warning level
88
 #  -Wa,...:      tell GCC to pass this to the assembler.
88
 #  -Wa,...:      tell GCC to pass this to the assembler.
89
 #    -adhlns...: create assembler listing
89
 #    -adhlns...: create assembler listing
90
-CFLAGS = -g$(DEBUG)
91
-CFLAGS += $(CDEFS) $(CINCS)
90
+#CFLAGS = -g$(DEBUG)
91
+CFLAGS = $(CDEFS) $(CINCS)
92
 CFLAGS += -O$(OPT)
92
 CFLAGS += -O$(OPT)
93
 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
93
 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
94
 CFLAGS += -Wall -Wstrict-prototypes
94
 CFLAGS += -Wall -Wstrict-prototypes

+ 26
- 1
Hardware/LED Cube.sch View File

6
 <setting alwaysvectorfont="no"/>
6
 <setting alwaysvectorfont="no"/>
7
 <setting verticaltext="up"/>
7
 <setting verticaltext="up"/>
8
 </settings>
8
 </settings>
9
-<grid distance="0.1" unitdist="inch" unit="inch" style="lines" multiple="1" display="no" altdistance="0.05" altunitdist="inch" altunit="inch"/>
9
+<grid distance="0.1" unitdist="inch" unit="inch" style="lines" multiple="1" display="no" altdistance="0.01" altunitdist="inch" altunit="inch"/>
10
 <layers>
10
 <layers>
11
 <layer number="1" name="Top" color="4" fill="1" visible="no" active="no"/>
11
 <layer number="1" name="Top" color="4" fill="1" visible="no" active="no"/>
12
 <layer number="2" name="Route2" color="1" fill="3" visible="no" active="no"/>
12
 <layer number="2" name="Route2" color="1" fill="3" visible="no" active="no"/>
10800
 <part name="R28" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10800
 <part name="R28" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10801
 <part name="R29" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10801
 <part name="R29" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10802
 <part name="R30" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10802
 <part name="R30" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10803
+<part name="LED66" library="led" deviceset="LED" device="5MM"/>
10804
+<part name="R31" library="resistor" deviceset="R-EU_" device="0204/7" value="1k"/>
10805
+<part name="GND38" library="supply1" deviceset="GND" device=""/>
10803
 </parts>
10806
 </parts>
10804
 <sheets>
10807
 <sheets>
10805
 <sheet>
10808
 <sheet>
10939
 <instance part="R28" gate="G$1" x="88.9" y="-33.02"/>
10942
 <instance part="R28" gate="G$1" x="88.9" y="-33.02"/>
10940
 <instance part="R29" gate="G$1" x="88.9" y="-43.18"/>
10943
 <instance part="R29" gate="G$1" x="88.9" y="-43.18"/>
10941
 <instance part="R30" gate="G$1" x="88.9" y="-53.34"/>
10944
 <instance part="R30" gate="G$1" x="88.9" y="-53.34"/>
10945
+<instance part="LED66" gate="G$1" x="-17.78" y="-124.46" rot="R90"/>
10946
+<instance part="R31" gate="G$1" x="-5.08" y="-124.46" rot="R180"/>
10947
+<instance part="GND38" gate="1" x="5.08" y="-124.46" rot="R90"/>
10942
 </instances>
10948
 </instances>
10943
 <busses>
10949
 <busses>
10944
 </busses>
10950
 </busses>
11301
 <pinref part="GND37" gate="1" pin="GND"/>
11307
 <pinref part="GND37" gate="1" pin="GND"/>
11302
 <pinref part="R22" gate="G$1" pin="A"/>
11308
 <pinref part="R22" gate="G$1" pin="A"/>
11303
 </segment>
11309
 </segment>
11310
+<segment>
11311
+<pinref part="R31" gate="G$1" pin="1"/>
11312
+<pinref part="GND38" gate="1" pin="GND"/>
11313
+<wire x1="0" y1="-124.46" x2="2.54" y2="-124.46" width="0.1524" layer="91"/>
11314
+</segment>
11304
 </net>
11315
 </net>
11305
 <net name="VCC" class="0">
11316
 <net name="VCC" class="0">
11306
 <segment>
11317
 <segment>
12446
 <pinref part="IC13" gate="G$1" pin="PB5(SCK)"/>
12457
 <pinref part="IC13" gate="G$1" pin="PB5(SCK)"/>
12447
 </segment>
12458
 </segment>
12448
 </net>
12459
 </net>
12460
+<net name="N$82" class="0">
12461
+<segment>
12462
+<pinref part="IC13" gate="G$1" pin="PB1(OC1A)"/>
12463
+<pinref part="LED66" gate="G$1" pin="A"/>
12464
+<wire x1="-25.4" y1="-124.46" x2="-20.32" y2="-124.46" width="0.1524" layer="91"/>
12465
+</segment>
12466
+</net>
12467
+<net name="N$83" class="0">
12468
+<segment>
12469
+<pinref part="LED66" gate="G$1" pin="C"/>
12470
+<pinref part="R31" gate="G$1" pin="2"/>
12471
+<wire x1="-12.7" y1="-124.46" x2="-10.16" y2="-124.46" width="0.1524" layer="91"/>
12472
+</segment>
12473
+</net>
12449
 </nets>
12474
 </nets>
12450
 </sheet>
12475
 </sheet>
12451
 <sheet>
12476
 <sheet>

BIN
Hardware/Schematic.png View File


Loading…
Cancel
Save