Found a strange behavior of TPM2 on MC9S08SH4 simulator [CodeWarrior]
This is a report regarding a strange behavior of the TPM2 module found on the MC9S08SH4 Full Chip Simulation.
PWM Emulation on Ganged Output
It was found that a software emulation must be written to use the ganged output as a PWM output. So, I have develop a PWM emulation program using the Output Compare (OC) feature and the Over Flow (OVF) feature of the TPM2 module which is one of the timer modules contained in the MC9S08SH4 MCU.
My idea is very simple. Set the ganged output when an OVF event occurs, and clear the ganged output when an OC event. Both events can be captured by interrupts and the procedure can be included in the interrupt service routine (ISR). As the result, it is expected to get a PWM waveform at the ganged output.
Attempted Program
I have written a program and tried with the "Full Chip Simulation" on the CodeWarrior.
word count_tpm2ch0; // Timer count at TPM2CH0 word count_tpm2ovf; // Timer count at TPM2OVF void __interrupt VectorNumber_Vtpm2ch0 tpm2ch0_isr(void) { count_tpm2ch0 = TPM2CNT; TPM2C0SC_CH0F = 0; // clear channel flag } void __interrupt VectorNumber_Vtpm2ovf tpm2ovf_isr(void) { count_tpm2ovf = TPM2CNT; TPM2SC_TOF = 0; // clear overflow flag } void main(void) { TPM2SC_TOIE = 1; // Enable overflow interrupt TPM2SC_CPWMS =0; // Edge aligned TPM2C0SC_CH0IE = 1; // Enable channel 0 interrupt TPM2C0SC_MS0x = 1; // Output compare TPM2C0SC_ELS0x = 0; // software only TPM2C0V = 10000; // 50% duty TPM2MOD = 19999; // 20000 cycle TPM2SC_PS = 0; // 1X TPM2SC_CLKSx = 1; // bus clock EnableInterrupts; /* enable interrupts */ /* include your code here */ for(;;) { __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ }
This is a very simple test program to check the PWM behavior. It is expected that the two variables "count_tpm2ch0" and "count_tpm2ovf" become 50% and 0% value of the timer counter respectively. As a simulation result, an unexpected strange result at the Global variable pane found.
count_tpm2ch0 17 unsigned int count_tpm2ovf 56 unsigned int
"count_tpm2ch0" indicates 0.0% and "count_tpm2ovf" indicates 0.3%. What's wrong ?
Strange Behavior
A strange behavior of the "TPM2C0SC_CH0F" OC event flag was confirmed by step simulation. When the timer counter returns to 0x0000 from the modulo value (TPM2MOD - 1) , the "TPM2C0SC_CH0F" flag is set as well as the "TPM2SC_TOF" OVF event flag set. As the result, the OC event ISR is processed prior to the OVF event ISR according to the interrupt vector priority order to get the 0.0% timer counter value. And then, OVF event ISR gets 0.3% timer count value.
In addition, no OC events occur when the timer counter reaches to the 50% value. So, I think the logic to set the OC event flag is incorrect.
TPM1 Works Well
MC9S08SH4 has an another TPM module TPM1. A simulation is attempted with same program for TPM1.
word count_tpm1ch0; // Timer count at TPM1CH0 word count_tpm1ovf; // Timer count at TPM1OVF void __interrupt VectorNumber_Vtpm1ch0 tpm1ch0_isr(void) { count_tpm1ch0 = TPM1CNT; TPM1C0SC_CH0F = 0; // clear channel flag } void __interrupt VectorNumber_Vtpm1ovf tpm1ovf_isr(void) { count_tpm1ovf = TPM1CNT; TPM1SC_TOF = 0; // clear overflow flag } void main(void) { TPM1SC_TOIE = 1; // Enable overflow interrupt TPM1SC_CPWMS =0; // Edge aligned TPM1C0SC_CH0IE = 1; // Enable channel 0 interrupt TPM1C0SC_MS0x = 1; // Output compare TPM1C0SC_ELS0x = 0; // software only TPM1C0V = 10000; // 50% duty TPM1MOD = 19999; // 20000 cycle TPM1SC_PS = 0; // 1X TPM1SC_CLKSx = 1; // bus clock EnableInterrupts; /* enable interrupts */ /* include your code here */ for(;;) { __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ }
In this case, the variables indicate expected values, 50% and 0%
count_tpm1ch0 10015 unsigned int count_tpm1ovf 17 unsigned int
I suppose that this is a TPM2's own problem.
Project Archive
Please try a sample project if you interested in. To get a ZIP file, Save this as "SH07.zip"
References
HCS08 Unleashed: Designer's Guide to the HCS08 Microcontrollers
- 作者: Fabio Pereira
- 出版社/メーカー: Booksurge Llc
- 発売日: 2007/11/13
- メディア: ペーパーバック
コメント 0