Hi, I am playing with PWM for speed control of a small DC motor. The motor is from a Tamiya "Universal Gearbox" kit. Actually the motor is a small 3V motor from Mabuchi, and Tamiya have put it together with a neat little gearbox that gears it all down to a useful speed. There are three gear ratios you can choose, and three output shaft orientations, but I digress. The thing that piqued my interest was that the first driven wheel in the gearbox kit has a little hole in it. This immediately conjured up an image of a slotted opto-switch, speed measurement and closed loop control. It has taken me a long while to find the time to actually put together what I visualised, but I am having fun doing it. Anyway, I am using a Zetex ZHB6718 H-bridge to drive the motor, and a PIC12C508A to measure speed and adjust the PWM. The PWM code is Scott Dattalo's, from www.dattalo.com, which works nicely on the 12xx devices. If you take a look at that code you can see that he has left various timeslots for your own code, so that's where I put my speed measurement and PWM adjustment. How it works. The opto switch output is wired to T0CKI, so the LSB of TMR0 toggles for every revolution. I time how long it stays high, which gives me the period. Scott's code is basically a big loop which repeats every 256us, so my time is in units of 256us. I then have some simple math that works out whether the period is too long or too short for the desired speed (which is expressed in terms of percentage of full speed) and I nudge the PWM up or down accordingly. The output is PWM and a direction pin, so you can drive an H-bridge or other driver circuit. It seems to work quite well, but I need to implement "proper" P-control as it is not very responsive to changes in load. Right now I only nudge the PWM up or down by one unit per loop, which means that it takes a while to settle. I do have stall detection though, that boosts the PWM in an attempt to 'unstick' the motor, and a 2400bd serial interface to set the desired speed. For three motors it would probably be cheap to have a PIC12xx for each motor, although the speed detector would have to be a long way up the gear train to give a significant number of pulses per second, so the code can do a significant number of adjustments per second. Also, the code could be easily modified to operate for a certain number of revolutions and then stop, but it would require a lot more math to compensate for acceleration and deceleration of the motor. --- Andrew Errington http://www.piclist.com/member/AME-SYNA-FA8a PIC/PICList FAQ: http://www.piclist.com -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.