Monday, October 03, 2005

Examples from Class

Servo

DEFINE OSC 40
start: pulseWidth var byte ' set up constants with the minimum and maximum pulsewidths minPulse CON 20 maxPulse CON 250 ' set up a constant with the time between pulses: refreshPeriod CON 20 ' set an initial pulsewidth: pulseWidth = minPulse
main: 'take the output pin low so we can pulse it high Low PORTC.3 ' pulse the pin PulsOut PORTC.3, pulseWidth ' pause for as long as needed: Pause refreshPeriod ' change the angle for the next time around: IF pulseWidth > maxPulse Then pulseWidth = minPulse Else pulseWidth = pulseWidth + 1 Endif GoTo main

Pulsout(blinking)

define osc 40
myadcvar var worddutycycle var byteblinky var byte
output portb.7output portb.0
pause 500
blinky = 0

while blinky < 10portb.7 = 1pause 500portb.7 = 0pause 500blinky = blinky +1 wend
portb.7 = 1
main:
for dutycycle

Dim

' This example takes an analog input on RA2 and uses it to generate' a duty cycle for the PWM command. The PWM command is used' to dim an LED on pin B0.
define osc 40 'set the clock at 40mhz
' Define ADCIN parametersDEFINE ADC_BITS 10 ' Set number of bits in resultDEFINE ADC_CLOCK 3 ' Set clock source (3=rc)DEFINE ADC_SAMPLEUS 15 ' Set sampling time in uS
adcVar VAR WORD ' ADC resultdutyCycle var byte ' Duty cycle for PWM
' Set PORTA to all inputTRISA = %11111111 ' Set up ADCON1ADCON1 = %10000010
output portb.7portb.7 = 1
main: ADCIN 2, adcVar ' convert ADC value to a byte value: dutyCycle = adcVar / 4 ' PWM the LED. The third parameter is the number of ' cycles to repeat the PWM. at 4MHz, 1 cycle = 5 ms. pwm portb.0, dutyCycle, 100 'increase for chunkiness
goto main

0 Comments:

Post a Comment

<< Home