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 3 on PIC18F452

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







Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 10:32 am     Reply with quote

I'm using Timer 3 as the timer. I'm using a 8 Mhz crystal. I set up timer 3 for internal div by 8 and preloaded timer 3 with 50,000 (since it is 16 bit). The timer should update every 4 us and with the preload should rollover every .2 sec. When it rolls over 5 times this should be 1 sec.

It is updating much faster. I have done a similar application using RTCC. Is there something about Timer 3 that is different.

I'm using Timer0 and Timer1 to count 2 different pulses over the 1 minute.

Any ideas?

Thanks,
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514888
Tomi
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 10:41 am     Reply with quote

Timer3 is an up-counter. It will rollover after 65535th cycle.
If you preload it with 50000 then the timer3 will rollover in every (65536-50000)= 15536 cycles. What is cca. 62 msec.
Preload (65536-50000) = 15536 to get the desired period.

:=I'm using Timer 3 as the timer. I'm using a 8 Mhz crystal. I set up timer 3 for internal div by 8 and preloaded timer 3 with 50,000 (since it is 16 bit). The timer should update every 4 us and with the preload should rollover every .2 sec. When it rolls over 5 times this should be 1 sec.
:=
:=It is updating much faster. I have done a similar application using RTCC. Is there something about Timer 3 that is different.
:=
:=I'm using Timer0 and Timer1 to count 2 different pulses over the 1 minute.
:=
:=Any ideas?
:=
:=Thanks,
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514889
keith landreth
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 10:53 am     Reply with quote

:=Timer3 is an up-counter. It will rollover after 65535th cycle.
:=If you preload it with 50000 then the timer3 will rollover in every (65536-50000)= 15536 cycles. What is cca. 62 msec.
:=Preload (65536-50000) = 15536 to get the desired period.
:=
:=:=I'm using Timer 3 as the timer. I'm using a 8 Mhz crystal. I set up timer 3 for internal div by 8 and preloaded timer 3 with 50,000 (since it is 16 bit). The timer should update every 4 us and with the preload should rollover every .2 sec. When it rolls over 5 times this should be 1 sec.
:=:=
:=:=It is updating much faster. I have done a similar application using RTCC. Is there something about Timer 3 that is different.
:=:=
:=:=I'm using Timer0 and Timer1 to count 2 different pulses over the 1 minute.
:=:=
:=:=Any ideas?
:=:=
:=:=Thanks,


I preloaded the 15536 and it still is way to fast. He is my code.


#int_TIMER3
TIMER3_isr()
{
set_timer3(preload);
TMR3IF_BIT = 0;

if(++timer = 5)
{
++second;
timer = 0;
}



setup_timer_0(RTCC_EXT_H_TO_L | RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
enable_interrupts(INT_TIMER3);
TMROIF_BIT = 0;
TMR3IF_BIT = 0;
INT1IF_BIT = 0;
LCD_init();
CLEAR_DISP;
delay_ms(1000);

RD16_BIT = 1;

LCD_position (FIRST_LINE);
printf(LCD_char, "Time = \%d sec", i );

cnt_time = 60;

set_timer0(0);
set_timer1(0);
set_timer3(preload);
enable_interrupts(global);

while(i < (cnt_time +1))
{

if(second = 1)
{
LCD_position (FIRST_LINE);
printf(LCD_char, "Time = \%u sec", ++i );
second = 0;
}
}

}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514890
Tomi
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 10:59 am     Reply with quote

How faster? 5 times Smile ?
Maybe you want to write
if(++timer == 5)
instead of
if(++timer = 5)

:=:=Timer3 is an up-counter. It will rollover after 65535th cycle.
:=:=If you preload it with 50000 then the timer3 will rollover in every (65536-50000)= 15536 cycles. What is cca. 62 msec.
:=:=Preload (65536-50000) = 15536 to get the desired period.
:=:=
:=:=:=I'm using Timer 3 as the timer. I'm using a 8 Mhz crystal. I set up timer 3 for internal div by 8 and preloaded timer 3 with 50,000 (since it is 16 bit). The timer should update every 4 us and with the preload should rollover every .2 sec. When it rolls over 5 times this should be 1 sec.
:=:=:=
:=:=:=It is updating much faster. I have done a similar application using RTCC. Is there something about Timer 3 that is different.
:=:=:=
:=:=:=I'm using Timer0 and Timer1 to count 2 different pulses over the 1 minute.
:=:=:=
:=:=:=Any ideas?
:=:=:=
:=:=:=Thanks,
:=
:=
:=I preloaded the 15536 and it still is way to fast. He is my code.
:=
:=
:=#int_TIMER3
:=TIMER3_isr()
:= {
:= set_timer3(preload);
:= TMR3IF_BIT = 0;
:=
:= if(++timer = 5)
:= {
:= ++second;
:= timer = 0;
:= }
:=
:=
:=
:= setup_timer_0(RTCC_EXT_H_TO_L | RTCC_DIV_1);
:= setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
:= enable_interrupts(INT_TIMER3);
:= TMROIF_BIT = 0;
:= TMR3IF_BIT = 0;
:= INT1IF_BIT = 0;
:= LCD_init();
:= CLEAR_DISP;
:= delay_ms(1000);
:=
:= RD16_BIT = 1;
:=
:= LCD_position (FIRST_LINE);
:= printf(LCD_char, "Time = \%d sec", i );
:=
:= cnt_time = 60;
:=
:= set_timer0(0);
:= set_timer1(0);
:= set_timer3(preload);
:= enable_interrupts(global);
:=
:= while(i < (cnt_time +1))
:= {
:=
:= if(second = 1)
:= {
:= LCD_position (FIRST_LINE);
:= printf(LCD_char, "Time = \%u sec", ++i );
:= second = 0;
:= }
:= }
:=
:= }
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514891
keith landreth
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 11:15 am     Reply with quote

:=How faster? 5 times <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0"> ?
:=Maybe you want to write
:=if(++timer == 5)
:=instead of
:=if(++timer = 5)
:=
:=:=:=Timer3 is an up-counter. It will rollover after 65535th cycle.
:=:=:=If you preload it with 50000 then the timer3 will rollover in every (65536-50000)= 15536 cycles. What is cca. 62 msec.
:=:=:=Preload (65536-50000) = 15536 to get the desired period.
:=:=:=
:=:=:=:=I'm using Timer 3 as the timer. I'm using a 8 Mhz crystal. I set up timer 3 for internal div by 8 and preloaded timer 3 with 50,000 (since it is 16 bit). The timer should update every 4 us and with the preload should rollover every .2 sec. When it rolls over 5 times this should be 1 sec.
:=:=:=:=
:=:=:=:=It is updating much faster. I have done a similar application using RTCC. Is there something about Timer 3 that is different.
:=:=:=:=
:=:=:=:=I'm using Timer0 and Timer1 to count 2 different pulses over the 1 minute.
:=:=:=:=
:=:=:=:=Any ideas?
:=:=:=:=
:=:=:=:=Thanks,
:=:=
:=:=
:=:=I preloaded the 15536 and it still is way to fast. He is my code.
:=:=
:=:=
:=:=#int_TIMER3
:=:=TIMER3_isr()
:=:= {
:=:= set_timer3(preload);
:=:= TMR3IF_BIT = 0;
:=:=
:=:= if(++timer = 5)
:=:= {
:=:= ++second;
:=:= timer = 0;
:=:= }
:=:=
:=:=
:=:=
:=:= setup_timer_0(RTCC_EXT_H_TO_L | RTCC_DIV_1);
:=:= setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
:=:= setup_timer_2(T2_DISABLED,0,1);
:=:= setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
:=:= enable_interrupts(INT_TIMER3);
:=:= TMROIF_BIT = 0;
:=:= TMR3IF_BIT = 0;
:=:= INT1IF_BIT = 0;
:=:= LCD_init();
:=:= CLEAR_DISP;
:=:= delay_ms(1000);
:=:=
:=:= RD16_BIT = 1;
:=:=
:=:= LCD_position (FIRST_LINE);
:=:= printf(LCD_char, "Time = \%d sec", i );
:=:=
:=:= cnt_time = 60;
:=:=
:=:= set_timer0(0);
:=:= set_timer1(0);
:=:= set_timer3(preload);
:=:= enable_interrupts(global);
:=:=
:=:= while(i < (cnt_time +1))
:=:= {
:=:=
:=:= if(second = 1)
:=:= {
:=:= LCD_position (FIRST_LINE);
:=:= printf(LCD_char, "Time = \%u sec", ++i );
:=:= second = 0;
:=:= }
:=:= }
:=:=
:=:= }


It is running about 4 times faster than it should. I have a signal gen set to 600 hz on Timer0 and Timer1. At the end of the 1 minute clock cycle, the counters are returning around 8,900 instead of the 36,000.
I made the correction [if(++timer = 5)] and it still does the same thing.


___________________________
This message was ported from CCS's old forum
Original Post ID: 144514892
Tomi
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 11:34 am     Reply with quote

<font face="Courier New" size=-1>Check your code for proper C syntax/semantics. You have at least another typo:
if(second = 1) ...... // if(second == 1) ???????
:=:=:= if(second = 1)
:=:=:= {
:=:=:= LCD_position (FIRST_LINE);
:=:=:= printf(LCD_char, "Time = \%u sec", ++i );
:=:=:= second = 0;
:=:=:= }
:=:=:= }</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514893
keith landreth
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 11:36 am     Reply with quote

:=<font face="Courier New" size=-1>Check your code for proper C sytax/semantics. You have at least another typo:
:=if(second = 1) ...... // if(second == 1) ???????
:=:=:=:= if(second = 1)
:=:=:=:= {
:=:=:=:= LCD_position (FIRST_LINE);
:=:=:=:= printf(LCD_char, "Time = \%u sec", ++i );
:=:=:=:= second = 0;
:=:=:=:= }
:=:=:=:= }</font>


Tomi,

Thanks for the help. That was it.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514894
R.J.Hamlett
Guest







Re: Timer 3 on PIC18F452
PostPosted: Fri May 30, 2003 2:55 pm     Reply with quote

:=:=<font face="Courier New" size=-1>Check your code for proper C sytax/semantics. You have at least another typo:
:=:=if(second = 1) ...... // if(second == 1) ???????
:=:=:=:=:= if(second = 1)
:=:=:=:=:= {
:=:=:=:=:= LCD_position (FIRST_LINE);
:=:=:=:=:= printf(LCD_char, "Time = \%u sec", ++i );
:=:=:=:=:= second = 0;
:=:=:=:=:= }
:=:=:=:=:= }</font>
:=
:=
:=Tomi,
:=
:=Thanks for the help. That was it.
Worth also adding, that if you want the timer to be reasonably accurate, _add_ the starting number to the current value, rather than just setting it.
The problem otherwise is that the interrupt handler, will typically add perhaps 10 to 15uSec from the moment the interrupt occurs, till you reach the routine (and this is assuming that nothing else had the interrupts disabled - such as another interrupt handler, or some parts of the code). Since this time can vary, the reset 'point', may be at different points in the timer. If instead you add the required number to the current count (except for the latency between reading the value, and re-writing it), the timing will be much more accurate. :-)

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514904
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