Soft PWM for RGB LEDS

General discussion on mikroC.
Post Reply
Author
Message
Mayer_y
Posts: 12
Joined: 29 Sep 2009 13:58
Location: Israel

Soft PWM for RGB LEDS

#1 Post by Mayer_y » 05 Dec 2009 21:17

Hello

I want do fade to RGB LEDS with my MCU - PIC12F683

My problem is that :
In the end of the loop - (Duty cycle 0% --> 100%)
the line ends with '1' but jump to low state '0' for a 1 mSec

and from end of the second loop - (Duty cycle 100% --> 0%)
the line ends with '0' but jump to high state '1' for a 1 mSec

All the way every thing is OK but in the end of the loop I got
somthing I don't want

My code :

Code: Select all


int i, PWN_COUNTER = 600;

void main(void)
{
 
  ANSEL  = 0x00;          // Digital I/O
  CMCON = 0x07;         // Comparator is Off    digital IO
  OPTION_REG.F7 = 0;  // pull-ups are enabled
  WPU = 0xFF;              // Pull-up enabled

  TRISIO = 0x00;   // output

  while(1)
  {
     // PWM_FADE_0_TO_1();
     
    for (i = 0; i < PWN_COUNTER; i++)   
     {
           GPIO      = 0xff;
           Delay(i);
           GPIO      = 0x00;
           Delay(PWN_COUNTER - i);
     }        
      // in the end of loop the port should be '1' - but the line jump to '0' for a 1 msec 
    
    Delay_ms(1000); 
   
     // PWM_FADE_1_TO_0(); 
     
     for (i = 0; i < PWN_COUNTER; i++)    
     {
            GPIO      = 0xff;
            Delay(PWN_COUNTER - i);
            GPIO      = 0x00;
            Delay(i);
     } 
     // in the end of loop the port should be '0' - but the line jump to '1' for a 1 msec 
  }
}  // End of main



void PWM_FADE_0_TO_1(void)        // Duty cycle growing  100%
{
     for (i = 0; i < PWN_COUNTER; i++)
     {
           GPIO      = 0xff;
           Delay(i);
           GPIO      = 0x00;
           Delay(PWN_COUNTER - i);
     }
}

void PWM_FADE_1_TO_0(void)         // Duty cycle grow smaller  0%
{
     for (i = 0; i < PWN_COUNTER; i++)
     {
            GPIO      = 0xff;
            Delay(PWN_COUNTER - i);
            GPIO      = 0x00;
            Delay(i);
     }

}
What can be the problem with line
why the line jump to '0' \ '1' in the end of the loop ??

Thanks

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#2 Post by Sobrietytest » 06 Dec 2009 05:01

why the line jump to '0' \ '1' in the end of the loop ??
On the first loop (0 -> 1) it should finish on 0 and stay there for the duration of Delay_ms(1000). That is what your code tells it to do (think about what happens when i = 599).

On the second loop there is no delay at the end so the jump to 1 is probably the start of the the next iteration of loop 1.

Try this...

Code: Select all

while(1)
  {
     // PWM_FADE_0_TO_1();

    for (i = 0; i < PWN_COUNTER; i++)    
     {
           GPIO      = 0x00;
           Delay(i);
           GPIO      = 0xFF;              //SWITCH THE LOGIC SO THE LOOP ENDS ON 1...
                                          //...PREVIOUSLY THE LOOP WAS ENDING ON 0
           Delay(PWN_COUNTER - i);        
     }
     

    Delay_ms(1000);

     // PWM_FADE_1_TO_0();

     for (i = 0; i < PWN_COUNTER; i++)   
     {
            GPIO      = 0xFF;
            Delay(PWN_COUNTER - i);
            GPIO      = 0x00;
            Delay(i);
     }
   // Delay_ms(1000); YOU COULD ADD THIS DELAY TO CONFIRM THE LOOP OPERATION 
  }

I haven't seen those delay functions before, I guess you're not using MikroC?

Mayer_y
Posts: 12
Joined: 29 Sep 2009 13:58
Location: Israel

#3 Post by Mayer_y » 06 Dec 2009 08:26

Thanks for your replay
I haven't seen those delay functions before, I guess you're not using MikroC?
I forgot to put the function

Code: Select all

void Delay(int num)
{
     while(num > 0)
       num--;
}
I used this function because I need to change the argument of the the delay function and the MikroC delay fucntion arguments are just constant so build that function - I didn't find MikroC delay function that can get variable to his function - send me if find one

I will check the code on the hardware and come back with results

Thanks

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#4 Post by Sobrietytest » 07 Dec 2009 05:02

I didn't find MikroC delay function that can get variable to his function - send me if find one
I see, you're quite right. There are the VDelay functions but I think they only work with PIC16 upwards.

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

#5 Post by tihomir.losic » 08 Dec 2009 11:37

Hi,
There are the VDelay functions but I think they only work with PIC16 upwards.
I've tested your code and it worked correctly. Also, you can use VDelay_ms() function in your code for PIC12 family in mikroC.

Regards,

Losic Tihomir
mikroElektronika [Support team]

Mayer_y
Posts: 12
Joined: 29 Sep 2009 13:58
Location: Israel

#6 Post by Mayer_y » 14 Dec 2009 23:21

Hello

I tested my code on PIC18F452 and it work perfectly
I got fade in and fade out without any jumping in the end of the fade

I test the code on PIC12F629 or PIC12F683 and I got a jump in the end of the loop :
PWM_FADE_0_TO_1(); ends with '1' but jump to 0 for 1mSec
PWM_FADE_1_TO_0(); ends with '0' but jump to 1 for 1mSec

My code for PIC12F683 :

Code: Select all

#define RED           GPIO.F0  // Output
#define GREEN         GPIO.F1
#define BLUE          GPIO.F4
#define RING_BLOCK    GPIO.F5

int i = 0 ,PWN_COUNTER = 700;

void PWM_FADE_0_TO_1(void);
void PWM_FADE_1_TO_0(void);
void Delay(int num);

void main(void)
{
  ANSEL   = 0x00;      //  Digital IO
  CMCON0  = 0x07;      //  Comparator is Off
  TRISIO  = 0x00;
  GPIO    = 0x00;

  while(1)
  {
    PWM_FADE_0_TO_1();
    PWM_FADE_1_TO_0();
   }
}

void PWM_FADE_0_TO_1(void)        // Duty cycle -  0%  to  100%
{
     for (i = 0; i < PWN_COUNTER; i++)
     {
         GREEN = 0;
         Delay(PWN_COUNTER - i);
         GREEN = 1;
         Delay(i);
     }
}

void PWM_FADE_1_TO_0(void)         // Duty cycle -  100% to 0%
{
     for (i = 0; i < PWN_COUNTER; i++)
     {
         GREEN = 1;
         Delay(PWN_COUNTER - i);
         GREEN = 0;
         Delay(i);
     }
}

void Delay(int num)
{
     while(--num);
}

How it can be that on PIC18F452 the code working perfectly
and on PIC12F683 I got jumping in the end of the fade of the LEDS

Can anyone explain that problem ?

What is the diffrence between PIC18F452 to PIC12F683 ???


Thanks

Mayer

Hearty
Posts: 11
Joined: 02 Mar 2010 14:10

Re: Soft PWM for RGB LEDS

#7 Post by Hearty » 22 Jul 2010 20:46

Hi,

I tried this code with PIC16F876A

with:

Code: Select all

void Delay(int num)
{
     while(--num);
}
I have a "flash light" at the end of both cycles

and with:

Code: Select all

void Delay(int num)
{
     while(num>0)
            num--;
}
all is right.

Post Reply

Return to “mikroC General”