Nanosecond Timers

22 Apr 2012

Hi,

I have recently being doing some PWM work with the trusty ATmega328, and that required me to set up the timers with nanosecond precision (500ns to be precise :o) )... which is a bit of a struggle as the old ATmega328 can't really do very much in 500ns... this made me wonder about porting my application to my beloved mbed...

I've not had a chance to read the LPC's timer registers yet, but will have to do so when I get some time to see if I can set it up for nanosecond work... but then I thought, why can't the mbed standard library provide ns precision?

Any plans to add such a feature?

23 Apr 2012

nanosecond timers? Many a resolution of few hundred

25 May 2012

Hi Steve, in my Application I use 500ns... So half a us resolution :)

25 May 2012

I assume you are using the 1768 board and I think it is clocked at 96MHz

If you set the timer clock divider to 1/1 and use LPC_TIM0->PR = 0x0000002F; /* decimal 47 */ you will get 500us ticks but I don't know if there is enough cpu time to do what you want.

Alex

25 May 2012

Alexan E wrote:

I assume you are using the 1768 board and I think it is clocked at 96MHz

If you set the timer clock divider to 1/1 and use LPC_TIM0->PR = 0x0000002F; /* decimal 47 */ you will get 500us ticks but I don't know if there is enough cpu time to do what you want.

Alex

Yeah thanks for the tip, I've had a quick look at the registers, and will probably have to hit the hardware directly, it just would have been nice have access to the higher resolution timer via the standard mbed API! :)

Cheers :)

25 May 2012

I' not familiar with the mbed libraries but if you decide to hit the registers directly I think I can help. Check ARMwizard in my profile.

Alex

25 May 2012

Alexan E wrote:

I' not familiar with the mbed libraries but if you decide to hit the registers directly I think I can help. Check ARMwizard in my profile.

Alex

That is very kind, I will return to this topic in due course :)

27 Jun 2012

Hi all.

I want to generate a square wave with nanosecond resolution period, but i know very little about LPC1768 timer register and interruption to write a class allows me generate delays with nanosecond resolutions (like "timer" class).

Where I can find information about this topic?

Thanks in advance for your help.

Darío

28 Jun 2012

Hello Dario. I think first reply on this thread addressed this. I believe you will need a faster clock. How does a CPU running at approx 100MHz provide single digit resolution of nanosecond scaled times?

Maybe an FPGA clocked very high would meet your needs? http://www.altera.com/devices/fpga/cyclone-iv/cyiv-index.jsp

Dario Delvalle wrote:

Hi all.

I want to generate a square wave with nanosecond resolution period, but i know very little about LPC1768 timer register and interruption to write a class allows me generate delays with nanosecond resolutions (like "timer" class).

Where I can find information about this topic?

Thanks in advance for your help.

Darío

28 Jun 2012

Hi Anthony.

Resolution must be in a few hundred nanosecond.

For a particular application I will need 500 ns.

I think the LPC1768 is sufficient for this application.

Regards

28 Jun 2012

You could do this by carefully crafting an assembler loop, execution time of each instruction is around 10 ns (at 100 MHz)

By using assembler you can reach an interrupt routine in about 120 nsec so that is probably not exact enough.

28 Jun 2012

He wants a square wave, so won't directly addressing PWM hardware allow for that? You will need to make sure the PWM units runs at full clock speed instead of default quarter clock speed, but I guess it should work.

28 Jun 2012

Besides creating a square wave I need read a digital input during the output positive cycle. I think PWM function not let me know the output level.

28 Jun 2012

Well you can connect an interrupt with the PWM edges, that reads a digital input. You say during the positive cycle, so it does not need to be exactly on the edge? Because it wont be fast enough for that for sure, just reading a digital input is possible, but then you probably have to do something with it.

29 Jun 2012

Thanks Erik.

For now I'm trying to work directly with the timer counter registers and interruptions. But I do not find a reference to show me the way to indicate the names of this registers in the compiler, where I can find this information?

regards

29 Jun 2012

There is the LPC17xx.h header file: http://mbed.org/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h?rev=24

There you combine the typedefs with the declarations at the bottom. So for example LPC_PWM1->IR=...

30 Jun 2012

The register names are

LPC_TIM0->CTCR
LPC_TIM0->TC
LPC_TIM0->PR
LPC_TIM0->MCR
LPC_TIM0->MR0
LPC_TIM0->MR1
LPC_TIM0->MR2
LPC_TIM0->MR3
LPC_TIM0->CCR
LPC_TIM0->EMR
LPC_TIM0->TCR

for timer 1,2,3 use LPC_TIM1, LPC_TIM2, LPC_TIM3

Alex

30 Jun 2012

Thanks Alex.

I knew the definition of registers are in the file headers LPC17xx.h but I could not find it. However by Keil Microvision compiler I acceded to this file and I can know the names of all registers.

I'll be working on it.

Regards

30 Jun 2012

I think you can get things easier using ARMwizard, take a look http://alexan.edaboard.eu/

Note that you have to be careful not to change the operation of the pins used by the mbed chip. Also you have to add extern "C" in front of the inteerupt hangler function.

I assume you are using a core clock of 96MHz, if you set the timer clock divider to 1/1 then you can set PR for 250us and set a compare match output to toggle every 250ns for a total period of 500ns without any need of additional code. Also use the reset on match and interrupt on match.

Alex