Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

External interrupt program

Status
Not open for further replies.

adrianvon

Member
Hi all,

I am trying to use an external interrupt pin but i'm finding it difficult to understand the datasheet. Does someone know from where i can find an interrupt coding IN ASSEMBLY ?? or maybe a tutorial ??

The microcontroller which i am going to use is the PIC18F4550.

All kind of help is appreciated...

Thanks in advance.
 
What specifically are you finding difficult to understand in the datasheet?
 
Just 3 hours before this post an example of interrupt code was posted!!

Mike.
 
Hi,

I'm not understanding what registers to declare, how to declare them, ... basically a lot of it. I'm a beginner in programming and this is the first time that i am working with interrupts and that's why i would like to see a coding which includes an external interrupt in assembly, maybe i will understand it better.

Thanks for your reply.
 
Last edited:
Pommie, for those that don't read the forums in a linear fashion a direct link is nice =)
 
Hi all,

I have read several tutorials about external interrupts, but still cannot manage to make it work :(

One of the tutorials i used was the one below:

https://www.hobbyprojects.com/pic_tutorials/tutorial11.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.

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 in this please. I did my utmost to make this program function...

Thanks in advance
 
Last edited:
How do you assemble this.... Is it working or are you getting errors...

I use MpAsmWin.exe

Code:
;REGISTER DECLERATIONS
       LIST    P=18F4550

#include "P18f4550.INC"
 
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
	MOVLW B'01111'
 	MOVWF ADCON1
	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 20
	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 20
	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

Include ADCON1 = 0xf and it will work...
 
Last edited:
Hi Ian,

I tried the program again and somehow it worked :) (without including ADCON1 = 0xf)

Now the next step is to make it function on the falling edge (5V to 0V)

Thanks for your help
 
Hi,

In the tutorial attached before, it tells you to set register 'option' bit 6 to 0 for the the interrupt to occur at the falling edge.

When this was done, the below error was given:

"Symbol not previously defined (OPTION)"
 
Last edited:
Ok, i had to clear bit 6 of register INTCON2:

Finally the program is ready and working as expected... :)

Thanks for your help
 

Attachments

  • Capture.JPG
    Capture.JPG
    68.6 KB · Views: 171
Microchip are confusing people.. It's still called OPTION in the datasheet... Like many people I use the datasheet for that sort of quick reference..
At least Microchip have included a new menu item in MPLAB.... "Get headers". it is then placed in the code explorer... makes it a little easier.
 
Hi all,

I want to add another external interrupt which is ignored when the first interrupt is active (low priority).

Can someone help me on how to do this?? do i have to initialize the interrupt register again or is it a change in the current initializations ??

Thanks
 
When I do this.... If I don't use priority... I just disable the Global flag...

Code:
void interrupt ISR()
   {
   if(T0IF)
      {
       GIE = 0;    // disable all or one interrupt if code gets here
       // TMR0 code here
       T0IF = 0;
       GIE = 1;  // re enable when ready 
      }
    if(INTF)
      {
       // int code goes here
       INTF = 0; 
      }
   
   }

Note... code is for a 14 bit pic using HTC
 
Last edited:
If you want the lower priority to be ignored if it occurred whilst the high priority ISR is executing, clear the interrupt flag for that peripheral at the end of the high ISR.

On the 16 series, GIE is automatically cleared and restored during an interrupt so there is no need to touch it.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top