• AVR Freaks

Newbie: controlling duty cycle of PWM set up with MCC

Page: << < ..11121314 > Showing page 11 of 14
Author
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 14:01:30 (permalink)
0
***CODE
PStechPaul
I think the PS may be 4, to get your 25 kHz.

 
PStechPaul
The example in the reference you supplied is for a PIC32 device, which uses the system clock for PWM. PIC16 devices use the instruction clock which is fOsc/4.

 
You are much more used to/skilled in communicating via the register values directly. I'm looking at settings in MCC rather than register values (although you are rightly forcing me to look more at the latter). :)
 
I have prescaler set to 1:16 in MCC TMR2 "Easy Setup". I read your "PS", therefore, to be 16. I see the PR2 register value is 0x4 i.e. 4. Perhaps coincidentally here the two cancelled each other. 
 
PStechPaul 
[edit] I thought you might be using just CCPRxL and ignoring the CCPxCON LSB bits to make things easier. If you don't need fine precision for PWM, that will work quite well. But it seems you are setting the full CCPR (2*4+1), for 50%, although it really should be set to 10 for 50% duty cycle.


 
I'm just trying to use MCC to KISforSpeoplelikeme. :)  MCC generates a header file for each PWM (3 and 4). In it there is:
 
void PWM3_LoadDutyValue(uint16_t dutyValue);

 
and it also produces the related pwm3.c file which includes:
 


void PWM3_LoadDutyValue(uint16_t dutyValue)
{
// Writing to 8 MSBs of pwm duty cycle in CCPRL register
CCPR3L = ((dutyValue & 0x03FC)>>2);

// Writing to 2 LSBs of pwm duty cycle in CCPCON register
CCP3CON = ((uint8_t)(CCP3CON & 0xCF) | ((dutyValue & 0x0003)<<4));
}

 
Merely by looking at what happens to the value displayed for CCPR in CCPx "Easy Setup" when I change the duty cycle I understood that dutyValue could (only) be integers 0-19. I developed a simple algorithm to match what I saw in this Easy Setup page when I changed the duty cycle in 5% increments. Nothing smarter than that. Unfortunately MCC was reporting incorrect duty cycles. 
 
At the moment I'm confused as to
 
1. why the number of possible states - and hence integer range for dutyValue - isn't 21 rather than 20. See penultimate paragraph in post 198. 
 
2. given I am using the MCC-generated code for setting the PWM duty cycle (above) what the duty cycle is for each of the possible integer values for dutyValue. 
 
 
**** FANS 
 
 
PStechPaul
You may want to make sure you have a commutating diode across the fan to reduce inductive spikes at turn-off. Also make sure the fans are meant to have speed controlled by PWM. If they are BLDC they may have an internal speed regulator. Also, feeding them with unfiltered PWM may cause the circuitry to respond to the peak voltage. Maybe try a capacitor across the fans, although that will cause high current spikes when the PWM voltage is applied to the capacitor.[code]
 
I repeated the test I had done above to examine the current draw. I have two fans of the same model. Previously I had connected one fan directly and the other to the board to compare.  I repeated the test but with the same fan to avoid sample to sample variation. The board without fans draws 10mA. The fan attached directly draws 0.29A. When connected to the board and supposedly doing 45% duty cycle it draws a total of 0.27A (including the 10mA of the board). 
 
The fans are these from Mouser: https://www.mouser.com/ProductDetail/369-EEC0251B2000UA99 Datasheet here:
http://portal.sunon.com.tw/pls/portal/sunonap.sunon_html_d_pkg.open_file?input_file_name=7264646F632F3230313430312F3137363539372F28443132303136383930472D3030292D322E706466  I don't see anything to suggest they can't be PWM controlled. I don't measure significant capacitance probing their leads. Adding a diode didn't change the current draw. I will try to find a way to provide links to the scope shots of the scope shots. Sure if you zoom in with the scope there is some ringing but the waveform doesn't look bad overall.  
 
 
 
 



SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 14:03:13 (permalink)
0
Sorry I can't edit the post above for the error trying to finish the quote.  Access denied. 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 14:14:08 (permalink)
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 15:00:41 (permalink)
0
I also added the schematic to the album at that link
 
PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 18:20:41 (permalink)
+1 (1)
From the fan's datasheet:

7. DO NOT use power or ground PWM to control the fan speed. If the fan speed needs
to be adjusted, please contact SUNON to customize the product design for your
application.

 
Searching the Mouser website, I found only a few PWM capable fans, and some were blowers, some were 48V, and some were not stocked. Here is a good article on speed control for DC fans:
 
http://www.analog.com/en/analog-dialogue/articles/how-to-control-fan-speed.html
 
It may be better to use low frequency PWM, or even simple ON/OFF control. I was unaware of the problems with adjusting fan speed, but now I know.
post edited by PStechPaul - 2018/07/26 18:58:00

 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 19:25:39 (permalink)
0
Thanks.  After probing the circuit again and seeing nothing wrong with the driving circuit I went back to the data sheet, saw the note, threw my toys out of the pram and decided that was enough for one evening. What a waste of $26 and time (including yours). A glass of wine made things a little better. 
 
It seems there are a few 120mm x 120mm x 25mm fans which have PWM listed specifically in the description column. I will read the article you linked to and take a more careful look in the morning.
 
*** EDIT I took a quick read through the article just now and will read it again more thoroughly in the morning.  Clearly I am trying to do 'low speed PWM' without any feedback from a tachometer in the fan, but at a high freq above the audible range.  A friend did similar in an amp build. I'm not going to redo the board for their example HF direct PWM drive with feedback from the tachometer (using 4 wire fans).  I do though need to find a suitable fan. 
 
I will also try to get to the bottom of my questions in #201 regarding setting the duty cycle.  
post edited by SGK - 2018/07/26 19:47:42
PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 20:35:26 (permalink)
0
By low frequency PWM I mean something with a period of perhaps 500 mSec to a few seconds. You would need to see how long it takes for the span to spin up to speed, and set a duty cycle time of the same order of magnitude. Once the PWM frequency reaches the kHz range it will be similar to using a variable DC value, and most 12V fans need about 7-8 VDC to start. I think for something like an annealer it would be OK to use simple ON/OFF control based on high/low temperature setpoints. For wide range speed control I would suggest a stepper motor.
 
Here is a suitable fan:
https://www.ebay.com/itm/...To-12V-In/323327381294
post edited by PStechPaul - 2018/07/26 20:59:12

 
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/26 20:45:06 (permalink)
0
With regard to the comment in the fan datasheet, I would assume that the fan is regulating the speed itself, so it grabs extra current during the on period, so the average power consumption stays fairly constant.
That's why they tell you to not try to use PWM on the supply.
 
Regarding the duty cycle error in MCC. What the datasheet says is "the horse's mouth".
If there's a discrepancy in what MCC says, then the error is probably in MCC.
That's why experienced engineers prefer to work directly with the datasheet and the peripheral registers, then any mistakes are your own fault.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 07:45:37 (permalink)
0
On/off drive won't work for this application. I have coolant circulating through the inductor coil, radiator etc.  An on/off will have likely significant cooling/thermal lag.  I don't need to drive to a specific RPM i.e. have a closed loop regulation of fan speed.  I just need something that can scale with temp and hopefully with a decent range and 'somewhat' linear response.
 
There could be any number  of reasons why they don't recommend PWM control of this fan. I think (in line with the comment above) the issue with this fan's inability to respond to PWM drive is capacitance.  The capacitance is charged in the on-state period and used in the off-state period.  The surge/off current flow is averaged by the ammeter on my bench PSU.  I knew this was a risk but I tried to check for capacitance in the fan by measuring with my multimeter but I suspect this was an invalid test as I was only probing the fan leads. However, there's no excuse for not noticing the warning in the datasheet.
 
The induction board generates a lot of heat.  With an unknown thermal situation (just how fast will the inside of the enclosure heat up for varying enclosures) the plan was to over-specify the potential airflow (big screamer fans) and start them slow, with plenty of potential for them to ramp up airflow as temps rise. I'm not the first to use this approach in this application.
 
I can always lower the frequency.  However, low frequencies bring unwanted audible noise. So the idea was to start with a high frequency and see if that would work. 
 
Regarding the errors in MCC, sure I take the point onboard, but it is also much less frustrating to work with reliable software, especially as a newbie.  The entire point of MCC is to make programming PICs easier, to provide a reliable 'easy setup' interface above the registers.  In just this one, first project I have hit three bugs in MCC/MPLAB. 
 
1. MCC cannot properly reverse out selections made in the Interrupt Module (I had to restart the project from scratch)
2. MCC reports incorrect duty cycle/CCPR combinations
3. I had to place a dummy eeprom variable setting for the compiler to properly process the last valid one
Jim Nickerson
User 452
  • Total Posts : 6566
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 08:45:27 (permalink)
0
Is this a brushless fan ?
I would think on/off is the only control available.
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 09:04:35 (permalink)
0
Yes it is a DC brushless 4 pole motor design.
 
Clearly I've made a purchase mistake.  I will try to resell it to someone who doesn't need speed control.  It could have been avoided by a more complete reading of the datasheet.
 
Searching for a suitable fans isn't easy.  If there are criteria that would help I'd love to know them.  E.g. avoid brushless because of the way they work.
Jim Nickerson
User 452
  • Total Posts : 6566
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 09:06:01 (permalink)
0
SGK
Yes it is a DC brushless 4 pole motor design.
 
Clearly I've made a purchase mistake.  I will try to resell it to someone who doesn't need speed control.  It could have been avoided by a more complete reading of the datasheet.
 
Searching for a suitable fans isn't easy.  If there are criteria that would help I'd love to know them.  E.g. avoid brushless because of the way they work.


Some brushless fans have a pwm/speed input wire.
Jim Nickerson
User 452
  • Total Posts : 6566
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 09:11:37 (permalink)
+1 (1)
I have built PID controls that utilize On/Off control of the device when PWM was not possible.
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 09:34:17 (permalink)
0
Yes some fans, more commonly these days, can accept a PWM control signal directly.  They are typically more expensive although this is changing. 
 
My board has the PIC PWM driving a MOSFET to low-side switch the supply to the fan.  Unless I jumper over the MOSFET I need to find a conventional fan that is happy being driven by a PWM-driven supply. It needs to have no capacitance which would otherwise cancel the supply off (and be happy with the start/stop drive). 
Jim Nickerson
User 452
  • Total Posts : 6566
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 09:48:07 (permalink)
0
Surely this is a software change to stop using the PWM and use digital On/Off ?
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 10:08:56 (permalink)
0
Doesn't provide speed control versus temperature. Idea is to modulate the fan duty cycle according to temp. 2 NTC per fan.
 
The design choices are well described in the article PSTechPaul linked to above.  Can be done by controlling supply to the fan with PWM signal driving MOS gate at low or, possibly, high frequency. Driving the supply this way means its open-loop control (the fan can't properly provide feedback via a tachometer signal because its supply is being chopped) but that's ok by me.  But if the fan has significant capacitance it cancels the affect of modulating its supply via PWM.  
 
Using a fan which can accept a PWM signal directly - internally the fan controls supply to the motor elements but supply to the fan is maintained at all times - would require some board modifications/hacks or new boards. 
 
 
Jim Nickerson
User 452
  • Total Posts : 6566
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 10:22:51 (permalink)
+1 (1)
Varying the On , Off time relative to temperature will maintain the temperature.
This is not a new idea http://www.tekmarcontrols...-width-modulation.html
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 10:39:09 (permalink)
0
I'm not sure what we are arguing about.  I have a PWM signal from the PIC controlling the on/off of the fan via a MOS switch. Vary the duty cycle according to temperature and you have temperature control. You just need a fan that can handle having its supply turned on and off quickly and doesn't have, for example, significant capacitance which would negate the on/off supply control. (If it has significant capacitance it won't slow down when you lower the duty cycle because it has stored power on-board which it uses when the supply is off and refills via a surge when the supply is on.) Did you look at the schematic? see #203  
 
Were I do do this with simple temperature thresholds, the fans would either be fully on (blasting) or fully off.  It's kind of like ultra-low frequency PWM drive.  Acoustically this is as annoying as all get out. Furthermore the control system is slower in response/has greater lag.  Better to modulate fan duty cycle and ideally at a frequency above the audible range. 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 11:12:10 (permalink)
0
Hacking the board for a fan with PWM input isn't too bad.  It's just not 'pretty' because there is provision for just two-pin connectors and so the third wire has to be soldered directly to the board.  Still, a good fall-back position. See: 
https://www.icloud.com/sharedalbum/#B0Q532ODW4xxMZ
 
Now I will get back to reviewing the formulae for CCPR values and duty cycles.  
 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/27 13:48:27 (permalink)
0
PStechPaul
The formula for PWM period is [PR+1]*4*tOsc*PS. So for PR=19 and PS=16 and 8 MHz, that comes to:
 
20*4*125*16 = 160 uSec => 6.67 kHz. I think the PS may be 4, to get your 25 kHz.
 

 
Is there not a problem with this formula?  It just happens to work for 40uS but not for other periods.  I see the formula in the datasheet however I believe the 'PR' that is expected is not 19 (in the case of 40uS) but rather the value in the PR2 register (for Timer 2).  For 40uS / 25kHz that's 4. So:
 
(4+1)*4*.125*16 =  40uS
 
Try doing it for 48uS / 20.8kHz. 
 
I used qɥb's formula to derive PWM frequencies and periods for various PR2 register values (given PR2 must be an integer). I'm now looking to calculate the largest CCPR value for each scenario. (19 for 25kHz, 23 for 20.8kHz etc).
 



Page: << < ..11121314 > Showing page 11 of 14
Jump to:
© 2020 APG vNext Commercial Version 4.5