CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Timer overhead of 218 ticks?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
louarnold



Joined: 13 May 2010
Posts: 42
Location: Ottawa, Canada

View user's profile Send private message

Timer overhead of 218 ticks?
PostPosted: Mon Jun 21, 2010 12:40 am     Reply with quote

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 );
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Mon Jun 21, 2010 7:38 am     Reply with quote

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
louarnold



Joined: 13 May 2010
Posts: 42
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Mon Jun 21, 2010 9:33 am     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 21, 2010 11:37 am     Reply with quote

Quote:
From a tutorial, the following code is used to

We don't have the tutorial documentation.
louarnold



Joined: 13 May 2010
Posts: 42
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Tue Jun 22, 2010 12:26 am     Reply with quote

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.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 22, 2010 3:52 am     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Jun 22, 2010 4:02 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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