Selaa lähdekoodia

Audio detection working

Thomas Buck 13 vuotta sitten
vanhempi
commit
baf026dbd2
6 muutettua tiedostoa jossa 79 lisäystä ja 30 poistoa
  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 Näytä tiedosto

@@ -26,16 +26,13 @@
26 26
 #include "adc.h"
27 27
 
28 28
 void adcInit(void) {
29
-	uint16_t tmp;
30
-	
31 29
 	DDRC &= ~(3);
32 30
 	ADMUX = 0;
33 31
 	ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
34 32
 	ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
35 33
 	ADCSRA |= (1 << ADEN); // Enable adc
36 34
 	adcStartConversion(0);
37
-	while (adcIsFinished() == 0);
38
-	tmp = ADCW;
35
+	adcGetResult();
39 36
 }
40 37
 
41 38
 void adcStartConversion(uint8_t channel) {
@@ -45,11 +42,17 @@ void adcStartConversion(uint8_t channel) {
45 42
 }
46 43
 
47 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 53
 uint16_t adcGetResult(void) {
52 54
 	while (adcIsFinished() == 0);
55
+	ADCSRA &= ~(1 << ADSC);
53 56
 	return ADCW;
54 57
 }
55 58
 

+ 2
- 2
AudioFirmware/eq.c Näytä tiedosto

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

+ 41
- 20
AudioFirmware/main.c Näytä tiedosto

@@ -34,43 +34,64 @@
34 34
 
35 35
 void blinkStatus(void) {
36 36
 	PORTB |= (1 << PB2);
37
+	PORTB &= ~(1 << PB1);
37 38
 	_delay_ms(250);
38 39
 	PORTB &= ~(1 << PB2);
40
+	PORTB |= (1 << PB1);
39 41
 	_delay_ms(250);
40 42
 }
41 43
 
42
-int main(void) {
44
+void showMusicOnLED(void) {
43 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 77
 	DDRB = 0x3F;
48 78
 	DDRC = 0x0C;
49 79
 	DDRD = 0xFF;
50 80
 	
51 81
 	adcInit();
52
-	// equalizerInit();
82
+	equalizerInit();
53 83
 
54 84
 	blinkStatus();
55 85
 	blinkStatus();
56 86
 
57 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 97
 	return 0;

+ 2
- 2
AudioFirmware/makefile Näytä tiedosto

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

+ 26
- 1
Hardware/LED Cube.sch Näytä tiedosto

@@ -6,7 +6,7 @@
6 6
 <setting alwaysvectorfont="no"/>
7 7
 <setting verticaltext="up"/>
8 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 10
 <layers>
11 11
 <layer number="1" name="Top" color="4" fill="1" visible="no" active="no"/>
12 12
 <layer number="2" name="Route2" color="1" fill="3" visible="no" active="no"/>
@@ -10800,6 +10800,9 @@ Distributor Buerklin, 11G810</description>
10800 10800
 <part name="R28" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10801 10801
 <part name="R29" library="resistor" deviceset="R-EU_" device="0204/7" value="100k"/>
10802 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 10806
 </parts>
10804 10807
 <sheets>
10805 10808
 <sheet>
@@ -10939,6 +10942,9 @@ Distributor Buerklin, 11G810</description>
10939 10942
 <instance part="R28" gate="G$1" x="88.9" y="-33.02"/>
10940 10943
 <instance part="R29" gate="G$1" x="88.9" y="-43.18"/>
10941 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 10948
 </instances>
10943 10949
 <busses>
10944 10950
 </busses>
@@ -11301,6 +11307,11 @@ Distributor Buerklin, 11G810</description>
11301 11307
 <pinref part="GND37" gate="1" pin="GND"/>
11302 11308
 <pinref part="R22" gate="G$1" pin="A"/>
11303 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 11315
 </net>
11305 11316
 <net name="VCC" class="0">
11306 11317
 <segment>
@@ -12446,6 +12457,20 @@ Distributor Buerklin, 11G810</description>
12446 12457
 <pinref part="IC13" gate="G$1" pin="PB5(SCK)"/>
12447 12458
 </segment>
12448 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 12474
 </nets>
12450 12475
 </sheet>
12451 12476
 <sheet>

BIN
Hardware/Schematic.png Näytä tiedosto


Loading…
Peruuta
Tallenna