/********************************************/
/*LED Data Port Configuration Code          */
/*PIC32MX795F512L-80I/PF                    */
/********************************************/

#include "xc.h"

void LED_Port_Init(void)
{    
    AD1PCFGbits.PCFG7 = 1;
    AD1PCFGbits.PCFG6 = 1;
    
    TRISBbits.TRISB7 = 0;
    TRISBbits.TRISB6 = 0;
    TRISDbits.TRISD1 = 0;
    TRISDbits.TRISD2 = 0;
    
    TRISCbits.TRISC1 = 0;
    TRISCbits.TRISC2 = 0;
    TRISCbits.TRISC3 = 0;
    TRISCbits.TRISC4 = 0;
    
    //LED_0
    PORTBbits.RB7 = 0;
    //LED_1
    PORTBbits.RB6 = 0;
    //LED_2
    PORTDbits.RD1 = 0;
    //LED_3
    PORTDbits.RD2 = 0;
    
    //LED_4
    PORTCbits.RC1 = 0;
    //LED_5
    PORTCbits.RC2 = 0;
    //LED_6
    PORTCbits.RC3 = 0;
    //LED_7
    PORTCbits.RC4 = 0;
    
    //LED self test
    //Light all LEDs and hold
    PORTBbits.RB7 = 1;
    PORTBbits.RB6 = 1;
    PORTDbits.RD1 = 1;
    PORTDbits.RD2 = 1;
    PORTCbits.RC1 = 1;
    PORTCbits.RC2 = 1;
    PORTCbits.RC3 = 1;
    PORTCbits.RC4 = 1;
    
    int count;
    for(count = 0; count < 1000000; count++)
    {
        //wait ...
    }
        
    //Turn off all LEDs
    PORTBbits.RB7 = 0;
    PORTBbits.RB6 = 0;
    PORTDbits.RD1 = 0;
    PORTDbits.RD2 = 0;
    PORTCbits.RC1 = 0;
    PORTCbits.RC2 = 0;
    PORTCbits.RC3 = 0;
    PORTCbits.RC4 = 0;
    
    
    return;
}

void LED_Port(uint8_t led_port_data)
{
    uint8_t temp;
    
    //clear the bits
    PORTCCLR = 0x1e;
    PORTDCLR = 0x06;
    PORTBCLR = 0xC0;
    
    //PORTC  1-4
    temp = led_port_data;
    temp = temp >> 3;
    PORTCSET = temp;
    
    //PORTD 1-2
    temp = led_port_data & 0x0c;
    temp = temp >> 1;
    PORTDSET = temp;
    
    //PORTB 7
    temp = led_port_data << 7;
    PORTBSET = temp;
    
    //PORTB 6
    temp = led_port_data & 0x02;
    temp = temp << 5;
    PORTBSET = temp;
    
    return;
}