diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2017-06-25 19:51:22 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2017-06-25 19:51:22 +0200 |
commit | 3d5fe2a0bfe5a9b6f39d7937eddd0a47a76c23f2 (patch) | |
tree | 281272ac0f0020ecd54cd44e4a49dbcb2aeaa3bc /floppyMusic | |
parent | 9d923b5a00916478a84d21c431532e5d3065968c (diff) | |
download | avrFloppy-3d5fe2a0bfe5a9b6f39d7937eddd0a47a76c23f2.tar.gz |
Cleanups
Diffstat (limited to 'floppyMusic')
-rw-r--r-- | floppyMusic/floppy.c | 12 | ||||
-rw-r--r-- | floppyMusic/floppy.h | 1 | ||||
-rw-r--r-- | floppyMusic/main.c | 13 | ||||
-rw-r--r-- | floppyMusic/midi.c | 105 | ||||
-rw-r--r-- | floppyMusic/midi.h | 29 | ||||
-rw-r--r-- | floppyMusic/music.c | 14 |
6 files changed, 85 insertions, 89 deletions
diff --git a/floppyMusic/floppy.c b/floppyMusic/floppy.c index b065302..3cd699a 100644 --- a/floppyMusic/floppy.c +++ b/floppyMusic/floppy.c @@ -9,8 +9,6 @@ ISR(TIMER0_OVF_vect) { - cli(); - timer_overflow_counter ++; *fPORT = 0xff; //Deactivate all previously activated pins @@ -23,8 +21,6 @@ ISR(TIMER0_OVF_vect) floppy_pulse(i); } } - - sei(); } void floppy_setup(unsigned char *_pulse_port, unsigned char *_pulse_ddr, unsigned char *_direction_port, unsigned char *_direction_ddr) @@ -89,12 +85,12 @@ void floppy_setup(unsigned char *_pulse_port, unsigned char *_pulse_ddr, unsigne uint8_t floppy_calc_freq(uint32_t _f_hz) { + uint32_t f = _f_hz; - ///TODO Shift notes out of effective range of FDD into range if(_f_hz < 31) return 255; - if(_f_hz > 7813) - return 1; + if(_f_hz > 400) //FDD cant go over 400Hz so the tone is shifted down in octaves + for(f = _f_hz; f > 400; f /= 2); - return 7913 / _f_hz; + return 7913 / f; }
\ No newline at end of file diff --git a/floppyMusic/floppy.h b/floppyMusic/floppy.h index b0a45cb..86d2be1 100644 --- a/floppyMusic/floppy.h +++ b/floppyMusic/floppy.h @@ -49,5 +49,4 @@ uint8_t floppy_calc_freq(uint32_t _f_hz); * _f_hz : Frequency in Hz */ - #endif /* FLOPPY_H_ */
\ No newline at end of file diff --git a/floppyMusic/main.c b/floppyMusic/main.c index 6256e97..8719312 100644 --- a/floppyMusic/main.c +++ b/floppyMusic/main.c @@ -5,15 +5,16 @@ * Author : Jonas */ -#define _TEST_MODE +//#define _TEST_MODE #include <avr/io.h> #include "floppy.h" -#include "midi.h" #ifdef _TEST_MODE #include "music.h" +#else +#include "midi.h" #endif int main(void) @@ -27,14 +28,18 @@ int main(void) for(;;) { play_imperial_march(); - _delay_ms(2000); + _delay_ms(1000); } #else //Normal MIDI mode midi_setup(); - for(;;); + for(;;) + { + while (!(UCSRA & (1<<UDRE))); + UDR = 0b10101010; + } #endif diff --git a/floppyMusic/midi.c b/floppyMusic/midi.c index ae99371..ca32d5a 100644 --- a/floppyMusic/midi.c +++ b/floppyMusic/midi.c @@ -5,100 +5,85 @@ * Author: Jonas */ - /* - Octave|| Note Numbers - # || - || C | C# | D | D# | E | F | F# | G | G# | A | A# | B - ----------------------------------------------------------------------------- - 0 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 - 1 || 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 - 2 || 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 - 3 || 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 - 4 || 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 - 5 || 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 - 6 || 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 - 7 || 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 - 8 || 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 - 9 || 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 - 10 || 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | - */ - #include "midi.h" -extern const uint16_t midi_code_frequency_table[] = {8,9,9,10,10,11,12,12,13,14,15,15,16,17,18,19,21,22,23,25,26,28,29,31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978,5274,5588,5920,6272}; +const uint16_t midi_code_frequency_table[] = {8,9,9,10,10,11,12,12,13,14,15,15,16,17,18,19,21,22,23,25,26,28,29,31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978,5274,5588,5920,6272}; ISR(USART_RXC_vect) { - cli(); - char status, data1, data2; - char command, channel, note, velocity; + char data[3] = {0x00, 0x00, 0x00}; + char command, /*channel,*/ note/*, velocity*/; - //read data from interface - status = midi_recieve(); - data1 = midi_recieve(); - data2 = midi_recieve(); + UCSRB &= ~(1<<RXCIE);//Disable UART interrupt - sei(); + //read data from interface + while(!(UCSRA & (1<<RXC))); //Wait for data + data[0] = UDR; + + for(int i = 0; i < 20; i++) { //Wait 100µs for next datapackage + _delay_us(1); + if((UCSRA & (1<<RXC))) { + data[1] = UDR; + break; + } + } + for(int i = 0; i < 20; i++) { + _delay_us(1); + if((UCSRA & (1<<RXC))) { + data[2] = UDR; + break; + } + } + UCSRB |= (1<<RXCIE);//reenable UART Interrupt //split data - command = status & 0x0f; //Mask out last 4 bits - channel = (status >> 4) & 0x0f; // Shift channel into first 4 bits; - note = data1; - velocity = data2; - + /*channel = data[0] & 0x0f; //Mask out last 4 bits*/ + command = (data[0] >> 4) & 0x0f; // Shift command into first 4 bits; + note = data[1]; + /*velocity = data2;*/ + + //PORTA = ~command; + switch(command) { case 0x08: //Note off + PORTA = 1; midi_update_note(note, 0); break; - midi_update_note(note, 1); - case 0x09: //Note on + case 0x09: //Note on + PORTA = 2; + midi_update_note(note, 1); break; + /* + case 0x0A:break; //Note Fade Out + + case 0x0B:break; //Control Change - case 0x0A: //Note Fade Out - //unused - break; - - case 0x0B: //Control Change - //unused - break; + case 0x0C:break; //Channel Fade Out - case 0x0C: //Channel Fade Out - //unused - break; - - case 0x0E: //Note Pitch change - //unused - break; + case 0x0E:break; //Note Pitch change + */ + default:break; } - - } void midi_setup() { midi_active_channels = 0x00; - + DDRA = 0xff; + PORTA = 0xff; //UART init unsigned int ubrr = (F_CPU / (16 * _MIDI_LINK_SPEED)) - 1; UBRRH = (unsigned char) (ubrr>>8) & 0xff; UBRRL = (unsigned char) ubrr & 0xff; - UCSRB = (1<<RXEN) | (1<<TXEN); + UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE); //Enable Tx, Rx and Rx Interrupt UCSRC = (1<<URSEL) | (1<<USBS) | (3<<UCSZ0); - UCSRB |= (1<<RXCIE); //enable interrupt } - char midi_recieve() - { - while(!(UCSRA & (1<<RXC))); //Wait for data - - return UDR; - } - void midi_update_note(uint16_t _note, uint8_t _status) { if(_status) //Note on diff --git a/floppyMusic/midi.h b/floppyMusic/midi.h index 733fe05..7ee76fc 100644 --- a/floppyMusic/midi.h +++ b/floppyMusic/midi.h @@ -5,6 +5,24 @@ * Author: Jonas */ + /* + Octave|| Note Numbers + # || + || C | C# | D | D# | E | F | F# | G | G# | A | A# | B + ----------------------------------------------------------------------------- + 0 || 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 + 1 || 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 + 2 || 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 + 3 || 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 + 4 || 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 + 5 || 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 + 6 || 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 + 7 || 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 + 8 || 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 + 9 || 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 + 10 || 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | + */ + #ifndef F_CPU #error F_CPU not defined for midi.h #endif @@ -14,9 +32,8 @@ #define _MIDI_LINK_SPEED 30000UL //Standard MIDI linkspeed is 30k baud - - #include <avr/interrupt.h> +#include <util/delay.h> #include <avr/io.h> #include "floppy.h" @@ -32,14 +49,8 @@ void midi_setup(); void midi_update_note(uint16_t _note, uint8_t _status); /* -* _note : MIDI standard note # +* _note : MIDI-standard note # * _status : 0=OFF >0=ON */ -char midi_recieve(); -/* -* reads UART recieve buffer -*/ - - #endif /* MIDI_H_ */
\ No newline at end of file diff --git a/floppyMusic/music.c b/floppyMusic/music.c index 5dc644f..f0aed39 100644 --- a/floppyMusic/music.c +++ b/floppyMusic/music.c @@ -26,7 +26,7 @@ beep(cH, 150); beep(a, 650); - delay_ms(150); + _delay_ms(150); //end of first bit beep(eH, 500); @@ -39,7 +39,7 @@ beep(cH, 150); beep(a, 650); - delay_ms(150); + _delay_ms(150); //end of second bit... beep(aH, 500); @@ -52,7 +52,7 @@ beep(fH, 125); beep(fSH, 250); - delay_ms(250); + _delay_ms(250); beep(aS, 250); beep(dSH, 400); @@ -62,7 +62,7 @@ beep(b, 125); beep(cH, 250); - delay_ms(250); + _delay_ms(250); beep(f, 125); beep(gS, 500); @@ -86,7 +86,7 @@ beep(fH, 125); beep(fSH, 250); - delay_ms(250); + _delay_ms(250); beep(aS, 250); beep(dSH, 400); @@ -96,7 +96,7 @@ beep(b, 125); beep(cH, 250); - delay_ms(250); + _delay_ms(250); beep(f, 250); beep(gS, 500); @@ -243,5 +243,5 @@ floppy_set_frequency(0, 0); floppy_set_frequency(1, 0); - delay_ms(10); + _delay_ms(10); }
\ No newline at end of file |