Cristopher Lubay

HCS0AW32 Timer Toggle  Function

Discussion created by Cristopher Lubay on Sep 10, 2007
Latest reply on Sep 10, 2007 by David Payne
I'm using a bus clock of 8 MHz.
 
here is the code:
 
bool pwmPhase;
 
// selects bus clock, and 8 as the divisor
T2SC = 0x0B;
// total duration is 55 microseconds
T2MODH = 0;
T2MODL = 55;
 
 // sets the channel timer as toggle type
T2CH0SC = 0x14;
 
// sets the timer channel value register
T2CH0VH = 0;
T2CH0VL = 55;
 
pwmPhase = false;
 
// clears the flag
T2CH0SC &= 0x7F;
// enables the interrupt
T2CH0IE = 1;
when an interrupt occurs, the following codes are executed:
//clears the flag
T2CH0SC &= 0x7F;
 
if( pwmPhase )
{
  // OFF time
  T2CH0VH = 0;
  T2CH0VL = 15;
  pwmPhase = false;
}
else
{
  // ON time
  T2CH0VH = 0;
  T2CH0VL = 40;
  pwmPhase = true;
}
 
 
The code above is running.
The problem is the output port of the timer channel 0 is not behaving properly.
Sometimes the ON time becomes the OFF time and the OFF time becomes the ON time for the PWM signal.
What seeems to be the problem?
Can you please help me.
Thank you in advance.

Outcomes