Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interrupt in pic 16F84A

Not open for further replies.


Junior Member level 3
Apr 28, 2011
Reaction score
Trophy points
Activity points
Good Morning Guys

I am learning Interrupts using pic 16F84A.I tried this below program and worked well in my pic . I found this program on the web. I can't understand the interrupt speciaclly line between 13-18. **broken link removed**


org 0x00 ;This is where we come on power up and reset 1

;*******************SETUP CONSTANTS*******************

INTCON EQU 0x0B ;Interrupt Control Register 2

PORTB EQU 0x06 ;Port B register address 3

PORTA EQU 0x05 ;Port A register address 4

TRISA EQU 0x85 ;TrisA register address 5

TRISB EQU 0x86 ;TrisB register address 6

STATUS EQU 0X03 ;Status register address 7

COUNT EQU 0x0c ;This will be our counting variable 8

TEMP EQU 0x0d ;Temporary store for w register 9

goto main ;Jump over the interrupt address 10

;***************INTERRUPT ROUTINE***************

org 0x04 ;This is where PC points on an interrupt 11

movwf TEMP ;Store the value of w temporarily 12

incf COUNT,1 ;Increment COUNT by 1, and put the result 13

;back into COUNT

movlw 0x0A ;Move the value 10 into w 14

subwf COUNT,0 ;Subtract w from COUNT, and put the 15

;result in w

btfss STATUS,0 ;Check the Carry flag. It will be set if 16

;COUNT is equal to, or is greater than w,

;and will be set as a result of the subwf


goto carry_on ;If COUNT is <10, then we can carry on 17

goto clear ;If COUNT is >9, then we need to clear it 18


bcf INTCON,0x01 ;We need to clear this flag to enable 19

;more interrupts

movfw TEMP ;Restore w to the value before the interrupt20

retfie ;Come out of the interrupt routine 21

clear 22

clrf COUNT ;Set COUNT back to 0 23

bcf INTCON,1 ;We need to clear this flag to enable 24

;more interrupts

retfie ;Come out of the interrupt routine 25

;*******************Main Program*********************


;*******************Set Up The Interrupt Registers****

bsf INTCON,7 ;GIE – Global interrupt enable (1=enable) 26

bsf INTCON,4 ;INTE - RB0 Interrupt Enable (1=enable) 27

bcf INTCON,1 ;INTF - Clear FLag Bit Just In Case 28

;*******************Set Up The Ports******************

bsf STATUS,5 ;Switch to Bank 1 29

movlw 0x01 30

movwf TRISB ;Set RB0 as input 31

movlw 0x10 32

movwf TRISA ;Set R 0 to RA3 on PortA as output 33

bcf STATUS,5 ;Come back to Bank 0 34

;*******************Now Send The Value Of COUNT To Port A

loop 35

movf COUNT,0 ;Move the contents of Count into W 36

movwf PORTA ;Now move it to Port A 37

goto loop ;Keep on doing this 38

end ;End Of Program 39


As the opening lines of the tutorial say its designed to count the swtich actions from 0 up to 9.

When RB0 detects a change in condition, it interrupts the main program.
In the ISR, COUNT is incremented, then it is compared to see if it is equal to 10 or greater.
Bit C , Carry Flag, in the Status register is set according to the result of the compare; so by testing that bit with btfss , the next action can be determined.
That is either, it is not 10 so just carry on, or it is 10 so clear the counter back to 0.

One other thing to notice, you will see how the ISR starts and ends by storing and restoring W into Temp.
When you enter the ISR the values contained in important registers like W and STATUS can be lost.
It is normal to do whats called ' context saving' - this is detailed in the Pics datasheet - Interputs section - its wise to always use it.

		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt

Dear all

Thankyou for your replay!!
Can soemone tell me what value has 'COUNT' in line 13 ?
The last question,I know that the value in W is 'A' Hex (line 14).Is it correct that the Instruction Subwf subtract W from 'Count' (Count minus W from pic datasheet)?I think that 'Count' is smaller than W and the result is negative (line 15) and can't understand this section.

Please can someone help me?
I will apperiacte!!!
Thank you in advance

Count is memory location defined by the user to store a value used during execution.

Count is memory location defined by the user to store a value used during execution.
What value has the register 'Count' intialy, I don't understand is why the value of W is bigger than the value of "Count" ?,I think the subtraction result is negative(line 15).


Perhaps this might help.

When the program starts it is good practice to Clear your registers like Count, you cannot rely on the registers being Zero when powered up.
So ideally you would run the sub routine Clear just after the Main but before your Loop.

The logic of those lines is as follows.

Start - ( all values here hex)

Count = 00
Increment Count = 01
Load W with 0A
Subtract W From Count 01 minus 0A = F7
The STATUS register Carry bit becomes 0 as a result .
BTFSS STATUS,C then tests the conditon of bit C
If it dectects a 0 then the next instruction if executed so it will Carry on.

This loop will continue the same until count has gone up to 09 - then this happens

Count = 09
Increment Count = 0A
Load W with 0A
Subtract W From Count 0A minus 0A = 00

The STATUS register Carry bit 0 becomes 1 as a result .

BTFSS STATUS,C then tests the conditon of bit C
If it dectects a 1 then the next instruction in jumped /ignored and the following instruction Clear is executed.

Hope thats cleared things up for you ..?

There is a feature of Mplab called MPLAB SIM where you can step though your code one instruction at a time and Watch the registers changing.
You can see where I have modified your code to the isr bit of code is in the main loop ( its easier to test that way).


  • ScreenShot001.jpg
    149.4 KB · Views: 65
Many thanks I know the program by heart .
Many thanks good night

Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to