//**Note, for this code to operate properly, the USER jumpers MUST be set on 
//        both the module and project board.

//******************************************************************************
// Lab1 - Example
// Purpose:  This program illustrates how to perform basic inputs and outputs of
//           the CSM12C32 module and PBMCUSLK project board.  Pressing SW1 will 
//           light up LED1 (on the module) and LEDs 1&2 (on the project board). 
//           Pressing SW2 will light up LED2 (on the module) and LEDs 3&4.  Note 
//           that PB1 is tied to SW1, and PB2 is tied to SW2.
//
//           To operate correctly, both the USER jumpers located on the module and
//           the project board must be set.
//******************************************************************************

#include <hidef.h>      /* common defines and macros */
#include <mc9s12c32.h>     /* derivative information */


#pragma LINK_INFO DERIVATIVE "mc9s12c32"

#pragma CODE_SEG DEFAULT


void main(void) {

  //These four commands configure the direction of ports A,B,E, and P.
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRE = 0x00;
  DDRP = 0x00;
  
  
  DDRT = PTT_PTT4_MASK|PTT_PTT5_MASK;                 // Sets direction of port T
  DDRAD = PTAD_PTAD7_MASK|PTAD_PTAD6_MASK|
              PTAD_PTAD5_MASK|PTAD_PTAD4_MASK;        // Sets direction of port AD
  
  PTT = ~(PTT_PTT4_MASK|PTT_PTT5_MASK);               // Electronically enables the pushbuttons
                                                      // and LEDs on projectboard.
  PTAD = PTAD_PTAD7_MASK|PTAD_PTAD6_MASK|
              PTAD_PTAD5_MASK|PTAD_PTAD4_MASK;        // Turns on all the project board LEDS
  

  for(;;) {
    if((PORTE & PORTE_BIT0_MASK) == 0) {              // Checks the current status of SW1.
      PORTA = ~PORTA_BIT0_MASK;                       // Activates LED1 on module, note that the LEDS on the module
                                                      // are active low, turning on when set to 0 and off when set to 1.
                                        
      PTAD = PTAD|(PTAD_PTAD5_MASK|PTAD_PTAD4_MASK);  // Turns LEDS 1&2 on the project board on.
                                                      // Unlike the module's, these LEDs are active high,
                                                      // off when set to 0 and on when set to 1.
    }      
    else {
      PORTA = PORTA_BIT0_MASK;                        // Turns LED1 on module off.
      PTAD = PTAD&~(PTAD_PTAD5_MASK|PTAD_PTAD4_MASK); // Turns LEDs on project board off.
    }

    
    if((PTP & PTP_PTP5_MASK) == 0) {                  // Check to see if SW2 is pressed.
      PORTB = ~PORTB_BIT4_MASK;                       // Activates LED2 on module
      PTAD = PTAD|(PTAD_PTAD7_MASK|PTAD_PTAD6_MASK);  // Turns LEDS 3&4 on project board on.
      
    }
    else {
      PORTB = PORTB_BIT4_MASK;                        // Turns LED2 on module off.
      PTAD = PTAD&~(PTAD_PTAD7_MASK|PTAD_PTAD6_MASK); // Turns LEDs 3&4, located on project board, off.
    }
  } 
  

}