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.

Basic timmer error??

Status
Not open for further replies.

kentken

New Member
If I was to use a 4mhz resonator, in place of a 4.096mhz resonator, how much seconds error will I get in say 1-12 hrs?
How do I calculate this.
I want to reduce the number of componets/price.

Is there a faster resonator that would work better?
 
:?

Ok, here is how you calculate the differnce (I hope I get this right!!)....

4.096MHZ/4 = instructions per uS = 1.024 instructions per uS or 0.9765625 uS per instrucion!
4.000MHZ/4 = instructions per uS = 1.000 instructions per uS or 1uS per instruction!

1.000 as a percentage of 1.024 is 97.65625%

This means that your time will be 100-97.65625 = 2.34375% slow (at 4.000Mhz).

There are 60 secs in a minute, so this means you will loose 1.40625 secs every minute.....

1.40625 * 60 (mins, 1hour) = 84.375 secs an hour or 1Min 24.375 secs

84.375 (secs, hourly loss) * 12 = 1012.5 seconds or..... 1012.5/60 = 16.875 Mins OR..... 16 Mins 52.5 Second error in 12 hours


If the timer is interupt driven, assuming (using) TMR0 is set to a ratio of 1:1 then at 4.096Mhz, TMR0 will generate an interrupt every 250uS, the 4.000Mhz crystal every 256uS...... if we add D'04' to TMR0 in the interrupt routine (allowing for the 2clks to update it, see the datasheets) this will correct the time for you!

I.E.

MOVLW D'04' ;MOVE TMR0 OFFSET VALUE TO W
ADDWF TMR0,F ;ADD TO TIMER 0 TO CORRECT FOR 4.000MHZ CLOCK

Hope this helps! I need a lie down!!
 
Ohh very nice!

Question. The say the PIC has a 4Mhz clock, what speed does the code run at 4Mhz or 1Mhz? I know that the tmr0 runs @ 1/4th the clock tho....

This has been bugging me a bit. I even noticed how you do a 4Mhz/4....so hrm...
 
bsodmike said:
Ohh very nice!

Question. The say the PIC has a 4Mhz clock, what speed does the code run at 4Mhz or 1Mhz? I know that the tmr0 runs @ 1/4th the clock tho....

This has been bugging me a bit. I even noticed how you do a 4Mhz/4....so hrm...

The code runs at 1/4 of the clock speed, so for a 4MHz clock each instruction takes 1uS - with a few exceptions (program branches) which take two.

This is perfectly normal with micro-processors, many run a great deal slower than that!.
 
Wow :) , that is exactly what I needed, I havent got all this mhz, khz, ms, etc to sink in yet.

That will get me up and running.

Thanks again
Kent Kenison
 
Nigel, why does the code run @ 1/4th - is it due to the PIC doing 'other' stuff.....or... ?!?
 
Matt(Pic progger) said:
:?
If the timer is interupt driven, assuming (using) TMR0 is set to a ratio of 1:1 then at 4.096Mhz, TMR0 will generate an interrupt every 250uS, the 4.000Mhz crystal every 256uS...... if we add D'04' to TMR0 in the interrupt routine (allowing for the 2clks to update it, see the datasheets) this will correct the time for you!

Please correct me if I am wrong. the lowest prescaler for TMR0 (to my knowledge, which is limited :p) is 1:2, i.e. 1 / (4.096Mhz/4/2) = 1.95 µS

How did you get 250µS? Thanks!
 
I don't see how he gets 250µS tho (even @ 1:1) = 0.97µS ... if tmr0 runs 1:1 does the code still exec @ 1/4th of clock?
 
bsodmike said:
Nigel, why does the code run @ 1/4th - is it due to the PIC doing 'other' stuff.....or... ?!?

The pic divides the OSC by 4 internally if you look at the data sheet it will show you why it does this. Maybe this will help:

**broken link removed**

Ron
 
Ahh thanks, that makes more sense. Another Q: how does one work out how long it will take for the WDT to trip - is it 1 / (clock/4/WDT scaler) ?

Thanks! Sorry for hi-jacking this thread....
 
bsodmike said:
Ahh thanks, that makes more sense. Another Q: how does one work out how long it will take for the WDT to trip - is it 1 / (clock/4/WDT scaler) ?

The WDT is totally seperate, it's a simple internal RC oscillator, and the frequency isn't particularly accurate - nor does it need to be as a watchdog timer.
 
How hard is it to set up the WDT as a simple timmer?

How acurate is it?

What is the longest time before int, on any tmr's?

How does all the Prescaler, and Post scaler work?
It isn't sinking in yet.

Thanks
Kent
 
kentken said:
How hard is it to set up the WDT as a simple timmer?

How acurate is it?

What is the longest time before int, on any tmr's?

How does all the Prescaler, and Post scaler work?
It isn't sinking in yet.

Thanks
Kent

Read the datasheet and application notes! :lol:

The WDT is fairly inaccurate, in fact there is an application note at MicroChip which uses the WDT variations with temperature to actually measure temperature!.

Maximum WDT delay is about 2.3 seconds.
 
bsodmike: The reason the PIC executes instructions at 1/4 the clock speed is that to execute each instruction the processor needs to multiple things. First the processor fetches the instruction, then it decodes the instruction. After decoding the requested operations are carried out (memmory accesses ALU operations etc.). To make the timing predictable each of these operations takes a clock cycle. The PIC happens to devide most instructions into 4 steps.

Modern processors like the AVR get around this bottleneck using something called pipelining. In the PIC when a particular step is being executed all the other steps arn't doing anything. But there isn't any reason why when an instruction is being executed you can't fetch the next instruction with the hardware that would otherwise be doing nothing. So pipelined processors work like a bucket brigade and keep each bucket full as they are passed along. The AVR claims single cycle instruction execution but each instruction from start to finish actualy takes several clocks but since the next instruction is running only one clock back in the pipe the throughput is one clock per instruction.

I didn't notice at first but the diagram RobH posted shows how pipelining works.

Hope this helps

Brent
 
Ok, watchdog wont work.
What is the max time for tmr0, tmr1?
I haven't realy found that in the data sheets.

Thanks
Kent
 
The timers can be prescaled by setting bits 3:0 of the Option register. The max they can go is with a 1:256 prescaler. How this works is = Clock speed / 4 / 256 = frequency of the timer. Simply do a 1/f to get the period of this frequency. Check the datasheet it has a detailed section on this. Better yet, check out Nigels tutes :D
 
Thanks, that helps it to sink in. I will take a closer look at his tut, and the data sheets.

Thanks for explaining things.

Kent
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top