simplify mario song

This commit is contained in:
Vineet K 2024-04-22 13:18:20 -04:00 committed by Shokara Kou
parent 8cacc107c9
commit b33e1fe796

187
notes.c
View File

@ -24,6 +24,69 @@
#define MARIO 50
unsigned short freqs[] = {
// PD3-7
100,
200,
250,
300,
350,
// PB0-4
400,
450, // middle c
500,
600,
700,
// PC0-4
750,
800,
850,
900,
950,
// notes
[MARIO] = 131, // C3
262, // C4
110, // A2
220, // A3
117, // Bb2
233, // Bb3
87,
175,
73,
147,
78,
156,
156,
147,
139,
131,
156,
147,
104,
98,
139,
156,
185,
175,
165,
233,
220,
208,
156,
123,
123,
110,
104,
['"'] = 16000,
[255] = 0 // used as a 'silent' note
};
unsigned short t_freqs[] = {
// PD3-7
FREQ(100),
FREQ(200),
@ -86,8 +149,9 @@ unsigned short freqs[] = {
[255] = 0 // used as a 'silent' note
};
unsigned char note = 0;
// 0 = buttons write note+cycles+\xff to UART
// 1 = buttons don't do anything, speaker reads note+cycles+\xff from UART
// 0 = normal/piano mode
// 1 = recording mode
// 2 = mario demo
unsigned char mode = '0';
ISR (PCINT0_vect)
@ -126,9 +190,9 @@ ISR (PCINT2_vect)
void
usart_init(void)
{
// 115200 bps, RX/TX enabled
// 9600 bps, TX enabled
UBRR0 = 103;
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
UCSR0B = 1 << TXEN0;
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
@ -158,7 +222,7 @@ playtone(void)
{
// Prepare Timer/Counter
TCNT1 = 1;
OCR1A = freqs[note];
OCR1A = t_freqs[note];
TCCR1B |= 1 << CS11;
// Monitor OutPut Compare Flag
@ -177,24 +241,10 @@ play_note(unsigned char _note, unsigned short _cycles)
usart_send(note);
if (note != 255) {
for (unsigned short i = 0; i < _cycles; i++)
playtone();
} else {
// when note is 255, don't play anything for _cycles ms
// Prepare Timer/Counter
TCNT1 = 1;
OCR1A = 16e3 * _cycles / T1_PRESCALAR;
TCCR1B |= 1 << CS11;
for (unsigned short i = 0; i < _cycles; i++)
playtone();
// Monitor OutPut Compare Flag
while ((TIFR1 & (1 << OCF1A)) == 0);
// Toggles port For Speaker
PORTD ^= 1 << P_SPKR;
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
}
_delay_ms(25);
}
int
@ -224,6 +274,8 @@ main(void)
for (unsigned char n = 0;; n = (n + 1) % 2) {
if (PINC & _BV(5))
mode = '2';
else if (PINB & _BV(5))
mode = '3';
else
mode = '0';
@ -243,79 +295,30 @@ main(void)
playtone();
cycles = (cycles + 1) % 0xffff;
} else if (mode == '2') {
play_note(MARIO + 0, 131 * 0.3);
play_note(MARIO + 1, 262 * 0.3);
play_note(MARIO + 2, 110 * 0.3);
play_note(MARIO + 3, 220 * 0.3);
play_note(MARIO + 4, 117 * 0.3);
play_note(MARIO + 5, 233 * 0.3);
_delay_ms(1800);
play_note(MARIO + 0, 131 * 0.3);
play_note(MARIO + 1, 262 * 0.3);
play_note(MARIO + 2, 110 * 0.3);
play_note(MARIO + 3, 220 * 0.3);
play_note(MARIO + 4, 117 * 0.3);
play_note(MARIO + 5, 233 * 0.3);
_delay_ms(1800);
} else if (mode == '1') {
} else if (mode == '3') {
for (unsigned char i = 0; i < 2; i++) {
for (unsigned char j = 0; j <= 5; j++)
play_note(MARIO + j, freqs[MARIO + j] * 0.3);
_delay_ms(1800);
}
play_note(MARIO + 6, 87 * 0.3);
play_note(MARIO + 7, 175 * 0.3);
play_note(MARIO + 8, 73 * 0.3);
play_note(MARIO + 9, 147 * 0.3);
play_note(MARIO + 10, 78 * 0.3);
play_note(MARIO + 11, 156 * 0.3);
_delay_ms(1800);
play_note(MARIO + 6, 87 * 0.3);
play_note(MARIO + 7, 175 * 0.3);
play_note(MARIO + 8, 73 * 0.3);
play_note(MARIO + 9, 147 * 0.3);
play_note(MARIO + 10, 78 * 0.3);
play_note(MARIO + 11, 156 * 0.3);
_delay_ms(1800);
for (unsigned char i = 0; i < 2; i++) {
for (unsigned char j = 6; j <= 11; j++)
play_note(MARIO + j, freqs[MARIO + j] * 0.3);
_delay_ms(1800);
}
play_note(MARIO + 12, 156 * 0.3);
_delay_ms(25);
play_note(MARIO + 13, 147 * 0.3);
_delay_ms(25);
play_note(MARIO + 14, 139 * 0.3);
_delay_ms(25);
play_note(MARIO + 15, 131 * 0.6);
_delay_ms(25);
play_note(MARIO + 16, 156 * 0.3);
_delay_ms(25);
play_note(MARIO + 17, 147 * 0.6);
_delay_ms(25);
play_note(MARIO + 18, 104 * 0.6);
_delay_ms(25);
play_note(MARIO + 19, 98 * 0.6);
_delay_ms(25);
play_note(MARIO + 20, 139 * 0.6);
for (unsigned char i = 12; i <= 14; i++)
play_note(MARIO + i, freqs[MARIO + i] * 0.3);
for (unsigned char i = 15; i <= 20; i++)
play_note(MARIO + i, freqs[MARIO + i] * 0.6);
play_note(MARIO + 21, 156 * 0.3);
_delay_ms(25);
play_note(MARIO + 22, 185 * 0.3);
_delay_ms(25);
play_note(MARIO + 23, 175 * 0.3);
_delay_ms(25);
play_note(MARIO + 24, 165 * 0.3);
_delay_ms(25);
play_note(MARIO + 25, 233 * 0.3);
_delay_ms(25);
play_note(MARIO + 26, 220 * 0.3);
_delay_ms(25);
play_note(MARIO + 27, 208 * 0.6);
_delay_ms(25);
play_note(MARIO + 28, 156 * 0.6);
_delay_ms(25);
play_note(MARIO + 29, 123 * 0.6);
_delay_ms(25);
play_note(MARIO + 30, 123 * 0.6);
_delay_ms(25);
play_note(MARIO + 31, 110 * 0.6);
_delay_ms(25);
play_note(MARIO + 32, 104 * 0.6);
_delay_ms(25);
for (unsigned char i = 21; i <= 26; i++)
play_note(MARIO + i, freqs[MARIO + i] * 0.3);
for (unsigned char i = 27; i <= 32; i++)
play_note(MARIO + i, freqs[MARIO + i] * 0.6);
}
}
}