LED Heart PWM Fading

MPLABX Project:

Archived Download:
led_bday_heart.X.zip

The Software
           There's really too much code to break out into one single page so I'll go through 2 exercepts of the code for this project:
    -PWM Control via Timer Interrupt
    -Main Loop LED Control


           The timer interrupt has an if statement for each individual LED that sets a bit in a buffer for each of the 3 ports. After all 16 leds have been evaluated each port is updated with the buffer value. This happens many times a second. Depending upong the current count value and the specific led_array value set in the main loop, an LED will be turned on or off for that count value.

PWM Control via Timer Interrupt

------------« Begin Code »------------
..
...						
void InterruptHandlerHigh()
{
    unsigned int porta_buffer = 0;
    unsigned int portb_buffer = 0;
    unsigned int portc_buffer = 0;

  if(INTCONbits.TMR0IF)
  {
        count++;
        if(count >= 100)
            count = 0;
//Modify LED 1 Status
        if(count <= led_array[0] && led_array[0] > 0)
            porta_buffer |= 0x01; 
        else
            porta_buffer &= 0xFE; 
//Modify LED 2 Status
        if(count <= led_array[1] && led_array[1] > 0)
            portc_buffer |= 0x04; 
        else
            portc_buffer &= 0xFB; 
...
...
...
//Modify LED 16 Status
        if(count <= led_array[15] && led_array[15] > 0)
            portb_buffer |= 0x08; //PORTBbits.RB4 = 1;
        else
            portb_buffer &= 0xF7; //PORTBbits.RB4 = 0;

        PORTA = porta_buffer;
        PORTB = portb_buffer;
        PORTC = portc_buffer;

        WriteTimer0(0xFF00);

        INTCONbits.TMR0IF = 0;
  }
INTCONbits.GIE = 1;
}
...
..
------------« End Code »------------

           For an example of how the main loop tells the LEDs to turn on and off we look at sequence of mode 1. This mode turns each LED on once. This is done in a for loop and the led_array values are always set to 50 which is a very bright LED setting. If you wanted the LEDs to be less bright, you would set the specific LED's array value to something smaller like 5 or 10.

Mode 1 Control Example

------------« Begin Code »------------
..
...
/****MODE 1------MODE 1-------MODE 1--------MODE 1*****/

/*
 * 'All Around The Heart' LED Display
 *
 * In this part, the LEDs will light up one at a time
 * LED1 to LED16, thus creating an all around the world
 * feeling since all leds get lit up once during a cycle.
 *
 *  */

//ALL LEDs OFF
for(i=0;i<16;i++){
     led_array[i]=0;
}

Delay10KTCYx(100);

//Initialize First Value
led_array[0]=50;
Delay10KTCYx(50);
//Let For Loop Do The Rest
for(i=1;i<16;i++){
        //Set New LED On
     led_array[i]=50;
        //Set Old LED Off
     led_array[i-1]=0;
     Delay10KTCYx(30);
}
led_array[15]=0;

//Initialize First Value
led_array[0]=50;
Delay10KTCYx(50);
//Let For Loop Do The Rest
for(i=1;i<16;i++){
        //Set New LED On
     led_array[i]=50;
        //Set Old LED Off
     led_array[i-1]=0;
     Delay10KTCYx(30);
}
led_array[15]=0;

/******************************************************/
------------« End Code »------------

           There's a whole host of extra code in the MPLABX project above that you can look at to see how this project works. I barely scratched the surface here, but if you look through the project you can read it all to your own delight. The compiled HEX file is also available in the archived download above.