main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #define F_CPU 20000000UL
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #include <avr/pgmspace.h>
  6. #include "wavetables.h"
  7. const uint16_t NOTE_CYCLES[108] PROGMEM = {
  8. 15420, 14534, 13742, 12950, 12244, 11544, 10920, 10308, 9712,
  9. 9182, 8670, 8180, 7710, 7294, 6872, 6496, 6122, 5788,
  10. 5460, 5154, 4868, 4592, 4336, 4090, 3864, 3648, 3442,
  11. 3248, 3066, 2894, 2730, 2578, 2432, 2296, 2168, 2046,
  12. 1930, 1822, 1720, 1624, 1532, 1446, 1366, 1290, 1218,
  13. 1148, 1084, 1024, 966, 912, 860, 812, 768, 724,
  14. 684, 646, 610, 574, 542, 512, 484, 456, 430,
  15. 406, 384, 362, 342, 324, 304, 288, 272, 256,
  16. 242, 228, 216, 204, 192, 182, 172, 162, 152,
  17. 144, 136, 128, 122, 114, 108, 102, 96, 92,
  18. 86, 82, 76, 72, 68, 64, 62, 58, 54,
  19. 52, 48, 46, 44, 42, 38, 36, 34, 32
  20. };
  21. volatile uint8_t pwm_cycle = 0;
  22. // ISR(TIMER1_COMPB_vect)
  23. // {
  24. // }
  25. ISR(TIMER1_OVF_vect)
  26. {
  27. ++pwm_cycle;
  28. }
  29. typedef struct {
  30. uint16_t pwm_cycle, pwm_num_cycles;
  31. uint8_t wt;
  32. } oscillator_t;
  33. //the PCINT0_vect vector is used to identify the pin change interrupt.
  34. // ISR(PCINT0_vect)
  35. // {
  36. // mute = !((PINB & (1 << PB1)));
  37. // }
  38. int main()
  39. {
  40. uint16_t n;
  41. uint8_t d, button_id = 1, line = 0, adc_timer = 0, lfo_volume = 0;
  42. oscillator_t vco = { 0, 0, 0 }, lfo = { 0, 0, 0 };
  43. //OSCCAL = 83;
  44. PLLCSR |= 0x6;
  45. // setup output pins
  46. DDRB = (1 << DDB4) | (1 << DDB2) | (1 << DDB1) | (1 << DDB0);
  47. // set line mux pins low and turn op ADC pull-up resistor
  48. PORTB = ~((1 << PB0) | (1 << PB1) | (1 << PB2)) | (1 << PB3);
  49. // Set the ADC input to PB2/ADC1, left adjust result
  50. ADMUX = (3 << MUX0) | (1 << ADLAR);
  51. // Set the prescaler to clock/8 & enable ADC
  52. ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);
  53. // Configure counter/timer1 for fast PWM on PB4
  54. GTCCR = (1 << PWM1B) | (1 << COM1B1);
  55. // Set Timer 1 prescaler to clock/1 = 64.0MHz.
  56. TCCR1 = (1 << CS10);
  57. // Interrupts on OC1A match + overflow
  58. //TIMSK |= (1 << OCIE1B) | (1 << TOIE1);
  59. // Interrupts on OC1A overflow
  60. TIMSK |= (1 << TOIE1);
  61. OCR1B = 0;
  62. OCR1C = 255;
  63. // GIMSK = (1 << PCIE); //Enable Pin Change Interrupts
  64. // PCMSK = (1 << PCINT1); //Set Pin 2 to cause an interrupt. You can also enable it on multiple pins
  65. ADCSRA |= (1 << ADSC); // Start A2D Conversions
  66. while (1) {
  67. if(((ADCSRA >> ADSC) & 0x1) == 0) {
  68. if(adc_timer == 0) {
  69. d = ADCH;
  70. switch(line) {
  71. case 0:
  72. if(button_id > 0) {
  73. d /= 5;
  74. switch(button_id) {
  75. case 7:
  76. d += 10;
  77. break;
  78. case 6:
  79. d += 9;
  80. break;
  81. case 5:
  82. d += 7;
  83. break;
  84. case 4:
  85. d += 5;
  86. break;
  87. case 3:
  88. d += 4;
  89. break;
  90. case 2:
  91. d += 2;
  92. break;
  93. default:
  94. break;
  95. }
  96. vco.pwm_num_cycles = pgm_read_word_near(NOTE_CYCLES + 21 + d);
  97. }
  98. //vco.pwm_num_cycles = 7812;
  99. break;
  100. case 1:
  101. if(button_id > 0) {
  102. lfo.pwm_num_cycles = 0xffff - (d << 8);
  103. }
  104. break;
  105. case 2:
  106. lfo_volume = d;
  107. //lfo_volume = 127;
  108. break;
  109. case 3:
  110. //button_id = (d < 77 ? 7 : (d > 250 ? 0 : (279 - d) / 29));
  111. if(d >= 241)
  112. button_id = 0;
  113. else if(d >= 216)
  114. button_id = 1;
  115. else if(d >= 195)
  116. button_id = 2;
  117. else if(d >= 173)
  118. button_id = 3;
  119. else if(d >= 144)
  120. button_id = 4;
  121. else if(d >= 106)
  122. button_id = 5;
  123. else if(d >= 42)
  124. button_id = 6;
  125. else
  126. button_id = 7;
  127. break;
  128. case 4:
  129. vco.wt = d / (256 / WAVETABLES_NUM);
  130. break;
  131. case 6:
  132. lfo.wt = d / (256 / WAVETABLES_NUM);
  133. break;
  134. default:
  135. // nop;
  136. break;
  137. }
  138. line = (line + 1) % 8;
  139. PORTB = (PORTB & ~0x7) | line;
  140. adc_timer = 1;
  141. }
  142. else if(adc_timer == 1) {
  143. ADCSRA |= (1 << ADSC); // Start A2D Conversions
  144. adc_timer = 0;
  145. }
  146. else
  147. ++adc_timer;
  148. }
  149. cli();
  150. n = pwm_cycle;
  151. sei();
  152. d = 0;
  153. if(lfo_volume <= 252) {
  154. vco.pwm_cycle = (vco.pwm_cycle + n) % vco.pwm_num_cycles;
  155. d += (uint16_t)pgm_read_byte_near(WAVETABLES[vco.wt] + (uint16_t)(((uint32_t)(vco.pwm_cycle)) * WAVETABLES_SIZE / vco.pwm_num_cycles)) * (255 - lfo_volume) >> 8;
  156. }
  157. if(lfo_volume > 8) {
  158. //n += 2;
  159. lfo.pwm_cycle = (lfo.pwm_cycle + n) % lfo.pwm_num_cycles;
  160. d += (uint16_t)pgm_read_byte_near(WAVETABLES[lfo.wt] + (uint16_t)(((uint32_t)(lfo.pwm_cycle)) * WAVETABLES_SIZE / lfo.pwm_num_cycles)) * lfo_volume >> 8;
  161. }
  162. cli();
  163. if(button_id > 0)
  164. OCR1B = d;
  165. pwm_cycle -= n;
  166. sei();
  167. }
  168. return 0;
  169. }