Ruth Hendrix

PART 1: PWM-Generation, function of the counter???

Discussion created by Ruth Hendrix Employee on Jan 26, 2006
This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.
Posted: Fri Jun 17, 2005 8:58 am    

This time a question about PWM:

I need probably a PWM-Frequency approximately = 2KHz.
I have a busclock = 24 Mhz.
When i use Clock A divided by 128, then is Clock A = 187,5KHz.
I set the PWMCAE=0 (left aligned) and PWMPeriod to PWMPER=100.
Thus I have the PWM-Frequency = (Clock A) / PWMPER = 187,5KHz/100 =
1875Hz. That´s okay for me! So far so good; I hope that´s right so far!
When I vary the Duty Cycle by software (1-99), I can adjust the
voltage output in 50mV-steps. (Output Voltage interval: 0-5V -->
That´s my consideration about controlling the output voltage. It is right?
But now the question:
What is the funtion of the counter??? Which value should I choose for
the counter???

Posted: Fri Jun 17, 2005 9:30 am    
The counter is used internally by the MCU to create the PWM signal on the output. It is incremented by the selected A clock every 1/187500 second.

With PWMPER = 100 and PWMDTY = 50:
The counter starts at 0, the PWM output is High until the counter reaches 50, then it goes Low. The PWM output stays Low until the counter reaches 100, and then the counter is reset to 0 (and the cycle repeats).

So: you should set the PWMPER and PWMDTY registers, you don't have to do anything with the counter at all. Hope this was somewhat understandably explained...

PS: for what do you want to use the output voltage?
Posted: Fri Jun 17, 2005 11:16 am    
Thanks for the answer.
I understood it fortunately Smile

I want to use the output voltage to control a fan over mosfet. I hope, it functions...
Posted: Fri Jun 17, 2005 5:35 pm    
Sounds good. Motors can be well controlled by PWM and the frequency should be ok. The MOSFET probably has an internal diode to take care of the turn-off spike of the fan, so it should work fine Smile
Posted: Mon Jun 20, 2005 6:11 am    
Good morning,

The MOSFET probably has an internal diode to take care of the turn-off spike of the fan, so it should work fine

I don´t have experience with mosfets. I want to use the IRFZ46N-Mosfet. In the datasheet is a dv/dt=5V/ns (Description --> Peak Diode Recovery) listed.

Do you mean that?
Posted: Wed Jun 22, 2005 10:05 am    

if you look at the chapter "Source-Drain Ratings and Characteristics" (bottom of page 2 of the datasheet), you see the "integral reverse p-n junction diode" (black triangle with horizontal (z-shaped) bar above it) which is what I am talking about.

I don't know how much you know about electrics and inductances, but I'll try to explain the reason for the diode: When you turn ON the MOSFET, current is running through the fan motor and a magnetic field is created around it.
When you turn OFF the MOSFET, the magnetic field will try to force a current through your MOSFET by setting up a (large!) voltage in the opposite direction. The diode allows this current to flow and reduces the turn-off voltage spike to about 1.3Vf (Vsd), preventing your MOSFET from melting...

Conclusion: it should work, your choice of MOSFET seems good and it will not have a problem with the turn-off spike because of its built-in diode.
Posted: Thu Jun 23, 2005 4:56 am    
Good morning!

I don't know how much you know about electrics and inductances, but I'll try to explain the reason for the diode:

Hmm, I would call me: "Hobby-electronic technician".
Thus, I´m thankful about every adjuvant tip. Thank you!!!

Conclusion: it should work

Wonderful!!! In a few weeks, I will test it and I will refer about the results.

Thank you once again!
Posted: Thu Jun 23, 2005 8:33 am

I have antoher question additionally:

Can somebody tell me, in which way I can check, whether the fan is running?
Has somebody an idea?

It is finally possible, that voltage is measurable between the terminals of the fan, but the fan doesn´t spin, because something blocks it.

Posted: Thu Jun 23, 2005 1:56 pm    

one possibility would be to put a (small, maybe 0.1 Ohm) resistance in series with the fan/motor. If you measure the voltage over this resistance it will be proportional to the running current (in this case 0.1V per Ampere), which is again more or less proportional to the load of the motor.

If the motor is not powered, you will measure 0V over the resistance. If the motor is blocked, you will have a "stall current" which is the maxium current and thus highest voltage. If the motor is running, the voltage will be something in between.

However some additional considerations:
* You would need to take care to place the resistor in the correct place in your circuit (beware of negative voltage due to motor turn-off spike)
* Make sure to measure voltage when the desired current is running (for instance maybe not when the MOSFET is OFF)
* Possibly integrate an amplifier for the current signal.
* Carefully consider the parameters of the series resistance (see how much voltage it may drop before the fan stops working, what range of current will be there (what is the stall current), and how much power it must be able to handle (P=R*Istall^2)

What does your circuit diagram look like btw? Will the MOSFET switch the positive voltage or ground?
Posted: Tue Jun 28, 2005 5:02 am    
Good morning!

Thank you for the detailed answer.
It seems to be very complicated.
I will consider this implementation and will make a report about the progress. (promised)
First, I think, I have to get started the system. I have enough problems with programming the software, thus... wait and see Smile

Okay, thank you once again.

The circuit diagram looks like below:

Posted: Tue Jun 28, 2005 12:26 pm    

Thanks for the circuit diagram. The diode in parallell to the load will simplify the current measurement. Due to that diode, the internal diode in the MOSFET will actually not be "used" (so to say).

The considerations aren't too complicated, actually. Here is how I would go about to measure the running current. I will be calculating an example with a 12V, 3W fan.

We get:
Nominal current In=P/U = 3W/12V = 250 mA

With a multimeter we can measure the internal resistance, say we find 12 Ohm.
We compute Istall = 12V/12 Ohm = 1A

Metal film resistors can typically handle 0.6W. So if we choose a 0.5 Ohm metal film resistor we get Pmax = Istall^2*R = 1A^2*0.5Ohm = 0.5W (Ok, less than 0.6W)

The Voltage over this resistor will be:
Nominal: U=I*R = 250mA*0.5 Ohm=125mV
Stall: 1000mA*0.5 Ohm = 500mV

I suggest connecting the 0.5 Ohm Resistor between the MOSFET (Source) and Ground. It will "take" a little of the control voltage (Vgs) of the MOSFET, but it should not be a problem since 5V-0.5V is still 4.5V, which should be okay. (A too big voltage signal over the resistor (say 3V) will reduce the Vgs so much that the MOSFET does not turn on properly, but we should manage fine).

Now, due to PWM and reverse diode, the current will only be there when the MOSFET is ON. So we have some options to measure the current:
1) Syncronize the measurement with the PWM output (you could wire the PWM output to an input and start A/D conversion after transition from low to high, i.e. when the MOSFET is turned on).
2) Put a capacitor in parallell with the resistor to filter the signal. Due to the low resistance we need a big capacitor, something like 3300 uF would give (1-e^-(0.5ms/(0.5 Ohm * 3300 uF)))=26% peak-to-peak noise worst case. (RC circuit natural response, 2 kHz=0.5ms discharge time).
3) Amplify the signal with an OP-AMP (voltage gain =10 to get full 5V out) and use an RC-filter on the output (possibly after a second buffering OP-AMP)

Option 1 might be tricky to program and gives a low signal but adds no extra circuitry
Option 2 gives a low and noisy signal with only an extra capacitor
Option 3 gives a strong signal with little noise, but requires extra circuitry

To increase the resolution of the A/D you can set VRH (High reference voltage) to about 1V (using a potential divider from 5V), which gives you 1mV steps (as opposed to 5mV with VRH=5V)

Hope this was helpful and not too confusing. If your program doesn't have to work very fast (i.e. if you can wait for 0.5 ms to measure the current), I would suggest going for option 1.

Message Edited by RChapman on 01-26-2006 03:38 PM