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 Overflow?

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







Timer Overflow?
PostPosted: Sat Jul 19, 2003 12:37 pm     Reply with quote

Hi,

I would like to flash my led every 200 ms (It must be in high accuracy).Sometime ago Neutone gave me a code about overflowing timer0 but i even dont have any idea about how it works??? I also dont have any document how i can use it. It is something like that :

#bit Timer1_Interupt_Flag = 0x0C.0
.
.
.
.
while(!Timer1_Interupt_Flag );
Timer1_Interupt_Flag =0;
set_timer1(15508);
.
.
.
.

I even dont understand a line of this code.Help Please!
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516180
John Taylor
Guest







Re: Timer Overflow?
PostPosted: Sat Jul 19, 2003 1:35 pm     Reply with quote

/*
If your clock is 4mhz and the timer1 prescaler is 4, timer1 will increment every 4us. That means you have to count 50,000
timer1 increments to get 200ms. One way to do it is to set timer 1 to zero and then sit there and compare the timer value to 50,000 until it hits, then reset to zero and start over. This is a messy way to do it. The way that is shown below sets the initial value at (65536-50000)=15536 and then simply waits for a timer overflow that is generated in hardware. The advantage of this is there is a lot less coding to do, plus the overflow will generate an interrupt that can be dealt with in the background. Note that the example shows 15508 vs the 15536 above. You have to count for the time it takes to deal with the overflow or else you cycle will be greater than the 200ms target. Note that if you do not initially set the timer to the preload value, your very first cycle will be greater than you expect (~25\%)
*/


//assign variable to bit which indicates timer overflow,
//adjust for your actual processors' sfr
#bit Timer1_Interupt_Flag = 0x0C.0

//wait for timer to overflow
while(!Timer1_Interupt_Flag );

//when it does, clear the overflow flag
Timer1_Interupt_Flag =0;

//set the timer such that (65536-15508) timer counts = 200ms
set_timer1(15508);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516182
No_Fear
Guest







Re: Timer Overflow?
PostPosted: Sun Jul 20, 2003 12:00 pm     Reply with quote

Thanks for your reply.
So if i want something happen in every 200 ms i should write a code like :

#bit Timer1_Interupt_Flag = 0x0C.0
.
.
.
.
while (1){

while(!Timer1_Interupt_Flag );
Timer1_Interupt_Flag =0;
set_timer1(15508);
flash_led(); // a private procedure
}


Is that true?

:=/*
:=If your clock is 4mhz and the timer1 prescaler is 4, timer1 will increment every 4us. That means you have to count 50,000
:=timer1 increments to get 200ms. One way to do it is to set timer 1 to zero and then sit there and compare the timer value to 50,000 until it hits, then reset to zero and start over. This is a messy way to do it. The way that is shown below sets the initial value at (65536-50000)=15536 and then simply waits for a timer overflow that is generated in hardware. The advantage of this is there is a lot less coding to do, plus the overflow will generate an interrupt that can be dealt with in the background. Note that the example shows 15508 vs the 15536 above. You have to count for the time it takes to deal with the overflow or else you cycle will be greater than the 200ms target. Note that if you do not initially set the timer to the preload value, your very first cycle will be greater than you expect (~25\%)
:=*/
:=
:=
:=//assign variable to bit which indicates timer overflow,
:=//adjust for your actual processors' sfr
:=#bit Timer1_Interupt_Flag = 0x0C.0
:=
:=//wait for timer to overflow
:=while(!Timer1_Interupt_Flag );
:=
:=//when it does, clear the overflow flag
:=Timer1_Interupt_Flag =0;
:=
:=//set the timer such that (65536-15508) timer counts = 200ms
:=set_timer1(15508);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516198
No_Fear
Guest







Re: Timer Overflow?
PostPosted: Sun Jul 20, 2003 12:04 pm     Reply with quote

Thanks for your reply.
So if i want something happen in every 200 ms i should write a code like :

#bit Timer1_Interupt_Flag = 0x0C.0
.
.
.
.
while (1){

while(!Timer1_Interupt_Flag );
Timer1_Interupt_Flag =0;
set_timer1(15508);
flash_led(); // a private procedure
}


Is that true?

:=/*
:=If your clock is 4mhz and the timer1 prescaler is 4, timer1 will increment every 4us. That means you have to count 50,000
:=timer1 increments to get 200ms. One way to do it is to set timer 1 to zero and then sit there and compare the timer value to 50,000 until it hits, then reset to zero and start over. This is a messy way to do it. The way that is shown below sets the initial value at (65536-50000)=15536 and then simply waits for a timer overflow that is generated in hardware. The advantage of this is there is a lot less coding to do, plus the overflow will generate an interrupt that can be dealt with in the background. Note that the example shows 15508 vs the 15536 above. You have to count for the time it takes to deal with the overflow or else you cycle will be greater than the 200ms target. Note that if you do not initially set the timer to the preload value, your very first cycle will be greater than you expect (~25\%)
:=*/
:=
:=
:=//assign variable to bit which indicates timer overflow,
:=//adjust for your actual processors' sfr
:=#bit Timer1_Interupt_Flag = 0x0C.0
:=
:=//wait for timer to overflow
:=while(!Timer1_Interupt_Flag );
:=
:=//when it does, clear the overflow flag
:=Timer1_Interupt_Flag =0;
:=
:=//set the timer such that (65536-15508) timer counts = 200ms
:=set_timer1(15508);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516199
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Timer Overflow?
PostPosted: Mon Jul 21, 2003 8:45 am     Reply with quote

:=Hi,
:=
:=I would like to flash my led every 200 ms (It must be in high accuracy).Sometime ago Neutone gave me a code about overflowing timer0 but i even dont have any idea about how it works??? I also dont have any document how i can use it. It is something like that :
:=
:=#bit Timer1_Interupt_Flag = 0x0C.0
:=.
:=.
:=.
:=.
:=while(!Timer1_Interupt_Flag );
:=Timer1_Interupt_Flag =0;
:=set_timer1(15508);
:=.
:=.
:=.
:=.
:=
:=I even dont understand a line of this code.Help Please!
:=
The timer is really a counter that increments with the crystle ossolations. When the timer goes from binary 1111 1111 1111 1111 to 0000 0000 0000 0000 it has overflowed. When this happens the timer interupt flag is set to 1. As long as the flag is not 1 the timer has not overflowed. The while loop waits for the timer to overflow before allowing the code to continue. It takes an amount of time to count from 0000 0000 0000 0000 to 1111 1111 1111 1111 and this is the timers period. When you preload the timer you are shorting the timers period.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516225
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