Fading LEDs via PWM

Program Download:

Main C File
LED_Fading.c
LED_Fading.hex

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 program has two parts and two purposes. The first part is the main function

Setting Brightness Intensity

------------« Begin Code »------------
     ..
	...
	while(1){
		// Gradually Become Brighter
		for(i=0;i<21;i++){
			led_intensity = clr_array[i];
			Delay10KTCYx(20);
		}
		// Gradually Become Darker
		for(i=20;i>0;i--){
			led_intensity = clr_array[i];
			Delay10KTCYx(20);
		}
	...
	..
------------« End Code »------------

           The interrupt portion of the firmware is triggered every time that the counter hits the 0xFFFF value. When the interrupt occurs, the ISR (interrupt sub-routine) increments an internal count value and checks to see what state the PWM output should be in, to achieve the proper duty cycle, which correlates to the LED brightness level.

Digital Timer Interrupt Controls PWM Duty Cycle

------------« Begin Code »------------
..
...
void InterruptHandlerHigh()
{							
    if(INTCONbits.TMR0IF)
    {
		count++;
				
		//Every 100 Timer0 Interrupts Reset Counter
		if(count >= 100)
			count = 0;

		//Set Brightness According To Count Value
			//This Will Be The Duty Cycle
		if(count <= led_intensity && led_intensity > 0)
			PORTAbits.RA0 = 1;
		else
			PORTAbits.RA0 = 0;

		WriteTimer0( 0xFF00 );
		INTCONbits.TMR0IF = 0;	//Clear TMR0 Flag                 
    }

    INTCONbits.GIE = 1;	//Re-enable all interrupts
}
...
..
------------« End Code »------------

           Each axis of the touch screen requires a similar process for testing to see if it is currently being touched and where. First the Power and Ground lines need to be turned on for the appropriate axis, all other connections should be high-impedance so as not to interfere. Then the touch screen output line should be read via the A/D converter. Do this for both X and Y axis and you have a 10-bit value signifying where the screen was touched.



;