1
\$\begingroup\$

The PIC 18FXX2 microcontroller has a Capture/Compare/PWM unit. I understand what capture and PWM are used for. Capture can be used to time the duration of an event. PWM is of course used to generate rectangular waveforms of variable duty cycle. But, I do not understand what the compare function is used for. Is it used to, for example, generate a pulse of a particular duration?

\$\endgroup\$
  • \$\begingroup\$ A PWM is basically a compare with automatic reload/restart function. Is the timer value higher than the compare value the output is set to HIGH e.g. otherwise it's set to LOW (depends on the configuration of the output). \$\endgroup\$ – c-a Jan 18 '16 at 19:54
1
\$\begingroup\$

Yes, the compare function can be used to generate very precise high-resolution and low-jitter pulses of various durations (there will be a minimum based on how quickly an interrupt to set the compare registers can be serviced, but there is no practical maximum to the pulse length since rollovers can be accommodated in firmware).

This is possible because the hardware can directly change the port pin state upon a match, and the processor need only set up the next match in time.

PWM is more for when you don't care so much about the period but want a precise ratio of on-to-off time.

Edit: as @gbulmer says the compare can be used to trigger a 'special event' which resets timer 1 and starts and A/D conversion (leaving the port pins untouched). It can also generate a software interrupt. In my experience (others may have different approaches) the most useful mode is to control the output pins directly and leave the counters free running with no resets or writes for cycle-perfect waveforms. The following options are available:

enter image description here

In order to extend the timer's range beyond a 16 bit rollover, a interrupt can be generated that does not alter the output pin, a software counter incremented and the next comparison set up. It's necessary to have a minimum amount of time between setting up the comparison and the next match or the timer could miss the match and roll over again before hitting the count. This requires careful thought and design- one of the costs of using a lower-end 8-bit microcontroller with 16-bit counters rather than 32-bit counters. With a few more bytes extension, the counters can be made to not roll over within a product or a human lifetime.

\$\endgroup\$
  • \$\begingroup\$ I don't feel this has clarified the difference between PWM, and streams of square waves. Maybe add that both the count triggering the pin change, and the overall timer count before 'wrapping or restarting' are available to the program? Also, I think "but there is no practical maximum to the pulse length since rollovers can be accommodated in firmware", might be overstating it a little bit. Something softer, like "the practical maximum duration may be extended beyond the timer's maximum count by firmware using interrupts or polling" would alert a reader to the mechanisms, and the constraints. \$\endgroup\$ – gbulmer Jan 18 '16 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.