9 years, 7 months ago.

Single microsecond timing

Hi I am trying to handle 2-way communication over a single wire interface that needs to update each microsecond. So each microsecond there is potentially a rising or falling edge.

It seems like the timing scale is too small for the wait_us() command and too big for relying on noops.

My idea was to use FastPWM to create a 1MHz clock and then feed it back into one of the Digital input pins with an interrupt triggered on each rise. Then each microsecond I could update my DigitalIO pin to send or receive bits.

I'm not sure if this is a dumb way to go about things.

I was thinking an alternative method may be to attempt to use the low level timing methods. It looks to me like I could use Timer 2 with input capture pins to find the time difference between the rising and falling edges. Then for the output use match registers.

Does this make any sense, and is it likely to produce the results I'm looking for? Any hints on understanding how the low level timers work would be greatly appreciated.

1 Answer

9 years, 7 months ago.

InterruptIn won't work at 1MHz, too much overhead. This for sure is something you want polling based, not interrupt based. So it probably would be a better idea to either use match registers (there are some examples on how to do that on your LPC1768), or just use it as regular timer running at high speed + FastIO for reading pin states/writing them.

However possibly another peripheral can be (ab)used to create your waveforms.

Accepted Answer

Thanks! I'm not sure what you mean by a regular timer running at high speed though. Are you saying to use the internal low level timers (like accessing Timer 2) or should I just repeatedly poll the PWM signal to find out when one microsecond has elapsed?

posted by Jimmy Hack 06 Oct 2014

I mean if you set timer 2 as free running timer at full speed (so 96MHz), then you got plenty of accuracy to poll that one to see when 1us has passed.

But could you link the format that is used for the interface?

posted by Erik - 06 Oct 2014

Ok that makes sense. I'm goofing around with trying to communicate with a gamecube controller. A low bit is 3us low, 1us high while a high bit is 1us low 3us high.

http://www.int03.co.uk/crema/hardware/gamecube/gc-control.html

posted by Jimmy Hack 06 Oct 2014

Ah that one. Generally such type of protocols should be fairly robust to timing errors: It only needs to be able to check which one is shorter. It might even work with regular wait, but it is kinda questionable accuracy wise. So I would simply do the option of using timer 2 as free running timer at full speed, and use that one for read/write operations using fastIO to minimize delays.

Using capture inputs is a good alternative that you can also consider.

posted by Erik - 06 Oct 2014