![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
Timer overhead of 218 ticks? |
Posted: Mon Jun 21, 2010 12:40 am |
|
|
From a tutorial, the following code is used to count clock ticks for setting and reading the timer. The tutorial then states: "The overhead of our timer code is 218. This number is the number of timer ticks that it took to set and read the timer."
I understand everything but how they got the 218 ticks. And, should that number be microsecs?
Here is the code: The PIC16F877A is being used on a board with a 20MHz crystal.
Code: |
void main() { //Timer ticks to set and read the timer
long time;
setup_timer_1 (T1_INTERNAL | T1_DIV_BY_1);
set_timer1(0);
time = get_timer1();
printf(“The time in ticks is %lu\r\n”,time);
//Result is 2, the "overhead" value.
}
Void main () {//microsecs used to execute a=b*c
long time;
long a, b, c;
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 );
set_timer1 (0);
a = b * c;
time = get_timer1 ();
time -= 2; // subtract overhead value.
printf ( "Time in usec is %lu\r\n", (time+2)/5 );
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Jun 21, 2010 7:38 am |
|
|
218, sounds completely 'wrong'. The value you show in the code, is 2....
No it is not in uSec, it is in 'ticks'. It is like your watch. It displays in seconds, but may well 'tick' at some different value (0.5 seconds for example). In computing, a 'tick', is a single count of a particular counter or clock. You may have multiple watches all displaying in uSec, but each having different internal 'ticks'.
The timer runs off the master oscillator/4. So a 'tick' of this, is 1/5000000th second. It takes two of these counts to go from the point where the register is written, to where it is read again. So 0.4uSec.
Where it comes from, is rather fundamantal. The actual core operation of the processor. The processor, when running off a 20MHz 'master clock', runs one instruction, every four counts of this clock. If you look at the .lst file from the compiled code, you get:
Code: | .................... set_timer1(0);
00E3: CLRF 0F
00E4: CLRF 0E
.................... time = get_timer1();
00E5: MOVF 0F,W
00E6: MOVWF 7A
00E7: MOVF 0E,W
00E8: MOVWF 77
00E9: MOVF 0F,W
00EA: SUBWF 7A,W
00EB: BTFSS 03.2
00EC: GOTO 0E5
00ED: MOVF 77,W
00EE: MOVWF 21
00EF: MOVF 7A,W
00F0: MOVWF 22
|
Addresses (hex) 0F, and 0E, are the timer registers. When setting it to '0', they clear the high register 'first', then the low register, which may have advanced by one at this point. So the timer goes to '0' at the end of the instruction at 00E4. Then they read the high register, and transfer this to memory at 7A. Two instructions. Then they read the low register at the start of the instruction on line E7. At this point the clock has ticked forwards by two instruction counts, hence the value.
The times involved in the instructions are in the data sheet, and for most PIC instructions (except jumps), one instruction = 1 tick of the timer clock.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Mon Jun 21, 2010 9:33 am |
|
|
Ttelmah wrote: | 218, sounds completely 'wrong'. The value you show in the code, is 2....
--snip--
The timer runs off the master oscillator/4. So a 'tick' of this, is 1/5000000th second. It takes two of these counts to go from the point where the register is written, to where it is read again. So 0.4uSec.
Where it comes from, is rather fundamantal. The actual core operation of the processor. The processor, when running off a 20MHz 'master clock', runs one instruction, every four counts of this clock.
--snip--
Addresses (hex) 0F, and 0E, are the timer registers. When setting it to '0', they clear the high register 'first', then the low register, which may have advanced by one at this point. So the timer goes to '0' at the end of the instruction at 00E4. Then they read the high register, and transfer this to memory at 7A. Two instructions. Then they read the low register at the start of the instruction on line E7. At this point the clock has ticked forwards by two instruction counts, hence the value.
--snip--
Best Wishes |
Yes, I concur with your conclusions, having arrived at them myself before I posted: I get the 2 ticks in the first program, and I agree that for a 20 MHz xtal, the 2 ticks correspond to 400 nanoseconds (ns).
But "218" is too specific to be dismissed. As you explained, the assembly language cannot account for this number, but there is some other basis for this number. Otherwise it would have been left out since we already calculate overhead at 2 ticks. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 21, 2010 11:37 am |
|
|
Quote: | From a tutorial, the following code is used to |
We don't have the tutorial documentation. |
|
![](templates/subSilver/images/spacer.gif) |
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Tue Jun 22, 2010 12:26 am |
|
|
PCM programmer wrote: | Quote: | From a tutorial, the following code is used to |
We don't have the tutorial documentation. |
This is a tutorial on Timers. There is no more information than I gave you beside what you can get out of the data sheets. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jun 22, 2010 3:52 am |
|
|
With the information you supplied the only valid outcome is '2'. Just as can be expected from reading the datasheets.
You insist on the 218 value being explained. We can't. Not with the information you have provided.
Provide more information like the text of the tutorial, or post a link to it. Otherwise accept '2' being the answer. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Tue Jun 22, 2010 4:02 am |
|
|
It is perhaps worth pointing out that there are typing errors in the 'code' posted, with the wrong inverted commas being used around the text string. I'd suspect this reflects the typographical accuracy of the 'tutorial'....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|