PIC Processor's - Using Interrupts

 

> Home
> PIC Microchip Tutorials
> PIC Getting Started Kit
> Robotics
> Contact
> About

 

 

So you've read the conceptual tutorial on interrupts and get the general idea. So let's get down to the practical stuff!

I'm using the 16F630 for this one. If you don't have that particular PIC, the general principles will apply, but it won't compile as-is.

As usual, here's the entire code up front:

;Anthony Rogers
;Oct 19th, 05
;TgglIntF630 - Interrupt Demo
	list p=16F630 ; list directive to define processor
	#include <p16F630.inc> ; processor specific variable definitions
 	__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF &
 _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 
INT_VAR UDATA_SHR 0x20 
w_temp RES 1 ; variable used for context saving 
status_temp RES 1 ; variable used for context saving
;**********************************************************************
RESET_VECTOR CODE 0x000 ; processor reset vector
	goto main ; go to beginning of program
INT_VECTOR CODE 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
	bcf INTCON, INTF ;clear interrupt
	incf PORTC, f
	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

main
	call 0x3FF ; retrieve factory calibration value
	bsf STATUS,RP0 ; set file register bank to 1 
	movwf OSCCAL ; update register with factory cal value 
	movlw 0x00 ; initialize TRISC to output
	movwf TRISC 
	movlw b'00000100' ; set PORTA, bit 3 (aka PORTA<3>) to input
	movwf TRISA 
	bcf STATUS,RP0 ; set file register bank to 0
                   
	movlw b'00000111' ; setting CMCON<2:0> to 1 puts the comparator module in
	movwf CMCON ; "off" mode, allowing us to use PORTA<2> as digital input
                   
	bsf INTCON, INTE ; enable External Interrupt
	bsf INTCON, GIE ; enable interrupts globally
                   
loop
	goto loop ; loop & wait...
                   END ; directive 'end of program'
                 

 

TO DO: Annotate Code

 

Circuit notes:
> R1 can be a smaller value for more brightness - just keep it above 100 ohms or so.
> R2 is a pullup resistor, and can be any value between 1K and 1M.
> C1's value is not critical either - it's purpose is to simply filter out mechanical noise from
the switch, and prevent the interrupt from triggering multiple time with one press.

 

 

top

Copyright © 2005 Anthony Rogers