L A B 4 C O D E
s e r v o c o d e
' Code for Servo Motor DEFINE OSC 4 DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 10 ' Set sampling time in uS TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result TRISD = %00000000 'Set PortD to all OUTPUT TRISC = %10000000 ' SET PORT C SERIAL IN on PORTC.7 and out on C.6 ADCvar VAR WORD ylLED VAR PORTD.0 pulseWidth var WORD pulseRange VAR WORD servo_1 VAR PORTC.3 txPin VAR PORTC.6 ' set up constants with the minimum and maximum pulsewidths minPulse CON 50 maxPulse CON 250 ' set up a constant with the time between pulses: refreshPeriod CON 20 pulseWidth = minPulse 'initial pulse width pulseRange = maxPulse - minPulse PAUSE 500 GOSUB blink main: ADCIN 0, adcVAR ' check value on scree with serial out serout2 txPin, 16468, ["my adcvar = ", DEC ADCvar, 13, 10] Low servo_1 'make output pin low so we can pulse it high PulsOut servo_1, pulseWidth ' PULSE THE SERVO PIN Pause refreshPeriod IF pulseWidth > maxPulse Then pulseWidth = minPulse ' change back to min to start over Else pulseWidth = minPulse + (pulseRange * (ADCVAR/10))/100 Endif GOTO main