Scalerobotics:
This snippet has the capabiltiy to increment the pulse size a little bit at a time.
With this code the car starts out driving very fast backwards, then after five to ten seconds it stops, but never starts up forward.
My theory is that the (PAUSE 20) is what is causing the 50 pulses per second, Is that correct?
Code:
duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
range VAR word
segment var word
TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
CCP1CON = %00001100 ' Set CCP1 to PWM
T2CON = %00000101 ' Turn on Timer2, Prescale=4
' Use formula to determine PR2 value for a 1KHz signal,
' 4MHz clock, and prescale=4. (4E6/(4*4*1E3))-1=249
' PR2 = 249 ' Set PR2 to get 1KHz out
' PR2 = 499 ' Set PR2 to get 2KHz out
' Use formula to determine CCPR1L:CCP1CON<5:4> value for
' ends of range 20% to 80%. (249+1)*4*0.2=200 (20% value)
' (249+1)*4*0.8=800 (80% value)
' duty = 200 ' Set duty cycle to 20%
range = 25
duty = 1650 ' Set duty cycle to 1.65msec
segment = 0
mainloop: CCP1CON.4 = duty.0 ' Store duty to registers as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY >> 2
Pause 20 ' Pause 1/50 of second
duty = duty + 2 ' Increase duty cycle
IF (duty < (1650 + segment + range)) Then mainloop ' Do it again unless 2 msec
duty = duty + 2 ' Reset to 20% duty cycle
segment = segment + range
PAUSE 2000 'Pause 2 seconds
GoTo mainloop ' Do it forever
Bookmarks