//Getting data from push-button SW1-SW4 and sending it to LED1-LED4 for HCS12G128 Tower Module using CodeWarrior //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 (SW4-SW1) //SW4-SW1 are active LOW (normally HIGH) See TWR-HCS12G128 schematic //There are 8 jumpers allowing the connection of the SW1-Sw4 and LED1-LED4 to the CPU pins. //If you use LEDs and SWs make sure the jumpers for them are set. //Notice the PAD7-PAD4 pins are normally used for Analog-to-Digital converter (ADC). //To use them for digital input via SW4-SW1, we must write 1 to ATDDIEN register bits. See Chap 12 of the HCS12G Ref. Manual. //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 //As you press the push-button Sw, an LED is turned on (LEDs shows the status of SW). //Modified and tested by Mazidi from Example 7-10 of Mazidi & Causey HCS12 textbook //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. #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void MSDelay(unsigned int); void main(void) { /* put your own code here */ unsigned char mybyte; DDRT = 0xF0; //PT7-PT4 as output for LED4-LED1 ATDDIEN = 0x00F0; //Make PAD7-PAD4 as digital Input for SW4-Sw1. (The default is analog for ADC) PER1AD = 0xF0; //Enable internal pull-ups for the PAD4-PAD7 pins DDR1AD= DDR1AD | 0x00; //Make PAD7-PAD4 pins as input. Optional since the default is input. for(;;) { mybyte=PT1AD; //Get data from push button SW mybyte=mybyte&0xF0; //Mask the unwanted bits PTT = mybyte; //and send it to LEDs on PortT MSDelay(1); //Optional } } void MSDelay(unsigned int itime) { unsigned int i; unsigned int j; for(i=0;i