summaryrefslogtreecommitdiff
path: root/floppyMusic
diff options
context:
space:
mode:
Diffstat (limited to 'floppyMusic')
-rw-r--r--floppyMusic/floppy.c11
-rw-r--r--floppyMusic/floppy.h2
-rw-r--r--floppyMusic/main.c158
-rw-r--r--floppyMusic/uart.c36
4 files changed, 178 insertions, 29 deletions
diff --git a/floppyMusic/floppy.c b/floppyMusic/floppy.c
index 440508b..9d6bd54 100644
--- a/floppyMusic/floppy.c
+++ b/floppyMusic/floppy.c
@@ -17,7 +17,7 @@ ISR(TIMER0_OVF_vect)
for(uint8_t i = 0; i < 8; i++)
{
- if(floppy_nextrun[i] == timer_overflow_counter )
+ if(floppy_nextrun[i] == timer_overflow_counter && floppy_frequencies[i] > 0)
{
floppy_nextrun[i] += floppy_frequencies[i];
floppy_pulse(i);
@@ -87,7 +87,12 @@ void floppy_setup(char *_pulse_port, char *_pulse_ddr, char *_direction_port, ch
}
}
- int floppy_calc_freq(uint32_t _f_hz)
+ uint8_t floppy_calc_freq(uint32_t _f_hz)
{
-
+ if(_f_hz < 31)
+ return 255;
+ if(_f_hz > 7813)
+ return 1;
+
+ return 7913 / _f_hz;
} \ No newline at end of file
diff --git a/floppyMusic/floppy.h b/floppyMusic/floppy.h
index f8b1c26..cc069c0 100644
--- a/floppyMusic/floppy.h
+++ b/floppyMusic/floppy.h
@@ -30,7 +30,7 @@ void floppy_set_frequency(uint8_t _floppy_id, uint8_t _freq);
void floppy_pulse(uint8_t _floppy_id);
-int floppy_calc_freq(uint32_t _f_hz);
+uint8_t floppy_calc_freq(uint32_t _f_hz);
#endif /* FLOPPY_H_ */ \ No newline at end of file
diff --git a/floppyMusic/main.c b/floppyMusic/main.c
index 09ca8d0..18e8dbf 100644
--- a/floppyMusic/main.c
+++ b/floppyMusic/main.c
@@ -5,6 +5,28 @@
* Author : Jonas
*/
+ #define c 130
+ #define d 147
+ #define e 165
+ #define f 175
+ #define g 195
+ #define gS 208
+ #define a 220
+ #define aS 228
+ #define b 233
+ #define cH 261
+ #define cSH 277
+ #define dH 294
+ #define dSH 311
+ #define eH 330
+ #define fH 349
+ #define fSH 370
+ #define gH 390
+ #define gSH 415
+ #define aH 440
+
+ #define _GET_UBBR(BAUD) ( (F_CPU / 16 * BAUD) - 1)
+
#ifndef F_CPU
#define F_CPU 16000000UL //CPU Running at 16MHz
#endif
@@ -12,25 +34,139 @@
#include <avr/io.h>
#include "floppy.h"
-//#include "uart.h"
+void imperial_march();
+
+void beep(int _f, unsigned int _t);
+
+void delay_ms(unsigned int ms );
+
+
int main(void)
{
- //uart_init(9600);
- //DDRC = 0xff;
floppy_setup(&PORTC, &DDRC, &PORTB, &DDRB);
- floppy_set_frequency(0, 70);
+ floppy_set_frequency(0, 0);
+ floppy_set_frequency(1, 0);
DDRD = 0xff;
- PORTD = 0xff; //DEBUG
-
- //uart_send_string("Floppy Music\n");
while (1)
{
- //floppy_set_frequency(0, uart_recieve());
- //uart_send_string("Floppy Music\n");
- //_delay_ms(500);
- //PORTD ^= 0xff;
+ play_imperial_march();
+ _delay_ms(2000);
}
+}
+
+void delay_ms(unsigned int ms )
+{
+ unsigned int i;
+ for (i = 0; i<= ms; i++)
+ _delay_ms(1);
+}
+
+void play_imperial_march()
+{
+ beep(a, 500);
+ beep(a, 500);
+ beep(a, 500);
+ beep(f, 350);
+ beep(cH, 150);
+ beep(a, 500);
+ beep(f, 350);
+ beep(cH, 150);
+ beep(a, 650);
+
+ delay_ms(150);
+ //end of first bit
+
+ beep(eH, 500);
+ beep(eH, 500);
+ beep(eH, 500);
+ beep(fH, 350);
+ beep(cH, 150);
+ beep(gS, 500);
+ beep(f, 350);
+ beep(cH, 150);
+ beep(a, 650);
+
+ delay_ms(150);
+ //end of second bit...
+
+ beep(aH, 500);
+ beep(a, 300);
+ beep(a, 150);
+ beep(aH, 400);
+ beep(gSH, 200);
+ beep(gH, 200);
+ beep(fSH, 125);
+ beep(fH, 125);
+ beep(fSH, 250);
+
+ delay_ms(250);
+
+ beep(aS, 250);
+ beep(dSH, 400);
+ beep(dH, 200);
+ beep(cSH, 200);
+ beep(cH, 125);
+ beep(b, 125);
+ beep(cH, 250);
+
+ delay_ms(250);
+
+ beep(f, 125);
+ beep(gS, 500);
+ beep(f, 375);
+ beep(a, 125);
+ beep(cH, 500);
+ beep(a, 375);
+ beep(cH, 125);
+ beep(eH, 650);
+
+ //end of third bit... (Though it doesn't play well)
+ //let's repeat it
+
+ beep(aH, 500);
+ beep(a, 300);
+ beep(a, 150);
+ beep(aH, 400);
+ beep(gSH, 200);
+ beep(gH, 200);
+ beep(fSH, 125);
+ beep(fH, 125);
+ beep(fSH, 250);
+
+ delay_ms(250);
+
+ beep(aS, 250);
+ beep(dSH, 400);
+ beep(dH, 200);
+ beep(cSH, 200);
+ beep(cH, 125);
+ beep(b, 125);
+ beep(cH, 250);
+
+ delay_ms(250);
+
+ beep(f, 250);
+ beep(gS, 500);
+ beep(f, 375);
+ beep(cH, 125);
+ beep(a, 500);
+ beep(f, 375);
+ beep(cH, 125);
+ beep(a, 650);
+ //end of the song
+}
+
+void beep(int _f, unsigned int _t)
+{
+ PORTD = ~ floppy_calc_freq(_f / 3);
+ floppy_set_frequency(0, floppy_calc_freq(_f / 3));
+ floppy_set_frequency(1, floppy_calc_freq(_f / 3));
+ delay_ms(_t);
+ PORTD = 0xff;
+ floppy_set_frequency(0, 0);
+ floppy_set_frequency(1, 0);
+ _delay_ms(10);
} \ No newline at end of file
diff --git a/floppyMusic/uart.c b/floppyMusic/uart.c
index d4d5ccf..aed8acd 100644
--- a/floppyMusic/uart.c
+++ b/floppyMusic/uart.c
@@ -9,32 +9,40 @@
void uart_init(uint32_t _baud)
{
- unsigned int ubrr = _GET_UBBR(_baud);
+ unsigned int ubrr = _GET_UBBR(_baud);
- UBRRH = (ubrr<<8);
- UBRRL = ubrr;
+ UBRRH = (ubrr<<8);
+ UBRRL = ubrr;
+
+ /* Enable receiver and transmitter */
+ UCSRB = (1<<RXEN)|(1<<TXEN);
+
+ /* Set frame format: 8data, 2stop bit */
+ UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
void uart_send(char _data)
{
- while(!(UCSRA & (1<<UDRE)));
- UDR = _data;
+ while(!(UCSRA & (1<<UDRE)));
+ UDR = _data;
}
void uart_send_string(char *_data)
{
- char *data = _data;
+ char *data = _data;
- while(*data != '\0')
- {
- uart_send(*data);
- data++;
- }
+ while(*data != '\0')
+ {
+ uart_send(*data);
+ data++;
+ }
}
char uart_recieve()
{
- while(!(UCSRA & (1<<RXC)));
+ while(!(UCSRA & (1<<RXC)));
+
+ return UDR;
+ }
+
- return UDR;
- } \ No newline at end of file