Hi,
I've been using timer 2 interrupt, happy with the results. Now I want to use timer 1 interrupt. I'm using external oscillator, 32768 Hz crystal at RB7 and RB6.
Do I need to preset the value of TMR1L ad TMR1H in the beginning and after the interrupt (timer 1 overflow)?
For other instructions, I use the internal 4 MHz oscillator of the PIC. So these are my configuration of T1CON register:
prescale 1:8
oscillator enabled
external clock from RB6 and RB7
do not synchronize external clock input
timer 1 enabled
32768/4=8192, prescale 1:8, so 1024 Hz which is 1 ms. So tmr1 needs to overflow when count to 1024 to get 1 second interrupt rate, am I right? Then I just set bit 15:10 of tmr1 after each interrupt?
Have a look at the special event trigger on the ccp module. It uses CCPH+CCPL as the interval timer for timer1. You can just set it to 0x8000 and you will get a 1 second interrupt.
Hi,
I didn't know that ccp module has to be used.
Set it to 0x8000, so it will count from 0 to 32767, which is the same as the crystal. So interrupt happens when ccp match tmr1 right?
What would happen if the setting is as my first post? I have set to tmr1 interrupt, and also external oscillator.
You don't have to use the ccp module, it just makes it a lot easier.
Another way to get a 1 second interrupt is to set bit 7 of TMR1H in your ISR. This way, you will get an interrupt every second. For this method your timer1 settings are correct except the prescaler should be 1:1.
This second way only works with a 32768Hz crystal. I used a 38k watch crystal and so used the ccp method.
Mike.
Edit, incorrectly said reset instead of set.
So for 38 kHz crystal, I just change it to .38000
Thanks, it is working with the 2nd way as mentioned above.
If I use ccp mode, do I need other setting on T1CON?
Hi,
I mean do I still need to do any setting on TICON? Or just leave it as default?
This method sounds similar to timer 2. Because this uses timer 1 to match ccp whereas the other one is using timer 2 to match PR2, am I right?
You setup timer1 exactly the same but you don't enable timer1 interrupts. You instead enable the ccp interrupt. This is similar to how timer2 uses PR2, except timer1 is 16 bit.
FYI, this is code from a timer I did that needed a 1S interrupt. It used the timer1 oscillator.
My crystal is cylinder type, lebelled 383, I bet it is a 38 kHz crystal.
I wonder why the timer 1 prescaler flag is T1CKPS0 instead of T1CKPS, and the CCPx Mode Select bits is CCP1M0 instead of CCP1M? I noticed that a '0' is added for the flag which has more than 2 bits.
I used 37999 because I assumed that timer1 was like timer2 and was reset on the next increment after a match. However, the data sheet doesn't mention this and so it should probably be set to 38000.
The reason why T1CKPS0 has a zero appended to it is because it is bit 0 of the prescaler (actual value 4). There is also a T1CKPS1 etc.
Thanks for the reply
I asked this because my friend bought 2 crystal oscillators, he asked for 32768 Hz but he got a 383. I felt weird on the label and suspected that they are 38000 Hz crystal oscillators.
*The fail safe clock monitoring function is not applicable for the Timer 1 crystal, am I right?
Well Pommie I always learn something new. I never thought of using the CCP1 special capture mode for speeding up the 32768Hz OSC interrupt. 2sec seemed too slow but you've shown the way. Put it to use in the Dragonfly and it works like a charm. I wanted double duty as a display referesh & real time clock.