PIC16F877 - Timer Interrupt

Page 1


PIC16F877 Timer Interrupt

R. Theagarajan. ME., MSc., PhD Rtd. Professor in Engineering email: rtheagarajan@yahoo.com rtrajan59@gmail.com


Timer-0 Interrupt

• • • •

Option_Register INTCON Register Timer 0 Interrupt Flag Incremented output in Port-C


Timer-0 Interrupt Timer-0 Interrupt is generated when TMR0 overflows from FFh to 00h. This overflow sets bit TOIF in INTCON register. Interrupt can be masked by clearing bit TOIE in INTCON. Bit TOIF must be cleared in software by the Timer-0 module interrupt service routine

before re-enabling the interrupt. TMR0 interrupt cannot awaken the processor from SLEEP, since the timer is shut off during sleep. Prescaler is shared between Timer-0 and the WDT. The PSA and PS2, PS1 & PS0 bit in Option-Reg determines the prescaler assignment and the ratio.


Option_Reg register - 81h, 181h D7

D6

D5

D4

D3

D2

D1

D0

RBPU

INTEDG

T0CS

T0SE

PSA

PS2

PS1

PS0

Bit value 000 001 010 011 100 101 110 111

Port-B pull-up 0 = Enable 1 = Disable

Interrupt Edge 0 = Falling 1 = Rising TMR0 source 0 = internal clock 1 = on RA4

Pre-scaler assignment 0 = Timer0 1 = WDT

TMR0 source edge 0 = Low to high 1 = High to low

= = = = = = = =

TMR0 rate 1:2 1:4 1:8 1 : 16 1 : 32 1 : 64 1 : 128 1 : 256


INTCON register - 0Bh, 8Bh, 10Bh, 18Bh D7

D6

D5

GIE

PEIE

TMR0IE

D4

D3

D2

D1

INTE

RBIE

TMR0IF

INTF

Global Int. Enable 1 = Enable 0 = Disable

D0 RBIF

Timer0 Int. Flag 1 = Overflow 0 = did not overflow

Peripheral Int. Enable 1 = Enables 0 = Disables

Port change Int. Flag RB7:RB4 pins 1 = pins changed 0 = no pins changed

TMR0 Int. Enable 1 = Enables 0 = Disables

RB port change interrupt enable bit 1 = Enables 0 = Disables

Ext. Interrupt 1 = Enables 0 = Disables

External Int. Flag 1 = Int. occurred 0 = did not occur


; Using Timer0 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC� org 0000h goto Main org 0004 goto int_sr org 0050h Main:

; goto label Main


; Using Timer0 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

movlw 0x00

; data to configure PortC as output

movwf TRISC

; PortC is configured as output

movlw B’10000111’

;

movwf OPTION_REG

: prescaler 1:256 for internal clock


; Using Timer0 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

movlw 0x00

; data to configure PortC as output

movwf TRISC

; PortC is configured as output

movlw B’10000111’

;

movwf OPTION_REG

: prescaler 1:256 for internal clock

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10100000’

; enabling the global and Timer0 Int.

movwf INTCON

;


; Using Timer0 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

repeat:

banksel TRISC

; select the bank1 where TRISC is available

movlw 0x00

; data to configure PortC as output

movwf TRISC

; PortC is configured as output

movlw B’10000111’

;

movwf OPTION_REG

: prescaler 1:256 for internal clock

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10100000’

; enabling the global and Timer0 Int.

movwf INTCON

;

movlw 0x00

; w register is initialised

movwf TMR0

; TMR0 register is initialised with 00

goto repeat

; stop execution / Halt


; Using Timer0 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

movlw 0x00

; data to configure PortC as output

movwf TRISC

; PortC is configured as output

movlw B’10000111’

;

movwf OPTION_REG

: prescaler 1:256 for internal clock

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10100000’

; enabling the global and Timer0 Int.

movwf INTCON

;

movlw 0x00

; w register is initialised

movwf TMR0

; TMR0 register is initialised with 00

repeat:

goto repeat

; stop execution / Halt

Int_sr:

bcf INTCON, TOIF

; clear timer0 int. flag

incf PORTC

; increment PortC

retfie

; return from this int. routine

end

; assembler directives


Timer-2 Interrupt

• INTCON Register • Timer 2 interrupt flag • Incremented output in Port-C


; Using Timer2 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC� org 0000h goto Main org 0004 goto int_sr org 0050h Main:

; goto label Main


; Using Timer2 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw D’249’

;

movwf PR2

: timer2 period register is set

bsf PIE1, TMR2IE

; enable timer2 interrupt


; Using Timer2 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw D’249’

;

movwf PR2

: timer2 period register is set

bsf PIE1, TMR2IE

; enable timer2 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’01111110’

; set timer2 pre-scale=16 & post-scale = 1:16

movwf T2CON

;


; Using Timer2 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

repeat:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw D’249’

;

movwf PR2

: timer2 period register is set

bsf PIE1, TMR2IE

; enable timer2 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’01111110’

; set timer2 pre-scale=16 & post-scale = 1:16

movwf T2CON

;

bsf

INTCON, PEIE

; enable peripheral interrupts

bsf

INTCON, GIE

; enable global interrupt

goto repeat

; stop execution / Halt


; Using Timer2 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw D’249’

;

movwf PR2

: timer2 period register is set

bsf PIE1, TMR2IE

; enable timer2 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’01111110’

; set timer2 pre-scale=16 & post-scale = 1:16

movwf T2CON

;

bsf

INTCON, PEIE

; enable peripheral interrupts

bsf

INTCON, GIE

; enable global interrupt

repeat:

goto repeat

; stop execution / Halt

Int_sr:

bcf PIR1, TMR2IF

; clear timer2 int. flag

incf PORTC

; increment PortC

retfie

; return from this int. routine

end

; assembler directives


Timer-1 Interrupt

• • • • •

T1CON INTCON Register Timer 1 interrupt flag Timer Register Incremented output in Port-C


; Using Timer1 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC� org 0000h goto Main org 0004 goto int_sr org 0050h

Main:

; goto label Main


; Using Timer1 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw 0x00

:

movwf TMR1L

;

movwf TMR1H

; Timer1 is initialised with 0000

bsf PIE1, TMR1IE

; enable timer1 interrupt


; Using Timer1 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw 0x00

:

movwf TMR1L

;

movwf TMR1H

; Timer1 is initialised with 0000

bsf PIE1, TMR1IE

; enable timer1 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’00110101’

; set timer1 pre-scale=1:8 fosc/4, async

movwf T1CON

;

bsf

; enable peripheral interrupts

INTCON, PEIE


; Using Timer1 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

repeat:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw 0x00

:

movwf TMR1L

;

movwf TMR1H

; Timer1 is initialised with 0000

bsf PIE1, TMR1IE

; enable timer1 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’00110101’

; set timer1 pre-scale=1:8 fosc/4, async

movwf T1CON

;

bsf

INTCON, PEIE

; enable peripheral interrupts

bsf

INTCON, GIE

; enable global interrupt

goto repeat

; stop execution / Halt


; Using Timer1 , Increment Port-C for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw 0x00

:

movwf TMR1L

;

movwf TMR1H

; Timer1 is initialised with 0000

bsf PIE1, TMR1IE

; enable timer1 interrupt

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’00110101’

; set timer1 pre-scale=1:8 fosc/4, async

movwf T1CON

;

bsf

INTCON, PEIE

; enable peripheral interrupts

bsf

INTCON, GIE

; enable global interrupt

repeat:

goto repeat

; stop execution / Halt

Int_sr:

bcf PIR1, TMR1IF

; clear timer1 int. flag

incf PORTC

; increment PortC

retfie

; return from this int. routine

end

; assembler directives


External Interrupt

• • • • •

Option_Reg INTCON Register TRISB TRISC Incremented output in Port-C


; External interrupt at RB0, PortC is incremented for every interrupt : INCLUDE “P16F877A.INC� org 0000h goto Main org 0004 goto int_sr org 0050h

Main:

; goto label Main


; External interrupt at RB0, PortC is incremented for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw

B’11111111’

movwf

OPTION_REG

movlw

B’00000001’

: make weak pull-up for portB

; make RB0 as input

movwf TRISB

;

clrf TRISC

; portC is defined as output


; External interrupt at RB0, PortC is incremented for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw

B’11111111’

movwf

OPTION_REG

movlw

B’00000001’

: make weak pull-up for portB

; make RB0 as input

movwf TRISB

;

clrf TRISC

; portC is defined as output

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10010000’

; set GIE and INTE to enable the interrupt

movwf INTCON

;


; External interrupt at RB0, PortC is incremented for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

repeat:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw

B’11111111’

movwf

OPTION_REG

movlw

B’00000001’

: make weak pull-up for portB

; make RB0 as input

movwf TRISB

;

clrf TRISC

; portC is defined as output

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10010000’

; set GIE and INTE to enable the interrupt

movwf INTCON

;

goto repeat

; stop execution / Halt


; External interrupt at RB0, PortC is incremented for every interrupt : INCLUDE “P16F877A.INC” org 0000h goto Main

; goto label Main

org 0004 goto int_sr org 0050h

Main:

banksel TRISC

; select the bank1 where TRISC is available

clrf

; PortC is configured as output

TRISC

movlw

B’11111111’

movwf

OPTION_REG

movlw

B’00000001’

: make weak pull-up for portB

; make RB0 as input

movwf TRISB

;

clrf TRISC

; portC defined as output

banksel PORTC

; select the bank0 where PortC is available

clrf

; PortC is initialised with 00

PORTC

movlw B’10010000’

; set GIE and INTE to enable the interrupt

movwf INTCON

;

repeat:

goto repeat

; stop execution / Halt

Int_sr:

bcf

; clear external int. flag

INTCON, INTF

incf PORTC

; increment PortC

retfie

; return from this int. routine

end

; assembler directives


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4

movlw B’00000110’ movwf ADCON1

; Port-A is defined as digital I/O


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4

movlw B’00000110’ movwf ADCON1

; Port-A is defined as digital I/O

banksel PORTC clrf

PORTC

; Port-C initialised with 00


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4

movlw B’00000110’ movwf ADCON1

; Port-A is defined as digital I/O

banksel PORTC clrf Sun1:

PORTC

; Port-C initialised with 00

movlw 0xfe movwf TMR0

; Timer-0 is initialised with 00

bcf INTCON, T0IF

; Clear the Timer0 int. flag


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4

movlw B’00000110’ movwf ADCON1

; Port-A is defined as digital I/O

banksel PORTC clrf Sun1:

Loop1:

PORTC

; Port-C initialised with 00

movlw 0xfe movwf TMR0

; Timer-0 is initialised with 00

bcf INTCON, T0IF

; Clear the Timer0 int. flag

btfss INTCON, T0IF

; Check for the timer0 int. flag

goto Loop1

; If time is not over, goto Loop1


; Counting every clock input through RA4, using Timer0 Port-C is incremented for every overflow INCLUDE “P16F877A.INC” org 0000h goto Main org 0050h Main:

banksel TRISC movlw 0x00 movwf TRISC

; Port-C defined as output

movlw B’10110000’

;

movwf OPTION_REG

; Prescaler 1:2 for Timer0, Ext. Input at RA4

movlw B’00000110’ movwf ADCON1

; Port-A is defined as digital I/O

banksel PORTC clrf Sun1:

Loop1:

PORTC

; Port-C initialised with 00

movlw 0xfe movwf TMR0

; Timer-0 is initialised with 00

bcf INTCON, T0IF

; Clear the Timer0 int. flag

btfss INTCON, T0IF

; Check for the timer0 int. flag

goto Loop1

; If time is not over, goto Loop1

incf PORTC

; If time is over, increment the Port-C

goto sUN1

; Goto Sun1 to repeat the operation

end



Avoid Plastics Plant a Tree Let us take care of our earth for future generation


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.