Hey Guys,
As you probably know, I'm VERY new to all this. I'm playing about with some digital R/C servos now which operate at 3mS wavelength (1 - 2ms high for full movement). I've managed to come up with some VERY crude and pretty much useless code that outputs the correct signal for 1.5mS high (followed by 1.5ms low). This 'should' centre a servo. However, I want to make something a bit more useful than this. Can anyone suggest a better way of going about generating a software PWM output that is actually useable in real life applications?
Please see my current messy code below:
Many Thanks - Trev
As you probably know, I'm VERY new to all this. I'm playing about with some digital R/C servos now which operate at 3mS wavelength (1 - 2ms high for full movement). I've managed to come up with some VERY crude and pretty much useless code that outputs the correct signal for 1.5mS high (followed by 1.5ms low). This 'should' centre a servo. However, I want to make something a bit more useful than this. Can anyone suggest a better way of going about generating a software PWM output that is actually useable in real life applications?
Please see my current messy code below:
list p=12f675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
;***** VARIABLE DEFINITIONS
INT_VAR UDATA_SHR 0x20
timer RES 1
counter RES 1
;**********************************************************************
RESET_VECTOR CODE 0x000 ; processor reset vector
goto main ; go to beginning of program
main:
call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
clrf TRISIO ; clear the tri state register
movlw b'00000001' ; set the tri state reg to AN0 as input, remainder output
movwf TRISIO
bcf STATUS,RP0 ; set file register bank to 0
clrf GPIO ; clear the i/o ports (turn all off)
movlw .50
movwf counter
;************************************************************************************************
; Centre servo position
;************************************************************************************************
centre:
bsf GPIO,02 ; set bit 2 of gpio to high
call delay1_5ms ; call delay to keep the pulse high
nop
nop ; need these to make 150uS
nop
bcf GPIO,02 ; set bit 2 of gpio to low
call delay1_5ms ; call delay to keep the pulse low
decfsz counter,1
goto centre ; repeat
goto null_loop
delay1_5ms:
movlw .24 ; move 24 into w
movwf timer ; move w into the timer variable
loopb:
decfsz timer,1 ; decrease timer by 1 and put result in timer
goto temploop ; repeat if timer is not zero
retlw 0 ; return
temploop:
nop ; added to make each loop worth 6uS
goto loopb ; we've taken up the time, go back
null_loop:
goto null_loop ; just don't do anything else
END ; directive 'end of program'
Many Thanks - Trev