LPC11U24: Generating a 5.4 MHz square wave output

16 Jun 2014

Hi, I need to generate a 5.4MHz square wave output from one of the pins on LPC11U24 kit. How could I do this? Help much appreciated!

17 Jun 2014

write code to configure an output port bit. add code to create a forever loop In the loop, invert, toggle the output port bit. use a library delay function to time the loop for the desired frequency.

Better: read about the timers in the CPU's peripherals. Determine how to configure a timer to have a period of 1/5.4MHz in time. Configure the timer to toggle a port bit on each overflow.

and there are more ways

17 Jun 2014

To make Steve's suggestion (about the counter/timer toggleing a pin) as accurate as the 12Mhz Xtal clock, you can also consider changing the CPU clock frequency. Observe that -

Quote:

5.4 MHz (desired output frequency ) * 2 * 4 = 43.2 MHz (useful CPU clock frequency) = 12 MHz (Xtal frequency) / 5 * (144 / (2 * 2))

You can search prior forum posts, library routine descriptions, mbed spec. sheets, etc. to figure out how to change the internal PLL clock operation per the above hint (don't forget to update any serial baud rates, etc. as may be needed).

17 Jun 2014

Just curious... why the specific number of 5.4MHz? (no accuracy requirement stated).

18 Jun 2014

Thanks for the replies. I have tried the following code using the FastPWM library, but it doesn't work. Anyone know why?

include the mbed library with this snippet

#include "mbed.h"
#include "FastPWM.h"

FastPWM clkoutput(p21);



int main()
{

    clkoutput.period(1.0/5430000);
    

    while(1) {
        clkoutput.write(0.5f);
                }
}

The 5.4MHz is for a resonant circuit. So doesn't need to be exact.

18 Jun 2014

Three items which may be of interest -

  1. Repeatedly calling clkoutput.write() in a loop probably keeps resetting the clock, thus never letting it run. Try moving the call to before the loop, and just leave an empty statement (single semi-colon) in the loop.
  2. Floating point arithmetic in the argument of the call to clkoutput.period() may make it appear like you are requesting the needed frequency, but that near to the CPU clock frequency the function's actual control of the output frequency is pretty coarse. Depending on the 'Q' of your resonant circuit, you may need tighter control than the general-purpose library function yields. I expect that a 6 MHz output is as close as the function can come. There may be similar limits on how close to a 50%/50% duty cycle can be achieved by the library functions.
  3. The factor of 144 in my hint can probably be varied in steps of 1 count, giving control of the output frequency in steps of 0.7%. A multiplier of 145 yields a nominal output frequency of 5.4375 MHz.
18 Jun 2014

He already uses FastPWM and not regular PWM (which can't go higher than 1MHz). It should reach 5.3333MHz with the default clock setup. (Btw if you would change the clock, you can still use FastPWM, it uses the actual clock rate, you just need to make sure after changing the clock setup SystemCoreClockUpdate or something similar is called, but as said, 5.33MHz should work with regular clock).

If you have the latest FastPWM version (if you imported it directly you should have it), then repeatedly calling write should work, only in older versions it didn't.

And it should in the period call use double precision by default if you do it like this (indeed regular floats lack precision, which is why FastPWM uses doubles). Might try also using 5430000.0, but I would be surprised if that is the problem.

Now I wanted to start speculating what the problem might be, but basic check: If you run that code, does it start blinking the LEDs? It should do so, unless they decided in the last changes to the mbed they didn't want it anymore (which would be a bad idea). If it indeed blinked the LEDs: This is the error pattern, it is called when an error happens, and if you check your serial terminal it should also give a basic description. (Insta-edit: Checked source code, doesn't do that for sure anymore, only the line number, can't say I am a fan about that).

Back to the actual error: p21 isn't a pwm capable pin on the LPC11u24. These are: https://mbed.org/handbook/PwmOut (actually some others work too, but then you lose all timer, wait, etc options).

19 Jun 2014

Thanks Erik, Got it working now, closest I can get is 5.32MHz measured, here is my new code:

code

#include "FastPWM.h"
#include "mbed.h"

FastPWM clkoutput(p20);



int main()
{

    clkoutput.period(1.7e-7);
    
    // 1.2 = 6.83 MHz
    // 1.5 = 6.02 MHz
    // 1.6 = 5.32 MHz
    // 1.7 = 5.32 MHz
    // 1.83= 4.85 MHz
    

    while(1) {
        clkoutput.write(0.5f);
                }
}

19 Jun 2014

That indeed sounds about right (with a small measurement/other error).