avr_piano/main.c

83 lines
1.4 KiB
C

/*
* EEL4746C - AVR Piano - Speaker Section
* speaker is connected to PD2 and ground
* buttons are connected to PB0-5, PC0-5, PD3-7
* the LCD Arduino UNO is connected to TX/RX (PD0/1)
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#define __DELAY_BACKWARD_COMPATIBLE__ /* to use variables with _delay_u/ms */
#include <util/delay.h>
#define P_RX 0
#define P_TX 1
#define P_SPKR 2
unsigned short freqs[] = {
1 / 100 * 1e6 / 2,
1 / 200 * 1e6 / 2,
1 / 250 * 1e6 / 2,
1 / 300 * 1e6 / 2,
1 / 350 * 1e6 / 2,
1 / 400 * 1e6 / 2,
1 / 500 * 1e6 / 2,
1 / 600 * 1e6 / 2,
1 / 700 * 1e6 / 2,
1 / 750 * 1e6 / 2,
1 / 1000 * 1e6 / 2,
1 / 1250 * 1e6 / 2,
1 / 1500 * 1e6 / 2,
1 / 1750 * 1e6 / 2,
1 / 2000 * 1e6 / 2,
1 / 3000 * 1e6 / 2,
1 / 4000 * 1e6 / 2,
1 / 5000 * 1e6 / 2,
1 / 6000 * 1e6 / 2,
1 / 7000 * 1e6 / 2,
1 / 8000 * 1e6 / 2
};
unsigned char note = 8;
unsigned char buttonpressed = 1;
int
main(void)
{
DDRB = 0x00;
DDRC = 0x00;
DDRD = (1 << P_TX) | (1 << P_SPKR);
PCMSK0 = 0xFF;
PCMSK1 = 0xFF;
PCMSK2 = ~(1 | (1 << 1) | (1 << 2));
PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2);
sei();
//for (;;);
for (unsigned char n = 0;; n = (n + 1) % 2) {
if (!buttonpressed && n == 0) continue;
_delay_us(freqs[note]);
// _delay_us(714.2857);
// _delay_us(70);
PORTD ^= 1 << P_SPKR;
}
}
ISR (PCINT0_vect)
{
}
ISR (PCINT1_vect)
{
}
ISR (PCINT2_vect)
{
}