170 likes | 227 Views
Timer 1. Timer 1. Some background: -16F628 has three different timers: Timer0, Timer1 and Timer2 Timer0 and Timer2 are 8 bit timers Timer1 is a 16 bit register. It employs two 8 bit registers TMR1H and TMR1L (0x0E and 0x0F in bank 0). Timer1.
E N D
Timer 1 Some background: -16F628 has three different timers: Timer0, Timer1 and Timer2 • Timer0 and Timer2 are 8 bit timers • Timer1 is a 16 bit register. It employs two 8 bit registers TMR1H and TMR1L (0x0E and 0x0F in bank 0)
Timer1 Timer1 (like Timer0) can be employed as either a timer or a counter As a counter, Timer1 increments with changes of state on an external pin As a timer, Timer1 increments on either an internal or external clock We will consider Timer1 as a timer employing the internal clock
Timer1 Timer1 (like Timer0) can be used with or without interrupts. In either case, an overflow of the register pair TMR1H:TMR1L from 0xffff to 0x0000 will set the Timer1 interrupt flag TMR1F
Timer1 Lets review a little about interrupts and INTCON: GIE enables interrupts (Global Interrupts). INTCON recognizes 3 types of interrupts: Timer0 (T0IE,T0IF), pin B0 (INTE,INTF), and state change on pins B4,B5,B6,B7 (RBIE,RBIF). BUT the chip has other interrupts! These are called “Peripheral Interrupts”. The corresponding bit-pairs for peripheral interrupts occur somewhere else on the chip. If we wish to use these interrupts, we must set the PEIE (Peripheral Interrupt Enable bit) in INTCON .
Timer1 Peripheral Interrupt Enable Register (bank 1) Peripheral Interrupt Flags (bank 0)
Timer1 OK. Great. How do we configure Timer1?
Timer1 Which of these bits concern us ? Bits 5-4 prescaler Bit 1 (TMR1CS) set this to 0 to use internal clock Bit 0 (TMR1ON). Keep at 0 to set things up. Set to 1 to start timer
Timer1 Example: let’s set an interrupt to occur in 500 msec (1/2 second). 16 bit timer can count 65536 “clicks”. A 2-1 prescaler would give 131072 microsec A 4-1 prescaler would give 262144 microsec An 8-1 prescaler would give 524288 microsec We need 500000 microsec. So let’s assume prescaler has been set to 8-1.
timer1 If TMR1H:TMR1L contains then interrupt occurs in 00:00 524288 micros 00:01 524288 – (1*8) 00:02 524288 – (2*8) 00:03 524288 – (3*8) We need to solve for N where 524288-(N*8)=500000 or 8N= 24288 or N = 3036 = 0x0BDC So we need to load TMR1H:TMR1L with 0x0B:0xDC
TIMER1 movlw 0xC0 movwf INTCON ; enable global and peripheral interrupts movlw 0xDC movwf TMR1L ; set Timer1 Low movlw 0x0B movwf TMR1H ; set Timer1 High movlw 0x30 movwf T1CON ;set 8-1 prescaler, use internal clock, TMR1 off bsf STATUS,RP0 ; goto bank 1 bsf PIE1,TMR1IE ; enable TMR1 interrupt bcf STATUS,RP0 ; goto bank 0 bcf PIR1,TMR1F ; make sure interrupt flag is off bsf T1CON,TMR1ON ; turn the timer on!! Interrupt in ½ sec