
Understanding Timer1 in Microcontrollers
Learn about Timer1 in microcontrollers such as the -16F628, its features, configuration, interrupts, and how to set it up for accurate timing. Explore the internal clock, prescaler settings, and examples for setting interrupts. Discover the use of interrupts, peripheral interrupts, and how to configure Timer1 effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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 movlw 0x0B movwf TMR1H ; set Timer1 Low ; set Timer1 High movlw 0x30 movwf T1CON ;set 8-1 prescaler, use internal clock, TMR1 off bsf bsf bcf STATUS,RP0 PIE1,TMR1IE STATUS,RP0 ; goto bank 1 ; enable TMR1 interrupt ; goto bank 0 bcf bsf PIR1,TMR1F T1CON,TMR1ON ; make sure interrupt flag is off ; turn the timer on!! Interrupt in sec