HCS0AW32 Timer Toggle  Function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HCS0AW32 Timer Toggle  Function

2,325 Views
cris2pher
Contributor I
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.
Labels (1)
0 Kudos
Reply
3 Replies

978 Views
Ake
Contributor III
Hi,
Here is a piece of code that does what you wanted.
 
Regards,
Ake
 
Message Edited by t.dowe on 2009-10-27 02:04 PM
0 Kudos
Reply

978 Views
peg
Senior Contributor IV
Hi,
 
Well Ake's code is saying the same thing as Mac's first part. i.e. change to edge aligned PWM mode from toggling output compare, which I would agree with.
What remains to be seen is which assumption made by these two is correct. i.e. did the OP really want a PWM that toggles between 27% and 73% every period or just running at 73%.
My money is on bigmac here.
 
0 Kudos
Reply

978 Views
bigmac
Specialist III
Hello,
 
It appears you are attempting to generate a waveform with an on period of 40 us and an off period of 15 us, to give a total waveform period of 55 us.  Is this correct?
 
There are two potential methods of doing this using the TPM -
  1. Edge aligned PWM mode for the TPM channel (interrupt not required), or
  2. Output compare interrupt for each edge of the waveform.
You seem to be attempting to combine both PWM operation and interrupts.  Because of the relatively short period the PWM method is probably more appropriate, with minimal software overheads.
 
For example:
 
T2MOD = 55;     /* TPM overflow period 55 us */
T2CH0V = 40;    /* PWM on Ch 0 with period 40 us */ 
T2CH0SC = 0x28; /* Edge aligned PWM mode for Ch 0 */

The waveform should now be generated without the need for interrupts.  The possible disadvantage of this method is that the other TPM channel are stuck with the same very short timer overflow period, which may limit their usefulness.

To use the output compare interrupt method, the T2MOD value should remain at its default value.  The "toggle on output compare" setting must be avoided to prevent the possibility of incorrect phasing.  Instead, within the ISR code, the setting for T2CH0SC should be either 0x58 (clear output on compare) or 0x5C (set output on compare) depending on the next required output state.  You will also need to calculate the next output compare value, which will depend on the edge currently being processed, and this value used to update T2CH0V.

The output compare interrupt method will be disrupted if interrupt processing takes too much time relative to the required  waveform period, or if the occurrence of other interrupts prevents update of the next output compare value in a timely manner.

Regards,
Mac

0 Kudos
Reply