Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer 1 interrupt

Status
Not open for further replies.

bananasiong

New Member
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?

BTW, I'm using PIC16F628A

Thanks
 
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.

Here's how you set it up,
Code:
		movlw	low(32768)
		movwf	CCPR1L
		movlw	high(32768)
		movwf	CCPR1H
		movlw	B'00001011';	enable special event trigger on CCP1
		movwf	CCP1CON;	

		bsf	STATUS,RP0
		bsf	PIE1,CCP1IE;	enable CCP1 interupt
		bcf	STATUS,RP0

		movlw	0C0h
		movwf	INTCON;		enable Peripheral interupts

In your isr you check (and clear) PIR1,CCP1IF

HTH

Mike.
 
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.

Thanks
 
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.
 
Last edited:
Microchip have been kind enough to supply the code you need in the timer1 section of the 16F88 data sheet.

Mike.
 
Hi,
It should be like this right? Because I found that it is in hexadecimal.
Code:
	movlw	low(.32768)
	movwf	CCPR1L
	movlw	high(.32768)
	movwf	CCPR1H
;	movlw	B'00001011';	enable special event trigger on CCP1
;	movwf	CCP1CON;	

	bsf	STATUS,RP0
;	bsf	PIE1,CCP1IE;	enable CCP1 interupt
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?

Thanks
 
Yes, it should be decimal.

The only changes you need to use the ccp are to the ISR as the CCP1IF bit is set instead of the timer1 interrupt bit.

Mike.
 
Pommie said:
The only changes you need to use the ccp are to the ISR as the CCP1IF bit is set instead of the timer1 interrupt bit.
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?

Thanks
 
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.
Code:
		movlw	(b'00'<<T1CKPS0|1<<T1OSCEN|1<<NOT_T1SYNC|1<<TMR1CS|1<<TMR1ON)
		movwf	T1CON;		enable timer 1

		movlw	low(.38000-1)
		movwf	CCPR1L
		movlw	high(.38000-1)
		movwf	CCPR1H

		movlw	(0<<CCP1X|0<<CCP1Y|b'1011'<<CCP1M0);	enable special event trigger on CCP1
		movwf	CCP1CON;	

		bsf	STATUS,RP0
		bsf	PIE1,CCP1IE;	enable CCP1 interupt
		bcf	STATUS,RP0

Mike.
 
use 37999 because timer 1 start from 0 right?

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.

Thanks
 
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.

Mike.
 
383 = "38" followed by "3" zeroes => 38000

Yes. Same as for capacitors. It's quite common.
 
atferrari said:
383 = "38" followed by "3" zeroes => 38000

Yes. Same as for capacitors. It's quite common.
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top