RGB LED Controller

Program Download:

Main C File
RGB_LED.c

The Software
           The code is too long to include everything on this page, so I'll just cover the two important specifics:
    - Main Loop Brightness Control
    - Interrupt Level Counting


           The main loop of this program controls the brightness level of each color of the Red-Green-Blue LED. To fade the LED colors in and out, a for loop is used along with a short pause. Changing the value of the pause changes speed of the color fading.

Main Function Excerpt

------------« Begin Code »------------
     ..
    ...
	// Green Up
	for(i=0;i<21;i++){
		red_color = 20;
		green_color = i;
		blue_color = 0;
		Delay10KTCYx(20);
	}
	// Red Down
	for(i=20;i>0;i--){
		red_color = i;
		green_color = 20;
		blue_color = 0;
		Delay10KTCYx(20);
	}
	// Blue Up
	for(i=0;i<21;i++){
		red_color = 0;
		green_color = 20;
		blue_color = i;
		Delay10KTCYx(20);
	}
    ...
    ..
------------« End Code »------------

           A digital timer (same as in Fading LEDs via PWM) is used to trigger interrupt very frequently and each interrupt is counted using an integer called count. This integer is then used to keep track of what duty cycle % each color should be outputting. If the count value is within set parameters for a specific brightness level commanded by the main function, then the corresponding pin on PORTD is set high '1', otherwise it is kept low '0'.

Digital Timer Interrupt Controls PWM Duty Cycle

------------« Begin Code »------------
..
...
void InterruptHandlerHigh()
{							
       if(INTCONbits.TMR0IF)
       {
			count++;
			if(count >= 100)
				count = 0;
				
			//Update Red
			if(count <= red_color && red_color > 0)
				PORTDbits.RD0 = 1;
			else
				PORTDbits.RD0 = 0;
				
			//Update Blue
			if(count <= green_color  && green_color > 0)
				PORTDbits.RD1 = 1;
			else
				PORTDbits.RD1 = 0;
				
			//Update Green
			if(count <= blue_color && blue_color > 0)
				PORTDbits.RD2 = 1;
			else
				PORTDbits.RD2 = 0;
				
				WriteTimer0( 0xFF80 );
			    INTCONbits.TMR0IF = 0;	//Clear TMR0 Flag                 
       }
    INTCONbits.GIE = 1;						//Re-enable all interrupts
}
...
..
------------« End Code »------------

           As you can see in the excerpt of code above, each color Red, Green and Blue is evaluated through simple if/else statements to see if their corresponding output should be turned on or off. This combination of interrupt and main function control works seemlessly as you'll see in the next section when we test out this project.



;