/*********************************************************************
    FileName:     	MainBrain32_0_10.c
    Dependencies:	See #includes
    Processor:		PIC32MX795F512
    Hardware:		MainBrain32 rev 0.10
    Complier:  	    XC32 4.35
    Author:         Larry Knight 2023

    Software License Agreement:

    Description:

    File Description:

    Change History:
/***********************************************************************/

#include <stdbool.h>
#include <stdint.h>
#include <proc/p32mx795f512l.h>
#include "MainBrain.h"
//#include "usb.h"

#pragma config FMIIEN = OFF             // Ethernet RMII/MII Enable (MII Enabled)
#pragma config FETHIO = OFF             // Ethernet I/O Pin Select (Default Ethernet I/O)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by USB Module)
#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits 
#pragma config POSCMOD = EC             // Primary Oscillator Configuration
#pragma config FPLLIDIV = DIV_5         // PLL Input Divider
const uint8_t PLL_In_Divisor = 4;
#pragma config FSOSCEN = ON             // Secondary Oscillator Enable

/*************************************************************************/
/*                   System clock frequency                              */
/*************************************************************************/
#if clock_speed == 96
    #pragma config FPLLMUL = MUL_24         // PLL Multiplier 
    const uint8_t PLL_multiplier = 7;
#endif

#if clock_speed == 80
    #pragma config FPLLMUL = MUL_20         // PLL Multiplier
    const uint8_t PLL_multiplier = 5;
#endif
/*************************************************************************/
#pragma config USERID = 0xFFFF          // Enter Hexadecimal value
#pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider
const uint8_t PLL_Out_Divisor = 0;
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor 
#pragma config UPLLIDIV = DIV_5         // USB PLL Input Divider
#pragma config UPLLEN = ON              // USB PLL Enable
#pragma config IESO = OFF               // Internal/External Switch Over 
#pragma config OSCIOFNC = ON            // CLKO Output Signal Active on the OSCO Pin 
#pragma config WDTPS = PS128            // Watchdog Timer Postscaler
#pragma config FWDTEN = OFF             // Watchdog Timer Enable
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection
#pragma config DEBUG = OFF              // Background Debugger
#pragma config FSRSSEL = PRIORITY_7     // SRS Select
#pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select 

//USB handles - Must be initialized to 0 at startup
extern void* USBGenericOutHandle; 
extern void* USBGenericInHandle;

//Packet buffers
extern unsigned char INPacket[64];
extern unsigned char  OUTPacket[64];

//extend definitions here for dumping data with the utility manager
extern volatile BDT_ENTRY BDT[8] __attribute__ ((coherent, aligned (512)));
extern volatile USTAT_FIELDS USTATcopy;

uint8_t Control;

struct _MotionStruct MotionSequence;

int j = 0;
uint16_t SRAM_size = 0x1fff;
int StatusPosition = 0;
int SRAMposition = 0;
int SRAMAddHigh = 0;
int SRAMAddLow = 0;
int MemoryAddress;

//dummy register to read RSWRST register to trigger reset
int ResetReg;
unsigned RTC_delay_counter;

const uint8_t OSCFREQ = 20;
const uint8_t Pri_OSC_Source = 3;
const uint8_t Pri_OSC_Mode = 0;

char SRAM_Fail_Str[13] = "SRAM Failed ";
char SRAM_Title_Str[11] = "SRAM Test:";
uint8_t NameArray[10] = {"MainBrain"};
uint8_t RevArray[9] = {"0.10.0001"};
uint8_t ProcessorRev = 0xA5;

bool fault = false;
bool faultACK = false;
bool MotionBoardPresent = false;
bool DeviceIsConnected = false;
bool DumpBDT = false;
bool GetRegData = false;
bool GetSRAMData = false;
bool GetDeviceData = false;
bool SRAM_Valid = false;

//This is the array that contains all the data to be passed to the host
char DeviceData[64] = 
    {
        //Device Name = "MainBrain "
        'M','a','i','n','B','r','a','i','n',' ',
        
        //Revision = "0.10.0001"
        '0','.','1','0','.','0','0','0','1',
        
        //Processor Name = PIC32MX795F512l
        'P','I','C','3','2','M','X','7','9','5','F','5','1','2','L',' ',
        
        //Processor Revision
        0xA5,
        
        //Pri OSC Mode
        Pri_OSC_Mode,
        
        //Pri OSC Source
        Pri_OSC_Source,
        
        //OSCCON
        0x00,0x00,0x00,0x00,
        
        //Primary Oscillator frequency
        OSCFREQ,
        
        //PLL out divisor
        PLL_Out_Divisor,
        
        //PLL multiplier,
        PLL_multiplier,
        
        //PLL in divisor
        PLL_In_Divisor,
        
        //SRAM Size
        0x1f,0xff,
        
        //Flash Size
        0x01,0xff,0xff,
        
        //Free
        0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,
        
        //Backlight value (59)
        0x05,
        
        //ADC - Board Current
        //Lo-Byte, Hi-Byte
        0x00,0x00,
        
        //ADC - Input Voltage 
        //Lo-Byte, Hi-Byte
        0x00,0x00      
    };

char ConnectedStr[10] = {"Connected"};
char ReadyStr[10] = {"Ready    "};
char OfflineStr[10] = {"Offline  "};
char ProcessorArray[18] = {"PIC32MX795F512L-80"};
char SRAMsize[10] = {"8Kx8 SRAM"};
char FlashSize[13] = {"512Kx8 Flash"};
char USBver[8] = {"USB 2.0"};

DISPLAY_STATE displayState;

bool Flash_test(void);
void Backlight_Control(uint8_t);

int main(void)
{   
    SRAMposition = 0;
    j = 0;
    uint32_t a = 0;
    
    //Unlock OSCCON
    //This is necessary to enable the secondary oscillator for the RTC
    SYSKEY = 0x0;               // ensure OSCCON is locked
    SYSKEY = 0xAA996655;        // Write Key1 to SYSKEY
    SYSKEY = 0x556699AA;        // Write Key2 to SYSKEY
    //OSCCON is now unlocked
    
//    OSCCONbits.COSC = 1; //Internal Fast RC Oscillator with PLL module via Postscaler (FRCPLL)
    
    TRISCbits.TRISC15 = 1;
    
    //Hardware Initialization begins
    //Intialize LED Port
    LED_Port_Init();
    
    //If we are using the PLL, wait for it to lock
    if((OSCCONbits.COSC == 3) || (OSCCONbits.COSC == 1))
    {
        //Wait for PLL Lock
        for(a=0;a<0xffff;a++)
        {
            LED7 = !LED7;
        }
    }
    
    if(!OSCCONbits.SLOCK)
    {
        LED7 = 1;
    }
    else
    {
        LED7 = 0;
    }
    
    //enable secondary oscillator
    OSCCONbits.SOSCEN = 1;

    //wait for secondary oscillator warm up
    //If the OSC is not running the LED will stay lit
    for(a=0;a<0xffff;a++)
    {
        LED6 = 1;
    }
    
    if(!OSCCONbits.SOSCRDY)
    {
        LED6 = 1;
    }
    else
    {
        LED6 = 0;
    }
    
    //Data Memory SRAM wait states: Default Setting = 1; set it to 0
    BMXCONbits.BMXWSDRM = 0;

    //Flash PM Wait States: MX Flash runs at 2 wait states @ 80 MHz
    CHECONbits.PFMWS = 2;

    //Prefetch-cache: Enable prefetch for cacheable PFM instructions
    CHECONbits.PREFEN = 1;

    //Disable JTAG Port
    DDPCONbits.JTAGEN = 0;

    //External Boards Reset
    TRISDbits.TRISD1 = 1;
    PORTDbits.RD1 = 1;
    
    //Setup the GPO-2 pins as inputs
    //GP0
    TRISCbits.TRISC15 = 1;
    //GP1
    TRISAbits.TRISA15 = 1;
    //GP2
    TRISDbits.TRISD8 = 1;
    
    Timer2_Init();
    Timer5_Init();

    ADC_Init();

    //Beep the speaker
    TRISDbits.TRISD14 = 0;
    int i;
    for(i=0;i<500;i++)
    {
        PORTDbits.RD14 = 1;
        Delay(20000);
        PORTDbits.RD14 = 0;
        Delay(20000);
    }

    //Initialize the Parallel Master Port
    PMP_Init();
    
    //SRAM Init
    REN70V05_Init();
    
    //Flash Init
    Flash_Init();
    
    //Initialize the Display
    Display_Init();   
    
    //control is with a P-channel MOSFET
    //so the higher the number, the dimmer
    //the screen
    Backlight_Control(10);
                
    //Clear the Screen
    Display_CLRSCN(white);   

    //Turn on the Display
    Display_DISPON();   

    //Real time clock
    RTC_Init();
    
    //Lock OSCCON to prevent any clock switching
    //SYSKEY = 0x0;               
    //OSCCON is now locked    

    //Initialize the Interrupts
    InterruptsInit();
    
    //SRAM test
    //SRAM_Test();    

    //All LEDs ON
    LED_Port(0xff);

    //Display the Logo
    ShowSplashScreen();

    //Turn the LEDs OFF
    LED_Port(0x0);
    
    //Hold External Boards in Reset so that the SRAM can be tested
    TRISDbits.TRISD1 = 0;
    PORTDbits.RD1 = 0;
    
    //Release the Reset on the Motion Board
    PORTDbits.RD1 = 1;
    TRISDbits.TRISD1 = 1;  
    
    //Initialize the Endpoint Handles 
    USBGenericOutHandle = 0;	
	USBGenericInHandle = 0;		
    
    //Setup the USB Procedures
    USB_Init();

    //USB - Configure the pull ups, full/low-speed mode, 
    //set the ping pong mode, and set internal transceiver
    USBDeviceAttach();
    
    //Read OSCCON and place it in the DeviceData[] array
    //0x41 is the hi-byte
    int osccon_data = OSCCON;
    DeviceData[38] = osccon_data;
    DeviceData[39] = osccon_data >> 8;
    DeviceData[40] = osccon_data >> 16;
    DeviceData[41] = osccon_data >> 24;    
    
    displayState = MAIN;
    
    //Main program loop
    while(1)
    {
        //This shows the main loop is running
        LED3 = !LED3;   
        
        //USB I/O 
        ProcessIO();
        
        //this switches to the various program screens
        switch(displayState)
        {
            case MAIN:
                if(USBDeviceState == CONFIGURED_STATE)
                {             
                    vchar = 280;
                    hchar = 10;

                    if(DeviceIsConnected == true)
                    {
                        WriteString(hchar, vchar, ConnectedStr, blue);          
                    }
                    else
                    {
                        WriteString(hchar, vchar, ReadyStr, black);          
                    }
                }  
                if(USBDeviceState == DEFAULT_STATE)
                {
                    vchar = 280;
                    hchar = 10;

                    WriteString(hchar, vchar, OfflineStr, red);
                }
                break;
            case DUMP_BDT:
                DisplayUSBData();
                //displayState = MAIN;
                break;
            case DUMP_MEM:                
                break;
            case CLOCK:
                //Clock(ClockData);
                break;
            case DUMP_SRAM:                
                break;
            case MOTION:
                MotionInterface();
                break;
            case SEQ:
                BuildSeqInterface();
                break;
        }
    } 
} 

void SRAM_Test(void)
{
    //Check SRAM
    //If the SRAM test fails the LED will stay on
    int failure_count = 0;
    int a;
    uint16_t i;
    
    fore_color = black;
    back_color = white;
    
    hchar = 10;
    vchar = 20;
    
    //SRAM Test String
    WriteString(hchar, vchar, SRAM_Title_Str, blue);
    
    hchar = 10;
    vchar = 40;
    
    for(i=0;i<=SRAM_size;i++)
    {
        //First write '0's
        while(REN70V05_WR(i, 0x0) == false);
        
        mdata_70V05 == 0x55;

        while(REN70V05_RD(i) == -1);
        
        Binary2ASCIIHex(i);
        WriteChar(hchar, vchar, d_hex[3], fore_color);
        WriteChar(hchar, vchar, d_hex[2], fore_color);
        WriteChar(hchar, vchar, d_hex[1], fore_color);
        WriteChar(hchar, vchar, d_hex[0], fore_color);
        
        hchar = 10;
        vchar = 40;

        if(mdata_70V05 > 0)
        {           
            failure_count++;
            
            WriteString(hchar, vchar, SRAM_Fail_Str, fore_color);
            WriteChar(hchar, vchar, ':', fore_color);
            
            Binary2ASCIIHex(i);
            WriteChar(hchar, vchar, d_hex[3], fore_color);
            WriteChar(hchar, vchar, d_hex[2], fore_color);
            WriteChar(hchar, vchar, d_hex[1], fore_color);
            WriteChar(hchar, vchar, d_hex[0], fore_color);

            WriteChar(hchar, vchar, ':', fore_color);
            
            Binary2ASCIIHex(mdata_70V05);
            WriteChar(hchar, vchar, d_hex[1], fore_color);
            WriteChar(hchar, vchar, d_hex[0], fore_color);
           
            WriteChar(hchar, vchar, ':', fore_color);
           
            WriteChar(hchar, vchar, '0', fore_color);
            
            hchar = 10;
            vchar = vchar + 20;
            
        }
    }
    
    for(i=0;i<=SRAM_size;i++)
    {
        //Now write '0xf's
        while(REN70V05_WR(i, 0xff) == false);
        
        mdata_70V05 == 0x55;

        while(REN70V05_RD(i) == -1);
        
        if(mdata_70V05 < 0xff)
        {           
            failure_count++;
            
            WriteString(hchar, vchar, SRAM_Fail_Str, red);
            WriteChar(hchar, vchar, ':', fore_color);
            
            Binary2ASCIIHex(i);
            WriteChar(hchar, vchar, d_hex[3], fore_color);
            WriteChar(hchar, vchar, d_hex[2], fore_color);
            WriteChar(hchar, vchar, d_hex[1], fore_color);
            WriteChar(hchar, vchar, d_hex[0], fore_color);

            WriteChar(hchar, vchar, ':', fore_color);
            
            Binary2ASCIIHex(mdata_70V05);
            WriteChar(hchar, vchar, d_hex[1], fore_color);
            WriteChar(hchar, vchar, d_hex[0], fore_color);
           
            WriteChar(hchar, vchar, ':', fore_color);
           
            WriteChar(hchar, vchar, 'f', fore_color);
            WriteChar(hchar, vchar, 'f', fore_color);
            
            hchar = 10;
            vchar = vchar + 20;
            
        }
    }

    if(failure_count > 0)
    {
        LED5 = 1;
        SRAM_Valid = false;
        while(1);
    }
    
    if(failure_count == 0)
    {
        LED5 = 0;
        SRAM_Valid = true;
        
        hchar = 100;
        WriteChar(hchar, vchar, 'O', green);
        WriteChar(hchar, vchar, 'K', green); 
        
        //Beep speaker
        TRISDbits.TRISD14 = 0;
        int i;
        for(i=0;i<500;i++)
        {
            PORTDbits.RD14 = 1;
            Delay(20000);
            PORTDbits.RD14 = 0;
            Delay(20000);
        }
    }
      
    //short delay for viewing results
    LongDelay(2);   
}

bool Flash_Test(void)
{
    if(!Flash_DID)
    {
        return false;
    }
    
    if(Flash_DID)
    {
        return true;
    }
}

void ProcessIO(void)
{   
    uint8_t ctrl = 0;
    
    //Read the ADC and store the data in a buffer
    //ADC reads the voltage on pin #33 (AN9)
    TRISBbits.TRISB9 = 1;
    AD1PCFGbits.PCFG9 = 0;           
    AD1CHSbits.CH0SA = 9;           //Channel 0 positive input is AN9
    //wait for ADC
    Delay(2500);

    /**************************************************************************/
    IFS1bits.AD1IF = 0;             //Clear interrupt flag

    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert   

    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert

    INPacket[60] = ADC1BUF0;
    INPacket[61] = ADC1BUF0 >> 8;

    //ADC reads the voltage on pin #21 (AN4)
    TRISBbits.TRISB4 = 1;
    AD1PCFGbits.PCFG4 = 0;           
    AD1CHSbits.CH0SA = 4;           //Channel 0 positive input is AN4

    Delay(2500);

    ADC1BUF0 = 0;
    IFS1bits.AD1IF = 0;             //Clear interrupt flag
    AD1CON1bits.ASAM = 1;           //Auto start sampling for 31 TAD, then convert
    while(!IFS1bits.AD1IF);         //Wait for conversion to complete
    AD1CON1bits.ASAM = 0;           //Stop sample/convert

    //Read the buffer

    INPacket[62] = ADC1BUF0;
    INPacket[63] = ADC1BUF0 >> 8;

    //User Application USB tasks below.
    //Note: The user application should not begin attempting to read/write over the USB
    //until after the device has been fully enumerated.  After the device is fully
    //enumerated, the USBDeviceState will be set to "CONFIGURED_STATE".
    if((USBDeviceState < CONFIGURED_STATE)||(U1PWRCbits.USUSPEND == 1))
    {
        return;
    }
    
    //As the device completes the enumeration process, the USBCBInitEP() function will
    //get called.  In this function, we initialize the user application endpoints (in this
    //example code, the user application makes use of endpoint 1 IN and endpoint 1 OUT).
    //The USBGenRead() function call in the USBCBInitEP() function initializes endpoint 1 OUT
    //and "arms" it so that it can receive a packet of data from the host.  Once the endpoint
    //has been armed, the host can then send data to it (assuming some kind of application software
    //is running on the host, and the application software tries to send data to the USB device).
    
    //If the host sends a packet of data to the endpoint 1 OUT buffer, the hardware of the SIE will
    //automatically receive it and store the data at the memory location pointed to when we called
    //USBGenRead().  Additionally, the endpoint handle (in this case USBGenericOutHandle) will indicate
    //that the endpoint is no longer busy.  At this point, it is safe for this firmware to begin reading
    //from the endpoint buffer, and processing the data.  In this example, we have implemented a few very
    //simple commands.  For example, if the host sends a packet of data to the endpoint 1 OUT buffer, with the
    //first byte = 0x80, this is being used as a command to indicate that the firmware should "Toggle LED(s)".
    
    //Check if the endpoint has received any data from the host.
    //if(!USBHandleBusy(USBGenericOutHandle))
    if(!(USBGenericOutHandle==0?0:((volatile BDT_ENTRY*)USBGenericOutHandle)->STAT.UOWN)) 
    {   
        //Turn on LED10 to show data transfer
        LED9 = !LED9;
        
        //Data arrived, check what kind of command might be in the packet of data.
        switch(OUTPacket[0])					
        {
            int i;
            //These are the Device Program Commands 0x20 - 0x2f
            
             /*The Program List
                0 Dump BDT
                1 Dump Device Memory
                2 Clock
                3 Set Time
                4 Get Device Info
                5 Read SRAM
                6 Dump SRAM to Display
                7 Write File to SRAM
                8 Read Flash
                9 Write Flash
                10 Restart Device
                11 Set LEDs
                12 Update Firmware
                13 Home
                14 Motion Interface
             */
            
            case 0x20:
                //Dump BDT
                Display_CLRSCN(white);
                displayState = DUMP_BDT;
                break;
            case 0x21:
                //Dump Device Memory
                displayState = DUMP_MEM;
                int temp = 0;
                
                //Get the address
                temp  = OUTPacket[1];
                MemoryAddress = temp << 24;
                        
                temp = OUTPacket[2];
                temp = temp << 16;
                MemoryAddress = MemoryAddress | temp;
                            
                temp = OUTPacket[3];
                temp = temp << 8;
                
                MemoryAddress = MemoryAddress | temp;
                
                MemoryAddress = MemoryAddress | OUTPacket[4];
                
                DumpMemory();
                break;
            case 0x22:
                //Clock
                Display_CLRSCN(white);
                displayState = CLOCK;                
                break;
            case 0x23:
                //Set Time
                setTime();
                break;
            case 0x24:
                //Get Device Information
                
                break;
            case 0x25:
                //Read SRAM
                SRAMAddHigh = OUTPacket[1];
                SRAMAddLow = OUTPacket[2];
                //SRAMAddLow = SRAMAddLow & 0xff;
                
                //now combine the 2 bytes to form a word
                SRAMAddHigh = SRAMAddHigh << 8;
                //SRAMAddHigh = SRAMAddHigh & 0xff00;
                SRAMposition = SRAMAddHigh + SRAMAddLow;
                GetSRAMData = true;                
                break;
            case 0x26:
                //Dump SRAM to Display
                displayState = DUMP_SRAM;
                //OUTPacket[1] = SRAM Block to display
                SRAM_Block = OUTPacket[1];
                DumpSRAM();
                break;
            case 0x27:
                Display_CLRSCN(white);
                hchar = 10;
                vchar = 20;
                Binary2ASCIIHex(OUTPacket[1]);
                WriteChar(hchar, vchar, d_hex[7], 0x0);
                WriteChar(hchar, vchar, d_hex[6], 0x0);
                WriteChar(hchar, vchar, d_hex[5], 0x0);
                WriteChar(hchar, vchar, d_hex[4], 0x0);
                WriteChar(hchar, vchar, d_hex[3], 0x0);
                WriteChar(hchar, vchar, d_hex[2], 0x0);
                WriteChar(hchar, vchar, d_hex[1], 0x0);
                WriteChar(hchar, vchar, d_hex[0], 0x0);

                //Write File to SRAM
                for(i=1;i<64;i++)
                {                 
                    //Write Data to SRAM
                    while(REN70V05_WR((j), OUTPacket[i]) == false);
                    j++;
                }       
                i = 1;
                j = 0;
                LED6 = 1;
                break;
            case 0x28:
                break;
            case 0x29:
                break;
            case 0x2a:
                //Reset Device
                SystemReset();
                break;
                //Set LEDs
            case 0x2b:
                LED0 = OUTPacket[8];
                LED1 = OUTPacket[7];
                LED2 = OUTPacket[6];
                LED3 = OUTPacket[5];
                LED4 = OUTPacket[4];
                LED5 = OUTPacket[3];
                LED6 = OUTPacket[2];
                LED7 = OUTPacket[1];
                break;
            case 0x2d:
                //Home           
                displayState = MAIN;    
                ShowSplashScreen();
                break;
                //Motion Interface
            case 0x2e:
                //Set default end position
                while(REN70V05_WR(0x3e6, 0xbb) == false);
                while(REN70V05_WR(0x3e7, 0x0b) == false);
                while(REN70V05_WR(0x3e8, 0x00) == false);
                while(REN70V05_WR(0x3e9, 0x00) == false);
                
                //Clear the Screen
                Display_CLRSCN(white);
                displayState = MOTION;                
                break;
                //Motor Run Command
            case 0x2f:
                while(REN70V05_RD(0x3ff) == -1);
                Control = mdata_70V05 | 0x01;
                while(REN70V05_WR(0x3ff, Control) == false);
                break;
                //Motor Stop Command
            case 0x30:
                Control = Control & 0xfe;
                while(REN70V05_WR(0x3ff, Control) == false);
                break;
                //Motor DIR Toggle Command
            case 0x31:
                //Toggle direction
                if((Control & 0x02) == 2)
                {
                    Control = Control & 0x0d;
                }
                else
                {
                    Control = Control | 0x02;
                }
                while(REN70V05_WR(0x3ff, Control) == false);
                break;
                //Run Sequence
            case 0x32:
                while(REN70V05_RD(0x3ff) == -1);
                Control = mdata_70V05 | 0x81;
                while(REN70V05_WR(0x3ff, Control) == false);

                break;
                //Write Sequence Data
            case 0x33:
                //Sequence number
                while(REN70V05_WR(0x300, OUTPacket[2]) == false);
                //Sequence command
                while(REN70V05_WR(0x301, OUTPacket[3]) == false);
                //Sequence end
                while(REN70V05_WR(0x302, OUTPacket[4]) == false);
                while(REN70V05_WR(0x303, OUTPacket[5]) == false);
                while(REN70V05_WR(0x304, OUTPacket[6]) == false);
                while(REN70V05_WR(0x305, OUTPacket[7]) == false);
                
                //Sequence ramp up
                //MotionSequence.ramp = OUTPacket[8];
                while(REN70V05_WR(0x306, OUTPacket[8]) == false);
                //Sequence Direction
                //MotionSequence.dir = OUTPacket[9];
                while(REN70V05_WR(0x307, OUTPacket[9]) == false);
                //Speed
                //MotionSequence.speed = OUTPacket[10];
                while(REN70V05_WR(0x308, OUTPacket[10]) == false);
                //Sequence Length
                while(REN70V05_WR(0x3d0, OUTPacket[11]) == false);
                break;
                //Clear count
            case 0x34:
//                //DEBUG - simulate a drive fault
//                while(REN70V05_RD(0x3fe) == -1);
//                int s = mdata_70V05;
//                if((s & 0x40) == 0x40)
//                {
//                    s = s & 0xbf;
//                    LED6 = 1;
//                    LED5 = 0;
//                }
//                else
//                {
//                    s = s | 0x40;
//                    LED6 = 0;
//                    LED5 = 1;
//                }
//                while(REN70V05_WR(0x3fe, s) == false);
                while(REN70V05_RD(0x3ff) == -1);                
                Control = (mdata_70V05 | 0x04);
                while(REN70V05_WR(0x3ff, Control) == false);
                break;
                //Reset Motion Board
            case 0x35:
                TRISDbits.TRISD1 = 0;
                PORTDbits.RD1 = 0;
                Delay(50000);
                PORTDbits.RD1 = 1;
                TRISDbits.TRISD1 = 1;
                break;
                //Motion Home
            case 0x36:
                Control = Control | 0x80;
                while(REN70V05_WR(0x3ff, Control) == false);
                break;
            case 0x37:
                //Build Sequence Screen
                Display_CLRSCN(white);
                displayState = SEQ;                
                break;
               //Set Back light 
            case 0x61:
                Backlight_Control(OUTPacket[1]);
                break;
            //Set Connected = true
            case 0x62:
                DeviceIsConnected = true;
                break;
            case 0x63:
                DeviceIsConnected = false;
                break;
            case 0x64:
                SetMotorData(OUTPacket[1], OUTPacket[2]);
                break;                
            //Set SRAM read start address
            case 0x70:
                if(displayState == DUMP_BDT)
                {
                    Display_CLRSCN(white);
                    displayState = MAIN;
                    ShowSplashScreen();                                    
                }
                GetSRAMData = true;
                break;
            case 0x71:
                GetRegData = true; 
                break;
            case 0x72:
                GetDeviceData = true; 
                break;
            case 0x73:
                //GetADCISense = true; 
                break;
            case 0x74:
                SRAMposition = 0;
                GetSRAMData = false;
                break;
            case 0x75:
                //If the SRAM is missing or fails test an endless loop can occur 
                //  if this function does not allow the program to pass through
                if(SRAM_Valid == true)
                {
                    //Find Motion Interface
                    while(REN70V05_RD(0x3ec) == -1);
                    if(mdata_70V05 == 0x99)
                    {
                        MotionBoardPresent = true;
                    }
                }
                break;
            //Read SRAM starting from SRAMposition
            case 0x81:
                //Turn on LED10 to show data transfer
                LED10 = !LED10;
                
                //If any attempts are still pending, we do not want to write to the endpoint 1 IN buffer again, until the previous 
                //transaction is complete.
				//Otherwise the unsent data waiting in the buffer will get overwritten and will result in unexpected behavior.    
                //if(!USBHandleBusy(USBGenericInHandle))
                if(!(USBGenericInHandle==0?0:((volatile BDT_ENTRY*)USBGenericInHandle)->STAT.UOWN))
    	        {	
    	            //The endpoint was not "busy", therefore it is safe to write to the buffer and arm the endpoint.
                    int temp;
                    if(GetDeviceData == true)
                    {
                        for(i=0;i<60;i++)
                        {
                            INPacket[i] = DeviceData[i];
                        }
                        GetDeviceData == false;
                    }
                    if(GetRegData == true)
                    {
                        //Device Name Array
                        int i;
                        for(i=0;i<10;i++)
                        {
                            INPacket[i] = NameArray[i];
                        }
                        
                        //Revision Array
                        for(i=0;i<9;i++)
                        {
                            INPacket[i + 10] = RevArray[i];
                        }
                        
                        //Processor Array
                        for(i=0;i<16;i++)
                        {
                            INPacket[i + 19] = ProcessorArray[i];
                        }
                        
                        //Processor Revision
                        INPacket[35] = ProcessorRev;
                        
                        //Primary Oscillator Mode
                        INPacket[36] = DEVCFG1bits.POSCMOD;
                        
                        //Current Primary Oscillator Source
                        INPacket[37] = DEVCFG1bits.FNOSC;
                        
                        //OSCCON
                        INPacket[38] = (char)OSCCON;
                        temp = OSCCON >> 8;
                        INPacket[39] = (char)temp;
                        temp = OSCCON >> 16;
                        INPacket[40] = (char)temp;
                        temp = OSCCON >> 24;
                        INPacket[41] = (char)temp;
                        
                        //Oscillator frequency
                        INPacket[42] = OSCFREQ;
                        
                        //PLL out divisor
                        INPacket[43] = OSCCONbits.PLLODIV;
                        
                        //PLL multiplier
                        INPacket[44] = OSCCONbits.PLLMULT;
                        
                        //PLL in divisor
                        INPacket[45] =  DEVCFG2bits.FPLLIDIV;
                        
                        GetRegData = false;
                    }
                    
                    if(GetSRAMData == true)
                    {
                        for(i=0;i<64;i++)
                        {
                            while(REN70V05_RD((SRAMposition + i)) == -1);
                            INPacket[i] = mdata_70V05; 	
                        }
                        SRAMposition = SRAMposition + 64;
                    }
                    
                    if(MotionBoardPresent == true)
                    {
                        INPacket[0] = 0x99;
                        MotionBoardPresent = false;
                    }
                    //The USBGenWrite() function call "arms" the endpoint (and makes the handle indicate the endpoint is busy).
	                //Once armed, the data will be automatically sent to the host (in hardware by the SIE) the next time the 
	                //host polls the endpoint.  Once the data is successfully sent, the handle (in this case USBGenericInHandle) 
	                //will indicate the the endpoint is no longer busy.
                    
					USBGenericInHandle = USBTransferOnePacket(1,1,(uint8_t*)&INPacket,64);	
                }
        }
        //Clear the LEDs
        //LED9 = _OFF;
        //LED10 = _OFF;
        
        //Re-arm the OUT endpoint for the next packet:
	    //The USBGenRead() function call "arms" the endpoint (and makes it "busy").  If the endpoint is armed, the SIE will 
	    //automatically accept data from the host, if the host tries to send a packet of data to the endpoint.  Once a data 
	    //packet addressed to this endpoint is received from the host, the endpoint will no longer be busy, and the application
	    //can read the data which will be sitting in the buffer.
        USBGenericOutHandle = USBTransferOnePacket(1, 0, (uint8_t*)&OUTPacket, 64);
        //#define USBRxOnePacket(ep,data,len)      USBTransferOnePacket(ep,OUT_FROM_HOST,data,len)
    }
                
}

void MotionInterface(void)
{
    uint8_t sram_data;
    
    hchar = 10;
    vchar = 20;
    
    //check for Drive Fault
    while(REN70V05_RD(0x3fe) == -1);
    if((mdata_70V05 & 0x40) == 0x40 && faultACK == false)
    {
        Display_CLRSCN(white);
        Display_Rect(130, 380, 100, 200, black);
        Display_Rect(132, 378, 102, 198, white);

        char FaultBuffer[13] = {"DRIVE FAULT!"};
        WriteString(165, 135, FaultBuffer, red);
        faultACK = true;
        fault = true;       
    }   
    
    if((mdata_70V05 & 0x40) == 0x0 && fault == false)
    {
        char MotionBuffer[18] = {"Motion Registers:"};   
        WriteString(hchar, vchar, MotionBuffer, black);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer1[11] = {"Control - "};
        WriteString(hchar, vchar, MotionBuffer1, black);

        while(REN70V05_RD(0x3ff) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = hchar + 20;

        char MotionBuffer2[10] = {"Status - "};
        WriteString(hchar, vchar, MotionBuffer2, black);

        while(REN70V05_RD(0x3fe) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = 10;
        vchar = vchar + 30;

        //the board key that shows motion board is present and ready
        char MotionBuffer3[6] = {"Key - "};
        WriteString(hchar, vchar, MotionBuffer3, black);

        while(REN70V05_RD(0x3ec) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer4[10] = {"Position:"};
        WriteString(hchar, vchar, MotionBuffer4, blue);

        //Display the position
        while(REN70V05_RD(0x3f3) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f2) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f1) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f0) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = hchar + 20;

        char MotionBuffer5[5] = {"PWM:"};
        WriteString(hchar, vchar, MotionBuffer5, blue);

        //Display the position
        while(REN70V05_RD(0x3d5) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer6[10] = {"Setpoint:"};
        WriteString(hchar, vchar, MotionBuffer6, blue);

        //Display the set speed
        while(REN70V05_RD(0x3f8) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = hchar + 20;

        char MotionBuffer7[10] = {"Endpoint:"};
        WriteString(hchar, vchar, MotionBuffer7, blue);

        while(REN70V05_RD(0x305) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x304) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x303) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x302) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer8[10] = {"QEI1STAT:"};
        WriteString(hchar, vchar, MotionBuffer8, blue);

        while(REN70V05_RD(0x3dd) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3dc) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer9[7] = {"0x3ff:"};
        WriteString(hchar, vchar, MotionBuffer9, blue);

        while(REN70V05_RD(0x3ff) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = hchar + 20;

        char MotionBuffer10[7] = {"0x3fe:"};
        WriteString(hchar, vchar, MotionBuffer10, blue);

        while(REN70V05_RD(0x3fe) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);
    }
}

void BuildSeqInterface()
{
    uint8_t sram_data;
    int r;
    int s;
    
    hchar = 10;
    vchar = 20;
    
    //check for Drive Fault
    while(REN70V05_RD(0x3fe) == -1);
    if((mdata_70V05 & 0x40) == 0x40 && faultACK == false)
    {
        //Display_CLRSCN(white);
        Display_Rect(130, 380, 100, 200, black);
        Display_Rect(132, 378, 102, 198, white);

        char FaultBuffer[13] = {"DRIVE FAULT!"};
        WriteString(165, 135, FaultBuffer, red);
        faultACK = true;
        fault = true;       
    }   
    
    //Build Interface
    if((mdata_70V05 & 0x40) == 0x0 && fault == false)
    {
        //Header
        char MotionBuffer[18] = {"Motion Registers:"};   
        WriteString(hchar, vchar, MotionBuffer, black);

        hchar = 10;
        vchar = vchar + 30;

        //Control Register
        char MotionBuffer1[11] = {"Control - "};
        WriteString(hchar, vchar, MotionBuffer1, black);

        while(REN70V05_RD(0x3ff) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = hchar + 20;

        //Status register+
        char MotionBuffer2[10] = {"Status - "};
        WriteString(hchar, vchar, MotionBuffer2, black);

        while(REN70V05_RD(0x3fe) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = 10;
        vchar = vchar + 30;

        //the board key that shows motion board is present and ready
        char MotionBuffer3[6] = {"Key - "};
        WriteString(hchar, vchar, MotionBuffer3, black);

        while(REN70V05_RD(0x3ec) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer4[10] = {"Position:"};
        WriteString(hchar, vchar, MotionBuffer4, blue);

        //Display the position
        while(REN70V05_RD(0x3f3) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f2) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f1) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3f0) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = hchar + 20;

        char MotionBuffer5[5] = {"PWM:"};
        WriteString(hchar, vchar, MotionBuffer5, blue);

        //Display the position
        while(REN70V05_RD(0x3d5) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer6[10] = {"Setpoint:"};
        WriteString(hchar, vchar, MotionBuffer6, blue);

        //Display the set speed
        while(REN70V05_RD(0x3f8) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = hchar + 20;

        //Display the Endpoint
        char MotionBuffer7[10] = {"Endpoint:"};
        WriteString(hchar, vchar, MotionBuffer7, blue);

        while(REN70V05_RD(0x305) == -1);
        s = mdata_70V05;
        s = s << 24;

        while(REN70V05_RD(0x304) == -1);        
        int x = mdata_70V05;
        x = x << 16;
        s = s + x;
        
        while(REN70V05_RD(0x303) == -1);
        x = mdata_70V05;
        x = x << 8;
        s = s + x;

        while(REN70V05_RD(0x302) == -1);
        s = s + mdata_70V05;

        Binary2ASCIIHex(s);
        WriteChar(hchar, vchar, d_hex[7], red);
        WriteChar(hchar, vchar, d_hex[6], red);
        WriteChar(hchar, vchar, d_hex[5], red);
        WriteChar(hchar, vchar, d_hex[4], red);
        WriteChar(hchar, vchar, d_hex[3], red);
        WriteChar(hchar, vchar, d_hex[2], red);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);
       
        hchar = 10;
        vchar = vchar + 30;

        char MotionBuffer8[10] = {"QEI1STAT:"};
        WriteString(hchar, vchar, MotionBuffer8, blue);

        while(REN70V05_RD(0x3dd) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        while(REN70V05_RD(0x3dc) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], red);
        WriteChar(hchar, vchar, d_hex[0], red);

        hchar = 10;
        vchar = vchar + 30;
    }
    
    //Dump the first two Sequences  
    vchar = vchar + 30;
    hchar = 40;
    
    int i;
    for(i=0;i<20;i++)
    {
        while(REN70V05_RD(0x300 + i) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], black);
        WriteChar(hchar, vchar, d_hex[0], black);
        
        hchar = hchar + 10;
        if(hchar > 430)
        {
            vchar = vchar + 30;
            hchar = 40;
        }
    }
}

void SetMotorData(uint8_t MotorSpeed, uint8_t Accel)
{
    //MotorSpeed
    while(REN70V05_WR(0x3f8, MotorSpeed) == false);
    
    //Acceleration Factor
    while(REN70V05_WR(0x3fa, Accel) == false);
}

void SystemReset(void)
{
    //unlock the system
    SYSKEY = 0x00000000;
    //write invalid key to force lock 
    SYSKEY = 0xAA996655;
    //write key1 to SYSKEY 
    SYSKEY = 0x556699AA;
    //write key2 to SYSKEY
    // OSCCON is now unlocked
    /* set SWRST bit to arm reset */ 
    RSWRSTSET = 1;
    /* read RSWRST register to trigger reset */ 
    ResetReg = RSWRST;
    /* prevent any unwanted code execution until reset occurs*/ 
    while(1);
}

void ShowSplashScreen(void)
{
    int i;

    //Clear the Screen
    Display_CLRSCN(white);
        
    //Load Splash Image
    /****************************/
    Display_CASET(0, 479);
    Display_RASET(120, 170);
    Display_RAMWR();

    //Data
    //RF3 = D/C 1=Data, 0=Command
    PORTFbits.RF3 = 1;        
    
    for(i=0;i<=23040; i++)
    {
        #if DisplayType == ILI19488
            PMDIN = SplashImage[i*2];
        #endif

        #if DisplayType == HX8357B
            PMDIN = ~SplashImage[i*2];       
         #endif

        while(PMMODEbits.BUSY == 1);    
    }
    
    LongDelay(2);
    /****************************/

    //Clear the Screen
    Display_CLRSCN(white);
        
    //Draw header
    fore_color = black;

    //Draw Header Bar
    back_color = HeaderBarBackColor;
    
    Display_Rect(0, 480, 0, 40, back_color);  
    
    hchar = 115;
    vchar = 7;
    
    //Display Header Text
    char MyString[10] = {"MainBrain "};
    WriteString(hchar, vchar, MyString, black);
    WriteString(hchar, vchar, RevArray, black);
    
    back_color = white;
    hchar = 10;
    vchar = 50; 
    
    //Display device information       
    //Processor
    hchar = 10;
    vchar = 50;
    WriteString(hchar, vchar, ProcessorArray, black);

    //Device ID    
    hchar = hchar + 20;
    
    Binary2ASCIIHex(DEVID);
    //Upper 4 bits is the processor revision
    //WriteChar(hchar, vchar, d_hex[7], black);
    
    WriteChar(hchar, vchar, d_hex[6], black);   
    WriteChar(hchar, vchar, d_hex[5], black);
    WriteChar(hchar, vchar, d_hex[4], black);
    WriteChar(hchar, vchar, d_hex[3], black);
    WriteChar(hchar, vchar, d_hex[2], black);
    WriteChar(hchar, vchar, d_hex[1], black);
    WriteChar(hchar, vchar, d_hex[0], black);
    
    //SRAM size
    hchar = 10;
    vchar = vchar + 25;
    WriteString(hchar, vchar, SRAMsize, black);

    //Flash Size
    hchar = 10;
    vchar = vchar + 25;
    WriteString(hchar, vchar, FlashSize, black);

    //USB ver
    hchar = 10;
    vchar = vchar + 25;
    WriteString(hchar, vchar, USBver, black);   
}

void Clock(int ClockData)
{       
    fore_color = black;
    
    //Colon 1
    Display_Rect(151, 163, 120, 130, fore_color);  
    Display_Rect(151, 163, 160, 170, fore_color);  

    //Colon 2
    Display_Rect(317, 329, 120, 130, fore_color);  
    Display_Rect(317, 329, 160, 170, fore_color);  
    
    if(ClockData == 0)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, fore_color);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, white);    

    }

    if(ClockData == 1)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, white);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, white);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, white);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, white);    

    }

    if(ClockData == 2)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, white);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, fore_color);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, white);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 3)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, white);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 4)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, white);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, white);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 5)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, white);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 6)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, white);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, white);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, fore_color);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 7)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, white);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, white);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, white);    

    }

    if(ClockData == 8)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, fore_color);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, fore_color);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    

    }

    if(ClockData == 9)
    {
            //segment A
            Display_Rect(hchar + 10, hchar + 50, vchar, vchar + 10, fore_color);    
            //segment B
            Display_Rect(hchar + 50, hchar + 60, vchar, vchar + 50, fore_color);
            //Segment C
            Display_Rect(hchar + 50, hchar + 60, vchar + 60, vchar + 110, fore_color);
            //Segment D
            Display_Rect(hchar + 10, hchar + 50, vchar + 100, vchar + 110, white);    
            //Segment E
            Display_Rect(hchar, hchar + 10, vchar + 60, vchar + 110, white);
            //Segment F
            Display_Rect(hchar, hchar + 10, vchar, vchar + 50, fore_color);
            //Segment G
            Display_Rect(hchar + 10, hchar + 50, vchar + 50, vchar + 60, fore_color);    
    }
}

void setTime(void)
{
    //Unlock the registers
    RTCCONbits.RTCWREN = 1;
    
    hchar = 10;
    vchar = 280;
    
    //24 to 12 hour conversion
    if(OUTPacket[1] > 12)
    {
        Binary2ASCIIBCD(OUTPacket[1] - 12);
    }
    else
    {
        Binary2ASCIIBCD(OUTPacket[1]);
    }
//    WriteChar(hchar, vchar, d1, 0x0);
//    WriteChar(hchar, vchar, d0, 0x0);
        
    RTCTIMEbits.HR10 = d1;
    RTCTIMEbits.HR01 = d0;
    
    hchar = hchar + 20;
    Binary2ASCIIBCD(OUTPacket[2]);
//    WriteChar(hchar, vchar, d1, 0x0);
//    WriteChar(hchar, vchar, d0, 0x0);
   
    RTCTIMEbits.MIN10 = d1;
    RTCTIMEbits.MIN01 = d0;

    hchar = hchar + 20;
    Binary2ASCIIBCD(OUTPacket[3]);
//    WriteChar(hchar, vchar, d1, 0x0);
//    WriteChar(hchar, vchar, d0, 0x0);
   
    
    RTCTIMEbits.SEC10 = d1;
    RTCTIMEbits.SEC01 = d0;    

    //Re-lock the registers
    RTCCONbits.RTCWREN = 0;
}

void DumpSRAM(void)
{
    //Clear the Screen
    Display_CLRSCN(white);   

    hchar = 10;
    vchar = 10;
    
    int i;
    char MyBuffer10[5] = {"SRAM"};
    WriteString(hchar, vchar, MyBuffer10, black);
    
    hchar = hchar + 50;
    
    char MyBuffer11[7] = {"block:"};
    WriteString(hchar, vchar, MyBuffer11, black);

    Binary2ASCIIHex(SRAM_Block);
    WriteChar(hchar, vchar, d_hex[1], blue);
    WriteChar(hchar, vchar, d_hex[0], blue);
    WriteChar(hchar, vchar, ':', black);
    
    //hchar = hchar + 30;
    
    //SRAM Block must be 0 - 127
    int mem;
    SRAM_Block = SRAM_Block * 64;
    
    Binary2ASCIIHex(SRAM_Block);
    WriteChar(hchar, vchar, d_hex[3], blue);
    WriteChar(hchar, vchar, d_hex[2], blue);
    WriteChar(hchar, vchar, d_hex[1], blue);
    WriteChar(hchar, vchar, d_hex[0], blue);

    hchar = 10;
    vchar = 35;
    
    for(i=1;i<=64;i++)
    {        
        
        while(REN70V05_RD(SRAM_Block) == -1);
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], 0x0);
        WriteChar(hchar, vchar, d_hex[0], 0x0);
        SRAM_Block++;
        hchar = hchar + 10;
        if(hchar > 400)
        {
            vchar = vchar + 20;
            hchar = 10;
        }
    }


}

void DumpMemory(void)
{
    //Clear the Screen
    Display_CLRSCN(white);   

    hchar = 10;
    vchar = 10;
    
    char MyBuffer11[8] = {"Memory:"};
    WriteString(hchar, vchar, MyBuffer11, red);
    
    hchar = hchar + 15;
    //display the address
        Binary2ASCIIHex(MemoryAddress);
        WriteChar(hchar, vchar, d_hex[7], fore_color);
        WriteChar(hchar, vchar, d_hex[6], fore_color);
        WriteChar(hchar, vchar, d_hex[5], fore_color);
        WriteChar(hchar, vchar, d_hex[4], fore_color);
        WriteChar(hchar, vchar, d_hex[3], fore_color);
        WriteChar(hchar, vchar, d_hex[2], fore_color);
        WriteChar(hchar, vchar, d_hex[1], fore_color);
        WriteChar(hchar, vchar, d_hex[0], fore_color);

    //My Pointer
    int i;
    
    vchar = 50;
    hchar = 30;
    
    //Pointer
    int *pTest;
    pTest = (int*)MemoryAddress;//MemoryAddress;//0xa0000220

    for(i=0;i<36;i++)
    {
        Binary2ASCIIHex(*pTest);
        WriteChar(hchar, vchar, d_hex[7], fore_color);
        WriteChar(hchar, vchar, d_hex[6], fore_color);
        WriteChar(hchar, vchar, d_hex[5], fore_color);
        WriteChar(hchar, vchar, d_hex[4], fore_color);
        WriteChar(hchar, vchar, d_hex[3], fore_color);
        WriteChar(hchar, vchar, d_hex[2], fore_color);
        WriteChar(hchar, vchar, d_hex[1], fore_color);
        WriteChar(hchar, vchar, d_hex[0], fore_color);
        
        pTest++;
                
        hchar = hchar + 10;
        
        if(hchar > 360)
        {
            vchar = vchar + 20;
            hchar = 30;
        }
    }
}

void SRAM_Semaphore_Test(void)
{
    int test;
    while(1)
    {
        hchar = 10;
        vchar = 200;

        while(REN70V05_RD(0x300) == -1);
        
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], fore_color);
        WriteChar(hchar, vchar, d_hex[0], fore_color);

        while(REN70V05_WR(0x301, 0x56) == false);
        
        hchar = 60;

        while(REN70V05_RD(0x302) == -1);
        
        Binary2ASCIIHex(mdata_70V05);
        WriteChar(hchar, vchar, d_hex[1], fore_color);
        WriteChar(hchar, vchar, d_hex[0], fore_color);


    }
    
        /*BEBUG - SRAM MEMORY TEST*/
//    uint8_t q;
//    while(1)
//    {
//        hchar = 10;
//        vchar = 200;
//        q = REN70V05_RD(0x300);
//        Binary2ASCIIHex(q);
//        WriteChar(hchar, vchar, d_hex[2], fore_color);
//        WriteChar(hchar, vchar, d_hex[1], fore_color);
//        WriteChar(hchar, vchar, d_hex[0], fore_color);
//    }
    //READ SEMAPHORE VALUE 
    uint8_t tp;
    while(1)
    {
        //pull SEMR low to access the flags
        PORTGbits.RG15 = 0;

        //write a zero to Flag    
        PMADDR = 0;
        PMDIN = 0x0;
        while(PMMODEbits.BUSY == 1);

        ///SEM
        PORTGbits.RG15 = 1;

        //Flush PMDIN 
        PMDIN = 0x55;
        while(PMMODEbits.BUSY == 1);

        ///SEM
        PORTGbits.RG15 = 0;

        //Read the result
        //Dummy read
        tp = PMDIN;
        while(PMMODEbits.BUSY == 1);

        tp = PMDIN;
        while(PMMODEbits.BUSY == 1);

        ///SEM
        PORTGbits.RG15 = 1;

        LED_Port(tp);
    }
    

}

void DisplayUSBData(void)
{
    //BDT base address
    char MyBuffer1[5] = {"BDT:"};
    hchar = 10;
    vchar = 20;
    fore_color = black;
    WriteString(hchar, vchar, MyBuffer1, fore_color);
    Binary2ASCIIHex((int)&BDT[0]);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //U1ADDR
    hchar = hchar + 20;
    char MyBuffer2[8] = {"U1ADDR:"};
    WriteString(hchar, vchar, MyBuffer2, fore_color);
    Binary2ASCIIHex(U1ADDR);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //U1OTGIR
    hchar = 10;
    vchar = 45;
    char MyBuffer2a[9] = {"U1OTGIR:"};
    WriteString(hchar, vchar, MyBuffer2a, fore_color);
    Binary2ASCIIHex(U1OTGIR);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //U1OTGIE
    hchar = hchar + 20;
    char MyBuffer2b[9] = {"U1OTGIE:"};
    WriteString(hchar, vchar, MyBuffer2b, fore_color);
    Binary2ASCIIHex(U1OTGIE);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //UICON
    hchar = 10;
    vchar = vchar + 20;
    char MyBuffer3[7] = {"U1CON:"};
    WriteString(hchar, vchar, MyBuffer3, fore_color);
    Binary2ASCIIHex(U1CON);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //U1STAT
    hchar = hchar + 20;
    char MyBuffer4[15] = {"U1STAT:"};
    WriteString(hchar, vchar, MyBuffer4, fore_color);
    Binary2ASCIIHex(USTATcopy.Val);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    //U1IE
    hchar = hchar + 20;
    char MyBuffer5[15] = {"U1IE:"};
    WriteString(hchar, vchar, MyBuffer5, fore_color);
    Binary2ASCIIHex(U1IE);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = 100;

    char MyBuffer6[15] = {"U1BDTP3:"};
    WriteString(hchar, vchar, MyBuffer6, fore_color);
    Binary2ASCIIHex(U1BDTP3);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = vchar + 20;
    
    char MyBuffer7[15] = {"U1BDTP2:"};
    WriteString(hchar, vchar, MyBuffer7, fore_color);
    Binary2ASCIIHex(U1BDTP2);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);
    
    hchar = 10;
    vchar = vchar + 20;      

    char MyBuffer8[15] = {"U1BDTP1:"};
    WriteString(hchar, vchar, MyBuffer8, fore_color);
    Binary2ASCIIHex(U1BDTP1);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = 180;
    //EP0-0
    char MyBuffer9[15] = {"EP00:"};
    WriteString(hchar, vchar, MyBuffer9, fore_color);
    Binary2ASCIIHex(BDT[0].ADR);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[0].CNT);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[0].STAT.Val);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = vchar + 20;
    //EP0-1
    char MyBuffer10[6] = {"EP01:"};
    WriteString(hchar, vchar, MyBuffer10, fore_color);
    Binary2ASCIIHex(BDT[2].ADR);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[2].CNT);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[2].STAT.Val);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = vchar + 20;
    //EP1-0
    char MyBuffer11[6] = {"EP10:"};
    WriteString(hchar, vchar, MyBuffer11, fore_color);
    Binary2ASCIIHex(BDT[4].ADR);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[4].CNT);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[4].STAT.Val);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    hchar = 10;
    vchar = vchar + 20;
    //EP1-1
    char MyBuffer12[6] = {"EP11:"};
    WriteString(hchar, vchar, MyBuffer12, fore_color);
    Binary2ASCIIHex(BDT[6].ADR);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[6].CNT);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    WriteChar(hchar, vchar, ':', fore_color);

    Binary2ASCIIHex(BDT[6].STAT.Val);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);

    vchar = vchar + 30;
    hchar = 10;
    
    //Pointer
    int *pTest;
    pTest = (int*)0xa0000220;

    Binary2ASCIIHex(*pTest);
    WriteChar(hchar, vchar, d_hex[7], fore_color);
    WriteChar(hchar, vchar, d_hex[6], fore_color);
    WriteChar(hchar, vchar, d_hex[5], fore_color);
    WriteChar(hchar, vchar, d_hex[4], fore_color);
    WriteChar(hchar, vchar, d_hex[3], fore_color);
    WriteChar(hchar, vchar, d_hex[2], fore_color);
    WriteChar(hchar, vchar, d_hex[1], fore_color);
    WriteChar(hchar, vchar, d_hex[0], fore_color);
}