Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Converting timer0 value to seconds

Status
Not open for further replies.

chris414

New Member
I've set up the timer0 on my PIC18F2550 to record time intervals, however I am not sure how to convert this reading into seconds. The oscillator is running at 1Mhz, timer0 is set to 16bit, the prescaler is on 1:1, and the timer is using the internal oscillator. The integer value I am reading off the timer is 6153 - what do I have to do to this number to give me a number in seconds?
 
Last edited:
Hi,

If your oscillator is 1mhz then the Clock frequency that the pic and timers use is OSC f / 4

However most osc frequencies will not give you an exact second.

You can do a very close second by doing a Delay loop or look at MR RBs recent post on doing a 1 second delay.

A more accurate way is the use a 32k timing crystal on Timer1 / ISR .
 
I've set up the timer0 on my PIC18F2550 to record time intervals, however I am not sure how to convert this reading into seconds. The oscillator is running at 1Mhz, timer0 is set to 16bit, the prescaler is on 1:1, and the timer is using the internal oscillator. The integer value I am reading off the timer is 6153 - what do I have to do to this number to give me a number in seconds?

hi,
Its usual to set the TMR0 to an integer sub multiple of a second, set the TMR0 to interrupt and use a register counter to count the interrupts until the count reaches 1second. OK.?
 
The integer value I am reading off the timer is 6153 - what do I have to do to this number to give me a number in seconds?
If all you're asking is how to convert 6153 instruction cycles into seconds, then just divide it by the instruction cycle frequency (which is fosc/4 or 250kHz). 6153/250k = 0.024612 seconds.
 
Thanks for the replies. I'm integrating the output from a gyroscope to give me the angular displacement at a certain time - is this method of timing going to be accurate enough? Wp100, you say I should use timer1 - is the timing crystal just more accurate?
 
Thanks for the replies. I'm integrating the output from a gyroscope to give me the angular displacement at a certain time - is this method of timing going to be accurate enough?
I take it the gyroscope sends digital information (I mean PWM or PPM e.g.). You probably want as much accuracy as you can get (although I've no idea how much you need for your application), as when you integrate, you're summing the error as well. Increasing the clock rate will increase the timing resolution/accuracy.

Wp100, you say I should use timer1 - is the timing crystal just more accurate?
There's no reason why the crystal would be any more accurate. Also, 32kHz gives worse timing resolution than your fosc/4. Using timer 1 (or 3) allows you to utilise the CCP (compare / capture) module which lets the hardware capture the timer value when, e.g., a pin changes. This gives you better precision than if you try to capture the timer value on change in software (due to variable interrupt latencies etc.). I would recommend using the main oscillator to clock the timer (don't bother with the 32kHz one).
 
I've set up the timer0 on my PIC18F2550 to record time intervals, however I am not sure how to convert this reading into seconds. The oscillator is running at 1Mhz, timer0 is set to 16bit, the prescaler is on 1:1, and the timer is using the internal oscillator. The integer value I am reading off the timer is 6153 - what do I have to do to this number to give me a number in seconds?

If I read you right that is a 4MHz xtal, and a 1MHz timer0 oscillator?

Then a TMR0 count of 6153 is 6153uS or 6.153mS.

It might be worth posting your code for the timer0 capture, so people could suggest ways to improve accuracy etc.
 
Status
Not open for further replies.

Latest threads

Back
Top