Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
list p=16f690
#include <p16f690.inc>
__CONFIG -_MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
display
count
endc
;**************************STEUP THE CONSTANTS*******************************
org 0x00 ;this is where we come on power up and reset
goto Main
;**************************INTRUPT ROUTINE************************************
org 0x04
movwf temp ;this and the lable Loop will run simultenously
;move w into f in case to there was someting in
;count(see first step of Loop)
incf count,f ;increase count by 1 and put result in count
movlw .10 ;load w with decimal 10
subwf count,w ;subtract count from w and place result in w
btfss STATUS,C ;if in above operation count<= w then carry bit
;is set, so check for that and skip next step
;if it is set
goto carry_on
goto clear
carry_on:
bcf INTCON,0x01 ;clear the INTF, just in case(read tuto. 11)
movfw temp ;move temp to w
retfie ;get out of intrupt loop (this will start over
; the whole INTRUPT LOOP again
clear:
clrf count ;set count to 0
bcf INTCON,0x01 ;clear INTF
retfie
;*****************************MAIN PROGRAMME************************
Main:
;*****************************SET UP THE INTRUP REGISTERS***********
bsf INTCON,0x07 ; set globel intrupt (tell PIC that we will use intrupts)
bsf INTCON,0x04 ;
bcf INTCON,0x01
;******************************SET UP THE PORTS**********************
bsf STATUS,RP0
movlw b'10000000'
movwf TRISC
movlw b'00000100'
movwf TRISA ;set RA2 as input (for interrupt)
bcf STATUS,RP0
bsf STATUS,RP1
[COLOR="Red"]bcf OPTION_REG,0x07
bsf WPUA,0x02[/COLOR]
clrf ANSEL
bcf STATUS,RP1
;******************************NOW SEND THE VALUE OF COUNT TO PORTC**
Loop:
movf count,w
movwf PORTC
goto Loop
END
list p=16f628
#include<p16f628.inc>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
endc
;******************************************************************
org 0x00
goto main
;***********************************INTERRUPT LOOP*****************
org 0X04
inter:
movlw b'10000000'
movwf PORTA ;set pin 7 of port a HIGH
goto inter
main:
bsf INTCON,0x07
bsf INTCON,0x03
bcf INTCON,0x00
;**********************************SET UP THE PORTS *****************
bsf STATUS,RP0 ;switch to BANK 1
movlw b'00000001'
movwf TRISB ;set RB0 as input
movlw b'00000000'
movwf TRISA ;set PORT A all output
bcf OPTION_REG,0x07
bcf STATUS,RP0 ;back to BANK 0
movlw 0x07
movwf CMCON
Loop:
movlw b'00000001'
movwf PORTA ;set pin 0 of port a HIGH
goto Loop
END
list p=16f628
#include<p16f628.inc>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
endc
;******************************************************************
org 0x00
goto main
;***********************************INTERRUPT LOOP*****************
org 0X04
inter:
movlw b'10000000'
movwf PORTA ;set pin 7 of port a HIGH
goto inter
main:
bsf INTCON,0x07
bsf INTCON,0x04
bcf INTCON,0x01
;**********************************SET UP THE PORTS *****************
bsf STATUS,RP0 ;switch to BANK 1
movlw b'00000001'
movwf TRISB ;set RB0 as input
movlw b'00000000'
movwf TRISA ;setPORT A all output
bcf OPTION_REG,0x07
bsf OPTION_REG,0x06
bcf STATUS,RP0 ;back to BANK 0
movlw 0x07
movwf CMCON
Loop:
bcf INTCON,0x01
movlw b'00000001'
movwf PORTA ;set pin 0 of port a HIGH
goto Loop
END
list p=16f628
#include<p16f628.inc>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
endc
;******************************************************************
org 0x00
goto main
;***********************************INTERRUPT LOOP*****************
org 0X04
inter:
btfss PORTA,7
goto TurnOn
bcf PORTA,7
bsf PORTA,0
goto ExitISR
TurnOn bsf PORTA,7
bcf PORTA,0
ExitISR bcf INTCON,1
retfie
main:
bsf INTCON,0x07
bsf INTCON,0x04
;**********************************SET UP THE PORTS *****************
bsf STATUS,RP0 ;switch to BANK 1
movlw b'00000001'
movwf TRISB ;set RB0 as input
movlw b'00000000'
movwf TRISA ;set PORT A all output
bcf OPTION_REG,0x07
bcf STATUS,RP0 ;back to BANK 0
movlw 0x07
movwf CMCON
Loop:
goto Loop
END
ORG 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
; isr code can go here or be located as a call subroutine elsewhere
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
Edit: I don't understand why you said this:
Note, this is not very good code. If the main program happened to be in bank 1 when the interrupt occurs then it wouldn't work correctly.
Why so ?
list p=16f628
#include<p16f628.inc>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
temp_s
endc
;******************************************************************
org 0x00
goto main
;***********************************INTERRUPT LOOP*****************
org 0X04
inter:
[COLOR="Red"]movwf temp
movf STATUS,w
movwf temp_s
movlw b'10000000'
movwf PORTA
btfsc PORTB,0x00
goto inter
bcf INTCON,0x01
movf temp_s,w
movwf STATUS
movf temp,w
retfie[/COLOR]
main:
bsf INTCON,0x07
bsf INTCON,0x04
bcf INTCON,0x01
;**********************************SET UP THE PORTS *****************
bsf STATUS,RP0 ;switch to BANK 1
movlw b'00000001'
movwf TRISB ;set RB0 as input
movlw b'00000000'
movwf TRISA ;setPORT A all output
bcf OPTION_REG,0x07
bsf OPTION_REG,0x06
bcf STATUS,RP0 ;back to BANK 0
movlw 0x07
movwf CMCON
Loop:
movlw b'00000001'
movwf PORTA ;set pin 0 of port a HIGH
goto Loop
END
org 0X04
inter:
movwf temp
movf STATUS,w
movwf temp_s
movlw b'10000000'
movwf PORTA
btfsc PORTB,0x00
goto inter
bcf INTCON,0x01
movf temp_s,w
movwf STATUS
[COLOR=red]movf temp,w ;<- This will affect the Z flag in the status register[/COLOR]
retfie
org 0X04
inter:
movwf temp
swapf STATUS,W
clrf STATUS
movwf temp_s
movlw b'10000000'
movwf PORTA
btfsc PORTB,0x00
goto inter
bcf INTCON,0x01
swapf temp_s,W
movwf STATUS
swapf temp,F
swapf temp,W
retfie
As 628 has only one external interrupt (RB0) how can I use it for two phototransistors for a line follower ?
swapf temp,F ; move whatever in temp to temp ???????
swapf temp,W ; move whatever in temp to W
list p=16f628
#include<p16f628.inc>
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
ERRORLEVEL -302
cblock 0x20
temp
temp_s
endc
;******************************************************************
org 0x00
goto main
;***********************************INTERRUPT LOOP*****************
org 0X04
inter:
movwf temp
swapf STATUS,w
clrf STATUS
movwf temp_s
movlw b'10000000'
movwf PORTA
btfsc PORTB,0x04
goto inter
bcf INTCON,0x01
swapf temp_s,w
movwf STATUS
swapf temp,w
retfie
main:
bsf INTCON,0x07 ;Globel interrupt enable(we are using interr.)
bsf INTCON,0x04 ;RB0 enabled as external interrupt
bsf INTCON,0x03 ;RB4-RB7 interrupt on change is enabled/ in other words these pins will also work as
;interrupts and interrupt will occur every time any of these pin changes state
; i.e.:HIGH to LOW or LOW to HIGH, we can not set it to be on eather one state change
bcf INTCON,0x01 ;Clear RB0 interuupt flag, in case it is set so
; that another interrupt can occur
bcf INTCON, 0x00 ;Clear RB4-RB7 interrupt flag so, that another interr. can occur.
;**********************************SET UP THE PORTS *****************
bsf STATUS,RP0 ;switch to BANK 1
movlw b'00010001'
movwf TRISB ;set RB0 & RB4 as input
movlw b'00000000'
movwf TRISA ;setPORT A all output
bcf OPTION_REG,0x07 ; enable weak pull ups on port b/ pull ups mean internal resistors
bsf OPTION_REG,0x06 ;interuupt on rising age/this is by defoult set
bcf STATUS,RP0 ;back to BANK 0
movlw 0x07 ;turn comparators off, so HIGH will be simple
; HIGH rather than +5V (compared to supply) so with the LOW
movwf CMCON
Loop:
movlw b'00000001'
movwf PORTA ;set pin 0 of port a HIGH
goto Loop
END