summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVineet K <git@vineetk.net>2024-04-17 10:07:39 -0400
committerShokara Kou <kou@13f0.net>2024-04-17 11:04:54 -0400
commit8f258e4e02e1821692d39d87d4073e884bc2798a (patch)
treee8476599c6fdd3befdb553dacebdfea105ece2e4
parent9d751dc020b43f32b1cd7e5216f4d8de0208dda0 (diff)
use timer1 ctc mode instead of util/delay functions
-rw-r--r--Makefile2
-rw-r--r--main.c70
2 files changed, 41 insertions, 31 deletions
diff --git a/Makefile b/Makefile
index 9eba3af..17df02d 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,8 @@ CC = avr-gcc
2CFLAGS = -O2 -Wall -Wextra -Werror -mmcu=atmega328p 2CFLAGS = -O2 -Wall -Wextra -Werror -mmcu=atmega328p
3OBJCOPY = avr-objcopy 3OBJCOPY = avr-objcopy
4 4
5PORT ?= /dev/ttyACM0
6
5all: 7all:
6 ${CC} ${CFLAGS} main.c 8 ${CC} ${CFLAGS} main.c
7 ${OBJCOPY} -j .text -j .data -O ihex a.out main.hex 9 ${OBJCOPY} -j .text -j .data -O ihex a.out main.hex
diff --git a/main.c b/main.c
index 461a970..448d7f7 100644
--- a/main.c
+++ b/main.c
@@ -1,43 +1,42 @@
1/* 1/*
2 * EEL4746C - AVR Piano - Speaker Section 2 * EEL4746C - AVR Piano - Speaker Section
3 * speaker is connected to PD2 and ground 3 * speaker is connected to PD2 and ground
4 * buttons are connected to PB0-5, PC0-5, PD3-7 4, PD3-7
5 * the LCD Arduino UNO is connected to TX/RX (PD0/1) 5 * the LCD Arduino UNO is connected to TX/RX (PD0/1)
6 */ 6 */
7 7
8#include <avr/io.h> 8#include <avr/io.h>
9#include <avr/interrupt.h> 9#include <avr/interrupt.h>
10 10
11#define F_CPU 16000000UL 11#define T1_PRESCALAR 8
12#define __DELAY_BACKWARD_COMPATIBLE__ /* to use variables with _delay_u/ms */ 12#define FREQ(f) (16e6 / f / T1_PRESCALAR / 2)
13#include <util/delay.h>
14 13
15#define P_RX 0 14#define P_RX 0
16#define P_TX 1 15#define P_TX 1
17#define P_SPKR 2 16#define P_SPKR 2
18 17
19unsigned short freqs[] = { 18unsigned short freqs[] = {
20 1 / 100 * 1e6 / 2, 19 FREQ(100),
21 1 / 200 * 1e6 / 2, 20 FREQ(200),
22 1 / 250 * 1e6 / 2, 21 FREQ(250),
23 1 / 300 * 1e6 / 2, 22 FREQ(300),
24 1 / 350 * 1e6 / 2, 23 FREQ(350),
25 1 / 400 * 1e6 / 2, 24 FREQ(400),
26 1 / 500 * 1e6 / 2, 25 FREQ(500),
27 1 / 600 * 1e6 / 2, 26 FREQ(600),
28 1 / 700 * 1e6 / 2, 27 FREQ(700),
29 1 / 750 * 1e6 / 2, 28 FREQ(750),
30 1 / 1000 * 1e6 / 2, 29 FREQ(1000),
31 1 / 1250 * 1e6 / 2, 30 FREQ(1250),
32 1 / 1500 * 1e6 / 2, 31 FREQ(1500),
33 1 / 1750 * 1e6 / 2, 32 FREQ(1750),
34 1 / 2000 * 1e6 / 2, 33 FREQ(2000),
35 1 / 3000 * 1e6 / 2, 34 FREQ(3000),
36 1 / 4000 * 1e6 / 2, 35 FREQ(4000),
37 1 / 5000 * 1e6 / 2, 36 FREQ(5000),
38 1 / 6000 * 1e6 / 2, 37 FREQ(6000),
39 1 / 7000 * 1e6 / 2, 38 FREQ(7000),
40 1 / 8000 * 1e6 / 2 39 FREQ(8000),
41}; 40};
42unsigned char note = 8; 41unsigned char note = 8;
43unsigned char buttonpressed = 1; 42unsigned char buttonpressed = 1;
@@ -54,15 +53,24 @@ main(void)
54 PCMSK2 = ~(1 | (1 << 1) | (1 << 2)); 53 PCMSK2 = ~(1 | (1 << 1) | (1 << 2));
55 PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); 54 PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
56 55
56 TCCR1A = 0x00; // use timer1 CTC mode
57 TCCR1B = 1 << WGM12; // already prescaled by 8 in table
58
57 sei(); 59 sei();
58 60
59 //for (;;); 61 //for (;;);
60 for (unsigned char n = 0;; n = (n + 1) % 2) { 62 for (unsigned char n = 0;; n = (n + 1) % 2) {
61 if (!buttonpressed && n == 0) continue; 63 if (!buttonpressed && n == 0) continue;
62 _delay_us(freqs[note]); 64
63// _delay_us(714.2857); 65 TCNT1 = 1;
64// _delay_us(70); 66 OCR1A = freqs[note];
67 TCCR1B |= 1 << CS11;
68
69 while ((TIFR1 & (1 << OCF1A)) == 0);
70
65 PORTD ^= 1 << P_SPKR; 71 PORTD ^= 1 << P_SPKR;
72 TIFR1 |= 1 << OCF1A;
73 TCCR1B &= ~(1 << CS11);
66 } 74 }
67} 75}
68 76