Menu

Hardware PWM frequency

Help
viscomjim
2015-03-25
2015-03-26
  • viscomjim

    viscomjim - 2015-03-25

    Here's a weird one, for me anyway. I am using a pic12f1840 at 32mhz using #chip 12f1840,32. I am implementing a hardware pwm using hpwm 1, 1, nn where nn is quickly changing (1-255 duty cycle) values. This is working just dandy changing the brightness of several different leds. As you can see, I am using a frequency of 1khz and this works well with MOST of the leds I am throwing at it. I am using a N-channel logic level mosfet to drive (sink) the 12v leds, which is working very well.

    One of the LEDs I have to use with this circuit is a 1 watt 12v unit. This one particular LED will not "perform" any where near as well as all the rest, and of course, this is the one I really need to be able to control well. I had a feeling that the frequency might be the culprit, so I tried the same setup using the same fet but driving the gate with another uController that allows the frequency to be changed from 20hz all the way up to 500khz. I found that the led responds extremely well from 0 to 100% duty cycle when using a frequency of about 200hz to 250hz.

    The slowest I can get the pic12f1840 to do a hardware pwm is about 1kz and this will not work for this application. For what I am doing, I cannot use the soft pwm as the pic is doing all sorts of things and just throws differing duty cycles at the hpwm. Is there any way on Gods green earth that the hardware pwm can have a lower frequency than 1khz? This whole project using GCB is working so well except for this one problem. I took a look at the pwm include file and as I am not that good at the coding thing, I could not figure out if there was a way to alter the frequency in this file.

    I will not have to change the frequency once I can get it down to about 200+hz, just the duty cycle, which changes quite often and randomly. This is why the simple hpwm 1, 1, nn is sooooo good in this situation. The ultimate would be hpwm 1, .2, nn, or something of this nature.

    Thanks again for any and all input or info!!!!!

     
  • William Roth

    William Roth - 2015-03-25

    I do not think it is possible to get the HPWM frequency to 200Hz with a PIC 12F1840 operating at 32Mhz. You would need to reduce the ChipMhz to 8 Mhz and then in PWM.h you would need to mess about with the Timer 2 Prescaler (1:64) and the maths.

    The PIC12F1840 is the same PIC used for Picaxe 08M2. After much clamoring about by users the Picaxe folks updated the firmware so that lower PWM frequencies could be used. This was done by allowing the users to select a timer 2 prescaler as low as 1:64.

    To do this in CGB would require a rewrite of pwm.h

     

    Last edit: William Roth 2015-03-25
  • William Roth

    William Roth - 2015-03-25

    @viscomjim

    Can you operate at 16Mhz instead of 32? Would 250 Hz be slow enough?

    If the answer to both is yes, then I can provide a solution using GCB HPWM. This requires modification of your pwm.h file. I am reluctant to post here ,so contact me via private message and provide me an Email Address, and I will send you a special PWM.H file that will do Hardware PWM at 250 Hz on the 12F1840.

    William

     

    Last edit: William Roth 2015-03-25
  • viscomjim

    viscomjim - 2015-03-25

    Hi William, This is great news. I sent you my email address. Please let me know if you received it as all I did was click on your name to send message. Not sure if this is the way to do that or not.

     
    • William Roth

      William Roth - 2015-03-25

      File Was sent

       
  • kent_twt4

    kent_twt4 - 2015-03-25

    @viscomjim said:

    One of the LEDs I have to use with this circuit is a 1 watt 12v unit. This one particular LED will not "perform" any where near as well as all the rest, and of course, this is the one I really need to be able to control well. I had a feeling that the frequency might be the culprit, so I tried the same setup using the same fet but driving the gate with another uController that allows the frequency to be changed from 20hz all the way up to 500khz. I found that the led responds extremely well from 0 to 100% duty cycle when using a frequency of about 200hz to 250hz.

    Done the direct drive nfet to led also, and have had to lower the freq. to around 1Khz for a Cree XM-L2 LED to avoid the blinkies. Is the problem from a noname Chinese multidie LED? Just from a curiosity standpoint on my part, link? Thanks.

     
  • viscomjim

    viscomjim - 2015-03-25

    Hi Kent, I am assuming it is a cheapy multi die unit. I have tested quite a few led types and this "type" just doesn't like anything faster than about 200 to 250hz. All the ones I have tested work great at this lower freq. also, so this is the reason to get to this freq. with hardware pwm. Even at 1khz most will work o.k. but some not. but all work great at the lower (200-250) frequency. Once I try the code from William, I will report back (hopefully with a big smile on my face).

     
  • viscomjim

    viscomjim - 2015-03-26

    Well, it looks like I'm going to need plastic surgery to remove the smile from my face. Williams modified pwm.h file worked perfectly. Now I have hardware pwm with a frequency of 250hz which my el cheapo leds seem to love. I am running the pic12f1840 at 16mhz and the rest of the program works well at this speed also.

    This is such a weird nichy thing that only one of my led units would need this to be able to respond well to 0 to 100% duty cycle, but this will cover all the bases for this project in case anyone else uses these type of leds.

    My hats off to William Roth, GCB and the great people of this forum that are so willing to help!!! Many, many thanks!!!!

     

    Last edit: viscomjim 2015-03-26

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.