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

1 sec timer/counter

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







1 sec timer/counter
PostPosted: Thu Apr 30, 2009 4:50 pm     Reply with quote

Let me start off by saying that I am very new to writing code. I have been given the task of creating a module that will trigger a process based on the input from a thumbwheel switch that represents time in minutes. This process has to happed with moderate precision( + or - .5sec).
I am using a 16f676 with and external crystal that is 32.768kHz. My problem is I can not correctly initialize timer1. I read a post on here about a hr:min:sec timer but that code did not work for my microcontroller for some reason. I was wondering if anyone could help me with this problem. If I understand the datasheet correctly I should be able to run the microcontroller off an external crystal. My question is if I have to define both internal (RC) and external oscillators to allow them to run with any kind of accuracy or what.
My idea was to create a 1 sec interrupt and create a flag and count the flags until I reached the desired time interval and then trigger the process. Again I don't know if this is possible on that microcontroller but I thought it was worth a try. I think my main problem is that I am not setting up the timer and the interrupt correctly, but I don't really know.
Any help on how to setup the timers and interrupts to get a 1 second tick would be greatly appreciated. I am about to break the semicolon off my keyboard if I don't figure this problem out soon, JK. Thanks in advance guys.

And I do have some code that I can post if that will help out. It is a very simple program though, about 30 lines. The main problem is that the interrupt is not firing at the correct times, almost seems to fire randomly.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 30, 2009 5:00 pm     Reply with quote

Quote:
I am using a 16f676 with an external crystal that is 32.768kHz.

Is this crystal connected to the Timer1 oscillator pins, or to the main
oscillator pins ? If it's on the Timer1 pins, what do you have on the
main oscillator pins ?
Noremac
Guest







PostPosted: Thu Apr 30, 2009 6:22 pm     Reply with quote

Thanks. As far as i can tell the crystal is on the timer1 pins(PIN 2 and 3). The datasheet says they are T1CKI and T1G respectively. I have no idea what the main oscillator pins are but i think they are 2 and 3. It says XTAL for both in the datasheet. If i set the clock to 32768 the micro controller comes on but the interupt wont fire. I know this because an LED should blink on each flag and it is not doing so.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 30, 2009 6:30 pm     Reply with quote

I didn't look at the data sheet before I asked that question.
On the 16F676, the Timer1 oscillator pins are on the same pins as
the main oscillator.

My question now is:
Do you want to run the entire PIC from the 32 KHz crystal, or just Timer1 ?

Do you want the PIC to execute instructions at 32 KHz ? Or do you
want the PIC to run at 4 MHz with the internal oscillator, while Timer1
runs off its external 32 KHz crystal ?
Noremac
Guest







PostPosted: Thu Apr 30, 2009 8:24 pm     Reply with quote

I think it would be ok for the entire pic to run off the 32kHz. I am not really looking for speed in this application my main concern is the accuracy of the interrupt. i need it to fire as close to a second as possible. i am open for suggestions though. the operation that is being executed each minute involves holding a pin low for 100ms and then taking it high again so it is not very complicated. but i think i would like for everything to run at 32kHz if it doesnt cause any problems. i really dont understand the advantages or disadvantages of doing it the different ways though. thanks for the help.
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