Microchip's 16F630 is a pretty cool little chip. One of its features is to interrupt when the input on a pin changes. I had a bit of a time figuring out how to do it so I'll put this up to save someone else the trouble.
The short version is: Enable interrupts with INTCON's GIE, PEIE and RAIE bits. Set bits in TRISA and IOCA that correspond to the pins to watch. In the interrupt handler read PORTA then clear INTCON's RAIF bit.
title "InputInterrupt" LIST P=16F630, F=INHX8M #include <p16f630.inc> __CONFIG _INTRC_OSC_NOCLKOUT & _CP_OFF & _MCLRE_OFF & _WDT_OFF XTAL EQU 4000000 ; internal crystal @ 4MHz ; backup spots for registers during interrupt handling w_bak equ 0x20 status_bak equ 0x21 fsr_bak equ 0x22 ; startup (at fixed address 0x0) org 0x0 goto Main ; interrupt handler (at fixed address 0x4) org 0x4 IntHandle ; save registers movwf w_bak swapf STATUS, W clrf STATUS movwf status_bak movfw FSR movwf fsr_bak ; jumpt to proper handler btfsc INTCON, RAIF ; check for port change goto PortHandler IntHandle_Return ; restore all the backed up registers movfw fsr_bak movwf FSR swapf status_bak, W movwf STATUS swapf w_bak, F swapf w_bak, W retfie ; return from interrupt PortHandler ; To clear the interrupt you have to do two things in order: ; * read the port ; * clear the interrupt flag ;DO STUFF HERE ; for testing copy inputs from port A to port C movfw PORTA movwf PORTC ; clear portchange interrupt flag bcf INTCON, RAIF goto IntHandle_Return ;End PortHandler Main ; enable interupts bsf INTCON, GIE ; enable global interrupts bsf INTCON, PEIE ; enable peripheral interrupts bsf INTCON, RAIE ; enable port change interrupts ; clear the port clrf PORTA ; make the changes to Bank 1 all at once bsf STATUS, RP0 ; Port A movlw B'00111111' ; all 6 pins will be inputs movwf TRISA ; set pin as input movwf IOCA ; interupt on change ; finished with Bank 1 bcf STATUS, RP0 MainLoop ; kill time here waiting for an interupt goto MainLoop endPosted by drewish at April 6, 2005 04:42 PM
TrackBack URL for this entry:
http://drewish.com/mt/mt-tb.cgi/1320
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)