summaryrefslogtreecommitdiff
path: root/notes.c
blob: dbbca44c933423b8865d4c0fb4230c3f5b8e61db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 *  EEL4746C - AVR Piano - Speaker Section
 *  speaker is connected to PD2 and ground, PD3-7
 *  the LCD Arduino UNO is connected to TX/RX (PD0/1)
 */

#include <avr/io.h>
#include <avr/interrupt.h>

#define	T1_PRESCALAR	8
#define FREQ(f)		(16e6 / f / T1_PRESCALAR / 2)

#define P_RX		0
#define P_TX		1
#define P_SPKR		2

unsigned short freqs[] = {
	FREQ(100),
	FREQ(200),
	FREQ(250),
	FREQ(300),
	FREQ(350),
	FREQ(400),
	FREQ(500),
	FREQ(600),
	FREQ(700),
	FREQ(750),
	FREQ(1000),
	FREQ(1250),
	FREQ(1500),
	FREQ(1750),
	FREQ(2000),
	FREQ(3000),
	FREQ(4000),
	FREQ(5000),
	FREQ(6000),
	FREQ(7000),
	FREQ(8000),
};
unsigned char note = 8;
unsigned char buttonpressed = 1;

int
main(void) 
{
	// Data Direction for Buttons, UART, Speaker
	DDRB = 0x00;
	DDRC = 0x00;
	DDRD = (1 << P_TX) | (1 << P_SPKR); 

	// Enabling Pin Change Interrupt for All Buttons
	PCMSK0 = 0xFF;
	PCMSK1 = 0xFF;
	PCMSK2 = ~(1 | (1 << 1) | (1 << 2));
	PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);

	TCCR1A = 0x00; // use timer1 CTC mode
	TCCR1B = 1 << WGM12; // already prescaled by 8 in table

	sei();

	// Sends Square wave to the Speaker When a Button is Pressed
	for (unsigned char n = 0;; n = (n + 1) % 2) {
		if (!buttonpressed && n == 0) continue;
		
		// Prepare Timer/Counter
		TCNT1 = 1;
		OCR1A = freqs[note];
		TCCR1B |= 1 << CS11;

		while ((TIFR1 & (1 << OCF1A)) == 0); // Monitor OutPut Compare Flag

		// Toggles port For Speaker
		PORTD ^= 1 << P_SPKR;
		TIFR1 |= 1 << OCF1A;
		TCCR1B &= ~(1 << CS11);
	}
}

ISR (PCINT0_vect)
{

}

ISR (PCINT1_vect)
{

}

ISR (PCINT2_vect)
{

}