#include /* common defines and macros */ #include /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c128" /* ECE348, Spring 2016 Group ## Carnegie Mellon 18-348 lab 01 program 2 -- C version This program uses the switches and LEDs on the microcontroller module itself. When SW1 is pressed, LED1 turns on and LED2 turns off until SW2 is pressed. When SW2 is pressed LED2 turns on and LED1 turns off. This program demonstrates how to use I/O on the microcontroller itself. This program uses bit 0 and 1 of port P as input because these bits are connected to SW1 and SW2, respectively, on the MCU. Bit 4 through 7of port B are used as output because these bits are connected to LED1 through LED4, respectively, on the MCU. */ void main(void) { EnableInterrupts; /* NOTE: Writing a 0 to bit y of DDRx sets bit y of Port x to input. Writing a 1 to bit y of DDRx sets bit y of Port x to output. */ /* set port B to output */ DDRB_BIT4 = 1; DDRB_BIT5 = 1; DDRB_BIT6 = 1; DDRB_BIT7 = 1; /* set port P to input */ DDRP = 0x00; /*enable pullups on pin 0 and 1 of port P*/ PERP_PERP1 = 1; PERP_PERP0 = 1; /* NOTE: LEDs on CSM12C128 board are inverted (e.g. putting a 1 or high value causes the LED to be off while putting a 0 or low value causes the LED to be on. */ /* Turn off LED1 and LED2 */ PORTB = 0xF0; /* NOTE: Reading a 1 from a bit connected to a SW on the MCU means the button is not pressed. Reading a 0 from a bit connected to a SW on the MCU means the button is pressed. */ /* loop forever */ for(;;) { if(PTP_PTP0 == 0) PORTB = 0xE0; else if(PTP_PTP1 == 0) PORTB = 0xD0; } /* please make sure that you never leave this function */ }