Microcontroller Timing - Need sub ms resolution

Status
Not open for further replies.

jamie_s

Member
Hi all,

Im currently playing around trying to make a device which outputs a certain pulsetrain.
The output needs to alternate, being high or low for exactly one or two milliseconds.

I have so far tried a PICAXE-18, but the actual pulses from this are more like 1.3ms output for a 1ms pulse in code and 2.5ms pulse output for a 2ms pulse in code. This was due to the timing inaccuracies in the Picaxe.

here is a snippet of my code:
symbol sig = 1
start:
low sig
pause 1
high sig
pause 2
low sig
pause 1
high sig
pause 1
low sig
pause 2
high sig
pause 1
low sig
pause 2

goto start

So, I am after some opinions on what microcontroller I can use to provide these pulses exactly 1 and 2 ms long. The micro needs to be very stable, and will be exposed to a wide temperature range ( 0 - 50 *C )

Thankyou

Jamie

Nigel Goodwin

Super Moderator
Just use any PIC - as long as it has an external crystal oscillator, a 16F628 (which I think is what the PICAXE18 uses) would be fine. Running with a 4MHz crystal would give you accurate timing to within a few uS, running at 20MHz to below 1uS.

Depending how stable (and accurate) it needs to be, the 0-50C temperature range will cause the oscillator frequency to vary slightly, if you need very high stability it's common to use a crystal oven to prevent temperature drift. However, it doesn't sound like that degree of accuracy is likely to be needed.

jamie_s

Member
Yeah, I did use the PICAXE-18 with an external 4mhz osc,
but the timing was off quite a bit.
I posted a question on the picaxe forum, and many other people also saw the same inaccuracies in the picaxe timings, when connected to a 'scope.

Thats why im looking at alternative micro's that would be more accurate, possibly with uS resolution, so i can fine tune my program to work.

The device the pulse train is fed to will only work with the correct pulse train, and when I tested with my Picaxe circuit, it would not function. When re-tested with the correct module (providing the pulsetrain) worked fine.

Jamie

Nigel Goodwin

Super Moderator
jamie_s said:
Yeah, I did use the PICAXE-18 with an external 4mhz osc,
but the timing was off quite a bit.
I posted a question on the picaxe forum, and many other people also saw the same inaccuracies in the picaxe timings, when connected to a 'scope.

Thats why im looking at alternative micro's that would be more accurate, possibly with uS resolution, so i can fine tune my program to work.

Sorry, I didn't make myself clear enough!.

Don't use a PICAXE - use a PIC!. Any interpreter isn't going to be fast or accurate enough - your program requirements are extremely simple, and you can always trim your delays to within 1uS (with a 4MHz clock) by simply adding NOP instructions (1uS each). It would be very simple to write in assembler - you can even find a delay code generator on the PICLIST at http://www.piclist.com.

jamie_s

Member
Thanks Nigel,

I've got a handful of 16f84's here so I'll give one a go...

I've never programmed in C or ASM before, but i'll try suss out some sample code and go from there.

Thanks again

Jamie

Nigel Goodwin

Super Moderator
jamie_s said:
Thanks Nigel,

I've got a handful of 16f84's here so I'll give one a go...

I've never programmed in C or ASM before, but i'll try suss out some sample code and go from there.

Thanks again

Jamie

As I mentioned, it will be nice and easy, a 16F84 will be a decent choice.

Looking at your existing code, you could simply do this (which is only a small segment):
Code:
Start:
bcf PORTB, 1 ;set pin low
call Delay1 ;delay 1mS
bsf PORTB, 1 ;set pin high
call Delay2 ;delay 2mS
bcf PORTB, 1 ;set pin low
call Delay1 ;delay 1mS
.
.
.
.
goto Start

All you would need to do is extend that for the rest of your pulses, provide initialisation code, and the two subroutines Delay1 and Delay2 (you could simply generate those using the delay generator on the PICLIST).

Status
Not open for further replies.

Replies
14
Views
5K
Replies
7
Views
3K
Replies
15
Views
2K
Replies
7
Views
909
Replies
0
Views
3K