Public paste
RGB-light
By: Short | Date: Jan 28 2009 21:00 | Format: None | Expires: never | Size: 2.84 KB | Hits: 1297

  1. /******************************************************************************
  2. |                           RGB Light                                          |
  3. ******************************************************************************/
  4. #define RED        (2)
  5. #define GREEN      (1)
  6. #define BLUE       (16)
  7.  
  8. #define CONFIG_RED     100
  9. #define CONFIG_GREEN   100
  10. #define CONFIG_BLUE    100
  11.  
  12. volatile signed char red = 0;
  13. volatile signed char blue = 0;
  14. volatile signed char green = 0;
  15. volatile signed char counter = 0;
  16.  
  17. void changecolour(signed char, signed char, signed char, unsigned long int);
  18.  
  19.  
  20. void main(void)
  21. {
  22.    OPTION_REG = 0b11011000;
  23.    INTCON = 0b10100000;
  24.    TRISB = 0;
  25.    PORTB = 0;
  26.    
  27.    while(1)
  28.    {
  29.        changecolour(100,0,0,1000);
  30.        changecolour(0,100,0,1000);
  31.        changecolour(0,0,100,1000);
  32.    }
  33. }
  34.  
  35. signed char calc_change(signed char input1, signed char input2)
  36. {
  37.    signed char temp;
  38.    if(input1 < input2)
  39.       temp = -1;
  40.    else
  41.       temp = 1;
  42.  
  43.    temp *= 100 / input1;
  44.    return temp;
  45. }
  46.  
  47. void changecolour(signed char newred, signed char newgreen, signed char newblue, unsigned long int tottime)
  48. {
  49.    signed char j;
  50.    signed char tempred, stepred;
  51.    signed char tempgreen, stepgreen;
  52.    signed char tempblue, stepblue;
  53.    unsigned long int i;
  54.  
  55.    tempred = newred - red;
  56.    stepred = calc_change(tempred, red);
  57.    tempred = stepred;
  58.    
  59.    tempgreen = newgreen - green;
  60.    stepgreen = calc_change(tempgreen, green);
  61.    tempgreen = stepgreen;
  62.    
  63.    tempblue = newblue - blue;
  64.    stepblue = calc_change(tempblue, blue);
  65.    tempblue = stepblue;
  66.    
  67.    for(j=1 ; j<=100 ; j++)
  68.    {
  69.       for(i=0 ; i < (tottime / 100); i++)
  70.          Delay_us(1);
  71.          
  72.       if(tempred >= j)
  73.       {
  74.          if(newred > red)
  75.             red++;
  76.          else
  77.             red--;
  78.          tempred += stepred;
  79.       }
  80.       if(tempgreen >= j)
  81.       {
  82.          if(newgreen > green)
  83.             green++;
  84.          else
  85.             green--;
  86.          tempgreen += stepgreen;
  87.       }
  88.       if(tempblue >= j)
  89.       {
  90.          if(newblue > blue)
  91.             blue++;
  92.          else
  93.             blue--;
  94.          tempblue += stepblue;
  95.       }
  96.    }
  97. }
  98.  
  99. void interrupt(void)
  100. {
  101.    unsigned int temp;
  102.    INTCON &= ~0b00000100;
  103.    TMR0 = 0x20;
  104.    if(++counter > 100)
  105.       counter = 1;
  106.  
  107.    if(red > 100)  red = 100;
  108.    if(green > 100) green = 100;
  109.    if(blue > 100) blue = 100;
  110.  
  111.    //temp = (red * CONFIG_RED) / 100;
  112.    temp = red;
  113.    if(temp >= counter)
  114.       PORTB |= RED;
  115.    else
  116.       PORTB &= ~RED;
  117.  
  118.    //temp = (green * CONFIG_GREEN) / 100;
  119.    temp = green;
  120.    if(temp >= counter)
  121.       PORTB |= GREEN;
  122.    else
  123.       PORTB &= ~GREEN;
  124.  
  125.    //temp = (blue * CONFIG_BLUE) / 100;
  126.    temp = blue;
  127.    if(temp >= counter)
  128.       PORTB |= BLUE;
  129.    else
  130.       PORTB &= ~BLUE;
  131. }