//Demo of assembly code in C -- //Both ISRs and some of the task code has been converted to ASM // //Note the pragma to turn off automatic register allocation //in the compiler config dialog so that C variables are //available to the assembler #include #include //for debugging using printf, etc #include //I like these definitions #define begin { #define end } void initialize(void); //all the usual mcu stuff #pragma regalloc- //following variables must not be registers //unsigned char prompt[]="Freq> " ; unsigned char reload; //timer 0 reload to set 1 mSec unsigned char time1, time2; //task scheduleing timeout counters unsigned char note; //current note to be played... //task 1 sets note to 0 to start song #define NoteOn = 1 #define NoteOff = 2 #define Velocity = 3 unsigned char sample; unsigned char counter; unsigned int frequency; unsigned char amplitude; unsigned char string[50]; unsigned char count; unsigned char on, off,a; int i; #define macVel = 0x7F #pragma regalloc+ //**************************************************** //table of periods for 1 cycle of each musical note in 1/4 microsec ticks // // midi note freq period in // Hz 1/4 microsec // // 0x30 C3 131 30534 // 0x31 C3# 139 28776 // 0x32 D3 147 27210 // 0x33 D3# 156 25641 // 0x34 E3 165 24242 // 0x35 F3 175 22857 // 0x36 F3# 185 21621 // 0x37 G3 196 20408 // 0x38 G3# 208 19230 // 0x39 A3 220 18181 // 0x3A A3# 233 17167 // 0x3B B3 247 16194 // 0x3C C4 262 15267 // 0x3D C4# 277 14440 // 0x3F D4 294 13605 // 0x40 D4# 311 12861 // 0x41 E4 330 12121 // 0x42 F4 349 11461 // 0x43 F4# 370 10810 // 0x44 G4 392 10204 // 0x45 G4# 415 9638 // 0x46 A4 440 9090 // 0x47 A4# 466 8583 // 0x48 B4 494 8097 // 0x49 C5 523 7648 // put the notes in flash to save RAM //not sure which is right //flash int notetable[8]={15267/4, 13605/4, 12121/4, 11461/4, 10204/4, 9090/4,8097/4, 7648/4}; flash int notetable[25] = {131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523}; //flash int notetable[8] = {262, 294, 330, 349, 392, 440, 494, 523}; flash unsigned char sinetable[16]={0x80, 0xb1, 0xda, 0xf6, 0xff, 0xf6, 0xda, 0xb1, 0x80, 0x4f, 0x26, 0x0a, 0x00, 0x0a, 0x26, 0x4f}; flash unsigned char squaretable[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff}; flash unsigned char sawtable[16] = {0x00, 0xff, 0xef, 0xdf, 0xcf, 0xbf, 0xaf, 0x9f, 0x8f, 0x6f, 0x5f, 0x4f, 0x3f, 0x2f, 0x1f, 0x0f}; flash unsigned char tritable[16] = {0x00, 0x1f, 0x3f, 0x5f, 0x7f, 0x9f, 0xbf, 0xdf, 0xff, 0xdf, 0xbf, 0x9f, 0x7f, 0x5f, 0x3f, 0x1f}; flash unsigned char ramptable[16] = {0x00, 0x0f, 0x1f, 0x2f, 0x3f, 0x4f, 0x5f, 0x6f, 0x8f, 0x9f, 0xaf, 0xbf, 0xcf, 0xdf, 0xef, 0xff}; flash unsigned char wave[16] = {0x00, 0x4f, 0x8f, 0xcf, 0xff, 0xcf, 0xaf, 0x8f, 0x6f, 0x8f, 0xaf, 0xcf, 0xff, 0xcf, 0x8f, 0x4f}; #pragma savereg- // main loop ********************* void main(void) begin initialize(); count = 0; string[0] = string[1] =string[2] = '0'; //main task scheduler loop -- never exits! while(1) begin //begin receiving data if (UCSRA.7) //RX done bit begin // receive data at baud rate = 31.25K for 10 bytes of data if (!on) begin if (counter <10) begin string[counter++] = UDR; end else begin on= 1; UBRRL = 103; end end end //Send data to hyperterminal at baud rate = 9600 // data is converted to a string of bytes where its printed // in binary form if (on & !off) begin putchar('\n'); putchar('\r'); if(count == 3) count = 0; if (count == 0) putchar('0'); if (count == 1) putchar('1'); if (count == 2) putchar('2'); putchar(':') ; putchar(':'); //putchar('0'); if (string[a] & 0x80) putchar('1') ; else putchar('0') ; //putchar('1'); if (string[a] & 0x40) putchar('1') ; else putchar('0') ; //putchar('2'); if (string[a] & 0x20) putchar('1') ; else putchar('0') ; //putchar('3'); if (string[a] & 0x10) putchar('1') ; else putchar('0') ; //putchar('4'); if (string[a] & 0x08) putchar('1') ; else putchar('0') ; //putchar('5'); if (string[a] &0x04) putchar('1') ; else putchar('0') ; //putchar('6'); if (string[a]& 0x02) putchar('1') ; else putchar('0') ; //putchar('7'); if (string[a] & 0x01) putchar('1'); else putchar('0'); a++; count++; if (a== 10) begin off = 1; printf("\n\n\n\n\r"); end //UBRRL = 31; end end end //Set it all up void initialize(void) begin //set up the ports DDRD=0x00; // PORT D is an input //serial setop for debugging using printf, etc. UCSRB = 0x10 + 0x08 ; UCSRC = 0b01000000; UBRRL = 31 ; //set up timer 0 //62.5 x (64x.25) microSec = 1.0 mSec, so prescale 64, and count 62 times. reload=256-250; //value for 1 Msec TCNT0=reload; //preload timer 1 so that is interrupts after 1 mSec. counter = 0; count = 0; on = 0; off = 0; a = 0; //crank up the ISRs #asm sei #endasm end