How to generate 0 to 20kHz square wave with FTM?

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

How to generate 0 to 20kHz square wave with FTM?

Jump to solution
1,294 Views
cfgmgr
Contributor III

Using a K60 (bare metal board), I am looking for a good way to generate a simple 50% duty cycle square wave on an output pin at a rate from 0Hz to approx 20kHz, with (hopefully) 1Hz resolution or better.  In essence, I'd like to create a function generator that can sweep from 0 to 20KHz.  

I have experimented with examples of using EPWM mode, and I can see how to use the FTM0 module for varying pulse widths and frequencies, but the problem I am having is how best to do this at low frequencies (i.e. < 9Hz).  

I'm assuming that with my system clock at 75Mhz, my lowest frequency achievable FTM frequency is (75Mhz / 128 / 65535) or about 8.9Hz?  If so, what is the best way to control the channel outputs below 9Hz?  Can this be done elegantly in hardware with just an FTM?  Or do I need a combination of periodic timer + ISR code?  

Any pointers would be greatly appreciated!  Thanks,

-Bill

1 Solution
1,035 Views
mjbcswitzerland
Specialist V

Hi

The problem with the FTM (in this case) is that it has 16 bit counters (with limited pre-scaling) so slow frequencies are troublesome.

I would look at using a PIT instead since, even when clocked by a 60MHz bus clock, frequencies less that 0.01Hz can be generated (and up to 30MHz).

PIT IRQ would probably be OK for your range but (much) higher accuracy and (much) lower jitter can be achieved by triggering a DMA transfer on each PIT match to write to a port toggle register (this generating a square wave).

Furthermore, the same DMA trigger can(/could) be used to load a new timer value to achieve smooth sweeps (or programmable sweeps that are controlled purely by HW, based on a sweep curve).

Regards

Mark

Kinetis for professionals: http://www.utasker.com/kinetis.html

View solution in original post

2 Replies
1,036 Views
mjbcswitzerland
Specialist V

Hi

The problem with the FTM (in this case) is that it has 16 bit counters (with limited pre-scaling) so slow frequencies are troublesome.

I would look at using a PIT instead since, even when clocked by a 60MHz bus clock, frequencies less that 0.01Hz can be generated (and up to 30MHz).

PIT IRQ would probably be OK for your range but (much) higher accuracy and (much) lower jitter can be achieved by triggering a DMA transfer on each PIT match to write to a port toggle register (this generating a square wave).

Furthermore, the same DMA trigger can(/could) be used to load a new timer value to achieve smooth sweeps (or programmable sweeps that are controlled purely by HW, based on a sweep curve).

Regards

Mark

Kinetis for professionals: http://www.utasker.com/kinetis.html

1,035 Views
cfgmgr
Contributor III

Thank you Mark!  

This was exactly the kind of advice I was looking for.  

I had considered a "PIT+ISR" solution, but did not like the idea of generating so many interrupts (especially at higher frequencies).  Your DMA idea is a very nice alternative, and a clever use of the hardware.  I'll give it a try.

Thank you so much for your prompt reply and good idea!

Regards,

-bill

0 Kudos