View previous topic :: View next topic |
Author |
Message |
rajeshkhan808
Joined: 29 Aug 2012 Posts: 17
|
About timers - Ticks and Microseconds |
Posted: Sat Sep 01, 2012 5:21 am |
|
|
I had a few questions regarding timers and I though I would ask them here
Q1) When we do setup_timer_1(T1_INTERNAL | T1_DIV_BY_1)
What does T1_DIV_BY_1 or T1_DIV_BY_8 mean ?
Q2) I read that the instruction clock is the oscillator divided by 4, so if 20MHz is then we get, 0.2us. I wanted to know why is it divided by 4 ?
Q2)I wanted to know whats the difference between microsecond ticks and Microseconds. Why do we divide microsecond ticks by 5 to get the microseconds ? Is it because (this is just a guess an explanation would be appreciated) 5 microsecond ticks have 1 microsecond ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Sat Sep 01, 2012 5:58 am |
|
|
Q1) TMR1_DIV_BY_1 means divide the timer by 1 (EX: 4MHz/1 = 4MHz), TMR1_DIV_BY_8 means divide the timer by 8 (EX: 4MHz/8 = 500kHz). They are used to slow down the clock incase you don't need your timer to run very fast. TMR_DIV_BY_1 is typically the default as this doesn't change the clock.
Q2) I don't know the exact reason why, but it is for hardware purposes. I am assuming it takes 4 ticks of the clock to execute an instruction on that platform, but that is just a guess. As a point of interest, PIC24 and dsPIC33 have FCY=FOSC/2 instead of 4.
Q3) I typically use "microsecond ticks" to mean the number of clock ticks (or even just FCY cycles, depends if you are referencing an external clock or an internal) per microsecond (as you described), however, the number is purely based on the clock value you pick, so it is not typically 5 nor is it always the same number. |
|
|
rajeshkhan808
Joined: 29 Aug 2012 Posts: 17
|
|
Posted: Sat Sep 01, 2012 6:09 am |
|
|
@jeremiah Thank you for the great post. So how would you calculate the number of ticks if the Clock was 20MHz ? Any suggestions ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Sat Sep 01, 2012 4:38 pm |
|
|
You calculate FCY (the clock divided by 4 you mentioned)
Invert that value to find the time it takes one tick.
Divide that time into the time you are interested to find the number of ticks. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Sep 01, 2012 5:03 pm |
|
|
jeremiah wrote: |
Q2) I don't know the exact reason why, but it is for hardware purposes. I am assuming it takes 4 ticks of the clock to execute an instruction on that platform, but that is just a guess. As a point of interest, PIC24 and dsPIC33 have FCY=FOSC/2 instead of 4.
|
It is. It synchronizes the clock with the system's instruction execution. At a minimum, there's no sense in the clock running faster than that since that means you'd have a possible interrupt in between instructions -- and since that interrupt isn't going to get serviced until the next instruction anyway, why bother having those interrupts occur in between. (and we won't talk about synchronous/metastability here, but that might be part of it too)
Additionally... let us say you have the clock set for 2x the instruction clock. How will you service 2 a possibly 2 interrupts in between instruction clocks. You wouldn't. So again. Doesn't make sense to have a timer's clock run faster than the instruction clock in the normal sense of a timer.
Now you could argue that the timer is being used as a counter/capture (for doing something like freq measurement with a gate) and so now the rules are different... and to some extent the settings of the modules that support counting are different... but I'm not sure how that factors into the current internal design bringing legacy issues forward. I haven't dug that deep.
But getting back to terms of a timer being used to count ticks...
A nice thing about a prescaler is that it reduces the number of interrupts the system has to deal with for longer term events.
Example: you want to count 10ths of a second. So 100mS ticks with a system clock of 40MHz. (or 10MHz after Fosc/4)
With a 16bit clock and a DIV_1, a full run from 0-FFFF takes 6mS. You have to get interrupted about 16 times for a 100mS period to count those 6mS ticks to make a bigger 100mS tick.
All those interrupts. What a waste of CPU.
So, let's DIV_8 which makes 10MHz 1.25MHz - NOW to get from 0-FFFF and roll TIMER1 takes 52mS -- and you are only interrupted twice to count a 100mS tick.
Obviously, 10MHz doesn't work so well into some binary divisors (1,2,4,8,16) so we can adjust TIMER1 accordingly if we want even outcomes.
But the idea is this: If you want to count big numbers, doing it with a fast clock required HUGE timers or a lot of constant interrupt overhead. A prescaler allows you to adjust how big that timer is (in hardware) so it bothers your software less often and you can now use those cycles to process something else.
Compile in a simple TIMER1 interrupt and look how big it is... count up the instructions or run it through MPLAB's code execution time calculator.
now imagine if you want that routine to run as little as possible or more than it really needs to.
Make sense?
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
rajeshkhan808
Joined: 29 Aug 2012 Posts: 17
|
|
Posted: Sun Sep 02, 2012 1:55 am |
|
|
Thanks for the great post, however what would I do if I wanted to find the number of ticks in a microsecond ? Say of a PIC running at 20 MHz. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Sep 02, 2012 2:31 am |
|
|
rajeshkhan808 wrote: | Thanks for the great post, however what would I do if I wanted to find the number of ticks in a microsecond ? Say of a PIC running at 20 MHz. |
The math.
There's a bunch of ways to figure it out depending on what you're doing.
You just say "a PIC @ 20MHz" -- are you using Timer1? Timer0?
PIC 18F or 16F?
I'll tell you what, tell me how many miles to a gallon my car gets if it's just me and a passenger, and I'll tell you how many ticks per uSec.
:D _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
rajeshkhan808
Joined: 29 Aug 2012 Posts: 17
|
|
Posted: Mon Sep 03, 2012 12:57 am |
|
|
I am using timer 1 and PIC 16F877A. Sorry I cant believe how I forgot that info. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 03, 2012 10:56 am |
|
|
rajeshkhan808 wrote: | I am using timer 1 and PIC 16F877A. Sorry I cant believe how I forgot that info. |
:D
Now -- you need to read up on Timer1 and all it will do.
When you're learning any device, reading the datasheet several times helps.
Before bed helps
Then again in the morning helps.
But your answer is in the datasheet. I promise.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|