External interrupt

Thread Starter

Dritech

Joined Sep 21, 2011
838
Hi all,

I have read several tutorials about external interrupts, but still cannot manage to make it work
The MC i am using is the PIC18F4550.

One of the tutorials i used was the one below:

http://www.hobbyprojects.com/pic_tut...utorial11.html

The program that i am trying to do is the one below. It will start blinking an led at port c,2 and when an interrupt occurs (at int0), the led will stop blinking for few seconds.

Rich (BB code):
;REGISTER DECLERATIONS
 
REG2 EQU 60H
REG3 EQU 61H
REG4 EQU 62H
 
 
 
ORG 0000H               ;WHEN SUPPLY ON OR RESET IS PRESSED, PROGRAM WILL START FROM HERE (SEE DATASHEET PG 57)
GOTO MAIN
 
ORG 0008H				;HIGH PRIORITY VECTOR... THE PROGRAM WILL GO HERE WHEN INTERRUPT IS ENABLED
GOTO ISR
 
 
;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE
 
 
;START MAIN PROGRAM HERE
 
MAIN
 
MOVLW B'000'              ;SET PORT C AS OUTPUT 
MOVWF TRISC
 
CLRF INTCON              ;CLEAR REGISTORS 
CLRF RCON
 
BSF RCON,7               ;SET IPIN TO 1
 
BSF INTCON,7             ;ENABLE ALL HIGH PRIORITY INTERRUPTS
BSF INTCON,4             ;SET INT0 AS EXTERNAL INTERRUPT
 
BCF INTCON,1             ;CLEAR FLAG
 
MOVLW B'00000001'              ;SET PORT B,0 AS INPUT (SINCE IT IS AN INTERRUPT)
MOVWF TRISB
 
 
;START BLINKING LED
 
LINE3
CLRF REG2
CLRF REG3
MOVLW 30
MOVWF REG4
BSF PORTC,2
LINE1
DECFSZ REG2
BRA LINE1
DECFSZ REG3
BRA LINE1
DECFSZ REG4
BRA LINE1
BCF PORTC,2
CLRF REG2
CLRF REG3
MOVLW 30
MOVWF REG4
LINE2
DECFSZ REG2
BRA LINE2
DECFSZ REG3
BRA LINE2
DECFSZ REG4
BRA LINE2
GOTO LINE3
 
 
;INTERRUPT SERVICE ROUTINE 
 
ISR
 
BCF PORTC,2    ;STOP LED AT PORT C FORM BLINKING WHEN THE INTERRUPT FLAG IS SET
CLRF REG2
CLRF REG3
CLRF REG4
LINE6
DECFSZ REG2
BRA LINE6
DECFSZ REG3
BRA LINE6
DECFSZ REG4
BRA LINE6
BCF INTCON,1   ;SINCE THE PIC DOES NOT RESET THE INTERRUPT PIN AS 0 AUTOMATICALLY, IT HAS TO BE SET MANUALLY
RETFIE         ; END INTERRUPT ROUTINE
 
 
 
 
GOTO $		; THIS WILL STOP THE MICRO FROM STARTING TO EXECUTRE THE PROGRAME AGAIN EVEN AFTER
			; SENDING IT TO THE END DIRECTIVE
 
END
Can someone help me solve this please.

Thanks in advance
 

ErnieM

Joined Apr 24, 2011
8,011
To get code to compile I had to add:

LIST P=18F4550 ;directive to define processor
#include <P18F4550.INC> ;processor specific variable definitions

Also, all lines require indentation for a build without warnings. Only labels can begin at position 1. That builds clean but has code another problem.

Then you need to turn off the analog functions (they are always and properly enabled by default) by setting ADCON1 to 0x0F.

I would guess this is one of the first programs you have ever written. You can get a nice jump start by using one of the template files Microchip gives you along with MPLAB. For your project you could have best begun using the supplied template file 18F4550TEMP.ASM as a start.

Next, never use a register name or line label that is meaningless. "REG2" means nothing, while something like CounterB means a bit more and helps to make the code more meaningful. Try to imagine someone reading your code fresh, or you yourself reading your own code 2 years from now.
 
Top