2024-04-15 21:55:51 -04:00
|
|
|
/*
|
|
|
|
* EEL4746C - AVR Piano - Speaker Section
|
2024-04-17 20:06:31 -04:00
|
|
|
* speaker is connected to PD2 and ground
|
|
|
|
* buttons are connceted to PB0-5, PC0-5, PD3-7
|
2024-04-15 21:55:51 -04:00
|
|
|
* the LCD Arduino UNO is connected to TX/RX (PD0/1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
2024-04-17 10:07:39 -04:00
|
|
|
#define T1_PRESCALAR 8
|
|
|
|
#define FREQ(f) (16e6 / f / T1_PRESCALAR / 2)
|
2024-04-15 21:55:51 -04:00
|
|
|
|
2024-04-17 10:07:39 -04:00
|
|
|
#define P_RX 0
|
|
|
|
#define P_TX 1
|
|
|
|
#define P_SPKR 2
|
2024-04-15 21:55:51 -04:00
|
|
|
|
|
|
|
unsigned short freqs[] = {
|
2024-04-17 10:07:39 -04:00
|
|
|
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),
|
2024-04-15 21:55:51 -04:00
|
|
|
};
|
2024-04-17 20:06:31 -04:00
|
|
|
unsigned char note = 0;
|
|
|
|
unsigned char buttonpressed = 0;
|
2024-04-15 21:55:51 -04:00
|
|
|
|
|
|
|
int
|
2024-04-17 15:26:01 -04:00
|
|
|
main(void)
|
2024-04-15 21:55:51 -04:00
|
|
|
{
|
2024-04-17 15:26:01 -04:00
|
|
|
// Data Direction for Buttons, UART, Speaker
|
2024-04-15 21:55:51 -04:00
|
|
|
DDRB = 0x00;
|
|
|
|
DDRC = 0x00;
|
2024-04-17 15:26:01 -04:00
|
|
|
DDRD = (1 << P_TX) | (1 << P_SPKR);
|
2024-04-15 21:55:51 -04:00
|
|
|
|
2024-04-17 15:26:01 -04:00
|
|
|
// Enabling Pin Change Interrupt for All Buttons
|
2024-04-15 21:55:51 -04:00
|
|
|
PCMSK0 = 0xFF;
|
|
|
|
PCMSK1 = 0xFF;
|
|
|
|
PCMSK2 = ~(1 | (1 << 1) | (1 << 2));
|
|
|
|
PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
|
|
|
|
|
2024-04-17 10:07:39 -04:00
|
|
|
TCCR1A = 0x00; // use timer1 CTC mode
|
|
|
|
TCCR1B = 1 << WGM12; // already prescaled by 8 in table
|
|
|
|
|
2024-04-15 21:55:51 -04:00
|
|
|
sei();
|
|
|
|
|
2024-04-17 15:26:01 -04:00
|
|
|
// Sends Square wave to the Speaker When a Button is Pressed
|
2024-04-15 21:55:51 -04:00
|
|
|
for (unsigned char n = 0;; n = (n + 1) % 2) {
|
2024-04-15 22:38:55 -04:00
|
|
|
if (!buttonpressed && n == 0) continue;
|
2024-04-17 15:26:01 -04:00
|
|
|
|
|
|
|
// Prepare Timer/Counter
|
2024-04-17 10:07:39 -04:00
|
|
|
TCNT1 = 1;
|
|
|
|
OCR1A = freqs[note];
|
|
|
|
TCCR1B |= 1 << CS11;
|
|
|
|
|
2024-04-17 15:26:01 -04:00
|
|
|
while ((TIFR1 & (1 << OCF1A)) == 0); // Monitor OutPut Compare Flag
|
2024-04-17 10:07:39 -04:00
|
|
|
|
2024-04-17 15:26:01 -04:00
|
|
|
// Toggles port For Speaker
|
2024-04-15 21:55:51 -04:00
|
|
|
PORTD ^= 1 << P_SPKR;
|
2024-04-17 10:07:39 -04:00
|
|
|
TIFR1 |= 1 << OCF1A;
|
|
|
|
TCCR1B &= ~(1 << CS11);
|
2024-04-15 21:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ISR (PCINT0_vect)
|
|
|
|
{
|
2024-04-17 20:06:31 -04:00
|
|
|
// check if any button on any port is pressed
|
|
|
|
if (PINB | PINC | PIND)
|
|
|
|
buttonpressed = 1;
|
|
|
|
else
|
|
|
|
buttonpressed = 0;
|
|
|
|
|
|
|
|
// check if any button in the whole port is pressed
|
|
|
|
for (unsigned char i = 0; i <= 5; i++) {
|
|
|
|
if (PINB & (1 << i)) {
|
|
|
|
note = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 21:55:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR (PCINT1_vect)
|
|
|
|
{
|
2024-04-17 20:06:31 -04:00
|
|
|
// check if any button on any port is pressed
|
|
|
|
if (PINB | PINC | PIND)
|
|
|
|
buttonpressed = 1;
|
|
|
|
else
|
|
|
|
buttonpressed = 0;
|
|
|
|
|
|
|
|
// check if any button in the whole port is pressed
|
|
|
|
for (unsigned char i = 0; i <= 5; i++) {
|
|
|
|
if (PINC & (1 << i)) {
|
|
|
|
note = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 21:55:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR (PCINT2_vect)
|
|
|
|
{
|
2024-04-17 20:06:31 -04:00
|
|
|
// check if any button on any port is pressed
|
|
|
|
if (PINB | PINC | PIND)
|
|
|
|
buttonpressed = 1;
|
|
|
|
else
|
|
|
|
buttonpressed = 0;
|
|
|
|
|
|
|
|
// check if any button in the whole port is pressed
|
|
|
|
for (unsigned char i = 3; i <= 7; i++) {
|
|
|
|
if (PIND & (1 << i)) {
|
|
|
|
note = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 21:55:51 -04:00
|
|
|
}
|