- /******************************************************************************
- | RGB Light |
- ******************************************************************************/
- #define RED (2)
- #define GREEN (1)
- #define BLUE (16)
- #define CONFIG_RED 100
- #define CONFIG_GREEN 100
- #define CONFIG_BLUE 100
- volatile signed char red = 0;
- volatile signed char blue = 0;
- volatile signed char green = 0;
- volatile signed char counter = 0;
- void changecolour(signed char, signed char, signed char, unsigned long int);
- void main(void)
- {
- OPTION_REG = 0b11011000;
- INTCON = 0b10100000;
- TRISB = 0;
- PORTB = 0;
- while(1)
- {
- changecolour(100,0,0,1000);
- changecolour(0,100,0,1000);
- changecolour(0,0,100,1000);
- }
- }
- signed char calc_change(signed char input1, signed char input2)
- {
- signed char temp;
- if(input1 < input2)
- temp = -1;
- else
- temp = 1;
- temp *= 100 / input1;
- return temp;
- }
- void changecolour(signed char newred, signed char newgreen, signed char newblue, unsigned long int tottime)
- {
- signed char j;
- signed char tempred, stepred;
- signed char tempgreen, stepgreen;
- signed char tempblue, stepblue;
- unsigned long int i;
- tempred = newred - red;
- stepred = calc_change(tempred, red);
- tempred = stepred;
- tempgreen = newgreen - green;
- stepgreen = calc_change(tempgreen, green);
- tempgreen = stepgreen;
- tempblue = newblue - blue;
- stepblue = calc_change(tempblue, blue);
- tempblue = stepblue;
- for(j=1 ; j<=100 ; j++)
- {
- for(i=0 ; i < (tottime / 100); i++)
- Delay_us(1);
- if(tempred >= j)
- {
- if(newred > red)
- red++;
- else
- red--;
- tempred += stepred;
- }
- if(tempgreen >= j)
- {
- if(newgreen > green)
- green++;
- else
- green--;
- tempgreen += stepgreen;
- }
- if(tempblue >= j)
- {
- if(newblue > blue)
- blue++;
- else
- blue--;
- tempblue += stepblue;
- }
- }
- }
- void interrupt(void)
- {
- unsigned int temp;
- INTCON &= ~0b00000100;
- TMR0 = 0x20;
- if(++counter > 100)
- counter = 1;
- if(red > 100) red = 100;
- if(green > 100) green = 100;
- if(blue > 100) blue = 100;
- //temp = (red * CONFIG_RED) / 100;
- temp = red;
- if(temp >= counter)
- PORTB |= RED;
- else
- PORTB &= ~RED;
- //temp = (green * CONFIG_GREEN) / 100;
- temp = green;
- if(temp >= counter)
- PORTB |= GREEN;
- else
- PORTB &= ~GREEN;
- //temp = (blue * CONFIG_BLUE) / 100;
- temp = blue;
- if(temp >= counter)
- PORTB |= BLUE;
- else
- PORTB &= ~BLUE;
- }
RGB-light
By: Short | Date: Jan 28 2009 21:00 | Format: None | Expires: never | Size: 2.84 KB | Hits: 1298
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago