//Interrupt Programming with CodeWarrior on HCS12G128 Tower Using PAD (Port AD) Interrupt. //In HCs12G128, beside the XIRQ and IRQ pins Ports P, J , and AD have harware interrupt capabilities. //Chapter 2 of HCS12G Ref. Manual describes all the ports of HCS12G128 //Sections 2.4.3.49 through 2.4.3.64 give the details of registers associated with PAD, inclduing the interrupt registers. //On TWR-S12G128 Tower board, 4 LEDs (LED4-LED1)are connected to PT7-PT4 pins. PT7=LED4,.., PT4=LED1 //LED1-LED4 are active LOW. See TWR-HCS12G128 schematic //The 4-pins of PAD7-PAD4 are also connected to the push-button (debounce) switches (SW1-SW4) //SW1-SW4 are active LOW (normally HIGH) See TWR-HCS12G128 schematic //Make sure the jumpers for LED4-LED1 and push-button SW4-SW1 are set. //In this program, LED4 on PORTT toggles continuously while waiting for an Interrupt to come in from any push-button Switches on PAD //Pressing any push-button switches of SW1-SW4 will interrupt the main and toggles LED7 for a short period of time. //Written and tested based on examples in Chapter 11 of HCS12 textbook by Mazidi & Causey //Make sure to choose "minimal startup code" and "Small" for memory model(no banked) when creating porject for Interrupts #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void MSDelay(unsigned int); void main(void) { DDRT = 0b10010000; //PT7 and PT4 as output for LED4 and LED1 ATDDIEN = 0x00F0; //Make PAD7-PAD4 as digitla Input for SW4-Sw1. (Th default PAD is analog for A-to-D) PER1AD = 0xF0; //Enable internal pull-ups for the PAD4-PAD7 pin DDR1AD= DDR1AD | 0x00; //Make PAD7-PAD4 as input. Optional since the default is input. //PAD interrupt setup PIE1AD = 0b11110000; //enable PAD interrupt for SW1-SW4 PPS1AD = 0x00; //Make it Falling Edge-Trig since SW4-Sw1 are active LOW (normally HIGH). __asm CLI; //Enable interrupts globally //EnableInterrupts; //another way for Enabling interrupts globally for(;;) { //do something PTT = PTT ^ 0b00010000; //Toggle LED1 continuously while waiting for Interrupt MSDelay(250); } //stay here until an interrupt comes in } //PAD Interrupt. Pressing any SW1-Sw4 debounce switche will toggle LED4 for a short period of time interrupt (((0x10000-Vportad)/2)-1) void PORTAD_ISR(void) //interrupt VectorNumber_Vportad void PORTAD_ISR(void) //another way declare interrupt { unsigned char x; for (x=0;x<30;x++) //Upon PAD Interrupt the LED4 toggles for a short period { PTT = PTT ^ 0b10000000; //toggle LED4 for short time MSDelay(50); //During the toggling LED4, the LED1 stops toggling. Why? } PIF1AD = PIF1AD | 0b11110000; //clear PAD Interupt Flags for the next round. Writing HIGH will clear the Interrupt flags } void MSDelay(unsigned int itime) { unsigned int i; unsigned int j; for(i=0;i