Let's Make Robots!

PWM and HPWM motor control.


Just wondering,

I just took apart an old ps2 controller and got my 28x1 reading the sticks and had one stick pwm a led.


How could I use this to control 2 motors? 

Im using the standard 28x1 board and have an l293d plugged in an ready to go.

 What i plan to achieve is pwm on forward and backwards on two motors using 2 analogue sticks.


Any ideas or help would be great.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

 Here is all the code you need... this is from walter's RC code so ignore the serial stuff if you are using a wire. Basically, you just need 2 variables from your stick(s). If it is two, it will be the forward/back pot on each stick. If it is just one, see the extra code at the bottom of the post -this is what I use in my single stick transmitter. 


serin 1,N2400,(85,85,"motornumbers"),b7,b6,bit2,rcmotorspeed 'all you need here is the 2 vars from the 2 sticks, b6 and b7

select case b6
case 130 to 136
let b6=0
low 1
case >136
let b6=b6-133
let b6=b6*2
let b6=b6 min 0 max revspeed
low 1
case <130
let b6=b6*2
let b6=255-b6
let b6=b6 min 0 max revspeed
high 1

select case b7
case 130 to 136
let b7=0
low 2
case >136
let b7=b7-133
let b7=b7*2
let b7=b7 min 0 max revspeed
low 2
case < 130
let b7=b7*2
let b7=255-b7
let b7=b7 min 0 max revspeed
high 2
gosub drive
goto rcmain

pwmduty 1,b6
pwmduty 2,b7


and if you want to use just one stick:

readadc 0,b1 let b1=b1 min 5 max 250 'Y F/R
readadc 1,b2 let b2=b2 min 5 max 250 'X R/L
let b5= b1-b2+130 min 0 max 255 'left
let b6= b1+b2-130 min 0 max 255'right
let b5=b5+6 min 0 max 255
high 3 pause 3 low 3 pause 1 serout 7,N2400, (85,85,"motornumbers",b5,b6,bit2,b3)

What does Hewlett-Packard Window Manager have to do with controlling motors?

Or, to put it another way: what's HPWM?


Picaxe includes an HPWM output system... It is basically a 4 channel output (i.e. to be connected to a motor driver) using a PWM output. This 4-way set-up gets you away from the problem of having to reverse your pwm pulses, when in reverse, to maintain speed. --Remember a while back the issue of 75% on one direction is 75% off in reverse? At any rate, it is a complicated mess of commands and connections and no one really knows how it works. I said, skip the whole thing and just use relays to reverse your motors!!

Did anyone read the manual? It's a commercial product for heaven's sake. If you handed over money for it, the least they could do is explain it.

The reversal thing isn't really complicated. You have one pin which is the PWM and the other is "low". If you demand reverse, teh pic swaps teh functionality of the pins, so the first becomes "low" and the second befomes PWM.

I guess the H is for H-bridge, then.

Thanks great help BaseOverApex.

Maybe a stupid question but, Are all pins capable of pwm on 28x1

I thought it was only a select few.

I was lookin at the pinout and saw more hpwms than pwms so thought i may have to use HPWM.

I suppose Chris has aswered that.


I'm afraid I'm not picaxe expert. My field is the raw pic. If I had to guess, I'd say there are only a limited number of the pins on the picaxe which can be PWM. It seems reaonable that the internal softwarewould only be coded to control some rather than all of the pins in order to reduce overhead. In teh raw pic, you can cause your program to address any pin.

Again, if I had to guess, I'd say my first explaination might have been over simple and it may in fact be the case that the PWM pin is always PWM, but it's paired with a HPWM pin. In this scenario, the for "25% forward" the PWM would output 25% and the HPWM would be set low. For "25% reverse," thePWM would output 75% and the HPWM would be set high.

I have to stress, I don't use these chips, so this is 100% speculation. However, if I were setting up a company to make basic bootloaders for PICs? That's how I'd do it!!!!


The high 1 low 1  high 2 low 2 thing... I use relays to reverse my motors and thus do not need to use the hpwm thing. You can still use the code above as a sample, but it will not cut and paste. --All the calculations are correct, though.

 BTW--I might just be an idiot, but I could never figure out the HPWm thing.

Assuming my second scenario above is correct, you could actually drive your relay coil from the HPWM output and your FET from the PWM. It's an ASSUMPTION, though...

Thanks for sharing the code from your amazing bot. 

Just a few questions though....

in your code in the section select case b6 you have

low 1

other code

low 1

other code

high 1

I dont know why...

and can your bot reverse aswell (with this code)? I cant see it in your code. Sorry!


Could I ask what you have connected to each pin (relavant to code obviously)