I am using a MC68HC908QY4A microcontroller. It has 2 timer channels. Is it possible to have one channel (PTA0) output a 1KHz square waveform and another channel (PTA1) output a 2KHz square waveform?
Thanks,
Hello,
I can see a couple of possibilities.
Use PWM mode with 50 percent duty for the 1kHz signal, and use output compare mode on the other channel to generate the 2kHz signal. The PWM output will not require any interrupts, but the output compare channel interrupt will require servicing every 250 microseconds. The modulo value will need to be set for an overflow period of 1 millisecond, and since it is not free-running, this will add complication to the calculation of the next output compare value.
Another alternative would be to use output compare mode for both channels. This would increase the number of interrupts required, with one every 500 microseconds, and another every 250 microseconds. However, since the TIM module could be free-running, this would simplify the calculation of the next output compare value for both signals.
Do you have a strict phase relationship requirement between the two waveforms?
Regards,
Mac
Hi Aurora,
Another option, similar to Mac's, is to set the modulus to overflow every half-millisecond.
You could then use one channel as 50% PWM to give you 2 kHz, and toggle the other channel (or an I/O pin) on every overflow (or output-compare) to give you 1kHz.
I believe (from un-refreshed memory) that there is either a toggle on compare or toggle on overflow function in the QY's timer, but I don't have access to the datasheet right now. That would allow generating both square-waves with no interrupts to service.
Hello,
I rather like Rocco's approach that would not require any interrupts.
Both toggle on overflow and toggle on compare are available for both channels, however it would appear than toggle on overflow would not work unless output compare mode is selected. So, toggle on compare for the 1kHz channel should give the desired result.
Perhaps the following setup would work, assuming trimmed 3.2MHz bus clock frequency, and with channel 0 used for the 2kHz output, and channel 1 for the 1kHz output.
TSC = 0x30; // Stop and clear TIM counter, prescale 1
TMOD = 1599; // Overflow period 500us
TCH0 = 800; // 50% duty for Ch 0
TSC0 = 0x1A; // Unbuffered PWM with clear O/P on compare
TCH1 = 800; // Toggle point for Ch 1 output
TSC1 = 0x14; // Output compare with toggle on compare
TSC_TSTOP = 0; // Enable TIM counter
With this setting, the 1kHz signal should toggle on each negative edge of the 2kHz signal.
Regards,
Mac