diff options
| author | Vineet K <git@vineetk.net> | 2024-04-15 21:55:51 -0400 |
|---|---|---|
| committer | kar <kar@13f0.net> | 2024-04-15 21:55:51 -0400 |
| commit | fc85413fa9891c32b4d82cd7d0e27fd04220be91 (patch) | |
| tree | f265522f5db981cbd5ef787564da85b016aeade6 | |
| parent | 8f3b27a6527cd267c450a3b8018f1ffcc11aeb54 (diff) | |
start working on the speaker arduino code
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | main.c | 79 |
2 files changed, 90 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9eba3af --- /dev/null +++ b/Makefile | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | CC = avr-gcc | ||
| 2 | CFLAGS = -O2 -Wall -Wextra -Werror -mmcu=atmega328p | ||
| 3 | OBJCOPY = avr-objcopy | ||
| 4 | |||
| 5 | all: | ||
| 6 | ${CC} ${CFLAGS} main.c | ||
| 7 | ${OBJCOPY} -j .text -j .data -O ihex a.out main.hex | ||
| 8 | rm -f a.out | ||
| 9 | |||
| 10 | flash: all | ||
| 11 | avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -b 115200 -U flash:w:main.hex:i | ||
| @@ -0,0 +1,79 @@ | |||
| 1 | /* | ||
| 2 | * EEL4746C - AVR Piano - Speaker Section | ||
| 3 | * speaker is connected to PD2 and ground | ||
| 4 | * buttons are connected to PB0-5, PC0-5, PD3-7 | ||
| 5 | * the LCD Arduino UNO is connected to TX/RX (PD0/1) | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include <avr/io.h> | ||
| 9 | #include <avr/interrupt.h> | ||
| 10 | |||
| 11 | #define F_CPU 16000000UL | ||
| 12 | #include <util/delay.h> | ||
| 13 | |||
| 14 | #define P_RX 0 | ||
| 15 | #define P_TX 1 | ||
| 16 | #define P_SPKR 2 | ||
| 17 | |||
| 18 | unsigned short freqs[] = { | ||
| 19 | 100, | ||
| 20 | 200, | ||
| 21 | 250, | ||
| 22 | 300, | ||
| 23 | 350, | ||
| 24 | 400, | ||
| 25 | 500, | ||
| 26 | 600, | ||
| 27 | 700, | ||
| 28 | 750, | ||
| 29 | 1000, | ||
| 30 | 1250, | ||
| 31 | 1500, | ||
| 32 | 1750, | ||
| 33 | 2000, | ||
| 34 | 3000, | ||
| 35 | 4000, | ||
| 36 | 5000, | ||
| 37 | 6000, | ||
| 38 | 7000, | ||
| 39 | 8000 | ||
| 40 | }; | ||
| 41 | unsigned char note = 0; | ||
| 42 | unsigned char buttonpressed = 1; | ||
| 43 | |||
| 44 | int | ||
| 45 | main(void) | ||
| 46 | { | ||
| 47 | DDRB = 0x00; | ||
| 48 | DDRC = 0x00; | ||
| 49 | DDRD = (1 << P_TX) | (1 << P_SPKR); | ||
| 50 | |||
| 51 | PCMSK0 = 0xFF; | ||
| 52 | PCMSK1 = 0xFF; | ||
| 53 | PCMSK2 = ~(1 | (1 << 1) | (1 << 2)); | ||
| 54 | PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); | ||
| 55 | |||
| 56 | sei(); | ||
| 57 | |||
| 58 | //for (;;); | ||
| 59 | for (unsigned char n = 0;; n = (n + 1) % 2) { | ||
| 60 | if (!buttonpressed) continue; | ||
| 61 | _delay_us(71.43); | ||
| 62 | PORTD ^= 1 << P_SPKR; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | ISR (PCINT0_vect) | ||
| 67 | { | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | ISR (PCINT1_vect) | ||
| 72 | { | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | ISR (PCINT2_vect) | ||
| 77 | { | ||
| 78 | |||
| 79 | } | ||
