ideas for led fading algorithm

Thread Starter

takao21203

Joined Apr 28, 2012
3,702
any?

heres my recent c program, not yet obfuscated

Code:
#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */

/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/

/* i.e. uint8_t <variable_name>; */
const unsigned char phase_patt[]={1,2,4};

const unsigned char animtbl1[]={0,0,1,2,4,8,16,32,64,128,0,0,128,64,32,16,8,4,2,1};
unsigned char animsteps=sizeof(animtbl1);

const unsigned char allred[]={0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,\
                              0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
                              0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};

const unsigned char allgreen[]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
                           0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,\
                           0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};

const unsigned char allblue[]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
                               0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
                               0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf};

unsigned char flags,porta_buf;

unsigned char rgbleds[32];

unsigned char phase,refresh_ctr,disp,i;
unsigned char output_buf,shl_val;
unsigned char color_ctr,blink_ctr;

unsigned char mask_outputbuf,animidx,moving_ctr;

void init_leds(unsigned char*s)
{unsigned char i;
 for(i=0;i<24;i++)
 {
  rgbleds[i]=*(s+i);
 }
}

void copyleds_phase()
{unsigned char disp,i;
    disp=phase<<3;
 for(i=0;i<8;i++)
 {
  rgbleds[24+i]=rgbleds[disp+i];
 }

}
/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();

    PORTB=0b10101010;
    PORTA=0b00000001;


    color_ctr=0;
    phase=0;
    blink_ctr=0;
    refresh_ctr=0;

    animidx=0;
    moving_ctr=0;

    init_leds(&allred[0]);

    while(1)
    {
    if(flags&0x01)
    {
       flags&=0xfe;
       refresh_ctr++;
       if(refresh_ctr==16)
       {
        refresh_ctr=0;
        copyleds_phase();
        porta_buf=phase_patt[phase];

        moving_ctr++;
        if(moving_ctr==0x10)
        {
           moving_ctr=0;
           mask_outputbuf=animtbl1[animidx];
           animidx++;if(animidx>animsteps)animidx=0;
        }
        blink_ctr++;if(blink_ctr==0xff)
        {
           blink_ctr=0;
           color_ctr++;if(color_ctr==3)color_ctr=0;

           if(color_ctr==0)    init_leds(&allred[0]);
           if(color_ctr==1)    init_leds(&allgreen[0]);
           if(color_ctr==2)    init_leds(&allblue[0]);

           copyleds_phase();
        }
        phase++;if(phase==3)phase=0;

       }
       
        shl_val=1;
        for(i=0;i<8;i++)
        {
        if(rgbleds[24+i]>0)
        {
            output_buf|=shl_val;
            rgbleds[24+i]--;
        }else output_buf&=0xff-shl_val;
        shl_val<<=1;
        }
       
    }

    PORTA=porta_buf;
    PORTB=output_buf&mask_outputbuf;
        /* TODO <INSERT USER APPLICATION CODE HERE> */
    }

}
rgbm.jpg
 

MrAl

Joined Jun 17, 2014
13,702
Hello,

When i did a bi color LED a long time ago, i used a PWM where the pulse width would vary slowly over time. It looked like a candle flame because the red would turn on and get brighter while the green would get dimmer, and vice versa later in time.
 
Top