Main Site Documentation

Generating a 20kHz clock signal without PWM


I’m trying to generate a 20kHz clock signal without using PWM since I’m already using those as PWMs at 1 Mhz, and until recently I didn’t realize that different pwms can’t run at different frequencies.

Bit banging won’t work for me since other code needs to run at the same time as this signal is generated, so i can’t have the processor stuck in a loop all the time. I tried setting up an interrupt using RLP to flip the bit but it’s too slow, the highest speed i could get to is about 12kHz.

I’ve been trying to figure out if i can use any other features on the board to generate a clock signal. From what I can tell, SPI, I2C, and MCI require me to constantly feed data into them in order for them to output their clock signal. If I could get around this, it seems like I can set any of their clock speeds to match what I need. I can’t have the processor feeding data into those peripherals constantly (unless theres a very efficient way of doing this that I don’t know of) or else the processor will be unavailable for other things. If theres a way to set up a UART to constantly send the same byte then I could use that, but as far as I can tell if need to keep providing the byte.

Any suggestions on how I can do this? I’m using a Fez Mini. Thanks,



Output compare can do it but it is software and 20khz will result into 20000 interrupts per second!


I hadn’t seen the output compare class, but I don’t see how it would be faster than interrupts in RLP, and that maxed out at about 12khz. I’ll try it on monday once I have access to an oscilloscope just be to sure.

I also just noticed that lpc23xx.h has addresses for 2 PWMs, but the user manual and spec sheets all say that there is only 1 available. Any idea why the second set of addresses are there and if i can use them?



It is not faster than RLP correct. Both are native. Maybe use additional hardware?


I’m trying to avoid using additional hardware if possible, but I might have to.

I was really hoping someone knew of a way to get the SPI, I2C or MCI clock outputs to run continuously without the processor having to be involved, since all 3 of these have a clock divider circuit inside that I’d really like to use.


Maybe have a look at the I2S port. This is meant to stream music at a fixed rate so maybe…

What about using the real time clock crystal? Put a 20kHz crystal on there and you have a permanently running clock source.


Use rlp and a serial port fe. When the transmit register is empty, you get an interrupt where you can fill it again.


Would a timer be acceptable? It can be configured to toggle an output pin whenever the counter matches a register and automatically restart. I use this in my TLC 5940 driver to generate the grey scale clock.


@ SteH: that sound like exaclty what he needs without any software overhead


That looks perfect, thanks a lot everyone


There are a lot of timers on the USBizi, of which only Timer0 is used.

Doesn’t all the timers support OutputCompare?

From the Timer section of the CPU

[quote]• A 32-bit Timer/Counter with a programmable 32-bit prescaler.
• Counter or Timer operation.
• Two 32-bit capture channels per timer, that can take a snapshot of the timer value
when an input signal transitions. A capture event may also generate an interrupt.
• Four 32-bit match registers that allow:
– Continuous operation with optional interrupt generation on match.
– Stop timer on match with optional interrupt generation.
– Reset timer on match with optional interrupt generation.
• Up to four external outputs corresponding to match registers, with the following
– Set LOW on match.
– Set HIGH on match.
– Toggle on match.
Thus you can set the Match register to Toggle a pin on Match, and also to Reset the Timer on Match.
Then you set your Match to 40KHz, and let it Toggle the output, which will give you 20KHz.

This is a full hardware solution that needs no software intervention after started…


Got the timer working, and it’s exactly what I needed, I just didn’t realize when I first posted that the timers could automatically toggle a pin, I thought i’d have to write an interrupt to do that. However, it seems that the match pins are only pinned out on the Fez Mini for timer 4 (MAT3.0 and 3.1). Not sure where pins 82 and 85 go, according to they go somewhere but I can’t find where to connect them to.

I also managed to hijack the MCI and I2S peripherals to generate clocks for me (thanks realiser for suggesting I2S). I was hoping to be able to output a few different frequencies and now I’ve got 3 to play with.

Thanks everyone!


Having a look at the schematics Mat3.0 and 3.1 are Di7 and Di8 on the header.
pins 82 and 85 are Mat2.0 and 2.1.
I use Mat3.0 in my TLC5940 driver. Just strip what is not needed.


Errol – so if Timer0 is the only timer being used, does that mean Timer1, Timer2, and Timer3 are free to be used for whatever we want? I have a somewhat similar application (pulse counter instead of PWM generator), and I know there’s code posted for using registers with Timer3, but I wasn’t sure if the other timers could also be used, or were reserved for something else.


Yep, Timer 1/2/3 are free for use.

I used them, with the 4 Match registers each, to drive 12 servos at once. All worked fine…