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

TIMER1 too slow

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



Joined: 08 Sep 2003
Posts: 21

View user's profile Send private message

TIMER1 too slow
PostPosted: Mon Sep 08, 2003 8:56 am     Reply with quote

I have activated a timer1 connected at a 32Khz oscillator to manage the date/time in my project.
The function to manage the overflow is this


#int_timer1
void clock_isr()
{
if(--int_count==0) {
++seconds;
int_count=INTS_PER_SECOND;
}
}

and this is the init procedure in the main funtion...

T1CON = 0x8F;
PIE1 = 1;
set_timer1(0x00);

enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

The T1CON value 0x8F set the prescaler to 1:1 and set the bits to activate the timer1.
The interrupt routine work ....but the problem is that TIMER1 call the overflow interrupt only 1 time every 2 seconds....
I tried to change the overflow value to 8 bits but the result is the same ....
There are some strange init procedure to activate this timer ?
ttelmah
Guest







Re: TIMER1 too slow
PostPosted: Mon Sep 08, 2003 9:49 am     Reply with quote

Flavio58 wrote:
I have activated a timer1 connected at a 32Khz oscillator to manage the date/time in my project.
The function to manage the overflow is this


#int_timer1
void clock_isr()
{
if(--int_count==0) {
++seconds;
int_count=INTS_PER_SECOND;
}
}

and this is the init procedure in the main funtion...

T1CON = 0x8F;
PIE1 = 1;
set_timer1(0x00);

enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

The T1CON value 0x8F set the prescaler to 1:1 and set the bits to activate the timer1.
The interrupt routine work ....but the problem is that TIMER1 call the overflow interrupt only 1 time every 2 seconds....
I tried to change the overflow value to 8 bits but the result is the same ....
There are some strange init procedure to activate this timer ?


I'm suprised it is this fast...
1) Why not use the internal setup functions, rather than going 'DIY'. It makes the code more readable, and more likely to work on different chips.
2) I presume from your description, that you have an external clock input, at 32768Hz, rather than the master oscillator running at this speed?. Otherwise I'd expect the timer to interrupt every eight seconds, since there is an /4 division automatically in place from the master clock.
3) Say what PIC is involved?. The use of the high bit in T1CON, suggests one of the 18F family, but it would be nice to know...
Now assuming I am right on the above, then your behaviour is exactly what you'd expect. The timer counts from 0 to 65536, which takes 2 seconds given a 32768Hz clock.
You then talk about 'changing the overflow value to 8 bits'. As far as I know, there is no such ability on this timer, on any PIC I can think of. There is an ability to switch the read/write mode between 8 bits and 16 bits, but the timer still counts to 65536.
If you want to have an interrupt at one second, then you will need to use the CCP module as well. If you set this to reset Timer1, when the count reaches 32768, then the CCP interrupt can be used at one per second.
Timer2, is capable of giving one/second interrupt from the same clock rate (set the prescaler to /128).

Best Wishes
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Real Time Keeping
PostPosted: Mon Sep 08, 2003 12:13 pm     Reply with quote

Funny you should mention this. I posted some code just before the forum was moved. The code you posted is not going to work accuratly using the system clock because you round the number of ints per second. Try something like this. The variable Ticker is the number of instruction cycles remaining until the second count is incremented. During each interupt, decrement ticker by the number of instruction cycles per timer interupt. If Ticker is less than the number of instructions per interupt, increment seconds and add instruction cycles per second to ticker. This will be as reliable as using an extra crystle for keeping time. Long term accuracy is as good as the crystle. If a crystle has +-10PPM you could be off by 10 seconds in about 11.5 days. This also allows the code in main to check for flags that are set at regular intervals.

int1 Initialized=1;
int1 Clock_mS_Tic;
int1 Clock_S_Tic;
int32 Ticker;
#locate Ticker = 0x020
int8 Ticker_byte_2;
#locate Ticker_byte_2 = 0x022
Int32 Seconds;

#inline
void Initialize_RTC(void);
/********************************************************************
* Initialize RTC *
********************************************************************/
#inline
void Initialize_RTC(void)
{ Ticker = 19660800;
setup_timer_2 ( T2_DIV_BY_4, 0xff, 1 );
enable_interrupts( INT_TIMER2 );
}

/********************************************************************
* Service RTC *
********************************************************************/
#int_TIMER2
void TIMER2_isr()
{ Clock_mS_Tic=1;
if(--Ticker_byte_2)
{ Ticker+=19660800;
Seconds++;
Clock_S_Tic=1;
}
}
regan



Joined: 10 Sep 2003
Posts: 5
Location: Wellington, New Zealand

View user's profile Send private message Send e-mail

Keeping it Real
PostPosted: Wed Sep 10, 2003 3:46 am     Reply with quote

To use Timer1 with a 32KHz crystal to get 1 second interrupts use the following code:

#define clk_reload 0x8000

// to setup the timer use:

void clk_init()
{
setup_timer_1(T1_CLK_OUT|T1_EXTERNAL|T1_DIV_BY_1);
set_timer1(clk_reload);
enable_interrupts(INT_TIMER1); // assuming INT_GLOBAL already enabled
}

// and the interrupt handler can be:

#int_timer1
void clk_isr()
{
set_timer1(clk_reload);
// put your code here.
}

regan.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 10, 2003 6:10 am     Reply with quote

It is better to add your reload time to the timer1 value to account for any latencies.
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