PWM from wheel speeds
The circuit again
DPDT switch is a relay, switched by "backwards" bit.
SPST switch is a FET, switch by "pulse width modulation" (PWM) bit
Average volage across motor is proportional to PWM "on" time.
The code
TMR0 is free-running from 0..255, incrementing every 64uS
Main program is just a loop (pulse widths handled in interrupts)
"backwards" bit energizes relay (slow)
FET at bottom completes circuit (fast)
First cut:
if (speed > TMR0) pin=ON else pin=OFF
Problem:
speed = 255
and
TMR0 = 255
means pin OFF
99.6% duty cycle -- not bad, but 100% would be better.
FET consumes power when switching, nice to leave it fully on if possible
Second cut:
if (speed >= TMR0) pin=ON else pin=OFF
Even worse:
speed = 0
and
TMR0 = 0
means pin ON
Wheels can't be fully stopped, robot twitches
Motors draw even more power than switching FET
Solution:
if (speed == 0) pin=OFF
else if (speed >= TMR0) pin=ON
else pin=OFF
Slight discontinuity at
speed=1
, who cares
No careful timing needed, TMR0 does all the work
Just do the comparison "often enough"
Lose resolution, not correctness, if checking slows
Self-correcting for "missed" checks
TMR0 overflows every 16mS, so PWM frequency is 61Hz. Slow but acceptable.
Previous: Wheel speeds
Next: Relay switching time
This page works best with
Lynx
.
mail me
or go
home
.