
Originally Posted by
Pommie Are you going to do the interrupt version as well?
Finally found the problem. Here's the asm port of Pommie's second program:
Code:
list p=18F1320
include <p18F1320.inc>
CONFIG OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF
cblock 0x00
Mode,Keys,OldKeys,Edges,ServoPosH,ServoPosL
srcH,srcL,dstH,dstL
endc
org 0x00
goto init
org 0x18
isr btfss PORTB,3 ;the ISR
goto else1
bcf LATB,3 ;turn off servo output
movlw high(.20000) ;Off time = 20ms - Servo Time
movwf srcH
movlw low(.20000)
movwf srcL
movff ServoPosH,dstH
movff ServoPosL,dstL
call sub16
movff dstH,CCPR1H
movff dstL,CCPR1L
goto isr_done
else1 bsf LATB,3 ;turn on servo output
movff ServoPosH,CCPR1H ;On time
movff ServoPosL,CCPR1L
isr_done
bcf PIR1,CCP1IF ;clear int flag
retfie FAST
init movlw 0x70
movwf OSCCON ;Osc=8MHz
movlw b'00000101' ;A2D on and select AN1
movwf ADCON0
movlw b'01111101' ;A1 = analog
movwf ADCON1
movlw b'10110101' ;Right justify - Fosc/16
movwf ADCON2
bcf TRISB,3 ;make servo pin output
bcf LATB,3 ;Servo output off
movlw b'00001011' ;Special event trigger
movwf CCP1CON
movlw b'10010001' ;Timer 1 on with Pre=2
movwf T1CON
movlw high(.1500) ;set servo to mid position
movwf ServoPosH
movwf CCPR1H ;and set CCP initial value
movlw low(.1500)
movwf ServoPosL
movwf CCPR1L
bsf PIE1,CCP1IE ;enable CCP1 interrupt
bsf INTCON,PEIE ;enable peripheral interrupts
bsf INTCON,GIE ;enable global interrupts
bcf INTCON2,RBPU ;enable PORTB weak pullups
main btfss PORTB,3 ;Wait for start of servo pulse
goto main
wait2 btfsc PORTB,3 ;wait for end - makes for good debounce
goto wait2
movff Keys,OldKeys ;make a copy of keys
movf PORTB,W ;get switch state
andlw b'00100101'
movwf Keys
movlw b'00100101' ;make pressed keys = 1
xorwf Keys,F
movlw b'00100101' ;all 3 pressed?
cpfseq Keys
goto not3
clrf Mode ;yes, set to use ADC input
not3 movf Keys,W ;keep only keys that have changed
xorwf OldKeys,W
movwf Edges
andwf Keys,W ;keep only new key presses - not key releases
movwf Edges
tstfsz Mode ;are we using the pot?
goto nomode
bsf ADCON0,GO ;yes, start conversion
waitcv btfsc ADCON0,DONE ;wait for it to complete
goto waitcv
movff ADRESH,srcH ;Pos will be 1ms to 2.023ms
movff ADRESL,srcL
movlw high(.1000)
movwf dstH
movlw low(.1000)
movwf dstL
call add16
movff dstH,ServoPosH
movff dstL,ServoPosL
nomode tstfsz Edges ;any key pressed
goto mode1 ;go switch to fixed mode
goto mode0
mode1 movlw 1 ;switch to fixed mode
movwf Mode
mode0 movlw 1
subwf Edges,W ;key 1 pressed
btfss STATUS,Z
goto key2
movlw high(.1000) ;set servo fully left
movwf ServoPosH
movlw low(.1000)
movwf ServoPosL
goto again
key2 movlw 0x04 ;key 2?
subwf Edges,W
btfss STATUS,Z
goto key3
movlw high(.1500) ;set center
movwf ServoPosH
movlw low(.1500)
movwf ServoPosL
goto again
key3 movlw 0x20 ;key 3?
subwf Edges,W
btfss STATUS,Z
goto again
movlw high(.2000) ;set fully right
movwf ServoPosH
movlw low(.2000)
movwf ServoPosL
again goto main
sub16 movf srcL,W
subwf dstL,F
movf srcH,W
btfss STATUS,C
incf srcH,W
subwf dstH,F
return
add16 movf srcL,W
addwf dstL,F
movf srcH,W
btfsc STATUS,C
incf srcH,W
addwf dstH,F
return
end
and here's the first one again, modified with the suggestions from a few posts ago:
Code:
list p=18F1320
include <p18F1320.inc>
CONFIG OSC=INTIO2,WDT=OFF,MCLRE=ON,LVP=OFF
cblock 0x00
ServoPosH,ServoPosL,srcH,srcL,dstH,dstL
endc
org 0x0000
init movlw 0x70
movwf OSCCON ;Osc=8MHz
movlw b'00000101' ;A2D on and select AN1
movwf ADCON0
movlw b'01111101' ;A1 = analog
movwf ADCON1
movlw b'10110101' ;Right justify - Fosc/16
movwf ADCON2
bcf TRISB,3 ;make servo pin output
bcf LATB,3 ;Servo output off
movlw b'00001011' ;Special event trigger
movwf CCP1CON
movlw b'10010001' ;Timer 1 on with Pre=2
movwf T1CON
movlw high(.1500) ;set servo to mid position
movwf ServoPosH
movwf CCPR1H ;and set CCP initial value
movlw low(.1500)
movwf ServoPosL
movwf CCPR1L
main btfss PIR1,CCP1IF ;wait for CCP interrupt bit
goto main
bcf LATB,3 ;end pulse
movlw high(.20000) ;Off time = 20ms - Servo Time
movwf srcH
movlw low(.20000)
movwf srcL
movff ServoPosH,dstH
movff ServoPosL,dstL
call sub16
movff dstH,CCPR1H
movff dstL,CCPR1L
bcf PIR1,CCP1IF ;clear int flag
bsf ADCON0,GO ;start conversion
conv btfsc ADCON0,DONE ;Wait for it to complete
goto conv
movff ADRESH,srcH ;Pos will be 1ms to 2.023ms
movff ADRESL,srcL
movlw high(.1000)
movwf dstH
movlw low(.1000)
movwf dstL
call add16
movff dstH,ServoPosH
movff dstL,ServoPosL
wait btfss PIR1,CCP1IF ;wait for int flag
goto wait
bsf LATB,3 ;start pulse
movff ServoPosH,CCPR1H ;Servo time in uS
movff ServoPosL,CCPR1L
bcf PIR1,CCP1IF ;clear int flag
goto main
sub16 movf srcL,W
subwf dstL,F
movf srcH,W
btfss STATUS,C
incf srcH,W
subwf dstH,F
return
add16 movf srcL,W
addwf dstL,F
movf srcH,W
btfsc STATUS,C
incf srcH,W
addwf dstH,F
return
end