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.

Which PIC Timer to start experimenting with ?

Status
Not open for further replies.

aljamri

Member
Hi,

I've flowed Nigel’s Tutorial in my PIC experiments since I’ve started, nowadays, I’m interested in Timers and having in hand many PIC ( 12F629, 12F675, 16F628/A, 16F876, 16F877 and 18F452 ), I’ve read about PIC three timers, TMR0 TMR1 and TMR2, I’m not sure which one is best to start with experimenting to know exactly how to use and benefited from PIC Timers.

Any hint would be appreciated.

Note: I have MPlab assembler and Mikro c Version: 8.2, so I can do it with assembly or with C.

Thanks
 
Most usually start with Timer0... Its the easiest in my book.... All the Interrupt control is in one register and the scaler is in the option reg..

Move up to timer1 when you need more control ( 16 bit timer )..... Timer 2 has post and pre scalers, making it more complex.

I use timer0 as a delay ( interrupt ).... and timer1 as a counter ( external clock ) Timer 0 counts seconds, while timer 1 counts pulses....
 
I use timer0 as a delay ( interrupt )....
In code we can make delay routine where we can see, isn't it?
One of my problem with timers is that they are working behined the seen, I can just feel the results only.


Timer 0 counts seconds,

If I'm using 16F628/A internal oscillator ( 4M Hz), how many rollover do I have to do to make one second?
 
4mhz is easy..... 4mhz / 4 = 1mhz...... Therefore each clock tick is 1uS... So.... 250 ticks with a 4:1 timer prescale...... 100us.. Load TMR0 with 5 every time, then count 10000..

Or 1:256 pre scaler. pre load TMR0 with 60 this gets you to approximately 50mS..

However!!!! you need to allow for the interrupt latency ( time to execute ) this can be 8/9 tick cycles... You need to work this in.
 
Last edited:
Yes, this is the way I want learn the things, I'll start putting the code and I'll let you know, but before that, some questions please:

4mhz / 4 = 1mhz

Why do we divided it by 4? is anything to do with Fitch/Execute of instructions


...... Therefore each clock tick is 1uS

That’s F = 1 / T ?


Or 1:256 pre scaler. pre load TMR0 with 60 this gets you to approximately 50mS..

And count 20 for 1 Second ?


However!!!! you need to allow for the interrupt latency ( time to execute ) this can be 8/9 tick cycles... You need to work this in.

This 8 ticks will be added in every TMR0 rollover, or code rollover?

Thanks
 
Why do we divided it by 4? is anything to do with Fitch/Execute of instructions
There is an internal clock divider in the pic chips "CLK0 = Fosc/4"...
That’s F = 1 / T ?
Yes! Frequency is the reciprocal of time....and via-versa.
And count 20 for 1 Second ?
There are many ways to get to 1 second.... I use the 100uS as I then use different timings here and there..
This 8 ticks will be added in every TMR0 rollover, or code rollover?
That was a guesstimate.... You'll need to work out YOUR interrupt latency.... That was mine...

I use ISIS so delays can be fine tuned relatively easy...
 
Have a read through this page;
https://www.romanblack.com/one_sec.htm

There are timing systems using the TMR0 or TMR1 to make actual (exact) seconds from any xtal value, or to generate any other timed period (like 1mS or 0.1 second) as a regular "event" controlled by interrupt.

One of my favourite techniques is to set up an interrpt to generate an event every 100th of a second, and just inc some variables in there. Then if I need a timer to light a LED for (say) 0.35 seconds I just clear the variable, light the LED then wait for the variable to equal 35, and turn the LED off. The good thing about that system is that you can be doing other things while the 0.35 seconds is timing out. :)
 
Status
Not open for further replies.

Latest threads

Back
Top