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

TIMER1 with 32khz osc doesnt work

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



Joined: 15 Nov 2004
Posts: 42

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

TIMER1 with 32khz osc doesnt work
PostPosted: Fri Feb 23, 2007 8:00 am     Reply with quote

I used TIMER1 with 32.768 khz external crystal and try to get 1 second
duration timer but after some test and measurements finally I find out that this 32khz oscillator isnt calibrated or maybe there is something wrong with my code cause everytime I run this code on pic it gaves me a different result and timing .besides If i use internal osc for timer1 everything is alright .
uP is PIC16f877A operates on 20MHZ and compiler ver :3.249

here is my code :

Code:

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void main(void)
{
 int16 k;
 int8 i;

 //with 32.768khz osc it must gaves us 30.5uS resolution with 2s overflow
 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);

 //this program is just for test and my original code is something else

 loop;
 set_timer1(32766);   // (65536/2)=32766 : 1s delay()
 delay_ms(950);       // 950ms (timer has 50ms to overflow)
 k=get_timer1();
 printf("\ntimer1=%lu\n\r",k);
 goto loop ;

}


On my serial port the result is something like this :
timer1=65432
timer1=65331
timer1=12
timer1=65510
....

these numbers must be the same what is the problem ? anyone has anyother suggestion?
Ttelmah
Guest







PostPosted: Fri Feb 23, 2007 9:13 am     Reply with quote

There are significant problems with what you are doing. The big one is that running off an external clock, timer1, is now asynchronous to the internal CPU clock. When this is the case, you cannot just read and write the timer. To write it, you need to stop the timer, write the required value, and then restart the timer. Otherwise indeterminate resuts may occur, depending on when in the write, the chip counts. There is a similar problem when reading, where if the counter changes between reading the high byte, and the low byte, a 'silly' result can be seen. Look at the timer1 reference notes on the Microchip site, to get details of the code needed.

Best Wishes
Imanjl



Joined: 15 Nov 2004
Posts: 42

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

PostPosted: Fri Feb 23, 2007 4:26 pm     Reply with quote

I tried to stop timer during write process and read process,but still
the problem didnt solve .At this time the results is closer to each other than the first time :
timer1=65421
timer1=65395
timer1=65345
timer1=65414
timer1=65395
timer1=65368

Code:
Loop:
output_high(pin_a0);
setup_timer_1(T1_DISABLED);
set_timer1(32768);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
delay_ms(950);
setup_timer_1(T1_DISABLED);
k=get_timer1();
printf("\ntimer1=%lu\n\r",k);
Goto loop;
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 5:51 pm     Reply with quote

Doing things this way will always have inaccurate results. Depending on what other things you have going on, other interrupts, routines, and so on.... the PIC shares time with everything that's going on. Setting the register to a known value won't always get you the same value at the end. If you have another timer running it will take up a certain amount of time and will cause your values, from the get_timer() function to vary. If you need accurate timing then you will need to allow the timer to roll over, causing an interrupt, and have that flag whatever you want done.

There are other posts on people trying to manipulate the timers the way you are and just can't seem to get it to be very accurate.

Ronald
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 5:54 pm     Reply with quote

I ran your program (shown below) on a PicDem2-Plus and I got this:
Quote:

61622
63899
63899
63899
63899
63899
63900
63900
63900
63899


Code:
#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void main(void)
{
int16 k;
int8 i;

for(i = 0; i < 10; i++)
   {
    setup_timer_1(T1_DISABLED);
    set_timer1(32768);
    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
    delay_ms(950);
    setup_timer_1(T1_DISABLED);
    k = get_timer1();
    printf("%lu\n\r", k);
   }

}
Imanjl



Joined: 15 Nov 2004
Posts: 42

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

PostPosted: Sat Feb 24, 2007 2:59 am     Reply with quote

rnielsen wrote:
If you need accurate timing then you will need to allow the timer to roll over, causing an interrupt, and have that flag whatever you want done.


I wrote a new program without using get_timer1(); at this time when timer overflows(1s) it generates an interrupt .but still 1 second isnt accurate . This program generates 1 second in about 850-950mS .


Code:
#int_TIMER1
TIMER1_isr(){
set_timer1(32768);
count++;}

void main(void)
{int16 k;
 int8 i;
 setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
 set_timer1(32766);
 
 loop:
 ...
 ...
 printf("\ntimer1=%lu\n\r",count);
 ...
 goto loop ;}


hi pcm programmer ,ur result shows that the program works fine on Picdem2 cause (63900-32768)*30.5us=949ms .Maybe there is something wrong with my hardware according to microchip refrence manual,32.768 need two 33pf capacitors I use 30pf for c1 and c2 but I dont think it affects the result so much ,whats the Picdem2 caps value for timer1 ?

Besides the pic is on the bread board :
Quote:
Design Tips
2. You should ensure the your layout uses good PCB layout techniques so that noise does not couple onto the Timer1 oscillator lines.
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