Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define F_CPU 16000000UL
- #include <util/delay.h>
- #include <avr/eeprom.h>
- int iceil = 1;
- int count = 0;
- int seccount = 0;
- int secceil = 120;
- int newn = 0;
- int newl = 30;
- int ebr_upscale = 0;
- int ebr_sharp = 0;
- int newf = 34;
- int notepointer = 0;
- void set7seg(unsigned char num) {
- PORTD = (num&0xFC);
- PORTC = (num&0x03);
- }
- unsigned char get7seg(unsigned char num) {
- if(num==0) return 0b10111111;
- else if(num==1) return 0b11000110;
- else if(num==2) return 0b10100001;
- else if(num==3) return 0b10000110;
- else if(num==4) return 0b10001110;
- else if(num==5) return 0b10010000;
- else if(num==6) return 0b10001000;
- else if(num==7) return 0b10000011;
- else if(num==8) return 0b01000110;
- else if(num==9) return 0b00100001;
- else if(num==10) return 0b00000110;
- else if(num==11) return 0b00001110;
- else if(num==15) return 0b10011011;
- return 0;
- }
- int get_frequency(int note,int sharp) {
- if(sharp==0) {
- switch(note) {
- case 0: return 0;
- case 1: return 57;
- case 2: return 51;
- case 3: return 45;
- case 4: return 43;
- case 5: return 38;
- case 6: return 34;
- case 7: return 30;
- case 8: return 28;
- case 9: return 25;
- case 10: return 22;
- case 11: return 21;
- case 15: return 255;
- }
- } else {
- switch(note) {
- case 0: return 0;
- case 1: return 54;
- case 2: return 48;
- case 3: return 43;
- case 4: return 40;
- case 5: return 36;
- case 6: return 32;
- case 7: return 28;
- case 8: return 26;
- case 9: return 24;
- case 10: return 22;
- case 11: return 20;
- case 15: return 255;
- }
- }
- return 34;
- }
- void change_note() {
- newf = get_frequency(newn,ebr_sharp);
- if(ebr_upscale==1) newf/=2;
- secceil = newl;
- iceil = newf;
- if(iceil==0) TIMSK2 = 0;
- else if(iceil==255) {
- TIMSK2 = 0;
- TIMSK0 = 0;
- } else TIMSK2 = 1;
- set7seg(get7seg(newn));
- }
- void read_note() {
- if(!eeprom_is_ready())
- eeprom_busy_wait() ;
- uint8_t value = eeprom_read_byte((unsigned int) notepointer++);
- newn = value&0x0F;
- if((value&0x30)==0) newl = 30;
- else if((value&0x30)==0x10) newl = 60;
- else if((value&0x30)==0x20) newl = 15;
- else newl=120;
- // ebr code
- ebr_upscale = (value&0x40)>>6;
- ebr_sharp = (value&0x80);
- if(newn==14) {
- // reset
- notepointer = 0;
- read_note();
- }
- }
- ISR(TIMER2_OVF_vect) {
- count++;
- if(count==iceil) {
- count=0;
- PORTB^=2;
- PORTB^=16;
- }
- return;
- }
- ISR(TIMER0_OVF_vect) {
- seccount++;
- if(seccount==secceil) {
- seccount=0;
- //do
- change_note();
- read_note();
- iceil = newf;
- count = 0;
- // load new note
- PORTB ^= 4;
- }
- return;
- }
- int main(void) {
- DDRB |= 30;
- DDRC |= 3;
- DDRD |= 0xFC;
- PORTD = 0;
- PORTC = 0;
- PORTB |= 8;
- TCNT2 = 0;
- TIMSK2 = 1;
- TCCR2B = 1;
- TCNT0 = 0;
- TIMSK0 = 1;
- TCCR0B = 5;
- sei();
- while(1);
- return 0;
- }
Add Comment
Please, Sign In to add comment