Example 15 Interrupt-Driven Controller Lecture L6.2
Connecting an LED to a Port P pin
// Example 15a Traffic Lights #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" int dtime; // delay time int ix; // index into states const int numstates = 6; const char PortH[] = { // --RYGRYG 0x0C, // 00001100 0x14, // 00010100 0x24, // 00100100 0x21, // 00100001 0x22, // 00100010 0x24, // 00100100 }; const int delay[] = { 488, // 5 sec delay 98, // 1 sec delay 98, // 1 sec delay 488, // 5 sec delay
void interrupt 7 handler(){ dtime--; if(dtime == 0){ PTH = PortH[ix]; // turn on next lights dtime = delay[ix]; // get next delay time ix++; // increment index if(ix == numstates){ // after going through all states ix = 0; // reset index to 0 } clear_RTI_flag();
void main(void) { PLL_init(); // set system clock frequency to 24 MHz DDRH = 0xFF; // all bits of Port H are outputs RTI_init(); // initialize RTI to 10.24 ms interrupts ix = 0; // reset index into states dtime = 1; // start traffic light right away while(1) { // do nothing while traffic light goes }
Interrupt-Driven Blinking SOS International Morse Code
// Example 15b: Interrupt-Driven Controller: SOS #include <hidef.h> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" unsigned short dtime; // delay time int ix; // index into states const int numstates = 18; const char seg7[] = { 0x6D, 0x00, 0x6D, 0x00, 0x6D, 0x00, // S 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x00, // O }; const char delay[] = { 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x18, // dots 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, // dashes 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x30, // dots
void interrupt 7 handler(){ dtime--; if(dtime == 0){ seg7_on(seg7[ix]); // turn on next display dtime = delay[ix]; // get next delay time ix++; // increment index if(ix == numstates){ // after going through all states ix = 0; // reset index to 0 } clear_RTI_flag();
void main(void) { PLL_init(); // set system clock frequency to 24 MHz seg7_enable(); RTI_init(); ix = 0; // reset index into states dtime = 1; // start display right away while(1) { // do nothing while display goes }