List p=pic16f84a
include <p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
; variables in PIC RAM
cblock 0x0d ; Start of block
hours ; counter of hours
mins ; counter of mins
secs ; counter of secs
NbHi
NbLo ; counters
NaHi
NaLo
old_w ; Context saving
old_STATUS ; saving the STATUS
bitsB47 ; Storage for previous value
; in port-B bits 4-7
temp ; Temporary storage
fwd_step
endc
rp0 equ .5
;=============================
; Reset vector
;=============================
org 0 ; start at address 0
goto main
;=============================
; interrupt handler
;=============================
org 0x04
goto IntServ
;***********************************************
;**************** Main program****************
;***********************************************
main:
; Disable port-B internal pullups
; Interrupts on falling edge of pushbutton action
movlw 0xbf ;RBPU=1 Pull up not use
movwf OPTION_REG ;Set OPTION_REG
bsf STATUS,rp0 ;Change to Bank1
clrf TRISA ;Set PORTA all OUT
movlw 0xf0 ;RB0,1,2.5=IN RB7=OUT
movwf TRISB ;Set PORTB
bcf STATUS,rp0 ;Change to Bank0
movlw 0x00 ;start with everything off
movwf PORTB
movwf bitsB47 ;Store in local variable
;============================
; set up interrupts
;============================
; Clear external interrupt flag (RBIF = bit 1)
bcf INTCON,RBIF ; Clear flag
; Enable global interrupts (GIE = bit 7)
; Enable RB0 interrupt (RBIE = bit 4)
bsf INTCON,GIE ; Enable global int (bit 7)
bsf INTCON,RBIE ; Enable the RB port change ; interrupt(bit 3)
;***********************************************
;**************** Motor drive *********************;
;***********************************************
drive1 call gofwd
goto drive1
;**************** Forward Drive *********************;
gofwd movlw d'12'
movwf fwd_step
loopf
movlw 0x05
movwf PORTB
call delay
movlw 0x09
movwf PORTB
call delay
movlw 0x0a
movwf PORTB
call delay
movlw 0x06
movwf PORTB
call delay
decfsz fwd_step,f
goto loopf
return
; Interrupt Service Routine
;====================================================
; Service routine receives control whenever any of
; port-B lines 4 to 7 change state
IntServ:
; First test: make sure source is an RB4-7 interrupt
btfss INTCON,RBIF ; RBIF flag is interrupt
goto notRBIF ; Go if not RBIF origin
; Save context
movwf old_w ; Save w register
swapf STATUS,w ; STATUS to w
movwf old_STATUS ; Save STATUS
;=========================
; interrupt action
;=========================
;Movement of second stepper motor
;The interrupt occurs when any of Port-B bits 4 to 7
; have changed STATUS.
movf PORTB,w ; Read Port-B bits
movwf temp ; Save reading
xorwf bitsB47,f ; Xor with old bits,
; result in f
; Test each meaningful bit(4 ; and 7)
btfsc bitsB47,4 ; Test bit 4
goto bit4Chng ; Routine for changed bit 4
btfsc bitsB47,7 ; Test bit 7
goto bit7Chng ; Routine for changed bit 7
bit4Chng
movlw 0x05
movwf PORTA
goto restore
bit7Chng
movlw 0x06
movwf PORTA
goto restore
;====================================
; Storing and restoring
;====================================
; Store new value of portB
restore
movf temp,w ; This port-B value to w
movwf bitsB47 ; Store
; Restore context
swapf old_STATUS,w ; Saved status to w
movfw STATUS ; To STATUS register
swapf old_w,f ; Swap file register in itself
swapf old_w,w ; re-swap back to w
notRBIF:
bcf INTCON,rbif ; Clear INTCON bit 0
retfie
List p=pic16f84a
include <p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
;constants
TMR0_RLD EQU D'178' ;reload value for TMR0
MASK EQU 0xF0 ;used to mask out unused bits of port b
OPTION_CFG EQU B'10000111' ;used to init option reg; no pullups, prescaler on TMR0 with 1:256 ratio
INTCON_CFG EQU B'10100000' ;enable GIE and T0IE, clear all flags
; variables in PIC RAM
cblock 0x0d ; Start of block
hours ; counter of hours
mins ; counter of mins
secs ; counter of secs
NbHi
NbLo ; counters
NaHi
NaLo
old_w ; Context saving
old_STATUS ; saving the STATUS
bitsB47 ; Storage for previous value
; in port-B bits 4-7
temp ; Temporary storage
fwd_step
endc
rp0 equ .5
;=============================
; Reset vector
;=============================
org 0 ; start at address 0
goto main
;=============================
; interrupt handler
;=============================
org 0x04
;***********************************************
;**************** Main program****************
;***********************************************
main:
; Disable port-B internal pullups
; Interrupts on falling edge of pushbutton action
; movlw 0xbf ;RBPU=1 Pull up not use
movlw OPTION_CFG ;no pullups, prescaler on TMR0 with 1:256 ratio
movwf OPTION_REG ;Set OPTION_REG
movlw TMR0_RLD ;set up TMR0 for interrupts every 20ms @4MHz clock
movwf TMR0 ;at 16MHz load TMR0 with 0, for 16ms interrupts
bsf STATUS,rp0 ;Change to Bank1
clrf TRISA ;Set PORTA all OUT
movlw 0xf0 ;RB0,1,2.5=IN RB7=OUT
movwf TRISB ;Set PORTB
bcf STATUS,rp0 ;Change to Bank0
;movlw 0x00 ;start with everything off
;movwf PORTB
;I do not agree with that above
movf PORTB,W ;take the actual reading
andlw MASK ;clear unnecessary bits
movwf bitsB47 ;Store in local variable
;============================
; set up interrupts
;============================
movlw INTCON_CFG ;enable GIE and T0IE, clear all flags
movwf INTCON ;
;***********************************************
;**************** Motor drive *********************;
;***********************************************
drive1 call gofwd
goto drive1
;**************** Forward Drive *********************;
gofwd movlw d'12'
movwf fwd_step
loopf
movlw 0x05
movwf PORTB
call delay
movlw 0x09
movwf PORTB
call delay
movlw 0x0a
movwf PORTB
call delay
movlw 0x06
movwf PORTB
call delay
decfsz fwd_step,f
goto loopf
return
; Interrupt Service Routine
;====================================================
; We get here on TMR0 interrupt
IntServ:
; Save context
movwf old_w ; Save w register
swapf STATUS,w ; STATUS to w
movwf old_STATUS ; Save STATUS
movlw TMR0_RLD
movwf TMR0 ;reload TMR0 for next interrupt
bcf INTCON, T0IF ;clear interrupt flag
movf PORTB,w ; Read Port-B bits
andlw MASK ;clear the other bits, because they can be in any state
movwf temp ;save new reading for now
xorwf bitsB47,f ;compare with old
btfsc STATUS,Z ;if anything changed, the result will not be zero, skip
goto restore ;if nothing changed, restore context and exit
btfsc bitsB47,4 ; Test bit 4
goto bit4Chng ; Routine for changed bit 4
btfsc bitsB47,7 ; Test bit 7
goto bit7Chng ; Routine for changed bit 7
goto restore ;it must be one of the other 2 bits, ignore and exit; this was missed in the original code and I did not see it, either
bit4Chng
movlw 0x05
movwf PORTA
goto restore
bit7Chng
movlw 0x06
movwf PORTA
;goto restore ;not needed, just fall through
;====================================
; Storing and restoring
;====================================
; Store new value of portB
restore
movf temp,w ; This port-B value to w
movwf bitsB47 ; Store
; Restore context
swapf old_STATUS,w ; Saved status to w
movfw STATUS ; To STATUS register
swapf old_w,f ; Swap file register in itself
swapf old_w,w ; re-swap back to w
retfie
Code:;constants TMR0_RLD EQU D'178' ;reload value for TMR0 MASK EQU 0xF0 ;used to mask out unused bits of port b OPTION_CFG EQU B'10000111' ;used to init option reg; no pullups, prescaler on TMR0 with 1:256 ratio INTCON_CFG EQU B'10100000' ;enable GIE and T0IE, clear all flags
Code:main: ; Disable port-B internal pullups ; Interrupts on falling edge of pushbutton action ; movlw 0xbf ;RBPU=1 Pull up not use movlw OPTION_CFG ;no pullups, prescaler on TMR0 with 1:256 ratio movwf OPTION_REG ;Set OPTION_REG movlw TMR0_RLD ;set up TMR0 for interrupts every 20ms @4MHz clock movwf TMR0 ;at 16MHz load TMR0 with 0, for 16ms interrupts bsf STATUS,rp0 ;Change to Bank1 clrf TRISA ;Set PORTA all OUT movlw 0xf0 ;RB0,1,2.5=IN RB7=OUT movwf TRISB ;Set PORTB bcf STATUS,rp0 ;Change to Bank0 ;movlw 0x00 ;start with everything off ;movwf PORTB ;I do not agree with that above
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?