123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #define F_CPU 20000000UL
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <avr/pgmspace.h>
- #include "wavetables.h"
- const uint16_t NOTE_CYCLES[108] PROGMEM = {
- 15420, 14534, 13742, 12950, 12244, 11544, 10920, 10308, 9712,
- 9182, 8670, 8180, 7710, 7294, 6872, 6496, 6122, 5788,
- 5460, 5154, 4868, 4592, 4336, 4090, 3864, 3648, 3442,
- 3248, 3066, 2894, 2730, 2578, 2432, 2296, 2168, 2046,
- 1930, 1822, 1720, 1624, 1532, 1446, 1366, 1290, 1218,
- 1148, 1084, 1024, 966, 912, 860, 812, 768, 724,
- 684, 646, 610, 574, 542, 512, 484, 456, 430,
- 406, 384, 362, 342, 324, 304, 288, 272, 256,
- 242, 228, 216, 204, 192, 182, 172, 162, 152,
- 144, 136, 128, 122, 114, 108, 102, 96, 92,
- 86, 82, 76, 72, 68, 64, 62, 58, 54,
- 52, 48, 46, 44, 42, 38, 36, 34, 32
- };
- volatile uint8_t pwm_cycle = 0;
- // ISR(TIMER1_COMPB_vect)
- // {
- // }
- ISR(TIMER1_OVF_vect)
- {
- ++pwm_cycle;
- }
- typedef struct {
- uint16_t pwm_cycle, pwm_num_cycles;
- uint8_t wt;
- } oscillator_t;
- //the PCINT0_vect vector is used to identify the pin change interrupt.
- // ISR(PCINT0_vect)
- // {
- // mute = !((PINB & (1 << PB1)));
- // }
- int main()
- {
- uint16_t n;
- uint8_t d, button_id = 1, line = 0, adc_timer = 0, lfo_volume = 0;
- oscillator_t vco = { 0, 0, 0 }, lfo = { 0, 0, 0 };
- //OSCCAL = 83;
- PLLCSR |= 0x6;
- // setup output pins
- DDRB = (1 << DDB4) | (1 << DDB2) | (1 << DDB1) | (1 << DDB0);
- // set line mux pins low and turn op ADC pull-up resistor
- PORTB = ~((1 << PB0) | (1 << PB1) | (1 << PB2)) | (1 << PB3);
- // Set the ADC input to PB2/ADC1, left adjust result
- ADMUX = (3 << MUX0) | (1 << ADLAR);
- // Set the prescaler to clock/8 & enable ADC
- ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);
- // Configure counter/timer1 for fast PWM on PB4
- GTCCR = (1 << PWM1B) | (1 << COM1B1);
- // Set Timer 1 prescaler to clock/1 = 64.0MHz.
- TCCR1 = (1 << CS10);
- // Interrupts on OC1A match + overflow
- //TIMSK |= (1 << OCIE1B) | (1 << TOIE1);
- // Interrupts on OC1A overflow
- TIMSK |= (1 << TOIE1);
- OCR1B = 0;
- OCR1C = 255;
- // GIMSK = (1 << PCIE); //Enable Pin Change Interrupts
- // PCMSK = (1 << PCINT1); //Set Pin 2 to cause an interrupt. You can also enable it on multiple pins
- ADCSRA |= (1 << ADSC); // Start A2D Conversions
- while (1) {
- if(((ADCSRA >> ADSC) & 0x1) == 0) {
- if(adc_timer == 0) {
- d = ADCH;
- switch(line) {
- case 0:
- if(button_id > 0) {
- d /= 5;
- switch(button_id) {
- case 7:
- d += 10;
- break;
- case 6:
- d += 9;
- break;
- case 5:
- d += 7;
- break;
- case 4:
- d += 5;
- break;
- case 3:
- d += 4;
- break;
- case 2:
- d += 2;
- break;
- default:
- break;
- }
- vco.pwm_num_cycles = pgm_read_word_near(NOTE_CYCLES + 21 + d);
- }
- //vco.pwm_num_cycles = 7812;
- break;
- case 1:
- if(button_id > 0) {
- lfo.pwm_num_cycles = 0xffff - (d << 8);
- }
- break;
- case 2:
- lfo_volume = d;
- //lfo_volume = 127;
- break;
- case 3:
- //button_id = (d < 77 ? 7 : (d > 250 ? 0 : (279 - d) / 29));
- if(d >= 241)
- button_id = 0;
- else if(d >= 216)
- button_id = 1;
- else if(d >= 195)
- button_id = 2;
- else if(d >= 173)
- button_id = 3;
- else if(d >= 144)
- button_id = 4;
- else if(d >= 106)
- button_id = 5;
- else if(d >= 42)
- button_id = 6;
- else
- button_id = 7;
- break;
- case 4:
- vco.wt = d / (256 / WAVETABLES_NUM);
- break;
- case 6:
- lfo.wt = d / (256 / WAVETABLES_NUM);
- break;
- default:
- // nop;
- break;
- }
- line = (line + 1) % 8;
- PORTB = (PORTB & ~0x7) | line;
- adc_timer = 1;
- }
- else if(adc_timer == 1) {
- ADCSRA |= (1 << ADSC); // Start A2D Conversions
- adc_timer = 0;
- }
- else
- ++adc_timer;
- }
- cli();
- n = pwm_cycle;
- sei();
- d = 0;
- if(lfo_volume <= 252) {
- vco.pwm_cycle = (vco.pwm_cycle + n) % vco.pwm_num_cycles;
- 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;
- }
- if(lfo_volume > 8) {
- //n += 2;
- lfo.pwm_cycle = (lfo.pwm_cycle + n) % lfo.pwm_num_cycles;
- 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;
- }
- cli();
- if(button_id > 0)
- OCR1B = d;
- pwm_cycle -= n;
- sei();
- }
- return 0;
- }
|