RGB Led fading with 0-5V

Discussion in 'Programmer's Corner' started by R!f@@, Mar 4, 2016.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I just started the next one I always wanted to do.
    An RGB LED color changing using a VR.

    PIC is 12F675

    Code is already running. I have implemented soft PWM using the method John taught me during the computer table light.

    So fay I can control the brightness for all or one color. Mixing is next. My approach is simple. But before that I like to figure out something.

    Below is the part I like to know

    Code (C):
    1. unsigned int Freq_temp;
    2. unsigned char GPIOimg, Freq;
    3.  
    4. Freq_temp = ADC_Read(0);
    5. Freq = Freq_temp/10;
    6. PWMduty = Freq;
    I am reading the ADC and using it to update the Duty.
    I get 64Hz flicker free brightness control.

    Q is in here "Freq = Freq_temp/10"
    You see the divider "10". If I go to ">100" brightness does not go to max and if low (<4) I get jumping in brightness around some ADC positions . I scoped the output and "10" seems a fine value as I can go from 0 PWM to MAX. How I got it doing trial and programing and checking......:D

    I figures since "Freq" is like 0-255 and the ADC is more. I tried to scale it down thinking that an "int" is like 655535. So I tried divide by 257 to scale it down and brightness was way low.

    I am thinking it should be 1023/255 = ~4 . Cause 4 is also working ( I just tried it ).
    But wait 4 is also not as good as 10 at high brightness. "10" seems better ( gives smooth brightness change when pot is changed all the way .

    So what value is "Freq" ?
    Better yet if ADC is 5V max, what is ADC value, is it 1023 or something else ?

    {ed}
    "20" seems better at low brightness than "10"

    Like to figure out the max value in which "Freq" is at max ADC
     
    Last edited: Mar 4, 2016
  2. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    8 bit ADC = 2^8 = 256 discrete levels (0-255)
    10 bit ADC = 2^10 = 1024 discrete levels (0-1023)
    12 bit ADC = 2^12....
    14 bit ADC = 2^14...
    etc..

    So if you have a 10 bit ADC your readings will be from 0-1023
     
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    so to scale down to 255, I should use 4 or 5 ?

    But I get far better brightness variation if I divide by 30
     
  4. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    So if use 31...

    Code (C):
    1. Freq_temp = ADC_Read(0);
    2. Freq = Freq_temp/31;
    3. PWMduty = Freq;
    Does the PWM Duty varies from 0 - 33 ?
     
  5. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    Assuming a 10 bit ADC then yes..
    1023/31 = 33
     
  6. Picbuster

    Member

    Dec 2, 2013
    373
    50
    Do not try to divide an integer and expect a real as answer.
     
  7. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    So how should I divide the int to vary the duty ?
    It is working so far....!
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Your PIC has ab A2D capable of either 8 or 10 bits of resolution. You do not state the resolution of your soft PWM.

    Unless you disclose your secret numbers no one can answer your query.
     
  9. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    umm .. That is kinda problem. I trying to figure out my PWM resolution. I not very good at soft PWM yet.
    How do I figure out if I am using 8 or 10 bit.
     
  10. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,801
    831
    What's the maximum value your ADC returns?

    If it's 255, it is an 8 bit ADC.
    If it's 1023, it is a 10 bit ADC.
     
  11. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    crap! 12F675 has no ICD. Tried to step thru the code to check that and MikroC said go fish. :(

    I believe I am using 10bit... So how can I found that out with this little bugger ?
     
  12. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I cannot see where in the data it says 8 bit ADC for 12F675. It just says 10 bit ADC.

    And by the way ADCON0 is set as Right Justified.
     
Loading...