#include #include #include // for sine //I like these definitions #define begin { #define end } #define countMS 62 //ticks/mSec unsigned long accumulator @0x2f0; unsigned char highbyte @0x2f3; //the HIGH byte of the accumulator variable unsigned long increment; char sineTable[256] @0x300; //need loc to avoid glitch unsigned int i, time ; char amp, count; interrupt [TIM0_OVF] void sgen(void) begin //the DDR code and scaling accumulator = accumulator + increment ; OCR0 = 128 + sineTable[highbyte] >> amp ; //generate rising amplitude // 62 counts is about 1 mSec count--; if (0 == count ) begin count=countMS; time++; //in mSec end end void main(void) begin DDRB=0xff; //fast PWM mode, full clock rate, toggle oc0 (pin B3) //16 microsec per PWM cycle implies max freq for 16 samples of // 1/(16e-6*16) = 3900 Hz. //TCCR0 = 0b01101001 ; //turn on timer 0 overflow ISR TIMSK = 0b00000001 ; //init the sine table for (i=0; i<256; i++) begin sineTable[i] = (char)(127.0 * sin(6.283*((float)i)/256.0)) ; end //init the DDS phase increment increment = 0x08ffffff ; time=0; TCCR0 = 0b00000001 ; #asm sei #endasm while(1) begin if (time==50) begin time=0; //phase lock the sine gen and timer accumulator = 0; TCNT0 = 0; OCR0 = 0; //generate the amplitude scaling -- start with divide by 16 amp = 4; //turn on pwm TCCR0 = 0b01101001 ; end //if if (time==1) amp=3; //ramp up if (time==2) amp=2; if (time==3) amp=1; if (time==4) amp=0; if (time==10) amp=1; //ramp down if (time==11) amp=2; if (time==12) amp=3 ; if (time==13) amp=4; if (time==14) TCCR0=0b00000001 ; end // end while end //end main