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 support@ccsinfo.com

Timer roll over

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








Timer roll over
PostPosted: Tue Mar 03, 2009 8:41 am     Reply with quote

Hi

I use a linear rotary encoder to measure some length.
The encoder running on a belt. If the belt is moving the encoder is too.
The encoder is connected to Timer0, and inc++ on every rising edge.
If I read Timer0 and save it, then after some time I do it again.

Ex1:
Code:
oldtmp=get_timer0();
 //some time after…
 tmp=get_timer0();


oldtmp can now be 10 and tmp can be 50. easy part. Diff is 40.

But if oldtmp is 250, and tmp is 10 then I pass the roll over(timer overflow -> start from 0 again...).

Code:
 if (tmp<tmpold){
  res=(0x100-tmpold)+tmp;
 }
 else {
  res=tmp-tmpold;
 }


Is there a easy way to do that?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 10:06 am     Reply with quote

Your thinking too complicated. If res is an unsigned int8, it will always give a correct result, without special processing of overflows.
Guest








PostPosted: Tue Mar 03, 2009 10:36 am     Reply with quote

Hmmm..

Timer0 is used as 16 bit and res, tmp, tmpold is all 16 bit too.

My ex. is only 8bit because I think it's the same, if not please help me out.

When you say I think complicated what did you mean?

I'm little confused?
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 10:38 am     Reply with quote

FvM wrote:
Your thinking too complicated. If res is an unsigned int8, it will always give a correct result, without special processing of overflows.


Correct, as long as you never expect that your result will be greater than 255. If you think your value ever may exceed 255, you might consider the following

- Create an int16 value (I called mine overflow).
- Generate an timer 0 overflow interrupt which will increment the int16 value by 0x100.

Code:

overflow = 0;
oldtmp=get_timer0();
//some time after…
tmp=get_timer0();
res=tmp + (overflow-tmpold);


Note: This code does not take into account the fact that the overflow value may change between the time timer0 has been read a second time and the result is calculated. Additional code is necessary to check for those conditions depending on your application.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 10:50 am     Reply with quote

Anonymous wrote:
Hmmm..

Timer0 is used as 16 bit and res, tmp, tmpold is all 16 bit too.

My ex. is only 8bit because I think it's the same, if not please help me out.

When you say I think complicated what did you mean?

I'm little confused?


You were only making it complicated (meaning: unnecessary calculations) if you know for sure that you will sample timer 0 before timer 0 has had the opportunity to complete one full counter cycle.

His response still holds true for a 16-bit timer as well when using unsigned int16 values.

Ex:

oldtmp = 0xFF00
tmp = 0x0280

res = tmp - oldtmp
res = 0x0280 - 0xFF00
res = 0xFFFF0380

but since you are storing to an unsigned int16:

res = 0x0380

which is the correct answer so there is no need to check for overflow.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
Guest








PostPosted: Tue Mar 03, 2009 12:38 pm     Reply with quote

Thanks:-)
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