View previous topic :: View next topic |
Author |
Message |
Noremac Guest
|
1 sec timer/counter |
Posted: Thu Apr 30, 2009 4:50 pm |
|
|
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
|
|
Posted: Thu Apr 30, 2009 5:00 pm |
|
|
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
|
|
Posted: Thu Apr 30, 2009 6:22 pm |
|
|
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
|
|
Posted: Thu Apr 30, 2009 6:30 pm |
|
|
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
|
|
Posted: Thu Apr 30, 2009 8:24 pm |
|
|
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. |
|
|
|