PDA

View Full Version : How to create variable length output pulses without blocking?



rbrockman
10-31-2012, 05:43 AM
I am looking for some guidance on creating four simultaneous and independent variable length output pulses on separate pins on the T3 without any blocking code to create the delays determining the pulse width. This is not PWM - but individual pulses.

I have this functionally working now, but I'm using a loop to comparing the clock to elapsed times for each output pin. This doesn't block like delay(), but I would like to use timers/interrupts to accomplish this if possible and stop relying on polling the clock.

Possible approach:
* calculate necessary pulse width
* set a pin high
* start a timer counting (the pulse width time of between 100uS and 700uS)
* move on to other code processing
* when timer reaches the set value - interrupt processing and perform function of setting pin low (completing the pulse)
* continue processing....

This would appear to require 4 timers and the ability to associate them with 4 output pins.

Is this the right approach? Are there better ways to solve this?

I am seeking any advice on ways to accomplish this on the T3.

Thanks!

Paul
11-01-2012, 08:52 PM
I believe you can do this using the timer hardware, but you'll need to directly manipulate the hardware registers. That also means reading the Flextimer chapter from the chip's reference manual, which is rather daunting. Here's my initial thoughts on how it might be done.

The FTM0 module has 8 compare registers, each associated with a pin. Normally those are controlled by analogWrite(). You might look at the analogWrite() code. The hardware can be configured in many different ways. The default is PWM. By default, the timer counts up and rolls back over to zero at some maximum value. If you can wait for your pulse to begin at the rollover, you could just use PWM mode. The registers are buffered, so if you write the PWM value in advance, it will take effect at the next rollover. The pin goes high at the rollover, than then goes low when the counter is equal to the compare register. To get just a single pulse, you'll need to respond after the compare but before the next rollover, to reconfigure the comparator to not keep creating more pulses. It's probably possible to do this with an interrupt.

The FTM0 also has a mode where 2 comparators can be chained together for a single pin. Then the pin can go high when the counter equals the first comparator and low when it equals the second. Using that approach, you could set the first just slightly past the current count, so the pulse begins very soon, and set the second one so it ends when you want. Again, you'll need to respond after the 2nd event so the pulse doesn't begin again when the counter rolls over and get back to whatever value you wrote to the 1st register.

At least that's the rough idea. Anything I've said here is trumped by whatever Freescale's reference manual says.