PDA

View Full Version : HPWM with zero duty cycle on PIC18F2550



missouri100
- 20th February 2014, 15:10
I am helping some Jr High students with a robot for science olympiad. We are using some potentiometers to control the duty cycle of a HPWM output for the drive motor. The HPWM runs an N channel mosfet that switches the 0V signal to the motor. It works fine until the analog value becomes zero, setting the duty cycle to zero in the code. When that happens the motors appear to run at full speed. We don't have an oscilloscope to check the actual output from the chip but we do have an LED connected in parallel with the mosfet. Its brightness correlates perfectly with the motor speed, even when the duty cycle is zero. It is as bright as it gets at the same time the motor is running full speed.

Does anyone know how the HPWM works with a zero duty cycle? It doesn't appear to be caused by some reaction in the mosfet because we see it in the LED. I thought about putting a pull down resistor on the output to make sure it goes off but it has a 1K current limiting resistor connected to the LED.

Look forward to anyone's help and suggestions.

cyberroth
- 25th February 2014, 15:36
To bad you don't have a scope. But the symptom suggests that the HPWM pin is going high, or at least to a very high duty cycle. It should go to zero volts(gnd). If the HPWM command is not playing nice, then you might try using the MIN operator to limit the ADC value to 1.

As far as as pull down resistor goes, a 47K resistor going from GND directly to the gate of the MOSFET base will keep the FET off in case for some reason the HPWM pin floats. 47K will also prevent glitching when the device is powered up. It will not affect the gave drive voltage or the LED. This is pretty much standard practice. I assume you are using a logic level FET?

Are you sure that the value is at zero when this happens and that the duty VAR is not somehow overflowing to 255? . A simple test would be a bare minimum program that simply does something like below. It should be on 2 sec then off 2 sec.


' Defines here

DO

HPWM 1, 0,1000.
PAUSE 2000
HPWM 1,255,1000
PAUSE 2000

LOOP


A schematic diagram and a posting of your code would be helpful.

missouri100
- 26th February 2014, 01:29
I have attempted to attach the code. I couldn't figure out how to insert it.

I have an autocad wiring diagram but haven't figured out how to attach it or insert it yet.

Sort of a humbling experience.

thanks

cyberroth
- 26th February 2014, 12:55
Consider this section of your code:



'create a +/- value centered at 512
left1 = abs(analogleft-512)
right1 = abs(analogright-512)

'scale value and create centered deadband of +/-10
'value should fall between -10 and 256 for duty cycle

dutycycleleft = ((left1 * 52)/100)-10
dutycycleright = ((right1 *52)/100)-10




PBP only supports unsigned integers, meaning negative numbers are not supported. There can be no "-10"

For example, you have a word variable "dutycycleleft". As a word, it can hold a value from 0 to 65555. If the current value is 5 and you subtract 10 then the variable will underflow by 5 and become 65531 instead of -5. This will cause the PWM to go full on.

A quick and dirty way to fix the code is to take this from your code:


if dutycycleleft < 1 then 'if dutycycle is between -10 and 1
dutycycleleft = 0 'then it equals zero
endif


And change it to:


if dutycycle left > 300 then
dutycycleleft = 0
endif



Do the same for dutycycleright

Try this and let me know how it works out

SUNFLOWER
- 27th February 2014, 01:17
if dutycycle left > 255 then
dutycycleleft = 0
endif

Or whatever -- HPWM value must be 0 to 255.

SUNFLOWER
- 27th February 2014, 01:39
if dutycycle left > 32777 then
dutycycleleft = 0
elseif dutycycle left > 255 then dutycycleleft = 255
endif

Or whatever -- HPWM value must be 0 to 255.