In: Electrical Engineering
#define _XTAL_FREQ 8000000
void main(void) {
int i=0;
int v;
int r,g,b; // to store the PWM ratio of each color
unsigned char c=0x00; // to store the pin output value
TRISC=0b00000000;
PORTC=0b00000000;
ANSEL = 0x00;
ANSELH = 0x00;
r=5; // Red ligt will remains fixed to intensity 5/255
for (i=0;i<=255;i++) { // a loop to vary the leds
intensity
g=i; // Green led will light from 0 to 255
b=255-i; // Blue led will light from 255 to 0
for (v=0;v<=255;v++) { // loop to simulate PWM over 256
values
if (v<r) { // if current v valuer is < to the red led
intensity
c|=0b00000001; // we light the led
} else {
c&=0b11111110; // else we shut it.
}
if (v<g) { // same for green
c|=0b00000010;
} else {
c&=0b11111101;
}
if (v<b) { // same for blue
c|=0b00000100;
} else {
c&=0b11111011;
}
PORTC=c; // and we finaly push the led status to PORTC (R led on
C0, G led on C1 and blue on C2)
__delay_us(1); // make a short break.
}
}
return;
}