ideas for led fading algorithm

Discussion in 'Homework Help' started by takao21203, Apr 30, 2015.

  1. takao21203

    Thread Starter Distinguished Member

    Apr 28, 2012
    3,577
    463
    any?

    heres my recent c program, not yet obfuscated

    Code (Text):
    1.  
    2.  
    3. #if defined(__XC)
    4.     #include <xc.h>         /* XC8 General Include File */
    5. #elif defined(HI_TECH_C)
    6.     #include <htc.h>        /* HiTech General Include File */
    7. #endif
    8.  
    9. #include <stdint.h>        /* For uint8_t definition */
    10. #include <stdbool.h>       /* For true/false definition */
    11.  
    12. #include "system.h"        /* System funct/params, like osc/peripheral config */
    13. #include "user.h"          /* User funct/params, such as InitApp */
    14.  
    15. /******************************************************************************/
    16. /* User Global Variable Declaration                                           */
    17. /******************************************************************************/
    18.  
    19. /* i.e. uint8_t <variable_name>; */
    20. const unsigned char phase_patt[]={1,2,4};
    21.  
    22. const unsigned char animtbl1[]={0,0,1,2,4,8,16,32,64,128,0,0,128,64,32,16,8,4,2,1};
    23. unsigned char animsteps=sizeof(animtbl1);
    24.  
    25. const unsigned char allred[]={0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,\
    26.                               0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
    27.                               0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    28.  
    29. const unsigned char allgreen[]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
    30.                            0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,\
    31.                            0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
    32.  
    33. const unsigned char allblue[]={0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
    34.                                0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,\
    35.                                0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf};
    36.  
    37. unsigned char flags,porta_buf;
    38.  
    39. unsigned char rgbleds[32];
    40.  
    41. unsigned char phase,refresh_ctr,disp,i;
    42. unsigned char output_buf,shl_val;
    43. unsigned char color_ctr,blink_ctr;
    44.  
    45. unsigned char mask_outputbuf,animidx,moving_ctr;
    46.  
    47. void init_leds(unsigned char*s)
    48. {unsigned char i;
    49.  for(i=0;i<24;i++)
    50.  {
    51.   rgbleds[i]=*(s+i);
    52.  }
    53. }
    54.  
    55. void copyleds_phase()
    56. {unsigned char disp,i;
    57.     disp=phase<<3;
    58.  for(i=0;i<8;i++)
    59.  {
    60.   rgbleds[24+i]=rgbleds[disp+i];
    61.  }
    62.  
    63. }
    64. /******************************************************************************/
    65. /* Main Program                                                               */
    66. /******************************************************************************/
    67. void main(void)
    68. {
    69.     /* Configure the oscillator for the device */
    70.     ConfigureOscillator();
    71.  
    72.     /* Initialize I/O and Peripherals for application */
    73.     InitApp();
    74.  
    75.     PORTB=0b10101010;
    76.     PORTA=0b00000001;
    77.  
    78.  
    79.     color_ctr=0;
    80.     phase=0;
    81.     blink_ctr=0;
    82.     refresh_ctr=0;
    83.  
    84.     animidx=0;
    85.     moving_ctr=0;
    86.  
    87.     init_leds(&allred[0]);
    88.  
    89.     while(1)
    90.     {
    91.     if(flags&0x01)
    92.     {
    93.        flags&=0xfe;
    94.        refresh_ctr++;
    95.        if(refresh_ctr==16)
    96.        {
    97.         refresh_ctr=0;
    98.         copyleds_phase();
    99.         porta_buf=phase_patt[phase];
    100.  
    101.         moving_ctr++;
    102.         if(moving_ctr==0x10)
    103.         {
    104.            moving_ctr=0;
    105.            mask_outputbuf=animtbl1[animidx];
    106.            animidx++;if(animidx>animsteps)animidx=0;
    107.         }
    108.         blink_ctr++;if(blink_ctr==0xff)
    109.         {
    110.            blink_ctr=0;
    111.            color_ctr++;if(color_ctr==3)color_ctr=0;
    112.  
    113.            if(color_ctr==0)    init_leds(&allred[0]);
    114.            if(color_ctr==1)    init_leds(&allgreen[0]);
    115.            if(color_ctr==2)    init_leds(&allblue[0]);
    116.  
    117.            copyleds_phase();
    118.         }
    119.         phase++;if(phase==3)phase=0;
    120.  
    121.        }
    122.        
    123.         shl_val=1;
    124.         for(i=0;i<8;i++)
    125.         {
    126.         if(rgbleds[24+i]>0)
    127.         {
    128.             output_buf|=shl_val;
    129.             rgbleds[24+i]--;
    130.         }else output_buf&=0xff-shl_val;
    131.         shl_val<<=1;
    132.         }
    133.        
    134.     }
    135.  
    136.     PORTA=porta_buf;
    137.     PORTB=output_buf&mask_outputbuf;
    138.         /* TODO <INSERT USER APPLICATION CODE HERE> */
    139.     }
    140.  
    141. }
    142.  
    rgbm.jpg
     
  2. MrAl

    Well-Known Member

    Jun 17, 2014
    2,433
    490
    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.
     
  3. takao21203

    Thread Starter Distinguished Member

    Apr 28, 2012
    3,577
    463
  4. MrAl

    Well-Known Member

    Jun 17, 2014
    2,433
    490
    Hi,

    Yes, very nice :)
     
Loading...