Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

PIC Interrupt PUSH button coding

Status
Not open for further replies.

BlackOps

Full Member level 5
Joined
Jan 1, 2005
Messages
280
Helped
14
Reputation
28
Reaction score
3
Trophy points
1,298
Location
AZERBAIJAN
Activity points
2,496
rb0 interrupt

Hello, experimenting with interrupts using PIC16F84A
So i connected one LED to the RB7 line of PORTB.
and i connected active-LOW Push button to RB0 line of PORTB.

pressing the button toggles On/OFF the LED.

so here is the code which i took from the book "Microcontroller Programming: the Microchip PIC", and remaked for my current circuit:

Code:
;=====================================================
; File: RB0Int.ASM
; Date: April 22, 2006
; Author: Julio Sanchez
; Processor: 16F84A
;=====================================================
; Description:
; Program to test interrupt on port RB0
; A pushbutton switch is connected to port RB0.
; The pushbutton toggles a LED on port-B, line 7
; 
; 
;=====================================================

;=========================
; setup and configuration
;=========================
processor 16f84A
include <p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;=====================================================
; variables in PIC RAM
;=====================================================

cblock 0x0d       ; Start of block
count2               ; ISR counter
old_w                 ; Context saving
old_STATUS 					
Delay1			
count1
endc


;========================================================
; ********** PROGRAM START **********
;========================================================
org 0 						; Program start at address 0
goto main

;=============================
; INTERRUPT HANDLER
;=============================
org 0x04
goto IntServ

;=============================
; MAIN PROGRAM
;=============================
main:
bsf		STATUS,RP0			; Select Bank1
bcf		OPTION_REG,INTEDG	; Set interrupt on falling edge	
							; by clearing INTEDG bit of OPTION_REG	

movlw 	b'00000001'			; PORTB bit 0
movwf	TRISB				; is Input, all other bits are Output

bcf		STATUS,RP0 			; Go back to Bank0	


clrf 	PORTB 				; Clear PORTB
;bsf 	PORTB,0 			; set PORTB bit 0

							; SETUP INTERRUPTS
bcf INTCON,INTF 			; Clear external interrupt flag (INTF = bit 1)
bsf INTCON,GIE 				; Enable global interrupts (GIE = bit 7)
bsf INTCON,INTE 			; Enable RB0 interrupt (INTE = bit 4)

;============================
; flash led
;============================
Loop:

nop
nop
nop
goto	Loop
;clrf	PORTB


;=======================================================
; Interrupt Service Routine
;=======================================================

IntServ:					; First test if source is an RB0 interrupt
btfss INTCON,INTF 			; INTF flag is RB0 interrupt
goto notRB0 				; Go if not RB0 origin

							; Save context
movwf old_w 				; Save w register
swapf STATUS,w				; STATUS to w
movwf old_STATUS 			; Save STATUS
							; Make sure that interrupt occurred on the falling edge
							; of the signal. If not, abort handler
btfsc PORTB,0 				; Is bit set?
goto exitISR 				; Go if clear

;=========================
; interrupt action
;=========================
;clrf	PORTB
movlw 10 					; Number of repetitions
movwf count2 				; To counter
wait:						; Check to see that port-B bit 0 is still 0
							; If not, wait until it changes
btfsc PORTB,0 				; Is bit set?
goto exitISR 				; Go if bit not 0
							; At this point RB0 bit is clear
decfsz count2,f 			; Count this iteration
goto wait 					; Continue if not zero
							; Interrupt action consists of toggling bit 2 of
							; port-B to turn LED on and off
; ACTION!!!!

movlw b'10000000' 			; Xoring with a 1-bit produces
						; the complement
xorwf	PORTB,1 				; Complement bit 2, port-B

;=========================
; exit ISR
;=========================
exitISR:					; Restore context

swapf old_STATUS,w 			; Saved STATUS to w
swapf old_w,f 				; Swap file register in itself
swapf old_w,w 				; re-swap back to w

notRB0:						; Reset,interrupt
bcf INTCON,INTF 			; Clear INTCON bit 1
retfie

delay:
movlw 4 
movwf count1 ; Store value in counter
repeat:
decfsz count1,f ; Decrement counter
goto repeat ; Continue if not 0
return

end
well, the programs were not so well documented in book, and when i remaked the initial program i didnt include this piece of code:

delay:
movlw 4
movwf count1 ; Store value in counter
repeat:
decfsz count1,f ; Decrement counter
goto repeat ; Continue if not 0
return


and my push button did not work correctly....

but when i included it, the program worked well! and no quit and hanging in simulation in Proteus! why? what does that piece of code do?

i tried to debug this program, and noticed that PC never goes to that piece of code... it reaches retfie instruction which is right before this piece of code, and then Loop begins again...and so on.... so...i notice that this code never executes....then how does it affect my program?

thanks
 

blueroomelectronics

Advanced Member level 5
Joined
Sep 17, 2006
Messages
1,686
Helped
179
Reputation
356
Reaction score
77
Trophy points
1,328
Location
Toronto, Canada
Activity points
8,700
pic24 interrupt push button

It's a very short delay loop, it waits for the pushbutton to settle.

I've just read that program in the book, true no call to the delay... Very odd
 

BlackOps

Full Member level 5
Joined
Jan 1, 2005
Messages
280
Helped
14
Reputation
28
Reaction score
3
Trophy points
1,298
Location
AZERBAIJAN
Activity points
2,496
pic button interrupt

it waits for push button to settle?
but how... if it doesnt call for it in the program?
 

yousafzai

Full Member level 5
Joined
Jun 1, 2006
Messages
244
Helped
15
Reputation
30
Reaction score
8
Trophy points
1,298
Activity points
2,292
pic24 interupt push button

Its a long time that i haven't programmed any pic but what i remember is that you need to specify if you want to detect a low to high or a high to low transition.... Have you done that?
 

BlackOps

Full Member level 5
Joined
Jan 1, 2005
Messages
280
Helped
14
Reputation
28
Reaction score
3
Trophy points
1,298
Location
AZERBAIJAN
Activity points
2,496
pic programm push button

yes i did that by clearing INTEDG bit of OPTION_REG register in the beginning of the main program routine...

so any idea of how is that piece of code connected logically to anywhere in program? :)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top