20X2 and HPWM or pwmout two simultaneous devices.

ferrymanr

Member
I am having a problem getting two independent and simultaneous PWM streams, each with a different duty cycle. What I am trying to do is speed and direction control of two motors from a 20X2. I am driving an external H-bridge Pwmout only permits use on C.5 and I can't see how hpwm can specify ports or even if I can use two simultaneously with different duty cycle. Can someone point me in the right direction.
Thanks
Richard
 

hippy

Ex-Staff (retired)
I am having a problem getting two independent and simultaneous PWM streams, each with a different duty cycle.
Unfortunately the 20X2 has only one PWM channel. While HPWM can generate up to four PWM signals, these are all derived from the single PWM channel so cannot have independent duty control.
 

ferrymanr

Member
I feared that might be the case. I am going to have to find a chip that can handle two independent pwm outputs although it's not clear if any will. Pity as my routines ar working beatifully on one channel which controls a 48W motor with superb precision and smoothness positioning a 20Kg mass.
Richard

Unfortunately the 20X2 has only one PWM channel. While HPWM can generate up to four PWM signals, these are all derived from the single PWM channel so cannot have independent duty control.
 

hippy

Ex-Staff (retired)
I feared that might be the case. I am going to have to find a chip that can handle two independent pwm outputs although it's not clear if any will.
The 18M2 has two PWM channels, the 14M2, 20M2, 28X2 have four, all of which can have duty independently controlled.
 

ferrymanr

Member
Thanks Hippy. I checked the data sheets I could find and it looked as if the newer releases of the 28X2 was the only one with independent timers. I have an older 28X2 but it was before the change of PIC and has the older B1 firmware and just one timer. I have ordered a new 28X2 and will try that. I understood the 14 pin and 18 pin chips only had one timer shared by the PWM channels.
Richard
 

hippy

Ex-Staff (retired)
Don't forget that sharing a timer only means they have to run at the same frequency / with the same period; the duty is still independent.
 

inglewoodpete

Senior Member
Don't forget that sharing a timer only means they have to run at the same frequency / with the same period; the duty is still independent.
To add clarification, I used the older 28X1 to drive two independent PWM channels. While the fundamental PWM frequency had to be the same in both channels, the duty cycle of each was different. This was adequate for running two motors at different speeds or 2 lighting channels at different intensities.

You will find that even the older 28X2 or 40X2 have similar capabilities.
 

ferrymanr

Member
That is interesting inglewoodpete. As the two motors I am driving are of the same type and similar loads I intended using the same frequency of drive on both. As the older 28X1 has pwm1 and pwm2 outputs I assume you can use those. The hardware pwm (hpwm) would still have only one common setting. The 28 pin chip is really overkill as far as I/O is concerned as I have more than I need on the 20 pin (2 x ADC, 2 x PWM, 4 x ouputs, 1 bidirectional serial comms). I did look at the 20M2 as it has more pwm but it is a bit strangled on memory.
Richard
 

techElder

Well-known member
It should be mentioned that you shouldn't forget that 2 is sometimes better than one. Give each motor it's own trimmable PWM hardware. You end up with 3 components, but would that be a really big problem?
 

pscs

New Member
It's worth remembering that while hpwm can drive 4 pins, they all have the same duty cycle, BUT pwm1 and pwm2 are totally separate from hpwm, and can run in the background as well (with the same frequency as each other (but different from hpwm), but with different duty cycles). This threw me a bit until I got my head around it

I needed 2 PWM signals (actually to control LED lighting levels, not motors) with different duty cycles. My first thought was to use 2 hpwm pins for both, until I realised you couldn't, so now I use hpwm (in 'single' mode) for one of them, and pwm1 for the second. (I could have used pwm1 and pwm2, but I'd designed the circuit to use hpwm)

I got confused because I'd been looking at hserin which can run in the background, and is 'better' than 'serin' which can't run in the background, so assumed that hpwmout would run in the background and pwmout wouldn't - but pwmout runs in the background as well, so is fine.
 
Top