Hi,
I’m trying to control a servo to a central position using a PIC 16f84A. I am using the rudimentary code below to go to a central position when switch one is pushed, with switch two not really doing anything at the moment.
I have tested this on MPLAB IDE and the timings seem OK, a 1500 cycle pulse and a 20000 cycle total. Running with a 4MHz oscillator this is supposed to centralise the servo. However the servo just skews hard over each time.
Have I messed up the timings somewhere??
;***********Set up the constants*********
COUNT1 equ 20h ;First counter for our delay loops
COUNT2 equ 21h ;Second counter for our delay loops
;*********Set up the port****
bsf STATUS,5 ;********Switch to bank 1****
movlw B'00000000'
movwf TRISB ;Set all port b to outputs
movlw B'11111111'
movwf TRISA ;set port A bit to input
bcf STATUS,5 ;switch back to bank 0
;****Start the program*******
Start movlw B'11111111'
movwf PORTB ;*****Set the ouput port B to high
BTFSC PORTA, SW1
goto Switch1
BTFSC PORTA, SW2
goto Switch2
goto Start ;go back to Start and turn LED on again
Switch1
movlw B'11111111'
movwf PORTB ;*****Set the ouput port B to high
;do the offset part
movlw D'255'
movwf COUNT1
call DelayByFour ;Wait Four instruciton cycles * COUNT1
; do the position part
movlw D'118'
movwf COUNT1
call DelayByFour ;Wait Four instruciton cycles * COUNT1
;finish the PWM cycle
movlw B'00000000' ;invert lights
movwf PORTB ;'*****Set the output port B to low
movlw D'120' ;remaing time - 10 additional instructions
movwf COUNT1
call DelayByFour ; now up to 2ms
movlw D'18' ;Do the final 18ms loop
movwf COUNT2
movlw D'115'
movwf COUNT1
loop3
call DelayByFour ;Wait Four instruction cycles * COUNT1
decfsz COUNT2,1
goto loop3
BTFSC PORTA,SW2
goto Switch2
goto Switch1 ;go back to the start of the loop
Switch2
movlw B'11110111'
movwf PORTB ;*****Set the ouput port B to high
BTFSC PORTA,SW1
goto Switch1
goto Switch2
DelayByFour
NOP ;make instruction count 4
decfsz COUNT1,1 ;Subtract 1 from COUNT1
goto DelayByFour ;Go back to the start of our loop
NOP ;make final loop 4 instruction cycles
return
;****End of program*******
end