//********************************************************* //Title: Keypad input is sent to LED4-LED1 of TWR-S12G128 Tower module // Based on Example 12.4C * // HCS12 Microcontroller And Embedded Systems * // Using Assembly and C with CodeWarrior * // 1st Edition * // by Mazidi & Causey //Modified and tested by Mazidi //with contribution from Travis Chandler and Shu-Jen Chen //Platform: Axiom Peripheral board on TWR-S12G128 Tower module board * //Compiler: Feescale CodeWarrior IDE v5.9.0 //********************************************************* //On TWR-S12G128 Tower module, 4 green LEDs of LED4-LED1 are connected to PT7-PT4 //They are active LOW. Make sure the jumers for all 4 LEDs are set //Make sure the Axiom Peripheral board is connected to Tower Elavator correctly //The J1 primary side of the Axiom peripheral board must be on the WHITE side of elevator //On Axiom Peripheral board, the rows and columns of 4x4 keypad are connected to PORTA. //On the Axiom TWR-DEV-PERIPH board make sure you have jumpers: JP1 set on the left, JP2 is set, and JP3,JP4, and JP5 on the right. //Use F7 to Make, F5 (Debug) to download, and F5 (Start) to run the program //In CodeWarrior,make sure you are in Open Source BDM when downloading and running. //As you press any key the binary value for the key is placed on the LED4-LED1 of HCS12 Tower Module #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void mSDelay(unsigned int); const unsigned char keypad[4][4] = { 0x01,0x02,0x03,0x0A, 0x04,0x05,0x06,0x0B, 0x07,0x08,0x09,0x0C, 0x2A,0x00,0x23,0x0D //Notice 0x2A is ASCII for '*' and 0x23 for '#' }; unsigned char column,row,mykey; /**************MAIN*******************************/ void main(void){ //OPEN MAIN DDRT = 0xF0; //MAKE UPPER 4-BIT OF PORTT OUTPUT DDRA = 0x0F; //MAKE ROWS INPUT AND COLUMNS OUTPUT while(1){ //OPEN WHILE(1) do{ //OPEN do1 PORTA = PORTA | 0x0F; //COLUMNS SET HIGH row = PORTA & 0xF0; //READ ROWS }while(row == 0x00); //WAIT UNTIL KEY PRESSED //CLOSE do1 do{ //OPEN do2 do{ //OPEN do3 mSDelay(1); //WAIT row = PORTA & 0xF0; //READ ROWS }while(row == 0x00); //CHECK FOR KEY PRESS //CLOSE do3 mSDelay(15); //WAIT FOR DEBOUNCE row = PORTA & 0xF0; }while(row == 0x00); //FALSE KEY PRESS //CLOSE do2 while(1){ //OPEN while(1) PORTA &= 0xF0; //CLEAR COLUMN PORTA |= 0x01; //COLUMN 0 SET HIGH row = PORTA & 0xF0; //READ ROWS if(row != 0x00){ //KEY IS IN COLUMN 0 column = 0; break; //BREAK OUT OF while(1) } PORTA &= 0xF0; //CLEAR COLUMN PORTA |= 0x02; //COLUMN 1 SET HIGH row = PORTA & 0xF0; //READ ROWS if(row != 0x00){ //KEY IS IN COLUMN 1 column = 1; break; //BREAK OUT OF while(1) } PORTA &= 0xF0; //CLEAR COLUMN PORTA |= 0x04; //COLUMN 2 SET HIGH row = PORTA & 0xF0; //READ ROWS if(row != 0x00){ //KEY IS IN COLUMN 2 column = 2; break; //BREAK OUT OF while(1) } PORTA &= 0xF0; //CLEAR COLUMN PORTA |= 0x08; //COLUMN 3 SET HIGH row = PORTA & 0xF0; //READ ROWS if(row != 0x00){ //KEY IS IN COLUMN 3 column = 3; break; //BREAK OUT OF while(1) } row = 0; //KEY NOT FOUND break; //step out of while(1) loop to not get stuck } //end while(1) if(row == 0x10){ mykey=keypad[0][column]; PTT = ~(mykey<<4); //shift left 4 times and OUTPUT TO upper 4-bit of PORTT LED //We invert it since LED4-LED1 are active LOW } else if(row == 0x20){ mykey=keypad[1][column]; PTT = ~(mykey<<4); //shift left 4 times and OUTPUT TO upper 4-bit of PORTT LED //We invert it since LED4-LED1 are active LOW } else if(row == 0x40){ mykey=keypad[2][column]; PTT = ~(mykey<<4); //shift left 4 times and OUTPUT TO upper 4-bit of PORTT LED //We invert it since LED4-LED1 are active LOW } else if(row == 0x80){ mykey=keypad[3][column]; PTT = ~(mykey<<4); //shift left 4 times and OUTPUT TO upper 4-bit of PORTT LED //We invert it since LED4-LED1 are active LOW } do{ mSDelay(15); PORTA = PORTA | 0x0F; //COLUMNS SET HIGH row = PORTA & 0xF0; //READ ROWS }while(row != 0x00); //MAKE SURE BUTTON IS NOT STILL HELD } //CLOSE WHILE(1) } //CLOSE MAIN /**********************SUBROUTINES***********/ void mSDelay(unsigned int itime){ unsigned int i; unsigned int j; for(i=0;i