Yo everyone this is my first ever post on this awesome forum I am currently working on my final year project to build a four-legged, stair-climbing robot...I am still relatively new to PIC and MPLAB stuff so I am looking for some advice and help here...
I am writing a test servo code which moves the servo position to 180 degrees and then back to 90 degrees...When I test it, the servo stops when it only reaches the 90 degrees position and kinda stuck at that position instead of moving to 180 then back to 90...Sometimes it even jitters but it just won't move beyond 90 degrees...Is it something wrong with the code I have written, perhaps a problem with my delay? I am using a 20Mhz crystal clock...
Any advice and help would be greatly appreciated...Thanks
p/s sorry for the messy code...I wrapped
I am writing a test servo code which moves the servo position to 180 degrees and then back to 90 degrees...When I test it, the servo stops when it only reaches the 90 degrees position and kinda stuck at that position instead of moving to 180 then back to 90...Sometimes it even jitters but it just won't move beyond 90 degrees...Is it something wrong with the code I have written, perhaps a problem with my delay? I am using a 20Mhz crystal clock...
Code:
; RC3 is output to servo
list p=16f877a
include <p16f877a.inc>
__CONFIG 0x3FF2
D1 EQU 0x20
D2 EQU 0x21
D3 EQU 0x22
COUNT EQU 0x23
ERRORLEVEL -305
ORG 0x200
GOTO IOSETUP
IOSETUP
ERRORLEVEL -302
BANKSEL TRISC
MOVLW b'00000000'
MOVWF TRISC
BANKSEL PORTC
ERRORLEVEL +302
CLRF PORTC
GOTO MAIN
MAIN
BANKSEL PORTC
TURN180
MOVLW d'25' ; repeat to hold the servo at 180 degree before moving on
MOVWF COUNT
LOOP BSF PORTC,3
CALL DELAY2m ; generates 2mS pulse for 180 degree turn
BCF PORTC,3
CALL DELAY18m ; total 20mS
DECFSZ COUNT
GOTO LOOP
GOTO TURN90
TURN90
BSF PORTC,3
CALL DELAY1p5m ; generates 1.5mS pulse for 90 degree turn
BCF PORTC,3
CALL DELAY18p5m ; total 20mS
GOTO TURN90
DELAY1p5m MOVLW d'1'
MOVWF D3
MOVLW d'10'
MOVWF D2
MOVLW d'249'
MOVWF D1
DECFSZ D1
GOTO $-1
DECFSZ D2
GOTO $-5
DECFSZ D3
GOTO $-9
RETURN
DELAY2m MOVLW d'1'
MOVWF D3
MOVLW d'14'
MOVWF D2
MOVLW d'237'
MOVWF D1
DECFSZ D1
GOTO $-1
DECFSZ D2
GOTO $-5
DECFSZ D3
GOTO $-9
RETURN
DELAY18p5m MOVLW d'1'
MOVWF D3
MOVLW d'121'
MOVWF D2
MOVLW d'254'
MOVWF D1
DECFSZ D1
GOTO $-1
DECFSZ D2
GOTO $-5
DECFSZ D3
GOTO $-9
RETURN
DELAY18m MOVLW d'1'
MOVWF D3
MOVLW d'119'
MOVWF D2
MOVLW d'251'
MOVWF D1
DECFSZ D1
GOTO $-1
DECFSZ D2
GOTO $-5
DECFSZ D3
GOTO $-9
RETURN
END
p/s sorry for the messy code...I wrapped
Code:
tags around it but it still turns out like this :confused:
Last edited: