Scott Phillips

9S12E - Toggling an external pin on an output compare event

Discussion created by Scott Phillips on Apr 9, 2007
Latest reply on Apr 13, 2007 by Scott Phillips

I'm having trouble getting an external pin to toggle on an output compare event.  I've had lots of success using the output compare feature to generate software delays.  I just can't figure out how to get the hardware to automatically toggle the associated IOC pin.
 
I'm using a MC9S12E64 device in a 112 pin package.
I'd like to get PortU.0 / IOC2.4 (pin 32) to toggle on a timer 2 channel 4 output compare event.
 
I put some test code in the Timer 2 channel 4 interrupt routine to toggle an unused pin on port P.  With an oscilloscope I can see that the output compare is working correctly, but the IOC24 pin is not toggling.
 
So far my code looks like this:

Code:

//---------------- Timer 2 ----------------// setups specific to Timer 2 TIM2_TSCR1_TEN = 0;  // inhibit timer 2 during register initializationTIM2_TSCR1_TSWAI = 1;  // timer disabled when CPU is in wait modeTIM2_TSCR1_TSFRZ = 1;  // timer disabled when CPU is in freeze modeTIM2_TSCR1_TFFCA = 0;  // normal flag clearing modeTIM2_TSCR2_TOI = 0;  // disable timer overflow interruptTIM2_TSCR2_TCRE = 0;  // counter reset inhibited and counter free runsTIM2_TSCR2_PR = 4;  // prescaler = bus clock/16 = 20Mhz/16 = 800nsec prescaler clk periodTIM2_TFLG1 = TIM2_TFLG1_C4F_MASK | TIM2_TFLG1_C5F_MASK |       TIM2_TFLG1_C6F_MASK | TIM2_TFLG1_C7F_MASK; // clear capture event flagsMODRR = 0x00;   // timer 2 channels 4,5,6,7 are on PU0,PU1,PU2,PU3 pins// - - - - - - - - - - - - - - - - - - - - - - - -// setups specific to channel 4TIM2_OC7M_OC7M4 = 0;  // no output port assignment on output compareTIM2_OC7D_OC7D4 = 0;  // no output compare 7 mapping to port 4TIM2_TIOS_IOS4 = 1;  // configured as output compareTIM2_TCTL1_OM4 = 0;  // toggle PortU.0 (IOC2.4)TIM2_TCTL1_OL4 = 1;TIM2_TTOV_TOV6 = 0;  // inhibit output compare pin toggle on timer overflowTIM2_TIE_C4I = 0;  // timer interrupt disabled    // The channel 4 initialization will enable this interrupt// - - - - - - - - - - - - - - - - - - - - - - - -// setups specific to Timer 2TIM2_TSCR1_TEN = 1;  // enable timer 2//-----------------------------------------//---------------- Port U ----------------    // set bits 0 and 4-7 as outputs / all others as inputsDDRU = DDRU_DDRU0_MASK | DDRU_DDRU4_MASK | DDRU_DDRU5_MASK |       DDRU_DDRU6_MASK | DDRU_DDRU7_MASK;RDRU = 0x00;   // all outputs drive at full strengthPTU = 0x00;   // set the output pins to their reset state   /****************************************************************************//* initChannel4() - initialize timer 2 channel 4         *//****************************************************************************/void initChannel4(){ PTP_PTP0 = 0;    // set the initial state of the debug pin TIM2_TC4 = TIM2_TCNT + Delay; // set up the first interrupt for 1msec latter  TIM2_TIE_C4I = 1;   // enable the Timer 2 channel 4 interrupt }/********************************************************************************//* time_ch4_isr() - Timer2 Channel 4 Interrupt code      *//********************************************************************************/__interrupt void tim2_ch4_isr(){ if (PTP_PTP0)    // toggle PortP.0 for DEBUG  PTP_PTP0 = 0; else  PTP_PTP0 = 1;  TIM2_TC4 = TIM2_TCNT + Delay; // set up the next interrupt TIM2_TFLG1 = TIM2_TFLG1_C4F_MASK; // clear timer 2 channel 4 interrupt request flag}


Does anyone have any suggestions as to why PortU.0 / ICO2.4 is not toggling?

I also tried creating a project using processor expert and the TimerOut bean and it generated similar code with similar results.

Is there a problem using bit fields to set up some some of the timer registers?  I do set up channels 5 - 7 after channel 4 using bit field defines as well.

Based on the documentation, I'm also slightly confused as to how the OC7M register should be set up.  I tried setting the bits in this register as a test, but got the same results.

Any help would be greatly appreciated.

Outcomes