////////////////////////////////////////////////////////////////////////////////  
//___________|PIC32MX795F512L-80I/PF|_________________________________________//
//////////////////////////////////////////////////////////////////////////////// 
// DEVCFG3
#pragma config USERID = 0xFFFF          // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config FSRSSEL = PRIORITY_7     // SRS Select (SRS Priority 7)
#pragma config FMIIEN = OFF             // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF             // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config FCANIO = OFF             // CAN I/O Pin Select (Alternate CAN I/O)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_12        // PLL Input Divider (12x Divider)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12        // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF             // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider (PLL Divide by 1)

// DEVCFG1
#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = OFF            // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = XT             // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = ON            // CLKO Output Signal Active on the OSCO Pin (Enabled)
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSECME           // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled)
#pragma config WDTPS = PS2048           // Watchdog Timer Postscaler (1:2048)
#pragma config FWDTEN = ON              // Watchdog Timer Enable (WDT Enabled)

// DEVCFG0
#pragma config DEBUG = OFF              // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF 


#include <xc.h>
#define _XTAL_FREQ              8000000


#define LED         PORTAbits.RA5
#define CLK         PORTDbits.RD5       //out   LCD_SHIFT

/*Macros for timing signals*/
#define DATA_ENABLE      LATAbits.LATA6         //PCAP
#define DATA_ENABLE_TRIS TRISAbits.TRISA6       //PCAP
#define HSYNC            LATDbits.LATD2         //PCAP
#define HSYNC_TRIS       TRISDbits.TRISD2       //PCAP
#define VSYNC            LATDbits.LATD3         //PCAP
#define VSYNC_TRIS       TRISDbits.TRISD3       //PCAP

/*Macros for LCD IO*/ 
#define BACKLIGHT      LATDbits.LATD1         //PCAP
#define BACKLIGHT_TRIS TRISDbits.TRISD1       //PCAP
#define LCD_RESET      LATCbits.LATC14        //PCAP
#define LCD_RESET_TRIS TRISCbits.TRISC14      //PCAP
#define LCD_CS         LATAbits.LATA7         //PCAP       
#define LCD_CS_TRIS    TRISAbits.TRISA7       //PCAP
#define LCD_DC         //
#define LCD_DC_TRIS    //

#define R1          PORTFbits.RF0       //out
#define R2          PORTDbits.RD12      //out
#define R3          PORTDbits.RD13      //out
#define R4          PORTDbits.RD6       //out
#define R5          PORTDbits.RD7       //out

#define G1          PORTEbits.RE5       //out
#define G2          PORTEbits.RE6       //out
#define G3          PORTEbits.RE7       //out
#define G4          PORTGbits.RG0       //out
#define G5          PORTGbits.RG1       //out
#define G6          PORTFbits.RF1       //out

#define B1          PORTEbits.RE0       //out
#define B2          PORTEbits.RE1       //out
#define B3          PORTEbits.RE2       //out      
#define B4          PORTEbits.RE3       //out
#define B5          PORTEbits.RE4       //out


void Hs(void){

        CLK = 0;
        CLK = 1;
        CLK = 0;
        CLK = 1;          

        HSYNC = 1;

        CLK = 0;
        CLK = 1;
        CLK = 0;
        CLK = 1;
        CLK = 0;
        CLK = 1;

        DATA_ENABLE = 1;
        for(int Hsy = 0;Hsy == 480;Hsy++){
            CLK = 0;
            R1 = 1; R2 = 0; R3 = 0; R4 = 1; R5 = 1;
            G1 = 1; G2 = 0; G3 = 0; G4 = 1; G5 = 1; G6 = 1;
            B1 = 1; B2 = 1; B3 = 0; B4 = 1; B5 = 1;
            CLK = 1;
        }
        DATA_ENABLE = 0;

        CLK = 0;
        CLK = 1;
        CLK = 0;
        CLK = 1;
        CLK = 0;
        CLK = 1;            
        CLK = 0;
        CLK = 1;    

        HSYNC = 0;

}

void main(void) {
    BACKLIGHT = 1;
    LCD_RESET = 0;
    LCD_CS = 0;


    while(1){
        
        Hs();
        Hs();
        VSYNC = 1;
        for (int i=0;i==279;i++){
            
            Hs();
        }
        VSYNC = 0 ;         
    }

    return;
}