//******************************************************************************
// Lab 2 Simulation Code
// Purpose:  This is a 4-bit counter that increments when SW1, or PB2 is pressed on the
//           project board. 
//           
//           Note that this code can be used with the CodeWarrior simulator.  
//           For the simulation, use the following memory values for the ports:
//           DDRE = 0x00000009         PORTE = 0x00000008
//           DDRP = 0x0000025A         PTP = 0x00000258 
//           DDRAD= 0x00000272         PTAD = 0x00000270
//******************************************************************************

#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.
  DDRB = 0xFF;
  DDRA = 0xFF;
  DDRE = 0x00;                                     // DDRE = 0x00000009  PORTE = 0x00000008 
  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.
                                                   // Not needed in simulation
 
  PTAD = 0x00;                                     // Sets Counter to zero.   


  for(;;) {
      if((PORTE & PORTE_BIT0_MASK) != 0) {            // See if push button on Port E is being pressed
         PTAD = PTAD+0x10;                            // Increments the upper 4-bits after SW1 or PB2 is pressed
         while(((PORTE & PORTE_BIT0_MASK) != 0));     // Loop until pushbutton is depressed
      }
    
  }
  

}