Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #define F_CPU 16000000UL
  4. #include <util/delay.h>
  5. #include <avr/eeprom.h>
  6.  
  7. int iceil = 1;
  8. int count = 0;
  9.  
  10. int seccount = 0;
  11. int secceil = 120;
  12.  
  13. int newn = 0;
  14. int newl = 30;
  15.  
  16. int ebr_upscale = 0;
  17. int ebr_sharp = 0;
  18.  
  19. int newf = 34;
  20.  
  21. int notepointer = 0;
  22.  
  23. void set7seg(unsigned char num) {
  24. PORTD = (num&0xFC);
  25. PORTC = (num&0x03);
  26. }
  27.  
  28. unsigned char get7seg(unsigned char num) {
  29. if(num==0) return 0b10111111;
  30. else if(num==1) return 0b11000110;
  31. else if(num==2) return 0b10100001;
  32. else if(num==3) return 0b10000110;
  33. else if(num==4) return 0b10001110;
  34. else if(num==5) return 0b10010000;
  35. else if(num==6) return 0b10001000;
  36. else if(num==7) return 0b10000011;
  37. else if(num==8) return 0b01000110;
  38. else if(num==9) return 0b00100001;
  39. else if(num==10) return 0b00000110;
  40. else if(num==11) return 0b00001110;
  41. else if(num==15) return 0b10011011;
  42. return 0;
  43. }
  44.  
  45. int get_frequency(int note,int sharp) {
  46. if(sharp==0) {
  47. switch(note) {
  48. case 0: return 0;
  49. case 1: return 57;
  50. case 2: return 51;
  51. case 3: return 45;
  52. case 4: return 43;
  53. case 5: return 38;
  54. case 6: return 34;
  55. case 7: return 30;
  56. case 8: return 28;
  57. case 9: return 25;
  58. case 10: return 22;
  59. case 11: return 21;
  60. case 15: return 255;
  61. }
  62. } else {
  63. switch(note) {
  64. case 0: return 0;
  65. case 1: return 54;
  66. case 2: return 48;
  67. case 3: return 43;
  68. case 4: return 40;
  69. case 5: return 36;
  70. case 6: return 32;
  71. case 7: return 28;
  72. case 8: return 26;
  73. case 9: return 24;
  74. case 10: return 22;
  75. case 11: return 20;
  76. case 15: return 255;
  77. }
  78. }
  79. return 34;
  80. }
  81.  
  82. void change_note() {
  83. newf = get_frequency(newn,ebr_sharp);
  84.  
  85. if(ebr_upscale==1) newf/=2;
  86.  
  87. secceil = newl;
  88. iceil = newf;
  89. if(iceil==0) TIMSK2 = 0;
  90. else if(iceil==255) {
  91. TIMSK2 = 0;
  92. TIMSK0 = 0;
  93. } else TIMSK2 = 1;
  94. set7seg(get7seg(newn));
  95. }
  96.  
  97. void read_note() {
  98. if(!eeprom_is_ready())
  99. eeprom_busy_wait() ;
  100. uint8_t value = eeprom_read_byte((unsigned int) notepointer++);
  101.  
  102. newn = value&0x0F;
  103. if((value&0x30)==0) newl = 30;
  104. else if((value&0x30)==0x10) newl = 60;
  105. else if((value&0x30)==0x20) newl = 15;
  106. else newl=120;
  107.  
  108. // ebr code
  109. ebr_upscale = (value&0x40)>>6;
  110. ebr_sharp = (value&0x80);
  111.  
  112. if(newn==14) {
  113. // reset
  114. notepointer = 0;
  115. read_note();
  116. }
  117.  
  118. }
  119.  
  120.  
  121. ISR(TIMER2_OVF_vect) {
  122. count++;
  123. if(count==iceil) {
  124. count=0;
  125. PORTB^=2;
  126. PORTB^=16;
  127. }
  128. return;
  129. }
  130.  
  131.  
  132.  
  133. ISR(TIMER0_OVF_vect) {
  134. seccount++;
  135. if(seccount==secceil) {
  136. seccount=0;
  137. //do
  138. change_note();
  139. read_note();
  140. iceil = newf;
  141. count = 0;
  142. // load new note
  143. PORTB ^= 4;
  144. }
  145. return;
  146. }
  147.  
  148. int main(void) {
  149. DDRB |= 30;
  150. DDRC |= 3;
  151. DDRD |= 0xFC;
  152.  
  153. PORTD = 0;
  154. PORTC = 0;
  155. PORTB |= 8;
  156.  
  157. TCNT2 = 0;
  158. TIMSK2 = 1;
  159. TCCR2B = 1;
  160.  
  161. TCNT0 = 0;
  162. TIMSK0 = 1;
  163. TCCR0B = 5;
  164.  
  165. sei();
  166.  
  167. while(1);
  168. return 0;
  169. }
Add Comment
Please, Sign In to add comment