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.
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)
if (speed > TMR0) pin=ON else pin=OFF
speed = 255
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
if (speed >= TMR0) pin=ON else pin=OFF
speed = 0
TMR0 = 0
means pin ON
Wheels can't be fully stopped, robot twitches
Motors draw even more power than switching FET
if (speed == 0) pin=OFF
else if (speed >= TMR0) pin=ON
Slight discontinuity at
, 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