#include "music.h" #include "tables.h" #include #include #define interrupt(x) void __attribute__((interrupt(x))) // table size 1024 (10bit) fraction 6bit // index = 10.6 sample = 8.8 #define RATE 8192 // #define RATE 16384 #define FTABLE_SIZE 1024 #define ETABLE_SIZE 256 #define NCHAN 4 struct channel { int8_t const *ftable, *etable; uint16_t findex, eindex; uint16_t fstep, estep; uint16_t amp; }; struct channel chan[NCHAN]; uint16_t next_pwm = 0; volatile uint8_t wait = 1; int16_t ac; uint16_t ticks; volatile uint16_t count; uint8_t state = 0; int main() { // Disable watchdog WDTCTL = WDTPW | WDTHOLD; // Internal timer BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; // TA1 on P1.2 P1DIR = 0x04; P1SEL = 0x04; P1SEL2 = 0x00; TACTL = TASSEL_2 | TACLR; // SMCLK source for timer TACCR0 = 256; TACCTL0 = CCIE; TACCTL1 = OUTMOD_7; TACTL |= MC0; // state = 66; // while (1); state = 2; __enable_interrupt(); int nextchan = 0, i = 0; state = 1; while (1) { for (i = 0; i < NCHAN; i++) { chan[i].ftable = 0; chan[i].etable = env_musicbox; chan[i].estep = 6; // chan[i].amp = 20; } int ni; for (ni = 0; ni < sizeof(notes); ni++) { uint8_t note = notes[ni]; if (note & 0x80) { chan[nextchan].ftable = wave_musicbox; chan[nextchan].fstep = note_fstep[note & 0x7F]; chan[nextchan].eindex = 0; nextchan++; if (nextchan == NCHAN) nextchan = 0; } else { uint32_t tick = note * 30; for (; tick; tick--) { ticks++; while (wait) ; int16_t accu = 0; for (i = 0; i < NCHAN; i++) { struct channel *c = &chan[i]; if (!c->ftable) continue; int16_t sample = (int16_t)c->ftable[c->findex >> 6]; sample = (int16_t)(sample * (int16_t)c->etable[c->eindex >> 8]) >> 8; sample = (int16_t)(sample * 50) >> 8; accu += sample; c->findex += c->fstep; if ((uint16_t)(c->eindex + c->estep) > c->eindex) c->eindex += c->estep; else c->ftable = 0; } ac = accu; next_pwm = ((accu + 0x7F) & 0xFF); wait = 1; } } } } state = 3; return 0; } interrupt(TIMER0_A0_VECTOR) timer_isr() { count++; if (count > 6) { TACCR1 = next_pwm; count = 0; wait = 0; } TACCTL0 &= ~CCIFG; }