<file_sep>#ifndef _ra8875_h
#define _ra8875_h

#define LCD_PORT PORTD
#define LCD_LAT  LATD
#define LCD_TRIS TRISD

#define RESET LATCbits.LATC13
#define RESET_TRIS TRISCbits.TRISC13
#define RESET_LOW()        RESET = 0
#define RESET_HIGH()       RESET = 1

#define C86 LATCbits.LATC14
#define C86_TRIS TRISCbits.TRISC14
#define C86_LOW()        C86 = 0
#define C86_HIGH()       C86 = 1

#define RD LATDbits.LATD8
#define RD_TRIS TRISDbits.TRISD8
#define RD_LOW()        RD = 0
#define RD_HIGH()       RD = 1

#define RS LATDbits.LATD11
#define RS_TRIS TRISDbits.TRISD11
#define RS_LOW()        RS = 0
#define RS_HIGH()       RS = 1

#define WR LATDbits.LATD9
#define WR_TRIS TRISDbits.TRISD9
#define WR_LOW()        WR = 0
#define WR_HIGH()       WR = 1

#define LCD_CS LATDbits.LATD10
#define LCD_CS_TRIS TRISDbits.TRISD10
#define LCD_CS_LOW()        LCD_CS = 0
#define LCD_CS_HIGH()       LCD_CS = 1

//typedef unsigned int uint;

typedef unsigned char uchar;
typedef unsigned long ulong;


#define RA8875_DCR                    0x90
#define RA8875_DCR_LINESQUTRI_START   0x80
#define RA8875_DCR_LINESQUTRI_STOP    0x00
#define RA8875_DCR_LINESQUTRI_STATUS  0x80
#define RA8875_DCR_CIRCLE_START       0x40
#define RA8875_DCR_CIRCLE_STATUS      0x40
#define RA8875_DCR_CIRCLE_STOP        0x00
#define RA8875_DCR_FILL               0x20
#define RA8875_DCR_NOFILL             0x00
#define RA8875_DCR_DRAWLINE           0x00
#define RA8875_DCR_DRAWTRIANGLE       0x01
#define RA8875_DCR_DRAWSQUARE         0x10

#define RA8875_P1CR             0x8A
#define RA8875_P1CR_ENABLE      0x80
#define RA8875_P1CR_DISABLE     0x00
#define RA8875_P1CR_CLKOUT      0x10
#define RA8875_P1CR_PWMOUT      0x00

#define RA8875_P1DCR            0x8B

#define RA8875_P2CR             0x8C
#define RA8875_P2CR_ENABLE      0x80
#define RA8875_P2CR_DISABLE     0x00
#define RA8875_P2CR_CLKOUT      0x10
#define RA8875_P2CR_PWMOUT      0x00

#define RA8875_P2DCR            0x8D

#define RA8875_PWM_CLK_DIV1     0x00
#define RA8875_PWM_CLK_DIV2     0x01
#define RA8875_PWM_CLK_DIV4     0x02
#define RA8875_PWM_CLK_DIV8     0x03
#define RA8875_PWM_CLK_DIV16    0x04
#define RA8875_PWM_CLK_DIV32    0x05
#define RA8875_PWM_CLK_DIV64    0x06
#define RA8875_PWM_CLK_DIV128   0x07
#define RA8875_PWM_CLK_DIV256   0x08
#define RA8875_PWM_CLK_DIV512   0x09
#define RA8875_PWM_CLK_DIV1024  0x0A
#define RA8875_PWM_CLK_DIV2048  0x0B
#define RA8875_PWM_CLK_DIV4096  0x0C
#define RA8875_PWM_CLK_DIV8192  0x0D
#define RA8875_PWM_CLK_DIV16384 0x0E
#define RA8875_PWM_CLK_DIV32768 0x0F

#define color_brown   0x40c0
#define color_black   0x0000
#define color_white   0xffff
#define color_red     0xf800
#define color_green   0x07e0
#define color_blue    0x001f
#define color_yellow  color_red|color_green
#define color_cyan    color_green|color_blue
#define color_purple  color_red|color_blue


void Delay1ms(unsigned int i);
void Delay10ms(unsigned int i);
void Delay100ms(unsigned int i);
void NextStep(void);

void LCD_CmdWrite(unsigned char cmd);
void LCD_DataWrite(unsigned char Data);
unsigned char LCD_DataRead(void);

void Write_Dir(unsigned char Cmd,unsigned char Data);
unsigned char LCD_StatusRead(void);

void LCD_Reset(void);
void Chk_Busy(void);
void Chk_BTE_Busy(void);
void Chk_DMA_Busy(void);
void PLL_ini(void);
void Active_Window(unsigned int XL,unsigned int XR ,unsigned int YT ,unsigned int YB);
void LCD_Initial(void);
void Text_Background_Color1(unsigned int b_color);
void Text_Background_Color(unsigned char setR, uchar setG, uchar setB);
void Text_Foreground_Color1(unsigned int b_color);
void Text_Foreground_Color(unsigned char setR,uchar setG,uchar setB);
void BTE_Size(unsigned int width, unsigned int height);
void BTE_Source(unsigned int SX,unsigned int DX ,unsigned int SY ,unsigned int DY);
void MemoryWrite_Position(unsigned int X,unsigned int Y);
void FontWrite_Position(unsigned int X,unsigned int Y);
void String(unsigned char *str);
void Scroll_Window(unsigned int XL,unsigned int XR ,unsigned int YT ,unsigned int YB);
void Scroll(unsigned int X,unsigned int Y);
void DMA_block_mode_size_setting(unsigned int BWR,unsigned int BHR,unsigned int SPWR);
void DMA_Start_address_setting(ulong set_address);
void  Draw_Circle(unsigned int X,unsigned int Y,unsigned int R);
void  Draw_Ellipse(unsigned int X,unsigned int Y,unsigned int R1,unsigned int R2);
void Draw_Line(unsigned int XS,unsigned int XE ,unsigned int YS,unsigned int YE);
void Draw_Triangle(unsigned int X3,unsigned int Y3);
char Touch_Status(void);
char Chk_INT(void);
char Chk_INT2(void);
unsigned char ADC_X(void);
unsigned char ADC_Y(void);
unsigned char ADC_XY(void);
void TP(void);
void Displaypicture(unsigned char picnum);
void CutPictrue(unsigned char picnum,unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned long x,unsigned long y);
void Test(void);

void MakeCircle(unsigned int x0, unsigned int y0, unsigned int r, unsigned int color, char filled);
char waitPoll(uchar regname, uchar waitflag);
void fillRect(void);
void drawRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int color, char filled);
void drawLine(unsigned int x0, unsigned int x1, unsigned int y0, unsigned int y1, unsigned int color);

#define FILL 1
#define NOFILL 0

typedef struct _Button_ {
    unsigned int top;
    unsigned int left;
    unsigned int width;
    unsigned int height;
} Button;


typedef struct _Fader_ {
    unsigned int    bar_value;
    unsigned int    bar_max;

    unsigned int    sel_width;
    unsigned int    sel_height;
    unsigned int    sel_color;

    unsigned int    bar_x;
    unsigned int    bar_y;
    unsigned int    bar_width;
    unsigned int    bar_height;
    unsigned int    bar_color;
    
} Fader;

typedef struct _TouchButton_ {
    unsigned int top;
    unsigned int left;
    unsigned int width;
    unsigned int height;
    unsigned int fore_color;
    unsigned int back_color;
    char text[13];
} TouchButton;

typedef struct _ImageButton_ {
    unsigned int top;
    unsigned int left;
    unsigned int width;
    unsigned int height;
    char up[13];
    char down[13];
} ImageButton;

//BELOW ARE ATOMSOFT TECH MODS
void PWM2config(char on, uchar clock);
void PWM1config(char on, uchar clock);
void PWM2out(uchar p);
void PWM1out(uchar p);

void ClearScreen(char area);
void Display(char val);
void Layers(char val);
void FontSize (char size);
void SetColors(unsigned int f_color, unsigned int b_color);
void GraphicCursor(unsigned int x, unsigned int y, uchar color1, uchar color2);
void Backlight(uchar div, uchar pwm);
void DrawLine (unsigned int xs,unsigned int ys,unsigned int xe,unsigned int ye, unsigned int color);
void DrawSquare ( unsigned int x,unsigned int y,unsigned int w,unsigned int h, unsigned int color, char fill);
void DrawCircle(unsigned int x0, unsigned int y0, unsigned int r, unsigned int color, char filled);
void LCD_ReadBuff(char *buff, unsigned int len);
void SetGraphicsCursorRead(unsigned int x, unsigned int y);
void SetGraphicsCursorWrite(unsigned int x, unsigned int y);
void WriteCommandW(uchar command, unsigned int data);
void WriteCommand(unsigned char command, unsigned int data);
void putPixelStream(uchar * p, unsigned int count, unsigned int x, unsigned int y);
void OpenASI (char *filename, unsigned int x, unsigned int y);
void ReplaceASI (char *filename, unsigned int x, unsigned int y, unsigned int w, unsigned int h);
char isImageButton (ImageButton btn);
void BtnClick(Button btn);
void DrawFinger(unsigned int x,unsigned int y,unsigned int colour);
#endif<file_sep>#include "fubsd.h"
#include <xc.h>
#include <plib.h>
#include "i2c2.h"

void delay1(int time)
{
    unsigned int j;
    while(time--)
        for(j=0;j<48;j++);
}
void delay2()
{
    unsigned char j;
    for(j=0;j<4;j++);
}
char sda_setup(void)
{
    SDA_HIGH;
    delay1(5);
    return isSDA;
}
//IIC start
void I2C_Start(void)
{
	SDA_HIGH;
	delay2();
	SCL_HIGH;
	delay1(5);
	SDA_LOW;
	delay1(5);
	SCL_LOW;
	delay2();
}
//IIC stop
void I2C_Stop(void)
{
	SDA_LOW;
	delay2();
	SCL_HIGH;
	delay1(5);
	SDA_HIGH;
	delay1(5);
	SCL_LOW;
	delay2();
}
//Wait for an answer signal
unsigned char I2C_Wait_Ack(void)
{	unsigned char errtime=0;

	SDA_HIGH;
	delay1(1);
	SCL_HIGH;
	delay1(1);
  	while(isSDA == 1)
	{
	    errtime++;
	    if(errtime>250)
            {
              I2C_Stop();
              return 1;
            }
	}
	SCL_LOW;
	delay2();
}
//Acknowledge
void I2C_Ack(void)
{	SCL_LOW;
	SDA_LOW;
	delay1(2);
	SCL_HIGH;
	delay1(2);
	SCL_LOW;
	delay2();
}
//NO Acknowledge
void I2C_NAck(void)
{	SCL_LOW;
	SDA_HIGH;
	delay1(2);
	SCL_HIGH;
	delay1(2);
	SCL_LOW;
	delay2();
}
void I2C_Send_Byte(unsigned char data)
{	unsigned char t;

    for(t=0;t<8;t++)
    {
        SCL_LOW;

        if((data & 0x80) > 0)
            SDA_HIGH;
        else
            SDA_LOW;

        data <<=1;
        delay1(2);
        SCL_HIGH;
        delay1(2);
        SCL_LOW;
        delay1(2);
    }


}
unsigned char I2C_Read_Byte(unsigned char ack)
{
    unsigned char t,receive=0;

    SCL_LOW;
    SDA_HIGH;
    for(t = 0; t < 8; t++)
    {
        delay2();
        SCL_HIGH;
        delay2();

        receive <<= 1;

        if(isSDA == 1)
            receive |= 0x01;

        delay1(2);
        SCL_LOW;
        delay1(2);
    }


    if (ack)  I2C_NAck();//NO Acknowledge
    else       I2C_Ack(); //Send Acknowledge

     return receive;
}
void I2C_Wr_Reg(unsigned char WRADD, unsigned char RegIndex,unsigned char RegValue1)
{
    I2C_Start();
    I2C_Send_Byte(WRADD);
    I2C_Wait_Ack();
    I2C_Send_Byte(RegIndex);
    I2C_Wait_Ack();

    I2C_Send_Byte(RegValue1);
    I2C_Wait_Ack();

    I2C_Stop();
    delay1(100);
}
unsigned char I2C_Read_Reg(unsigned char WRADD, unsigned char RegIndex)
{
    unsigned char receive=0;

    I2C_Start();
    I2C_Send_Byte(WRADD);
    I2C_Wait_Ack();
    I2C_Send_Byte(RegIndex);
    I2C_Wait_Ack();

    I2C_Start();
    I2C_Send_Byte((WRADD|1));
    I2C_Wait_Ack();
    receive=I2C_Read_Byte(1);
    I2C_Stop();

    return receive;
}

<file_sep>/* 
 * File:   keyboard.h
 * Author: Jason
 *
 * Created on July 7, 2014, 2:10 PM
 *
 *
    BACKSPACE = 0x08
    TAB = 0x09
    CAP = 0x1c
    SHIFT = 0x1d
    ENTER = 0x0D
    SPACE = 0x20
 *
 * 
 */
#ifndef KEYBOARD_H
#define	KEYBOARD_H

#include "ft5206.h"

typedef struct _KB_KEY_ {
    unsigned int top;
    unsigned int left;
    unsigned int width;
    unsigned int height;
    char ascii;
} KB_Key;

typedef struct _Keyboard_ {
    unsigned int top;
    unsigned int left;
    unsigned int width;
    unsigned int text_color;
    unsigned int back_color;
    unsigned int edge_color;
    unsigned int key_width;
    unsigned int key_height;
    unsigned int height;
    char caps;
    char shift;
    KB_Key keys[128];
} Keyboard;

#define CAPS 2
#define SHIFT 1
#define NOSHIFT 0

Keyboard draw_kb(char isShift, unsigned int left, unsigned int top, unsigned int width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor);
Keyboard draw_no_shift(unsigned int left, unsigned int top, unsigned int width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor);
Keyboard draw_shift(unsigned int left, unsigned int top, unsigned int width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor);
unsigned int draw_keys(unsigned int top, unsigned int left, char key_scale, char *text);
unsigned int getLen (char *text);

char isKey(Keyboard kb, unsigned int left, unsigned int top);
#endif	/* KEYBOARD_H */

<file_sep>#include "keyboard.h"
#include "RA8875.h"

#include "main.h"

unsigned int kb_back = 0xffff;
unsigned int kb_edge = 0x0000;
unsigned int kb_color = 0x0000;

unsigned int kb_width = 0;
unsigned int kb_height = 0;
unsigned int key_width = 0;
unsigned int key_height = 0;
unsigned int space_width = 0;
unsigned int key_top = 0;
unsigned int key_left = 0;

char isKey(Keyboard kb, unsigned int i_left, unsigned int i_top)
{
    unsigned int keycount = 128;
    unsigned int x;

    char isInX = 0;
    char isInY = 0;
    char KeyFound = 0;

    char keya;

    for(x=0;x<keycount;x++)
    {
        if(i_left >= kb.keys[x].left)
        {
            if(i_left <= (kb.keys[x].left+kb.keys[x].width))
            {
                isInY = 1;
            }
        }

        if(i_top >= kb.keys[x].top)
        {
            if(i_top <= (kb.keys[x].top+kb.keys[x].height))
            {
                isInX = 1;
            }
        }

        if((isInX + isInY) > 1)
            KeyFound = 1;

        isInX = isInY = 0;
        if(KeyFound > 0)
        {
            keya = (char)kb.keys[x].ascii;
            return keya;
        }
    }

    return 0;
}

Keyboard draw_kb(char isShift, unsigned int left, unsigned int top, unsigned int width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor)
{
    Keyboard tempKB2;

    if(isShift > 0)
    {
         tempKB2 = draw_shift(left,top,width,ecolor,bcolor,tcolor);
    }
    else
    {
        tempKB2 = draw_no_shift(left,top,width,ecolor,bcolor,tcolor);
    }

    tempKB2.shift = 0;
    tempKB2.caps = 0;
    tempKB2.left = left;
    tempKB2.top = top;
    
    if(isShift == SHIFT)
        tempKB2.shift = 1;
    if(isShift == CAPS)
        tempKB2.caps = 1;

    
    return tempKB2;
}

Keyboard draw_no_shift(unsigned int left, unsigned int top, unsigned int width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor)
{
    Keyboard tempKB;
    unsigned int next_y = left;
    int x;
    key_top = top;
    key_left = left;

    //LCD_Reset();
    tempKB.back_color = bcolor;
    tempKB.edge_color = ecolor;
    tempKB.text_color = tcolor;

    kb_back = bcolor;
    kb_edge = ecolor;
    kb_color = tcolor;

    tempKB.width = width;
    tempKB.key_width = width/15;
    tempKB.key_height = tempKB.key_width;
    tempKB.height = tempKB.key_height * 5;
    tempKB.left = left;
    tempKB.top = top;

    kb_width = width;
    key_width = kb_width / 15;
    key_height = key_width;
    kb_height = key_height * 5;

    for (x=0;x<128;x++)
    {
        tempKB.keys[x].width = key_width;
        tempKB.keys[x].height = key_height;
    }

    tempKB.keys[0].top = top;
    tempKB.keys[0].left = next_y;
    tempKB.keys[0].ascii = 0x60;
    next_y = draw_keys(tempKB.keys[0].left, tempKB.keys[0].top, 1, (char*)"`\0");

    tempKB.keys[1].top = top;
    tempKB.keys[1].left = next_y;
    tempKB.keys[1].ascii = 0x31;
    next_y = draw_keys(tempKB.keys[1].left, tempKB.keys[1].top, 1, (char*)"1\0");

    tempKB.keys[2].top = top;
    tempKB.keys[2].left = next_y;
    tempKB.keys[2].ascii = 0x32;
    next_y = draw_keys(tempKB.keys[2].left, tempKB.keys[2].top, 1, (char*)"2\0");

    tempKB.keys[3].top = top;
    tempKB.keys[3].left = next_y;
    tempKB.keys[3].ascii = 0x33;
    next_y = draw_keys(tempKB.keys[3].left, tempKB.keys[3].top, 1, (char*)"3\0");

    tempKB.keys[4].top = top;
    tempKB.keys[4].left = next_y;
    tempKB.keys[4].ascii = 0x34;
    next_y = draw_keys(tempKB.keys[4].left, tempKB.keys[4].top, 1, (char*)"4\0");

    tempKB.keys[5].top = top;
    tempKB.keys[5].left = next_y;
    tempKB.keys[5].ascii = 0x35;
    next_y = draw_keys(tempKB.keys[5].left, tempKB.keys[5].top, 1, (char*)"5\0");

    tempKB.keys[6].top = top;
    tempKB.keys[6].left = next_y;
    tempKB.keys[6].ascii = 0x36;
    next_y = draw_keys(tempKB.keys[6].left, tempKB.keys[6].top, 1, (char*)"6\0");

    tempKB.keys[7].top = top;
    tempKB.keys[7].left = next_y;
    tempKB.keys[7].ascii = 0x37;
    next_y = draw_keys(tempKB.keys[7].left, tempKB.keys[7].top, 1, (char*)"7\0");

    tempKB.keys[8].top = top;
    tempKB.keys[8].left = next_y;
    tempKB.keys[8].ascii = 0x38;
    next_y = draw_keys(tempKB.keys[8].left, tempKB.keys[8].top, 1, (char*)"8\0");

    tempKB.keys[9].top = top;
    tempKB.keys[9].left = next_y;
    tempKB.keys[9].ascii = 0x39;
    next_y = draw_keys(tempKB.keys[9].left, tempKB.keys[9].top, 1, (char*)"9\0");

    tempKB.keys[10].top = top;
    tempKB.keys[10].left = next_y;
    tempKB.keys[10].ascii = 0x30;
    next_y = draw_keys(tempKB.keys[10].left, tempKB.keys[10].top, 1, (char*)"0\0");

    tempKB.keys[11].top = top;
    tempKB.keys[11].left = next_y;
    tempKB.keys[11].ascii = 0x2d;
    next_y = draw_keys(tempKB.keys[11].left, tempKB.keys[11].top, 1, (char*)"-\0");

    tempKB.keys[12].top = top;
    tempKB.keys[12].left = next_y;
    tempKB.keys[12].ascii = 0x3d;
    next_y = draw_keys(tempKB.keys[12].left, tempKB.keys[12].top, 1, (char*)"=\0");

    tempKB.keys[13].top = top;
    tempKB.keys[13].width = key_width * 2;
    tempKB.keys[13].left = next_y;
    tempKB.keys[13].ascii = 0x08;
    next_y = draw_keys(tempKB.keys[13].left, tempKB.keys[13].top, 2, (char*)"BKSP\0");

    next_y = left;
    top += key_height;
    tempKB.keys[14].top = top;
    tempKB.keys[14].left = next_y;
    tempKB.keys[14].ascii = 0x09;
    tempKB.keys[14].width = key_width * 2;
    next_y = draw_keys(tempKB.keys[14].left, tempKB.keys[14].top, 2, (char*)"TAB\0");

    tempKB.keys[15].top = top;
    tempKB.keys[15].left = next_y;
    tempKB.keys[15].ascii = 0x71;
    next_y = draw_keys(tempKB.keys[15].left, tempKB.keys[15].top, 1, (char*)"q\0");

    tempKB.keys[16].top = top;
    tempKB.keys[16].left = next_y;
    tempKB.keys[16].ascii = 0x77;
    next_y = draw_keys(tempKB.keys[16].left, tempKB.keys[16].top, 1, (char*)"w\0");

    tempKB.keys[17].top = top;
    tempKB.keys[17].left = next_y;
    tempKB.keys[17].ascii = 0x65;
    next_y = draw_keys(tempKB.keys[17].left, tempKB.keys[17].top, 1, (char*)"e\0");

    tempKB.keys[18].top = top;
    tempKB.keys[18].left = next_y;
    tempKB.keys[18].ascii = 0x72;
    next_y = draw_keys(tempKB.keys[18].left, tempKB.keys[18].top, 1, (char*)"r\0");

    tempKB.keys[19].top = top;
    tempKB.keys[19].left = next_y;
    tempKB.keys[19].ascii = 0x74;
    next_y = draw_keys(tempKB.keys[19].left, tempKB.keys[19].top, 1, (char*)"t\0");

    tempKB.keys[20].top = top;
    tempKB.keys[20].left = next_y;
    tempKB.keys[20].ascii = 0x79;
    next_y = draw_keys(tempKB.keys[20].left, tempKB.keys[20].top, 1, (char*)"y\0");

    tempKB.keys[21].top = top;
    tempKB.keys[21].left = next_y;
    tempKB.keys[21].ascii = 0x75;
    next_y = draw_keys(tempKB.keys[21].left, tempKB.keys[21].top, 1, (char*)"u\0");

    tempKB.keys[22].top = top;
    tempKB.keys[22].left = next_y;
    tempKB.keys[22].ascii = 0x69;
    next_y = draw_keys(tempKB.keys[22].left, tempKB.keys[22].top, 1, (char*)"i\0");

    tempKB.keys[23].top = top;
    tempKB.keys[23].left = next_y;
    tempKB.keys[23].ascii = 0x6f;
    next_y = draw_keys(tempKB.keys[23].left, tempKB.keys[23].top, 1, (char*)"o\0");

    tempKB.keys[24].top = top;
    tempKB.keys[24].left = next_y;
    tempKB.keys[24].ascii = 0x70;
    next_y = draw_keys(tempKB.keys[24].left, tempKB.keys[24].top, 1, (char*)"p\0");

    tempKB.keys[25].top = top;
    tempKB.keys[25].left = next_y;
    tempKB.keys[25].ascii = 0x5b;
    next_y = draw_keys(tempKB.keys[25].left, tempKB.keys[25].top, 1, (char*)"[\0");

    tempKB.keys[26].top = top;
    tempKB.keys[26].left = next_y;
    tempKB.keys[26].ascii = 0x5d;
    next_y = draw_keys(tempKB.keys[26].left, tempKB.keys[26].top, 1, (char*)"]\0");

    tempKB.keys[27].top = top;
    tempKB.keys[27].left = next_y;
    tempKB.keys[27].ascii = 0x5c;
    next_y = draw_keys(tempKB.keys[27].left, tempKB.keys[27].top, 1, (char*)"\\\0");

    next_y = left;
    top += key_height;
    tempKB.keys[28].top = top;
    tempKB.keys[28].left = next_y;
    tempKB.keys[28].width = key_width * 2;
    tempKB.keys[28].ascii = 0x1c;
    next_y = draw_keys(tempKB.keys[28].left, tempKB.keys[28].top, 2, (char*)"CAPS\0");

    tempKB.keys[29].top = top;
    tempKB.keys[29].left = next_y;
    tempKB.keys[29].ascii = 0x61;
    next_y = draw_keys(tempKB.keys[29].left, tempKB.keys[29].top, 1, (char*)"a\0");

    tempKB.keys[30].top = top;
    tempKB.keys[30].left = next_y;
    tempKB.keys[30].ascii = 0x73;
    next_y = draw_keys(tempKB.keys[30].left, tempKB.keys[30].top, 1, (char*)"s\0");

    tempKB.keys[31].top = top;
    tempKB.keys[31].left = next_y;
    tempKB.keys[31].ascii = 0x64;
    next_y = draw_keys(tempKB.keys[31].left, tempKB.keys[31].top, 1, (char*)"d\0");

    tempKB.keys[32].top = top;
    tempKB.keys[32].left = next_y;
    tempKB.keys[32].ascii = 0x66;
    next_y = draw_keys(tempKB.keys[32].left, tempKB.keys[32].top, 1, (char*)"f\0");

    tempKB.keys[33].top = top;
    tempKB.keys[33].left = next_y;
    tempKB.keys[33].ascii = 0x67;
    next_y = draw_keys(tempKB.keys[33].left, tempKB.keys[33].top, 1, (char*)"g\0");

    tempKB.keys[34].top = top;
    tempKB.keys[34].left = next_y;
    tempKB.keys[34].ascii = 0x68;
    next_y = draw_keys(tempKB.keys[34].left, tempKB.keys[34].top, 1, (char*)"h\0");

    tempKB.keys[35].top = top;
    tempKB.keys[35].left = next_y;
    tempKB.keys[35].ascii = 0x6a;
    next_y = draw_keys(tempKB.keys[35].left, tempKB.keys[35].top, 1, (char*)"j\0");

    tempKB.keys[36].top = top;
    tempKB.keys[36].left = next_y;
    tempKB.keys[36].ascii = 0x6b;
    next_y = draw_keys(tempKB.keys[36].left, tempKB.keys[36].top, 1, (char*)"k\0");

    tempKB.keys[37].top = top;
    tempKB.keys[37].left = next_y;
    tempKB.keys[37].ascii = 0x6c;
    next_y = draw_keys(tempKB.keys[37].left, tempKB.keys[37].top, 1, (char*)"l\0");

    tempKB.keys[38].top = top;
    tempKB.keys[38].left = next_y;
    tempKB.keys[38].ascii = 0x3b;
    next_y = draw_keys(tempKB.keys[38].left, tempKB.keys[38].top, 1, (char*)";\0");

    tempKB.keys[39].top = top;
    tempKB.keys[39].left = next_y;
    tempKB.keys[39].ascii = 0x27;
    next_y = draw_keys(tempKB.keys[39].left, tempKB.keys[39].top, 1, (char*)"'\0");

    tempKB.keys[40].top = top;
    tempKB.keys[40].left = next_y;
    tempKB.keys[40].ascii = 0x0d;
    tempKB.keys[40].width = key_width * 2;
    next_y = draw_keys(tempKB.keys[40].left, tempKB.keys[40].top, 2, (char*)"ENTER\0");

    next_y = left+(tempKB.key_width/2);
    top += key_height;
    tempKB.keys[41].top = top;
    tempKB.keys[41].left = next_y;
    tempKB.keys[41].ascii = 0x1D;
    tempKB.keys[41].width = key_width * 2;
    next_y = draw_keys(tempKB.keys[41].left, tempKB.keys[41].top, 2, (char*)"SHIFT\0");

    tempKB.keys[42].top = top;
    tempKB.keys[42].left = next_y;
    tempKB.keys[42].ascii = 0x7a;
    next_y = draw_keys(tempKB.keys[42].left, tempKB.keys[42].top, 1, (char*)"z\0");

    tempKB.keys[43].top = top;
    tempKB.keys[43].left = next_y;
    tempKB.keys[43].ascii = 0x78;
    next_y = draw_keys(tempKB.keys[43].left, tempKB.keys[43].top, 1, (char*)"x\0");

    tempKB.keys[44].top = top;
    tempKB.keys[44].left = next_y;
    tempKB.keys[44].ascii = 0x63;
    next_y = draw_keys(tempKB.keys[44].left, tempKB.keys[44].top, 1, (char*)"c\0");

    tempKB.keys[45].top = top;
    tempKB.keys[45].left = next_y;
    tempKB.keys[45].ascii = 0x76;
    next_y = draw_keys(tempKB.keys[45].left, tempKB.keys[45].top, 1, (char*)"v\0");

    tempKB.keys[46].top = top;
    tempKB.keys[46].left = next_y;
    tempKB.keys[46].ascii = 0x62;
    next_y = draw_keys(tempKB.keys[46].left, tempKB.keys[46].top, 1, (char*)"b\0");

    tempKB.keys[47].top = top;
    tempKB.keys[47].left = next_y;
    tempKB.keys[47].ascii = 0x6e;
    next_y = draw_keys(tempKB.keys[47].left, tempKB.keys[47].top, 1, (char*)"n\0");

    tempKB.keys[48].top = top;
    tempKB.keys[48].left = next_y;
    tempKB.keys[48].ascii = 0x6d;
    next_y = draw_keys(tempKB.keys[48].left, tempKB.keys[48].top, 1, (char*)"m\0");

    tempKB.keys[49].top = top;
    tempKB.keys[49].left = next_y;
    tempKB.keys[49].ascii = 0x2c;
    next_y = draw_keys(tempKB.keys[49].left, tempKB.keys[49].top, 1, (char*)",\0");

    tempKB.keys[50].top = top;
    tempKB.keys[50].left = next_y;
    tempKB.keys[50].ascii = 0x2e;
    next_y = draw_keys(tempKB.keys[50].left, tempKB.keys[50].top, 1, (char*)".\0");

    tempKB.keys[51].top = top;
    tempKB.keys[51].left = next_y;
    tempKB.keys[51].ascii = 0x2f;
    next_y = draw_keys(tempKB.keys[51].left, tempKB.keys[51].top, 1, (char*)"/\0");

    tempKB.keys[52].top = top;
    tempKB.keys[52].left = next_y;
    tempKB.keys[52].ascii = 0x1D;
    tempKB.keys[52].width = key_width * 2;
    next_y = draw_keys(tempKB.keys[52].left, tempKB.keys[52].top, 2, (char*)"SHIFT\0");

    top += tempKB.key_height;
    next_y = left;
    tempKB.keys[53].top = top;
    tempKB.keys[53].left = next_y + (tempKB.key_width * 4) + (tempKB.key_width/2);
    tempKB.keys[53].ascii = 0x20;
    tempKB.keys[53].width = key_width * 6;
    next_y = draw_keys(tempKB.keys[53].left, tempKB.keys[53].top, 6, (char*)"SPACE\0");

    return tempKB;
}

Keyboard draw_shift(unsigned int s_left, unsigned int s_top, unsigned int s_width, unsigned int ecolor, unsigned int bcolor, unsigned int tcolor)
{
    Keyboard tempKB3;
    unsigned int next_y = s_left;
    int x;
    key_top = s_top;
    key_left = s_left;

    //LCD_Reset();
    tempKB3.back_color = bcolor;
    tempKB3.edge_color = ecolor;
    tempKB3.text_color = tcolor;

    kb_back = bcolor;
    kb_edge = ecolor;
    kb_color = tcolor;

    tempKB3.width = s_width;
    tempKB3.key_width = s_width/15;
    tempKB3.key_height = tempKB3.key_width;
    tempKB3.height = tempKB3.key_height * 5;
    tempKB3.left = s_left;
    tempKB3.top = s_top;

    kb_width = s_width;
    key_width = kb_width / 15;
    key_height = key_width;
    kb_height = key_height * 5;

    for (x=0;x<128;x++)
    {
        tempKB3.keys[x].width = key_width;
        tempKB3.keys[x].height = key_height;
    }

    tempKB3.keys[0].top = s_top;
    tempKB3.keys[0].left = next_y;
    tempKB3.keys[0].ascii = 0x7e;
    next_y = draw_keys(tempKB3.keys[0].left, tempKB3.keys[0].top, 1,  (char*)"~\0");

    tempKB3.keys[1].top = s_top;
    tempKB3.keys[1].left = next_y;
    tempKB3.keys[1].ascii = 0x21;
    next_y = draw_keys(tempKB3.keys[1].left, tempKB3.keys[1].top, 1,  (char*)"!\0");

    tempKB3.keys[2].top = s_top;
    tempKB3.keys[2].left = next_y;
    tempKB3.keys[2].ascii = 0x40;
    next_y = draw_keys(tempKB3.keys[2].left, tempKB3.keys[2].top, 1,  (char*)"@\0");

    tempKB3.keys[3].top = s_top;
    tempKB3.keys[3].left = next_y;
    tempKB3.keys[3].ascii = 0x23;
    next_y = draw_keys(tempKB3.keys[3].left, tempKB3.keys[3].top, 1,  (char*)"#\0");

    tempKB3.keys[4].top = s_top;
    tempKB3.keys[4].left = next_y;
    tempKB3.keys[4].ascii = 0x24;
    next_y = draw_keys(tempKB3.keys[4].left, tempKB3.keys[4].top, 1,  (char*)"$\0");

    tempKB3.keys[5].top = s_top;
    tempKB3.keys[5].left = next_y;
    tempKB3.keys[5].ascii = 0x25;
    next_y = draw_keys(tempKB3.keys[5].left, tempKB3.keys[5].top, 1,  (char*)"%\0");

    tempKB3.keys[6].top = s_top;
    tempKB3.keys[6].left = next_y;
    tempKB3.keys[6].ascii = 0x5e;
    next_y = draw_keys(tempKB3.keys[6].left, tempKB3.keys[6].top, 1,  (char*)"^\0");

    tempKB3.keys[7].top = s_top;
    tempKB3.keys[7].left = next_y;
    tempKB3.keys[7].ascii = 0x26;
    next_y = draw_keys(tempKB3.keys[7].left, tempKB3.keys[7].top, 1,  (char*)"&\0");

    tempKB3.keys[8].top = s_top;
    tempKB3.keys[8].left = next_y;
    tempKB3.keys[8].ascii = 0x2a;
    next_y = draw_keys(tempKB3.keys[8].left, tempKB3.keys[8].top, 1,  (char*)"*\0");

    tempKB3.keys[9].top = s_top;
    tempKB3.keys[9].left = next_y;
    tempKB3.keys[9].ascii = 0x28;
    next_y = draw_keys(tempKB3.keys[9].left, tempKB3.keys[9].top, 1,  (char*)"(\0");

    tempKB3.keys[10].top = s_top;
    tempKB3.keys[10].left = next_y;
    tempKB3.keys[10].ascii = 0x29;
    next_y = draw_keys(tempKB3.keys[10].left, tempKB3.keys[10].top, 1,  (char*)")\0");

    tempKB3.keys[11].top = s_top;
    tempKB3.keys[11].left = next_y;
    tempKB3.keys[11].ascii = 0x5f;
    next_y = draw_keys(tempKB3.keys[11].left, tempKB3.keys[11].top, 1,  (char*)"_\0");

    tempKB3.keys[12].top = s_top;
    tempKB3.keys[12].left = next_y;
    tempKB3.keys[12].ascii = 0x2b;
    next_y = draw_keys(tempKB3.keys[12].left, tempKB3.keys[12].top, 1,  (char*)"+\0");

    tempKB3.keys[13].top = s_top;
    tempKB3.keys[13].left = next_y;
    tempKB3.keys[13].ascii = 0x08;
    next_y = draw_keys(tempKB3.keys[13].left, tempKB3.keys[13].top, 2,  (char*)"BKSP\0");

    s_top+= tempKB3.key_height;
    next_y = s_left;
    tempKB3.keys[14].top = s_top;
    tempKB3.keys[14].left = next_y;
    tempKB3.keys[14].ascii = 0x09;
    next_y = draw_keys(tempKB3.keys[14].left, tempKB3.keys[14].top, 2,  (char*)"TAB\0");

    tempKB3.keys[15].top = s_top;
    tempKB3.keys[15].left = next_y;
    tempKB3.keys[15].ascii = 0x51;
    next_y = draw_keys(tempKB3.keys[15].left, tempKB3.keys[15].top, 1,  (char*)"Q\0");

    tempKB3.keys[16].top = s_top;
    tempKB3.keys[16].left = next_y;
    tempKB3.keys[16].ascii = 0x57;
    next_y = draw_keys(tempKB3.keys[16].left, tempKB3.keys[16].top, 1,  (char*)"W\0");

    tempKB3.keys[17].top = s_top;
    tempKB3.keys[17].left = next_y;
    tempKB3.keys[17].ascii = 0x45;
    next_y = draw_keys(tempKB3.keys[17].left, tempKB3.keys[17].top, 1,  (char*)"E\0");

    tempKB3.keys[18].top = s_top;
    tempKB3.keys[18].left = next_y;
    tempKB3.keys[18].ascii = 0x52;
    next_y = draw_keys(tempKB3.keys[18].left, tempKB3.keys[18].top, 1,  (char*)"R\0");

    tempKB3.keys[19].top = s_top;
    tempKB3.keys[19].left = next_y;
    tempKB3.keys[19].ascii = 0x54;
    next_y = draw_keys(tempKB3.keys[19].left, tempKB3.keys[19].top, 1,  (char*)"T\0");

    tempKB3.keys[20].top = s_top;
    tempKB3.keys[20].left = next_y;
    tempKB3.keys[20].ascii = 0x59;
    next_y = draw_keys(tempKB3.keys[20].left, tempKB3.keys[20].top, 1,  (char*)"Y\0");

    tempKB3.keys[21].top = s_top;
    tempKB3.keys[21].left = next_y;
    tempKB3.keys[21].ascii = 0x55;
    next_y = draw_keys(tempKB3.keys[21].left, tempKB3.keys[21].top, 1,  (char*)"U\0");

    tempKB3.keys[22].top = s_top;
    tempKB3.keys[22].left = next_y;
    tempKB3.keys[22].ascii = 0x49;
    next_y = draw_keys(tempKB3.keys[22].left, tempKB3.keys[22].top, 1,  (char*)"I\0");

    tempKB3.keys[23].top = s_top;
    tempKB3.keys[23].left = next_y;
    tempKB3.keys[23].ascii = 0x4f;
    next_y = draw_keys(tempKB3.keys[23].left, tempKB3.keys[23].top, 1,  (char*)"O\0");

    tempKB3.keys[24].top = s_top;
    tempKB3.keys[24].left = next_y;
    tempKB3.keys[24].ascii = 0x50;
    next_y = draw_keys(tempKB3.keys[24].left, tempKB3.keys[24].top, 1,  (char*)"P\0");

    tempKB3.keys[25].top = s_top;
    tempKB3.keys[25].left = next_y;
    tempKB3.keys[25].ascii = 0x7b;
    next_y = draw_keys(tempKB3.keys[25].left, tempKB3.keys[25].top, 1,  (char*)"{\0");

    tempKB3.keys[26].top = s_top;
    tempKB3.keys[26].left = next_y;
    tempKB3.keys[26].ascii = 0x7d;
    next_y = draw_keys(tempKB3.keys[26].left, tempKB3.keys[26].top, 1,  (char*)"}\0");

    tempKB3.keys[27].top = s_top;
    tempKB3.keys[27].left = next_y;
    tempKB3.keys[27].ascii = 0x7c;
    next_y = draw_keys(tempKB3.keys[27].left, tempKB3.keys[27].top, 1,  (char*)"|\0");

    s_top+= tempKB3.key_height;
    next_y = s_left;
    tempKB3.keys[28].top = s_top;
    tempKB3.keys[28].left = next_y;
    tempKB3.keys[28].ascii = 0x1c;
    next_y = draw_keys(tempKB3.keys[28].left, tempKB3.keys[28].top, 2,  (char*)"CAPS\0");

    tempKB3.keys[29].top = s_top;
    tempKB3.keys[29].left = next_y;
    tempKB3.keys[29].ascii = 0x41;
    next_y = draw_keys(tempKB3.keys[29].left, tempKB3.keys[29].top, 1,  (char*)"A\0");

    tempKB3.keys[30].top = s_top;
    tempKB3.keys[30].left = next_y;
    tempKB3.keys[30].ascii = 0x53;
    next_y = draw_keys(tempKB3.keys[30].left, tempKB3.keys[30].top, 1,  (char*)"S\0");

    tempKB3.keys[31].top = s_top;
    tempKB3.keys[31].left = next_y;
    tempKB3.keys[31].ascii = 0x44;
    next_y = draw_keys(tempKB3.keys[31].left, tempKB3.keys[31].top, 1,  (char*)"D\0");

    tempKB3.keys[32].top = s_top;
    tempKB3.keys[32].left = next_y;
    tempKB3.keys[32].ascii = 0x46;
    next_y = draw_keys(tempKB3.keys[32].left, tempKB3.keys[32].top, 1,  (char*)"F\0");

    tempKB3.keys[33].top = s_top;
    tempKB3.keys[33].left = next_y;
    tempKB3.keys[33].ascii = 0x47;
    next_y = draw_keys(tempKB3.keys[33].left, tempKB3.keys[33].top, 1,  (char*)"G\0");

    tempKB3.keys[34].top = s_top;
    tempKB3.keys[34].left = next_y;
    tempKB3.keys[34].ascii = 0x48;
    next_y = draw_keys(tempKB3.keys[34].left, tempKB3.keys[34].top, 1,  (char*)"H\0");

    tempKB3.keys[35].top = s_top;
    tempKB3.keys[35].left = next_y;
    tempKB3.keys[35].ascii = 0x4a;
    next_y = draw_keys(tempKB3.keys[35].left, tempKB3.keys[35].top, 1,  (char*)"J\0");

    tempKB3.keys[36].top = s_top;
    tempKB3.keys[36].left = next_y;
    tempKB3.keys[36].ascii = 0x4b;
    next_y = draw_keys(tempKB3.keys[36].left, tempKB3.keys[36].top, 1,  (char*)"K\0");

    tempKB3.keys[37].top = s_top;
    tempKB3.keys[37].left = next_y;
    tempKB3.keys[37].ascii = 0x4c;
    next_y = draw_keys(tempKB3.keys[37].left, tempKB3.keys[37].top, 1,  (char*)"L\0");

    tempKB3.keys[38].top = s_top;
    tempKB3.keys[38].left = next_y;
    tempKB3.keys[38].ascii = 0x3a;
    next_y = draw_keys(tempKB3.keys[38].left, tempKB3.keys[38].top, 1,  (char*)":\0");

    tempKB3.keys[39].top = s_top;
    tempKB3.keys[39].left = next_y;
    tempKB3.keys[39].ascii = 0x22;
    next_y = draw_keys(tempKB3.keys[39].left, tempKB3.keys[39].top, 1,  (char*)"\"\0");

    tempKB3.keys[40].top = s_top;
    tempKB3.keys[40].left = next_y;
    tempKB3.keys[40].ascii = 0x0d;
    next_y = draw_keys(tempKB3.keys[40].left, tempKB3.keys[40].top, 2,  (char*)"ENTER\0");

    s_top+= tempKB3.key_height;
    next_y = s_left+(tempKB3.key_width/2);
    tempKB3.keys[41].top = s_top;
    tempKB3.keys[41].left = next_y;
    tempKB3.keys[41].ascii = 0x1D;
    next_y = draw_keys(tempKB3.keys[41].left, tempKB3.keys[41].top, 2,  (char*)"SHIFT\0");

    tempKB3.keys[42].top = s_top;
    tempKB3.keys[42].left = next_y;
    tempKB3.keys[42].ascii = 0x5a;
    next_y = draw_keys(tempKB3.keys[42].left, tempKB3.keys[42].top, 1,  (char*)"Z\0");

    tempKB3.keys[43].top = s_top;
    tempKB3.keys[43].left = next_y;
    tempKB3.keys[43].ascii = 0x58;
    next_y = draw_keys(tempKB3.keys[43].left, tempKB3.keys[43].top, 1,  (char*)"X\0");

    tempKB3.keys[44].top = s_top;
    tempKB3.keys[44].left = next_y;
    tempKB3.keys[44].ascii = 0x43;
    next_y = draw_keys(tempKB3.keys[44].left, tempKB3.keys[44].top, 1,  (char*)"C\0");

    tempKB3.keys[45].top = s_top;
    tempKB3.keys[45].left = next_y;
    tempKB3.keys[45].ascii = 0x56;
    next_y = draw_keys(tempKB3.keys[45].left, tempKB3.keys[45].top, 1,  (char*)"V\0");

    tempKB3.keys[46].top = s_top;
    tempKB3.keys[46].left = next_y;
    tempKB3.keys[46].ascii = 0x42;
    next_y = draw_keys(tempKB3.keys[46].left, tempKB3.keys[46].top, 1,  (char*)"B\0");

    tempKB3.keys[47].top = s_top;
    tempKB3.keys[47].left = next_y;
    tempKB3.keys[47].ascii = 0x4e;
    next_y = draw_keys(tempKB3.keys[47].left, tempKB3.keys[47].top, 1,  (char*)"N\0");

    tempKB3.keys[48].top = s_top;
    tempKB3.keys[48].left = next_y;
    tempKB3.keys[48].ascii = 0x4d;
    next_y = draw_keys(tempKB3.keys[48].left, tempKB3.keys[48].top, 1,  (char*)"M\0");

    tempKB3.keys[49].top = s_top;
    tempKB3.keys[49].left = next_y;
    tempKB3.keys[49].ascii = 0x3c;
    next_y = draw_keys(tempKB3.keys[49].left, tempKB3.keys[49].top, 1,  (char*)"<\0");

    tempKB3.keys[50].top = s_top;
    tempKB3.keys[50].left = next_y;
    tempKB3.keys[50].ascii = 0x3e;
    next_y = draw_keys(tempKB3.keys[50].left, tempKB3.keys[50].top, 1,  (char*)">\0");

    tempKB3.keys[51].top = s_top;
    tempKB3.keys[51].left = next_y;
    tempKB3.keys[51].ascii = 0x3f;
    next_y = draw_keys(tempKB3.keys[51].left, tempKB3.keys[51].top, 1,  (char*)"?\0");

    tempKB3.keys[52].top = s_top;
    tempKB3.keys[52].left = next_y;
    tempKB3.keys[52].ascii = 0x1D;
    next_y = draw_keys(tempKB3.keys[52].left, tempKB3.keys[52].top, 2,  (char*)"SHIFT\0");

    s_top += tempKB3.key_height;
    next_y = s_left;
    tempKB3.keys[53].top = s_top;
    tempKB3.keys[53].left = next_y + (tempKB3.key_width * 4) + (tempKB3.key_width/2);
    tempKB3.keys[53].ascii = 0x20;
    next_y = draw_keys(tempKB3.keys[53].left, tempKB3.keys[53].top, 6, (char*)"SPACE\0");

    return tempKB3;
}
unsigned int getLen (char *text)
{
    unsigned int len = 0;

    while(*text)
    {
        text++;
        len++;
    }

    return len;
}
unsigned int draw_keys(unsigned int left, unsigned int top, char key_scale, char *text)
{
    unsigned int keyW = key_width*key_scale;
    unsigned int next_left = left + keyW;
    unsigned int tLeft, tTop;
    unsigned int tLen;


    
    tLen = getLen(text) * 6;
    tLeft = (keyW / 2) - (tLen / 2) + left - 3;
    tTop = ((key_height / 2) - 7) + top;

    
    drawRect(left+1, top+1, keyW-2, key_height-2, kb_back, 1); delay_us(100);//delay_us(40);
    drawRect(left, top, keyW, key_height, kb_edge, 0); delay_us(100);


    FontWrite_Position(tLeft,tTop);
    SetColors(kb_color,kb_back);

    String(text);
    
    return next_left;
}<file_sep>//----------------------------------------------------------------------
//EASTRISING TECHNOLOGY CO,.LTD.//
// Module    : ER-TFTM070-5  7.0 INCH TFT LCD  800*480   7 INCH  Resistive Touch Screen
// Lanuage   : C51 Code
// Create    : <NAME>
// Date      : Nov-30-2013
// Drive IC  : RA8875    Font chip  23L32S4W    Flash:128M char
// INTERFACE : 4-Wire SPI
// MCU 		 : STC12LE5C60S2  //51 series  1T MCU
// MCU VDD		 : 3.3V   
//----------------------------------------------------------------------
#include "main.h"
#include <xc.h>
#include <plib.h>

#include "spi2.h"
#include "ra8875.h"
#include "ff.h"
#include "diskio.h"
#include "ft5206.h"


#define write_data_addr  0x0c  //slave addresses with write data
#define read_data_addr  0x0d  //slave addresses with write data
#define write_cmd_addr  0x0e  //slave addresses with write command
#define read_cmd_addr  0x0f  //slave addresses with read status

#define SPI_Write(var) SpiTransfer(var)
#define SPI_Read() SpiTransfer(0xFF)

unsigned int X1,Y1,X2,Y2,X3,Y3,X4,Y4;
unsigned char taby[4];
unsigned char tabx[4];
unsigned int x[6],y[6],xmin,ymin,xmax,ymax;

volatile uchar IMAGE_BUFF[IMAGEBUFF_LEN];

void Delay1ms(unsigned int i)
{
    while(i--)
        delay_ms(1);
}

void Delay10ms(unsigned int i)
{
    while(i--)
        delay_ms(10);
}

void Delay100ms(unsigned int i)
{
    while(i--)
	delay_ms(100);
}

void Delay1us(unsigned int i)
{
   delay_us(i);
}
void NextStep(void)
{
     delay_ms(20);
}


//////////////SPI Write command
void LCD_CmdWrite(unsigned char cmd)
{	
	  RD = 1;
	  LCD_CS = 0;
	  RS = 1;
          LCD_LAT &= 0xFF00;
	  LCD_LAT |= cmd;
	  WR = 0;
	  WR = 1;
	  LCD_CS= 1;
	  LCD_LAT &= 0xFF00;
	  RS = 0;
}

//////////////SPI Write data or  parameter
void LCD_DataWrite(unsigned char Data)
{
	  RD = 1;       
	  LCD_CS = 0;
	  RS = 0;
          LCD_LAT &= 0xFF00;
	  LCD_LAT |= Data;
	  WR = 0;
	  WR = 1;
	  LCD_CS = 1;
	  LCD_LAT &= 0xFF00;
	  RS = 1;
}

void Write_Dir(unsigned char Cmd,unsigned char Data)
{
    LCD_CmdWrite(Cmd);
    LCD_DataWrite(Data);
}

///////////SPI Read  status
unsigned char LCD_StatusRead(void)
{
	  uchar Data;
	  LCD_TRIS |= 0xFF;
	  LCD_CS = 0;
	  RS= 1;
	  WR = 1;
	  RD = 0;  
	  Data = LCD_PORT;  
	  RD = 1;
	  LCD_CS= 1;
	  LCD_TRIS &= 0xFF00;
	  LCD_LAT &= 0xFF00;
	  RS = 0;
	  return Data;
}

///////////////Read data or  parameter
unsigned char LCD_DataRead(void)
{
	  uchar Data;
	  LCD_TRIS |= 0x00FF;
	  WR = 1;
	  LCD_CS = 0;
	  RS = 0;
	  RD = 0;  
	  Data = LCD_PORT;  
	  RD = 1;
	  LCD_CS = 1;
	  RS = 1;
	  LCD_TRIS &= 0xFF00;
          LCD_LAT &= 0xFF00;
	  return Data;
}

void LCD_Reset(void)
{
	RESET = 0;
	Delay1ms(1);
	RESET = 1;
	Delay1ms(1);
}	

void Chk_Busy(void)
{
	uchar temp; 	
	do
	{
	temp=LCD_StatusRead();
	}while((temp&0x80)==0x80);		   
}
///////////////check bte busy
void Chk_BTE_Busy(void)
{
	uchar temp; 	
	do
	{
	temp=LCD_StatusRead();
	}while((temp&0x40)==0x40);		   
}
///////////////check dma busy
void Chk_DMA_Busy(void)
{
	uchar temp; 	
	do
	{
	LCD_CmdWrite(0xBF);
	temp =LCD_DataRead();
	}while((temp&0x01)==0x01);   
}

/////////////PLL setting
void PLL_ini(void)
{
    LCD_CmdWrite(0x88);      
    LCD_DataWrite(0x0C);
    Delay1ms(1);     
    LCD_CmdWrite(0x89);        
    LCD_DataWrite(0x02);  
    Delay1ms(1);
}	

void Active_Window(unsigned int XL,unsigned int XR ,unsigned int YT ,unsigned int YB)
{
	uchar temp;
    //setting active window X
	temp=XL;   
    LCD_CmdWrite(0x30);//HSAW0
	LCD_DataWrite(temp);
	temp=XL>>8;   
    LCD_CmdWrite(0x31);//HSAW1	   
	LCD_DataWrite(temp);

	temp=XR;   
    LCD_CmdWrite(0x34);//HEAW0
	LCD_DataWrite(temp);
	temp=XR>>8;   
    LCD_CmdWrite(0x35);//HEAW1	   
	LCD_DataWrite(temp);

    //setting active window Y
	temp=YT;   
    LCD_CmdWrite(0x32);//VSAW0
	LCD_DataWrite(temp);
	temp=YT>>8;   
    LCD_CmdWrite(0x33);//VSAW1	   
	LCD_DataWrite(temp);

	temp=YB;   
    LCD_CmdWrite(0x36);//VEAW0
	LCD_DataWrite(temp);
	temp=YB>>8;   
    LCD_CmdWrite(0x37);//VEAW1	   
	LCD_DataWrite(temp);
}




/////////////LCM initial
void LCD_Initial(void)
{

    LCD_TRIS = 0;
    RESET_TRIS = 0;
    C86_TRIS = 0;
    RD_TRIS = 0;
    RS_TRIS = 0;
    WR_TRIS = 0;
    LCD_CS_TRIS = 0;
    
    LCD_Reset();

    PLL_ini();
	LCD_CmdWrite(0x10);	 //
	LCD_DataWrite(0x0C);   //     65k   8bit mcu interface     						 
	
	LCD_CmdWrite(0x04);    //PCLK
	LCD_DataWrite(0x81);   //
	Delay1ms(1);

	 //Horizontal set
	LCD_CmdWrite(0x14); //HDWR//Horizontal Display Width Setting Bit[6:0]                      
	LCD_DataWrite(0x63);//Horizontal display width(pixels) = (HDWR + 1)*8                      
	LCD_CmdWrite(0x15);//Horizontal Non-Display Period Fine Tuning Option Register (HNDFTR)   
	LCD_DataWrite(0x00);//Horizontal Non-Display Period Fine Tuning(HNDFT) [3:0]               
	LCD_CmdWrite(0x16); //HNDR//Horizontal Non-Display Period Bit[4:0]                         
	LCD_DataWrite(0x03);//Horizontal Non-Display Period (pixels) = (HNDR + 1)*8                
	LCD_CmdWrite(0x17); //HSTR//HSYNC Start Position[4:0]                                      
	LCD_DataWrite(0x03);//HSYNC Start Position(PCLK) = (HSTR + 1)*8                            
	LCD_CmdWrite(0x18); //HPWR//HSYNC Polarity ,The period width of HSYNC.                     
	LCD_DataWrite(0x0B);//HSYNC Width [4:0]   HSYNC Pulse width(PCLK) = (HPWR + 1)*8    
	 //Vertical set                             
	LCD_CmdWrite(0x19); //VDHR0 //Vertical Display Height Bit [7:0]                            
	LCD_DataWrite(0xdf);//Vertical pixels = VDHR + 1                                           
	LCD_CmdWrite(0x1a); //VDHR1 //Vertical Display Height Bit [8]                              
	LCD_DataWrite(0x01);//Vertical pixels = VDHR + 1                                           
	LCD_CmdWrite(0x1b); //VNDR0 //Vertical Non-Display Period Bit [7:0]                        
	LCD_DataWrite(0x20);//Vertical Non-Display area = (VNDR + 1)                               
	LCD_CmdWrite(0x1c); //VNDR1 //Vertical Non-Display Period Bit [8]                          
	LCD_DataWrite(0x00);//Vertical Non-Display area = (VNDR + 1)                               
	LCD_CmdWrite(0x1d); //VSTR0 //VSYNC Start Position[7:0]                                    
	LCD_DataWrite(0x16);//VSYNC Start Position(PCLK) = (VSTR + 1)                              
	LCD_CmdWrite(0x1e); //VSTR1 //VSYNC Start Position[8]                                      
	LCD_DataWrite(0x00);//VSYNC Start Position(PCLK) = (VSTR + 1)                              
	LCD_CmdWrite(0x1f); //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]                        
	LCD_DataWrite(0x01);//VSYNC Pulse Width(PCLK) = (VPWR + 1)   
 
    

	Active_Window(0,799,0,479);

	LCD_CmdWrite(0x8a);//PWM setting
	LCD_DataWrite(0x80);
	LCD_CmdWrite(0x8a);//PWM setting
	LCD_DataWrite(0x81);//open PWM
	LCD_CmdWrite(0x8b);//Backlight brightness setting
	LCD_DataWrite(0xff);//Brightness parameter 0xff-0x00
}




///////////////Background color settings
void Text_Background_Color1(unsigned int b_color)
{
	
	LCD_CmdWrite(0x60);//BGCR0
	LCD_DataWrite((uchar)(b_color>>11));
	
	LCD_CmdWrite(0x61);//BGCR0
	LCD_DataWrite((uchar)(b_color>>5));
	
	LCD_CmdWrite(0x62);//BGCR0
	LCD_DataWrite((uchar)(b_color));
} 

///////////////Background color settings
void Text_Background_Color(uchar setR, uchar setG, uchar setB)
{
    LCD_CmdWrite(0x60);//BGCR0
	LCD_DataWrite(setR);
   
    LCD_CmdWrite(0x61);//BGCR0
	LCD_DataWrite(setG);

    LCD_CmdWrite(0x62);//BGCR0
	LCD_DataWrite(setB);
} 

////////////////Foreground color settings
void Text_Foreground_Color1(unsigned int b_color)
{
	
	LCD_CmdWrite(0x63);//BGCR0
	LCD_DataWrite((uchar)(b_color>>11));
	
	LCD_CmdWrite(0x64);//BGCR0
	LCD_DataWrite((uchar)(b_color>>5));
	
	LCD_CmdWrite(0x65);//BGCR0
	LCD_DataWrite((uchar)(b_color));
} 

////////////////Foreground color settings
void Text_Foreground_Color(uchar setR,uchar setG,uchar setB)
{	    
    LCD_CmdWrite(0x63);//BGCR0
	LCD_DataWrite(setR);
   
    LCD_CmdWrite(0x64);//BGCR0
	LCD_DataWrite(setG);

    LCD_CmdWrite(0x65);//BGCR0กค
	LCD_DataWrite(setB);
}
//////////////////BTE area size settings
void BTE_Size(unsigned int width,unsigned int height)
{
    uchar temp;
	temp=width;   
    LCD_CmdWrite(0x5c);//BET area width literacy
	LCD_DataWrite(temp);
	temp=width>>8;   
    LCD_CmdWrite(0x5d);//BET area width literacy	   
	LCD_DataWrite(temp);

	temp=height;   
    LCD_CmdWrite(0x5e);//BET area height literacy
	LCD_DataWrite(temp);
	temp=height>>8;   
    LCD_CmdWrite(0x5f);//BET area height literacy	   
	LCD_DataWrite(temp);
}		

////////////////////BTE starting position
void BTE_Source(unsigned int SX,unsigned int DX ,unsigned int SY ,unsigned int DY)
{
	uchar temp,temp1;
    
	temp=SX;   
    LCD_CmdWrite(0x54);//BTE horizontal position of read/write data
	LCD_DataWrite(temp);
	temp=SX>>8;   
    LCD_CmdWrite(0x55);//BTE horizontal position of read/write data   
	LCD_DataWrite(temp);

	temp=DX;
    LCD_CmdWrite(0x58);//BET written to the target horizontal position
	LCD_DataWrite(temp);
	temp=DX>>8;   
    LCD_CmdWrite(0x59);//BET written to the target horizontal position	   
	LCD_DataWrite(temp); 
    
	temp=SY;   
    LCD_CmdWrite(0x56);//BTE vertical position of read/write data
	LCD_DataWrite(temp);
	temp=SY>>8;   
    LCD_CmdWrite(0x57);
	temp1 = LCD_DataRead();
	temp1 &= 0x80;
    temp=temp|temp1; 
	LCD_CmdWrite(0x57);//BTE vertical position of read/write data  
	LCD_DataWrite(temp);

	temp=DY;   
    LCD_CmdWrite(0x5a);//BET written to the target  vertical  position
	LCD_DataWrite(temp);
	temp=DY>>8;   
    LCD_CmdWrite(0x5b);
	temp1 = LCD_DataRead();
	temp1 &= 0x80;
	temp=temp|temp1;	
	LCD_CmdWrite(0x5b);//BET written to the target  vertical  position 
	LCD_DataWrite(temp);
}				
///////////////Memory write position
void MemoryWrite_Position(unsigned int X,unsigned int Y)
{
	uchar temp;

	temp=X;   
    LCD_CmdWrite(0x46);
	LCD_DataWrite(temp);
	temp=X>>8;   
    LCD_CmdWrite(0x47);	   
	LCD_DataWrite(temp);

	temp=Y;   
    LCD_CmdWrite(0x48);
	LCD_DataWrite(temp);
	temp=Y>>8;   
    LCD_CmdWrite(0x49);	   
	LCD_DataWrite(temp);
}

////////////////Text write position
void FontWrite_Position(unsigned int X,unsigned int Y)
{
	uchar temp;
	temp=X;   
    LCD_CmdWrite(0x2A);
	LCD_DataWrite(temp);
	temp=X>>8;   
    LCD_CmdWrite(0x2B);	   
	LCD_DataWrite(temp);

	temp=Y;   
    LCD_CmdWrite(0x2C);
	LCD_DataWrite(temp);
	temp=Y>>8;   
    LCD_CmdWrite(0x2D);	   
	LCD_DataWrite(temp);
}

//////////////writing text
void String(uchar *str)
{   
    Write_Dir(0x40,0x80);//Set the character mode
	LCD_CmdWrite(0x02);
	while(*str != '\0')
	{
	 LCD_DataWrite(*str);
	 ++str;	 	
	 Chk_Busy();		
	} 
}

		

/////////////////Scroll window size
void Scroll_Window(unsigned int XL,unsigned int XR ,unsigned int YT ,unsigned int YB)
{
	uchar temp;    
	temp=XL;   
    LCD_CmdWrite(0x38);//HSSW0
	LCD_DataWrite(temp);
	temp=XL>>8;   
    LCD_CmdWrite(0x39);//HSSW1	   
	LCD_DataWrite(temp);

	temp=XR;   
    LCD_CmdWrite(0x3c);//HESW0
	LCD_DataWrite(temp);
	temp=XR>>8;   
    LCD_CmdWrite(0x3d);//HESW1	   
	LCD_DataWrite(temp);   
    
	temp=YT;   
    LCD_CmdWrite(0x3a);//VSSW0
	LCD_DataWrite(temp);
	temp=YT>>8;   
    LCD_CmdWrite(0x3b);//VSSW1	   
	LCD_DataWrite(temp);

	temp=YB;   
    LCD_CmdWrite(0x3e);//VESW0
	LCD_DataWrite(temp);
	temp=YB>>8;   
    LCD_CmdWrite(0x3f);//VESW1	   
	LCD_DataWrite(temp);
}  

///////////////Window scroll offset Settings
void Scroll(unsigned int X,unsigned int Y)
{
	uchar temp;
    
	temp=X;   
    LCD_CmdWrite(0x24);//HOFS0
	LCD_DataWrite(temp);
	temp=X>>8;   
    LCD_CmdWrite(0x25);//HOFS1	   
	LCD_DataWrite(temp);

	temp=Y;   
    LCD_CmdWrite(0x26);//VOFS0
	LCD_DataWrite(temp);
	temp=Y>>8;   
    LCD_CmdWrite(0x27);//VOFS1	   
	LCD_DataWrite(temp); 
}	   	  

///////////////The FLASH reading area   setting
void DMA_block_mode_size_setting(unsigned int BWR,unsigned int BHR,unsigned int SPWR)
{
  	LCD_CmdWrite(0xB4);
  	LCD_DataWrite(BWR);
  	LCD_CmdWrite(0xB5);
  	LCD_DataWrite(BWR>>8);

  	LCD_CmdWrite(0xB6);
  	LCD_DataWrite(BHR);
  	LCD_CmdWrite(0xB7);
  	LCD_DataWrite(BHR>>8);

  	LCD_CmdWrite(0xB8);
  	LCD_DataWrite(SPWR);
  	LCD_CmdWrite(0xB9);
  	LCD_DataWrite(SPWR>>8);  
}

/////////////FLASH read start position Settings
void DMA_Start_address_setting(ulong set_address)
{ 
  	LCD_CmdWrite(0xB0);
  	LCD_DataWrite(set_address);

  	LCD_CmdWrite(0xB1);
  	LCD_DataWrite(set_address>>8);

	LCD_CmdWrite(0xB2);
  	LCD_DataWrite(set_address>>16);

  	LCD_CmdWrite(0xB3);
  	LCD_DataWrite(set_address>>24);
}
///////////drawing circle
void  Draw_Circle(unsigned int X,unsigned int Y,unsigned int R)
{
	uchar temp;
    
	temp=X;   
    LCD_CmdWrite(0x99);
	LCD_DataWrite(temp);
	temp=X>>8;   
    LCD_CmdWrite(0x9a);	   
	LCD_DataWrite(temp);  
	  
	temp=Y;   
    LCD_CmdWrite(0x9b);
	LCD_DataWrite(temp);
	temp=Y>>8;   
    LCD_CmdWrite(0x9c);	   
	LCD_DataWrite(temp);

	temp=R;   
    LCD_CmdWrite(0x9d);
	LCD_DataWrite(temp);
} 

///////////drawing elliptic curve
void  Draw_Ellipse(unsigned int X,unsigned int Y,unsigned int R1,unsigned int R2)
{
	uchar temp;    
	temp=X;   
    LCD_CmdWrite(0xA5);
	LCD_DataWrite(temp);
	temp=X>>8;   
    LCD_CmdWrite(0xA6);	   
	LCD_DataWrite(temp);  
	  
	temp=Y;   
    LCD_CmdWrite(0xA7);
	LCD_DataWrite(temp);
	temp=Y>>8;   
    LCD_CmdWrite(0xA8);	   
	LCD_DataWrite(temp);

	temp=R1;   
    LCD_CmdWrite(0xA1);
	LCD_DataWrite(temp);
	temp=R1>>8;   
    LCD_CmdWrite(0xA2);	   
	LCD_DataWrite(temp);  
	  
	temp=R2;   
    LCD_CmdWrite(0xA3);
	LCD_DataWrite(temp);
	temp=R2>>8;   
    LCD_CmdWrite(0xA4);	   
	LCD_DataWrite(temp);
} 

///////////drawing line, rectangle, triangle
void Draw_Line(unsigned int XS,unsigned int XE ,unsigned int YS,unsigned int YE)
{	
    uchar temp;    
	temp=XS;   
    LCD_CmdWrite(0x91);
	LCD_DataWrite(temp);
	temp=XS>>8;   
    LCD_CmdWrite(0x92);	   
	LCD_DataWrite(temp);

	temp=XE;
    LCD_CmdWrite(0x95);
	LCD_DataWrite(temp);
	temp=XE>>8;   
    LCD_CmdWrite(0x96);	   
	LCD_DataWrite(temp);

	temp=YS;   
    LCD_CmdWrite(0x93);
	LCD_DataWrite(temp);
	temp=YS>>8;   
    LCD_CmdWrite(0x94);	   
	LCD_DataWrite(temp);

	temp=YE;   
    LCD_CmdWrite(0x97);
	LCD_DataWrite(temp);
	temp=YE>>8;   
    LCD_CmdWrite(0x98);	   
	LCD_DataWrite(temp);
}

////////////draw a triangle of three point 
void Draw_Triangle(unsigned int X3,unsigned int Y3)
{
    uchar temp;    
	temp=X3;   
    LCD_CmdWrite(0xA9);
	LCD_DataWrite(temp);
	temp=X3>>8;   
    LCD_CmdWrite(0xAA);	   
	LCD_DataWrite(temp);

	temp=Y3;
    LCD_CmdWrite(0xAB);
	LCD_DataWrite(temp);
	temp=Y3>>8;   
    LCD_CmdWrite(0xAC);	   
	LCD_DataWrite(temp);
}
//////////check interrupt flag char
char Chk_INT(void)
{
    unsigned char temp;
    temp=LCD_StatusRead();
    if ((temp&0x20)==0x20)
        return 1;
    else
        return 0;
}

char Chk_INT2(void)
{
    unsigned char temp;
    LCD_CmdWrite(0x74);//INTC
    temp =LCD_DataRead();
    if ((temp&0x80)==0x80)
        return 1;
    else
        return 0;
}

  

////////////Show the picture of the flash
void Displaypicture(unsigned char picnum)
{  
   unsigned char picnumtemp;
   Write_Dir(0X06,0X00);//FLASH frequency setting
   Write_Dir(0X05,0X87);//FLASH setting 

	picnumtemp=picnum;

   Write_Dir(0XBF,0X02);//FLASH setting
   Active_Window(0,799,0,479); 
   MemoryWrite_Position(0,0);//Memory write position
   DMA_Start_address_setting(768000*(picnumtemp-1));//DMA Start address setting
   DMA_block_mode_size_setting(800,480,800);
   Write_Dir(0XBF,0X03);//FLASH setting
	Chk_DMA_Busy();
} 

//////Shear pictures
//Shear pictures number:picnum
//display position:x1,y1,x2,y2
//the upper left of the shear image cooRDinates :x,y
void CutPictrue(unsigned char picnum,unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned long x,unsigned long y)
{
    unsigned long cutaddress;
    unsigned char picnumtemp;
    Write_Dir(0X06,0X00);//FLASH frequency setting   
    Write_Dir(0X05,0Xac);//FLASH setting

    picnumtemp=picnum;

    Write_Dir(0XBF,0X02);//FLASH setting
    Active_Window(x1,x2,y1,y2);
    MemoryWrite_Position(x1,y1);//Memory write position
    cutaddress=(picnumtemp-1)*768000+y*1600+x*2;
    DMA_Start_address_setting(cutaddress);
    DMA_block_mode_size_setting(x2-x1+1,y2-y1+1,800);
    Write_Dir(0XBF,0X03);//FLASH setting
    Chk_DMA_Busy();
}

//full display test
void Test(void)
{	///display red
	Text_Background_Color1(color_red);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display green
	Text_Background_Color1(color_green);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display blue
	Text_Background_Color1(color_blue);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display white
	Text_Background_Color1(color_white);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display cyan
	Text_Background_Color1(color_cyan);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display yellow
	Text_Background_Color1(color_yellow);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display purple
	Text_Background_Color1(color_purple);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
	///display black
	Text_Background_Color1(color_black);//Background color setting
	Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
	NextStep();
}


void drawLine3(unsigned int x0, unsigned int x1, unsigned int y0, unsigned int y1, unsigned int color)
{
  /* Set X */
  LCD_CmdWrite(0x91);
  LCD_DataWrite(x0);
  LCD_CmdWrite(0x92);
  LCD_DataWrite(x0 >> 8);

  /* Set Y */
  LCD_CmdWrite(0x93);
  LCD_DataWrite(y0);
  LCD_CmdWrite(0x94);
  LCD_DataWrite(y0 >> 8);

  /* Set X1 */
  LCD_CmdWrite(0x95);
  LCD_DataWrite(x1);
  LCD_CmdWrite(0x96);
  LCD_DataWrite((x1) >> 8);

  /* Set Y1 */
  LCD_CmdWrite(0x97);
  LCD_DataWrite(y1);
  LCD_CmdWrite(0x98);
  LCD_DataWrite((y1) >> 8);

  /* Set Color */
  LCD_CmdWrite(0x63);
  LCD_DataWrite((color & 0xf800) >> 11);
  LCD_CmdWrite(0x64);
  LCD_DataWrite((color & 0x07e0) >> 5);
  LCD_CmdWrite(0x65);
  LCD_DataWrite((color & 0x001f));

  /* Draw! */
  LCD_CmdWrite(RA8875_DCR);
  LCD_DataWrite(0x80);

  /* Wait for the command to finish */
  //waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
}

void writeReg(uchar reg, uchar val) 
{
  LCD_CmdWrite(reg);
  LCD_DataWrite(val);
}
/**************************************************************************/
void PWM1out(uchar p) {
  writeReg(RA8875_P1DCR, p);
}

/**************************************************************************/
/*!

*/
/**************************************************************************/
void PWM2out(uchar p) {
  writeReg(RA8875_P2DCR, p);
}

/**************************************************************************/
/*!

*/
/**************************************************************************/
void PWM1config(char on, uchar clock) {
  if (on) {
    writeReg(RA8875_P1CR, RA8875_P1CR_ENABLE | (clock & 0xF));
  } else {
    writeReg(RA8875_P1CR, RA8875_P1CR_DISABLE | (clock & 0xF));
  }
}

void PWM2config(char on, uchar clock) {
  if (on) {
    writeReg(RA8875_P2CR, RA8875_P2CR_ENABLE | (clock & 0xF));
  } else {
    writeReg(RA8875_P2CR, RA8875_P2CR_DISABLE | (clock & 0xF));
  }
}

/**************************************************************************/
/*
    Below Code added by AtomSoft aka <NAME>
*/
/**************************************************************************/

//
// Clear the window aka Clear Screen (CLS)
//
// if area is > 0 then active window is cleared
// if area is 0 then entire window is cleared
void ClearScreen(char area)
{
    char val = 0x80;

    if(area)
        val |= 0x40;

    LCD_CmdWrite(0x8E);     //Memory Clear Control Register
    LCD_DataWrite(val);    //Clear full window
}
//
// Display On or Off
//
void Display(char val)
{
    LCD_CmdWrite(0x01);
    LCD_DataWrite((val<<8));
}
//
// Layer Control
//
void LayeRS(char val)
{
    LCD_CmdWrite(0x20);
    LCD_DataWrite((val<<8));
}
//
// Change Font Size
//
void FontSize (char size)
{
    uchar reg ;
    size-=1;
    reg = (size<<2) | size;

    LCD_CmdWrite(0x22);
    LCD_DataWrite(reg);
}
//
// Set Fore/Back ColoRS
//
void SetColoRS(unsigned int f_color, unsigned int b_color)
{
    LCD_CmdWrite(0x60);//BGCR0
    LCD_DataWrite((unsigned char)(b_color>>11));

    LCD_CmdWrite(0x61);//BGCR0
    LCD_DataWrite((unsigned char)(b_color>>5));

    LCD_CmdWrite(0x62);//BGCR0
    LCD_DataWrite((unsigned char)(b_color));

    LCD_CmdWrite(0x63);//BGCR0
    LCD_DataWrite((unsigned char)(f_color>>11));

    LCD_CmdWrite(0x64);//BGCR0
    LCD_DataWrite((unsigned char)(f_color>>5));

    LCD_CmdWrite(0x65);//BGCR0
    LCD_DataWrite((unsigned char)(f_color));
}
//
// Setup Graphic CuRSor ???
//
void GraphicCuRSor(unsigned int x, unsigned int y, uchar color1, uchar color2)
{
    LCD_CmdWrite(0x80);
    LCD_DataWrite((uchar)(x&0xFF));

    LCD_CmdWrite(0x81);
    LCD_DataWrite((x>>8));

    LCD_CmdWrite(0x82);
    LCD_DataWrite((uchar)(y&0xFF));

    LCD_CmdWrite(0x83);
    LCD_DataWrite((y>>8));

    LCD_CmdWrite(0x84);
    LCD_DataWrite(color1);

    LCD_CmdWrite(0x85);
    LCD_DataWrite(color2);


}
//
// Backlight Control
//
void Backlight(uchar div, uchar pwm)
{
    PWM1config(1,div);
    PWM1out(pwm);
}
//
// Draw Routines
//
// Draw Line :
// xs = POINT 1 X location
// ys = POINT 1 Y location
//
// xe = POINT 2 X location
// ye = POINT 2 Y location
// color = color of line
//
void DrawLine ( unsigned int xs,unsigned int ys,unsigned int xe,unsigned int ye, unsigned int color)
{
    LCD_CmdWrite(0x91);
    LCD_DataWrite((uchar)xs);
    LCD_CmdWrite(0x92);
    LCD_DataWrite(xs>>8);

    LCD_CmdWrite(0x93);
    LCD_DataWrite((uchar)xe);
    LCD_CmdWrite(0x94);
    LCD_DataWrite(xe>>8);

    LCD_CmdWrite(0x95);
    LCD_DataWrite((uchar)ys);
    LCD_CmdWrite(0x96);
    LCD_DataWrite(ys>>8);

    LCD_CmdWrite(0x97);
    LCD_DataWrite((uchar)ye);
    LCD_CmdWrite(0x98);
    LCD_DataWrite(ye>>8);

    LCD_CmdWrite(0x63);
    LCD_DataWrite((color & 0xf800) >> 11);
    LCD_CmdWrite(0x64);
    LCD_DataWrite((color & 0x07e0) >> 5);
    LCD_CmdWrite(0x65);
    LCD_DataWrite((color & 0x001f));

    LCD_CmdWrite(0x90);
    LCD_DataWrite(RA8875_DCR_LINESQUTRI_START|RA8875_DCR_DRAWLINE);    // Start Line Drawing

//    waitPoll(RA8875_DCR,RA8875_DCR_LINESQUTRI_STATUS);
}
//
//  Draw Square/Rectangle
//  DrawSquare :
//  x = LEFT
//  y = TOP
//  w = WIDTH
//  h = HEIGHT
//  color = COLOR of Square/Rect
//  fill = 0 = NOFILL, 1+ = FILL
//
void DrawSquare ( unsigned int x,unsigned int y,unsigned int w,unsigned int h, unsigned int color, char fill)
{
    w+= x;
    h+= y;

    /* Set X */
    LCD_CmdWrite(0x91);
    LCD_DataWrite(x);
    LCD_CmdWrite(0x92);
    LCD_DataWrite(x >> 8);

    /* Set Y */
    LCD_CmdWrite(0x93);
    LCD_DataWrite(y);
    LCD_CmdWrite(0x94);
    LCD_DataWrite(y >> 8);

    /* Set X1 */
    LCD_CmdWrite(0x95);
    LCD_DataWrite(w);
    LCD_CmdWrite(0x96);
    LCD_DataWrite((w) >> 8);

    /* Set Y1 */
    LCD_CmdWrite(0x97);
    LCD_DataWrite(h);
    LCD_CmdWrite(0x98);
    LCD_DataWrite((h) >> 8);

    /* Set Color */
    LCD_CmdWrite(0x63);
    LCD_DataWrite((color & 0xf800) >> 11);
    LCD_CmdWrite(0x64);
    LCD_DataWrite((color & 0x07e0) >> 5);
    LCD_CmdWrite(0x65);
    LCD_DataWrite((color & 0x001f));

    /* Draw! */
    LCD_CmdWrite(RA8875_DCR);
    if (fill)
        LCD_DataWrite(0xB0);
    else
        LCD_DataWrite(0x90);

    /* Wait for the command to finish */
//    waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
}
void DrawCircle(unsigned int x0, unsigned int y0, unsigned int r, unsigned int color, char filled)
{
  /* Set X */
  LCD_CmdWrite(0x99);
  LCD_DataWrite(x0);
  LCD_CmdWrite(0x9a);
  LCD_DataWrite(x0 >> 8);

  /* Set Y */
  LCD_CmdWrite(0x9b);
  LCD_DataWrite(y0);
  LCD_CmdWrite(0x9c);
  LCD_DataWrite(y0 >> 8);

  /* Set Radius */
  LCD_CmdWrite(0x9d);
  LCD_DataWrite(r);

  /* Set Color */
  LCD_CmdWrite(0x63);
  LCD_DataWrite((color & 0xf800) >> 11);
  LCD_CmdWrite(0x64);
  LCD_DataWrite((color & 0x07e0) >> 5);
  LCD_CmdWrite(0x65);
  LCD_DataWrite((color & 0x001f));

  /* Draw! */
  LCD_CmdWrite(RA8875_DCR);
  if (filled)
  {
    LCD_DataWrite(RA8875_DCR_CIRCLE_START | RA8875_DCR_FILL);
  }
  else
  {
    LCD_DataWrite(RA8875_DCR_CIRCLE_START | RA8875_DCR_NOFILL);
  }

  /* Wait for the command to finish */
//  waitPoll(RA8875_DCR, RA8875_DCR_CIRCLE_STATUS);
}

char isImageButton (ImageButton btn)
{
    char isInX = 0;
    char isInY = 0;
    char isIt = 0;
    unsigned int x = ts_event.x1;
    unsigned int y = ts_event.y1;

    if(x >= btn.left)
    {
        if(x <= (btn.left+btn.width))
        {
            isInX = 1;
        }
    }

    if(y >= btn.top)
    {
        if(y <= (btn.top+btn.height))
        {
            isInY = 1;
        }
    }

    isIt = isInX + isInY;

    if(isIt >= 2)
    {
        return 1;
    }
    else
        return 0;
}

void SetColors(unsigned int f_color, unsigned int b_color)
{
    LCD_CmdWrite(0x60);//BGCR0
    LCD_DataWrite((unsigned char)(b_color>>11));

    LCD_CmdWrite(0x61);//BGCR0
    LCD_DataWrite((unsigned char)(b_color>>5));

    LCD_CmdWrite(0x62);//BGCR0
    LCD_DataWrite((unsigned char)(b_color));

    LCD_CmdWrite(0x63);//BGCR0
    LCD_DataWrite((unsigned char)(f_color>>11));

    LCD_CmdWrite(0x64);//BGCR0
    LCD_DataWrite((unsigned char)(f_color>>5));

    LCD_CmdWrite(0x65);//BGCR0
    LCD_DataWrite((unsigned char)(f_color));
}

void DrawFinger(unsigned int x,unsigned int y,unsigned int colour)
{
    Write_Dir(0x40,0x00);//The drawing mode
    MemoryWrite_Position(x,y);//Memory write position
    LCD_CmdWrite(0x02);//Memory write Data
    LCD_DataWrite(colour);
    LCD_DataWrite(colour);
    MemoryWrite_Position(x,y+1);//Memory write position
    LCD_CmdWrite(0x02);////Memory write Data
    LCD_DataWrite(colour);
    LCD_DataWrite(colour);
}
void SetGraphicsCursorRead(unsigned int x, unsigned int y)
{
    //Write_Dir(0x40, 0);  // Graphics mode
    //Write_Dir(0x45, 0);  // left->right, top->bottom
    Write_Dir(0x4A, x& 0xFF);
    Write_Dir(0x4B, (x>>8));
    Write_Dir(0x4C, y& 0xFF);
    Write_Dir(0x4D, (y>>8));

}

void SetGraphicsCursorWrite(unsigned int x, unsigned int y)
{
    //Write_Dir(0x40, 0);  // Graphics mode
    //Write_Dir(0x45, 0);  // left->right, top->bottom
    Write_Dir(0x46, x & 0xFF);
    Write_Dir(0x47, (x>>8)&3);
    Write_Dir(0x48, y & 0xFF);
    Write_Dir(0x49, (y>>8)&1);

}
void OpenASI (char *filename, unsigned int x, unsigned int y)
{
    unsigned int w = 0;
    unsigned int h = 0;
    UINT br;         /* File read/write count */
    FRESULT fr;          /* FatFs return code */
    unsigned int len;
    unsigned int line;
    FIL fil;
    unsigned int top, left;

    Chk_Busy();
    fr = f_open(&fil, filename, FA_OPEN_EXISTING | FA_READ);
    fr = f_read(&fil, IMAGE_BUFF, 0x14, &br);

    h = IMAGE_BUFF[0x10] | (IMAGE_BUFF[0x11]<<8);
    w = IMAGE_BUFF[0x12] | (IMAGE_BUFF[0x13]<<8);;

    len = ((w*h)*2);
    line = (w*2);

    f_lseek(&fil, 0x14);

    for(top=0;top<h;top++)
    {
        Write_Dir(0x40,0x00);    // Graphics write mode
        SetGraphicsCursorWrite(x, (y+top));
        LCD_CmdWrite(0x02);

        fr = f_read(&fil, IMAGE_BUFF, line, &br);  /* Read a chunk of source file */
        if (fr != FR_OK) break;

        //Chk_Busy();
        //CS_LOW(LCD);
        //SPI_Write(0x00);         // Cmd: write data

        for(left=0;left<line;left++)
        {
            LCD_DataWrite(IMAGE_BUFF[left]);//Chk_Busy();
        }
        /*
        for(left=0;left<line;left+=2)
        {
            LCD_DataWrite(IMAGE_BUFF[left]);//Chk_Busy();
            LCD_DataWrite(IMAGE_BUFF[left+1]);//Chk_Busy();
        }
        */

        //CS_HIGH();
    }

    //CS_HIGH();
    f_close(&fil);
}

void fillRect(void) {
  LCD_CmdWrite(RA8875_DCR);
  LCD_DataWrite(RA8875_DCR_LINESQUTRI_STOP | RA8875_DCR_DRAWSQUARE);
  LCD_DataWrite(RA8875_DCR_LINESQUTRI_START | RA8875_DCR_FILL | RA8875_DCR_DRAWSQUARE);
}
void drawRect(unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned int color, char filled)
{
    w += x;
    h += y;

  /* Set X */
  LCD_CmdWrite(0x91);
  LCD_DataWrite(x);
  LCD_CmdWrite(0x92);
  LCD_DataWrite(x >> 8);

  /* Set Y */
  LCD_CmdWrite(0x93);
  LCD_DataWrite(y);
  LCD_CmdWrite(0x94);
  LCD_DataWrite(y >> 8);

  /* Set X1 */
  LCD_CmdWrite(0x95);
  LCD_DataWrite(w);
  LCD_CmdWrite(0x96);
  LCD_DataWrite((w) >> 8);

  /* Set Y1 */
  LCD_CmdWrite(0x97);
  LCD_DataWrite(h);
  LCD_CmdWrite(0x98);
  LCD_DataWrite((h) >> 8);

  /* Set Color */
  LCD_CmdWrite(0x63);
  LCD_DataWrite((color & 0xf800) >> 11);
  LCD_CmdWrite(0x64);
  LCD_DataWrite((color & 0x07e0) >> 5);
  LCD_CmdWrite(0x65);
  LCD_DataWrite((color & 0x001f));

  /* Draw! */
  LCD_CmdWrite(RA8875_DCR);
  if (filled)
  {
    LCD_DataWrite(0xB0);
  }
  else
  {
    LCD_DataWrite(0x90);
  }

  /* Wait for the command to finish */
//  waitPoll(RA8875_DCR, RA8875_DCR_LINESQUTRI_STATUS);
}<file_sep>#ifndef I2C2_H
#define I2C2_H


#define SDA TRISFbits.TRISF1
#define SDA_LOW        TRISFbits.TRISF1 = 0;LATFbits.LATF1 = 0
#define SDA_HIGH       TRISFbits.TRISF1 = 1
#define isSDA          PORTFbits.RF1

#define SCL TRISEbits.TRISE0
#define SCL_LOW        TRISEbits.TRISE0 = 0;LATEbits.LATE0 = 0
#define SCL_HIGH       TRISEbits.TRISE0 = 1

#define MMA8453Q 0x38

void delay1(int time);
void delay2();
char sda_setup(void);
void I2C_Start(void);
void I2C_Stop(void);
unsigned char I2C_Wait_Ack(void);
void I2C_Ack(void);
void I2C_NAck(void);
void I2C_Send_Byte(unsigned char data);
unsigned char I2C_Read_Byte(unsigned char ack);
void I2C_Wr_Reg(unsigned char WRADD, unsigned char RegIndex,unsigned char RegValue1);
unsigned char I2C_Read_Reg(unsigned char WRADD, unsigned char RegIndex);

#endif
<file_sep>/* 
 * File:   main.c
 * Author: Jason
 *
 * Created on July 5, 2014, 4:38 PM
 */

#include "main.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xc.h>
#include <plib.h>

#include "fubsd.h"
#include "RA8875.h"
#include "ft5206.h"

#include "spi2.h"
#include "ff.h"
#include "diskio.h"

#include "keyboard.h"

//* Oscillator Settings
#pragma config FNOSC    = PRIPLL                                // Oscillator selection
#pragma config POSCMOD  = HS                                    // Primary oscillator mode
#pragma config FPLLIDIV = DIV_2                                 // PLL input divider
#pragma config FPLLMUL  = MUL_20                                // PLL multiplier
#pragma config FPLLODIV = DIV_1                                 // PLL output divider
#pragma config FPBDIV   = DIV_1                                 // Peripheral bus clock divider
#pragma config FSOSCEN  = OFF                                   // Secondary oscillator enable

//* Clock control settings
#pragma config IESO     = OFF                                   // Internal/external clock switchover
#pragma config FCKSM    = CSECME                                // Clock switching (CSx)/Clock monitor (CMx)
#pragma config OSCIOFNC = OFF                                   // Clock output on OSCO pin enable

//* USB Settings
#pragma config UPLLEN   = ON                                    // USB PLL enable
#pragma config UPLLIDIV = DIV_2                                 // USB PLL input divider
#pragma config FUSBIDIO = OFF									// USB USID pin controlled by port function
#pragma config FVBUSONIO = OFF									// USB VBUSON pin controlled by port function

//* Other Peripheral Device settings
#pragma config FWDTEN   = OFF                                   // Watchdog timer enable
#pragma config WDTPS    = PS1024                                // Watchdog timer postscaler

//* Code Protection settings
#pragma config CP       = OFF                                   // Code protection
#pragma config BWP      = OFF                                   // Boot flash write protect
#pragma config PWP      = OFF                                   // Program flash write protect

//* Debug settings
#pragma config ICESEL   = ICS_PGx2                              // ICE pin selection

#define _CPU_NAME_                  "32MX795F512H"
#define VEND                        vendFubarino
#define PROD                        prodFubarinoSD_512K
#define F_CPU                       80000000UL

#define FLASH_BYTES                 0x80000                     // 512K
#define FLASH_PAGE_SIZE             4096
#define LoadFlashWaitStates()       (CHECON = 2)            // 0 for 0-30Mhz, 1 for 31-60Mhz, 2 for 61-80Mhz

#define SYS_FREQ             (80000000L)
#define PB_DIV                 8
#define PRESCALE               256
#define TOGGLES_PER_SEC        1
#define T1_TICK               (SYS_FREQ/PB_DIV/PRESCALE/TOGGLES_PER_SEC)


unsigned int fColor = color_black;
unsigned int bColor = color_white;

char usr_msg[128];

void init_board(void);

Touch OldTouch;
FATFS fs;          // Work area (file system object) for the volume
BYTE FILE_IN_BUFF[512];
DSTATUS iStat;
char messageOut[128];
FIL fil;       /* File object */
char line[82]; /* Line buffer */
volatile Keyboard AtomKB;

void main() {
    int fSize = 30;
    unsigned int tryCount = 1;
    unsigned int FFST = 0;
    unsigned int i,t;
    char keytemp;

    init_board();
    TRISEbits.TRISE5 = 0;   //USR_LED = output

    TOUCH_Init();

    LCD_Initial();
    //Backlight(RA8875_PWM_CLK_DIV8,80);

    Write_Dir(0X01,0X80);//display on

    Active_Window(0,799,0,479);//Set the working window size
    SetColors(fColor,bColor);
    ClearScreen(0);

    FontWrite_Position(0,10);
    String("  AtomSoftTech - RA8875 7\" TFT with Capacitive Touch");

    FontWrite_Position(0,30);
    String("  MCU: PIC32MX795F512H");

    FontWrite_Position(0,50);
    String("  LCD Mode: 8080 - 8bit");

    FontWrite_Position(0,90);
    String("  Insert SD Card...Waiting");

    SpiChnClose(2);
    SpiInitDevice(2,SPI_SLOWEST,0);

    while(isCD() == 1);
    FontWrite_Position(0,110);
    String("  SD Card...Found!");

    do
    {
        while(isCD() == 1);
        iStat = FR_NOT_READY;
        Delay10ms(20);
        FontWrite_Position(0,130);

        sprintf(messageOut, "  Mounting SD Card with Elm Chan's FatFS. Try #%d  \0", tryCount);
        String(messageOut);
        tryCount++;
        iStat = f_mount(&fs, "", 1);//(&fs);  //MountSD(fs);
    }
    while(iStat != FR_OK);

    if(fs.fs_type == FS_FAT12)
        FFST = 12;
    if(fs.fs_type == FS_FAT16)
        FFST = 16;
    if(fs.fs_type == FS_FAT32)
        FFST = 32;

    SpiChnClose(2);
    SpiInitDevice(2,SPI_MED1,0);

    FontWrite_Position(0,150);
    sprintf(messageOut, "  SD Card Filesystem... FAT%d  \0", FFST);
    String(messageOut);

    FontWrite_Position(0,190);
    sprintf(messageOut, "  Initiating Keyboard...  \0");
    String(messageOut);

    for(i=0;i<5;i++)
    {
        FontWrite_Position(0,210);
        sprintf(messageOut, "  Loading in ... %d\0", (5-i));
        String(messageOut);
        delay_ms(999);
    }

/*
    SetColors(fColor,bColor);//delay_ms(2);
    ClearScreen(0);delay_ms(30);
    draw_kb(SHIFT,100,260,600,color_black,color_white,color_black);
    delay_ms(2000);

    SetColors(fColor,bColor);//delay_ms(2);
    ClearScreen(0);delay_ms(30);
    draw_kb(SHIFT,100,260,600,color_black,color_red,color_white);
    delay_ms(2000);

    SetColors(fColor,bColor);//delay_ms(2);
    ClearScreen(0);delay_ms(30);
    draw_kb(SHIFT,100,260,600,color_black,color_purple,color_white);
    delay_ms(2000);
*/
    SetColors(fColor,bColor);//delay_ms(2);
    ClearScreen(0);delay_ms(30);
    AtomKB = draw_kb(NOSHIFT,100,200,600,color_black,color_blue,color_white);

    SetColors(fColor,bColor);//delay_ms(2);
    
    for(i=0;i<128;i++)
        messageOut[i] = 0x20;

    messageOut[0] = '_';
    i = 0;

    while(1)
    {
        if(CheckPen() == 0)        //The touch screen is pressed
        {
            do
            {
                ft5x0x_read_data();

                keytemp = isKey(AtomKB, ts_event.x1,ts_event.y1);
                
                if(keytemp)
                {
                    if(i == 100)
                    {
                        i = 0; //RESET CUR POS IN BUFF
                        for(t=0;t<128;t++){
                            messageOut[t] = 0x20;
                            messageOut[0] = '_';
                        }
                    }

                    

                    if(AtomKB.shift == 1)
                    {
                        if(AtomKB.caps == 0)
                        {
                            AtomKB = draw_kb(NOSHIFT,100,200,600,color_black,color_blue,color_white);
                        }
                    }
                    switch(keytemp)
                    {
                        case 0x08:  //DEL
                            if((i > 0))
                                i--;

                            messageOut[i] = 0x20;
                            messageOut[i+1] = 0x20;
                            messageOut[i] = '_';
                            break;
                        case 0x0d:  //RETURN
                            messageOut[i++] = '[';
                            messageOut[i++] = 'E';
                            messageOut[i++] = 'N';
                            messageOut[i++] = 'T';
                            messageOut[i++] = ']';
                            messageOut[i] = '_';
                            break;
                        case 0x09:  //TAB
                            messageOut[i++] = '[';
                            messageOut[i++] = 'T';
                            messageOut[i++] = 'A';
                            messageOut[i++] = 'B';
                            messageOut[i++] = ']';
                            messageOut[i] = '_';
                            break;
                        case 0x1c:
                            if(AtomKB.caps == 0){
                                AtomKB = draw_kb(CAPS,100,200,600,color_black,color_blue,color_white);
                            }else{
                                AtomKB = draw_kb(NOSHIFT,100,200,600,color_black,color_blue,color_white);
                            }
                            break;
                        case 0x1d:
                            if(AtomKB.caps == 1) break;

                            if(AtomKB.shift == 0){
                                AtomKB = draw_kb(SHIFT,100,200,600,color_black,color_blue,color_white);
                            } else {
                                AtomKB = draw_kb(NOSHIFT,100,200,600,color_black,color_blue,color_white);
                            }
                            break;
                        default:
                            messageOut[i++] = keytemp;
                            messageOut[i] = '_';
                            break;
                    }
                    SetColors(fColor,bColor);
                    FontWrite_Position(10,100);
                    String(messageOut);
                    delay_ms(160);
                }
            }while(isPEN()==0);


            ts_event.Key_Sta=Key_Up;
        }
    }
    while(1);   //TEST STOP HERE

    SetColors(fColor,bColor);
    //OpenASI("main.asi",0,0);
    delay_ms(10000);
    ClearScreen(0);
    delay_ms(200);

    while(1)
    {

        if(CheckPen() == 0)        //The touch screen is pressed
        {
            do
            {
                ft5x0x_read_data();

                SetColors(fColor,bColor);

                FontWrite_Position(0,180);
                sprintf(usr_msg, "  X1: %i    ",ts_event.x1);
                String(usr_msg);

                FontWrite_Position(0,200);
                sprintf(usr_msg, "  Y1: %i    ",ts_event.y1);
                String(usr_msg);

                FontWrite_Position(0,230);
                sprintf(usr_msg, "  X2: %i    ",ts_event.x2);
                String(usr_msg);

                FontWrite_Position(0,250);
                sprintf(usr_msg, "  Y2: %i    ",ts_event.y2);
                String(usr_msg);

                FontWrite_Position(0,280);
                sprintf(usr_msg, "  X3: %i    ",ts_event.x3);
                String(usr_msg);

                FontWrite_Position(0,300);
                sprintf(usr_msg, "  Y3: %i    ",ts_event.y3);
                String(usr_msg);

                FontWrite_Position(0,330);
                sprintf(usr_msg, "  X4: %i    ",ts_event.x4);
                String(usr_msg);

                FontWrite_Position(0,350);
                sprintf(usr_msg, "  Y4: %i    ",ts_event.y4);
                String(usr_msg);

                FontWrite_Position(0,380);
                sprintf(usr_msg, "  X5: %i    ",ts_event.x5);
                String(usr_msg);

                FontWrite_Position(0,400);
                sprintf(usr_msg, "  Y5: %i    ",ts_event.y5);
                String(usr_msg);

                switch(ts_event.touch_point)
                {
                    case 5:
                        DrawCircle(OldTouch.x5,OldTouch.y5,fSize,color_white,1);delay_us((fSize*15));
                        DrawCircle(ts_event.x5,ts_event.y5,fSize,color_purple,1);delay_us((fSize*15));
                    case 4:
                        DrawCircle(OldTouch.x4,OldTouch.y4,fSize,color_white,1);delay_us((fSize*15));
                        DrawCircle(ts_event.x4,ts_event.y4,fSize,color_cyan,1);delay_us((fSize*15));
                    case 3:
                        DrawCircle(OldTouch.x3,OldTouch.y3,fSize,color_white,1);delay_us((fSize*15));
                        DrawCircle(ts_event.x3,ts_event.y3,fSize,color_blue,1);delay_us((fSize*15));
                    case 2:
                        DrawCircle(OldTouch.x2,OldTouch.y2,fSize,color_white,1);delay_us((fSize*15));
                        DrawCircle(ts_event.x2,ts_event.y2,fSize,color_green,1);delay_us((fSize*15));
                    case 1:
                        DrawCircle(OldTouch.x1,OldTouch.y1,fSize,color_white,1);delay_us((fSize*15));
                        DrawCircle(ts_event.x1,ts_event.y1,fSize,color_red,1);delay_us((fSize*15));
                        break;
                }

                OldTouch = ts_event;
            }while(isPEN()==0);


            ts_event.Key_Sta=Key_Up;
        }
    }

}

void init_board(void)
{
    AD1CON1bits.ADON = 0;       //ADC Module OFF
    AD1PCFG = 0x00FF;           //ALL DIGITAL
    IEC1 = 0;                   //ALL Interrupt Disabled

    CM1CON = 0;			//Comparator 1 OFF
    CM2CON = 0;			//Comparator 2 OFF
    CMSTAT = 0;
    CNCON = 0;
    ODCD = ODCE = ODCF = ODCC = 0;
    PMCON = 0;
	
    OpenTimer1(T1_ON | T1_IDLE_CON | T1_PS_1_8 | T1_SOURCE_INT,  25000);  //10MHz Timer

}

void delay_us(int time)
{
    while(time--)
    {
        WriteTimer1(0);
        while(ReadTimer1() < 10);
    }
}
void delay_ms(int time)
{
    while(time--)
    {
        WriteTimer1(0);
        while(ReadTimer1() < 10000);
    }
}<file_sep>#ifndef FT5206_H
#define	FT5206_H
#include "main.h"

void _nop_();
/* touch panel interface define */

#define PEN PORTFbits.RF0
#define isPEN()     PORTFbits.RF0


#define CONFIG_FT5X0X_MULTITOUCH    //Define the multi-touch
//Touch Status
#define Key_Down 0x01
#define Key_Up   0x00


typedef struct _Touch_ {
    unsigned int    x1;
    unsigned int    y1;
    unsigned int    x2;
    unsigned int    y2;
    unsigned int    x3;
    unsigned int    y3;
    unsigned int    x4;
    unsigned int    y4;
    unsigned int    x5;
    unsigned int    y5;
    unsigned char   touch_point;
    unsigned char   Key_Sta;
} Touch;


extern Touch ts_event;
#define FT5206_DEVICE_MODE 0x00

#define WRITE_ADD	0x70
#define READ_ADD	0x71

void  TOUCH_Init(void);
void  TOUCH_Start(void);
void  TOUCH_Stop(void);
unsigned char TOUCH_Wait_Ack(void);
void  TOUCH_Ack(void);
void  TOUCH_NAck(void);

void  TOUCH_Send_Byte(unsigned char txd);
unsigned char TOUCH_Read_Byte(unsigned char ack);
void  TOUCH_Wr_Reg(unsigned char RegIndex,unsigned char RegValue1);
void  TOUCH_RdParFrPCTPFun(unsigned char *PCTP_Par,unsigned char ValFlag);
unsigned char TOUCH_Read_Reg(unsigned char RegIndex);
void  Draw_Big_Point(unsigned int x,unsigned int y,unsigned int colour);
unsigned char ft5x0x_read_data(void);  //unsigned char ft5x0x_read_data(void);

char CheckPen(void);

#endif<file_sep>/* 
 * File:   fubsd.h
 * Author: Jason
 *
 * Created on JULY 5, 2014, 3:27 PM
 */

#ifndef FUBSD_H
#define	FUBSD_H

#define FUB_PAD0  (1<<8)
#define FUB_PAD1  (1<<9)
#define FUB_PAD2  (1<<10)
#define FUB_PAD3  (1<<11)
#define FUB_PAD4  (1<<0)
#define FUB_PAD5  (1<<13)
#define FUB_PAD6  (1<<14)
#define FUB_PAD7  (1<<1)
#define FUB_PAD8  (1<<2)
#define FUB_PAD9  (1<<3)
#define FUB_PAD10 (1<<4)
#define FUB_PAD11 (1<<5)
#define FUB_PAD12 (1<<6)
#define FUB_PAD13 (1<<7)
#define FUB_PAD14 (1<<0)
#define FUB_PAD15 (1<<1)
#define FUB_PAD16 (1<<0)
#define FUB_PAD17 (1<<1)
#define FUB_PAD18 (1<<2)
#define FUB_PAD19 (1<<3)
#define FUB_PAD20 (1<<4)
#define FUB_PAD21 (1<<5)
#define FUB_PAD22 (1<<6)
#define FUB_PAD23 (1<<7)
#define FUB_PAD24 (1<<6)
#define FUB_PAD25 (1<<7)
#define FUB_PAD26 (1<<8)
#define FUB_PAD27 (1<<9)
#define FUB_PAD28 (1<<4)
#define FUB_PAD29 (1<<5)
#define FUB_PAD30 (1<<4)
#define FUB_PAD31 (1<<3)
#define FUB_PAD32 (1<<2)
#define FUB_PAD33 (1<<1)
#define FUB_PAD34 (1<<0)
#define FUB_PAD35 (1<<7)
#define FUB_PAD36 (1<<6)
#define FUB_PAD37 (1<<8)
#define FUB_PAD38 (1<<9)
#define FUB_PAD39 (1<<10)
#define FUB_PAD40 (1<<11)
#define FUB_PAD41 (1<<12)
#define FUB_PAD42 (1<<13)
#define FUB_PAD43 (1<<14)
#define FUB_PAD44 (1<<15)

#define PORT_FUB_P0  LATD
#define PORT_FUB_P1  LATD
#define PORT_FUB_P2  LATD
#define PORT_FUB_P3  LATD
#define PORT_FUB_P4  LATD
#define PORT_FUB_P5  LATC
#define PORT_FUB_P6  LATC
#define PORT_FUB_P7  LATD
#define PORT_FUB_P8  LATD
#define PORT_FUB_P9  LATD
#define PORT_FUB_P10 LATD
#define PORT_FUB_P11 LATD
#define PORT_FUB_P12 LATD
#define PORT_FUB_P13 LATD
#define PORT_FUB_P14 LATF
#define PORT_FUB_P15 LATF
#define PORT_FUB_P16 LATE
#define PORT_FUB_P17 LATE
#define PORT_FUB_P18 LATE
#define PORT_FUB_P19 LATE
#define PORT_FUB_P20 LATE
#define PORT_FUB_P21 LATE
#define PORT_FUB_P22 LATE
#define PORT_FUB_P23 LATE
#define PORT_FUB_P24 LATG
#define PORT_FUB_P25 LATG
#define PORT_FUB_P26 LATG
#define PORT_FUB_P27 LATG
#define PORT_FUB_P28 LATF
#define PORT_FUB_P29 LATF
#define PORT_FUB_P30 LATB
#define PORT_FUB_P31 LATB
#define PORT_FUB_P32 LATB
#define PORT_FUB_P33 LATB
#define PORT_FUB_P34 LATB
#define PORT_FUB_P35 LATB
#define PORT_FUB_P36 LATB
#define PORT_FUB_P37 LATB
#define PORT_FUB_P38 LATB
#define PORT_FUB_P39 LATB
#define PORT_FUB_P40 LATB
#define PORT_FUB_P41 LATB
#define PORT_FUB_P42 LATB
#define PORT_FUB_P43 LATB
#define PORT_FUB_P44 LATB


#define FUB_P0  IOPORT_D,FUB_PAD0
#define FUB_P1  IOPORT_D,FUB_PAD1
#define FUB_P2  IOPORT_D,FUB_PAD2
#define FUB_P3  IOPORT_D,FUB_PAD3
#define FUB_P4  IOPORT_D,FUB_PAD4
#define FUB_P5  IOPORT_C,FUB_PAD5
#define FUB_P6  IOPORT_C,FUB_PAD6
#define FUB_P7  IOPORT_D,FUB_PAD7
#define FUB_P8  IOPORT_D,FUB_PAD8
#define FUB_P9  IOPORT_D,FUB_PAD9
#define FUB_P10 IOPORT_D,FUB_PAD10
#define FUB_P11 IOPORT_D,FUB_PAD11
#define FUB_P12 IOPORT_D,FUB_PAD12
#define FUB_P13 IOPORT_D,FUB_PAD13
#define FUB_P14 IOPORT_F,FUB_PAD14
#define FUB_P15 IOPORT_F,FUB_PAD15
#define FUB_P16 IOPORT_E,FUB_PAD16
#define FUB_P17 IOPORT_E,FUB_PAD17
#define FUB_P18 IOPORT_E,FUB_PAD18
#define FUB_P19 IOPORT_E,FUB_PAD19
#define FUB_P20 IOPORT_E,FUB_PAD20
#define FUB_P21 IOPORT_E,FUB_PAD21
#define FUB_P22 IOPORT_E,FUB_PAD22
#define FUB_P23 IOPORT_E,FUB_PAD23
#define FUB_P24 IOPORT_G,FUB_PAD24
#define FUB_P25 IOPORT_G,FUB_PAD25
#define FUB_P26 IOPORT_G,FUB_PAD26
#define FUB_P27 IOPORT_G,FUB_PAD27
#define FUB_P28 IOPORT_F,FUB_PAD28
#define FUB_P29 IOPORT_F,FUB_PAD29
#define FUB_P30 IOPORT_B,FUB_PAD30
#define FUB_P31 IOPORT_B,FUB_PAD31
#define FUB_P32 IOPORT_B,FUB_PAD32
#define FUB_P33 IOPORT_B,FUB_PAD1
#define FUB_P34 IOPORT_B,FUB_PAD2
#define FUB_P35 IOPORT_B,FUB_PAD3
#define FUB_P36 IOPORT_B,FUB_PAD4
#define FUB_P37 IOPORT_B,FUB_PAD5
#define FUB_P38 IOPORT_B,FUB_PAD6
#define FUB_P39 IOPORT_B,FUB_PAD7
#define FUB_P40 IOPORT_B,FUB_PAD8
#define FUB_P41 IOPORT_B,FUB_PAD9
#define FUB_P42 IOPORT_B,FUB_PAD10
#define FUB_P43 IOPORT_B,FUB_PAD11
#define FUB_P44 IOPORT_B,FUB_PAD12


#define USR_LED  LATEbits.LATE5
#define USR_BTN  IOPORT_E,FUB_PAD27


#endif	/* FUBSD_H */

<file_sep>#include <xc.h>
#include <plib.h>
#include "spi2.h"
#include "RA8875.h"

void spi_delay()
{
    UINT tt;
    tt = 10;
    while(tt--)
        Nop();
}

unsigned char SpiTransfer2(unsigned char byte)
{
    unsigned char counter;

    for(counter=0; counter < 8; counter++)
    {
        if (byte & 0x80)
            MOSI_HIGH();
        else
            MOSI_LOW();

        byte <<= 1;

        SCK_HIGH(); /* a slave latches input data bit */
        //spi_delay();
        if (isMISO())
            byte |= 0x01;

        SCK_LOW(); /* a slave shifts out next output data bit */
        //spi_delay();
    }

    return(byte);
}


void SpiInitDevice(int chn, int srcClkDiv, int samp)
{
    unsigned int config = SPI_OPEN_MSTEN|SPI_OPEN_MODE8|SPI_OPEN_CKE_REV|SPI_OPEN_SMP_END;
    //unsigned int config = SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_SMP_OFF | SPI_CKE_ON | CLK_POL_ACTIVE_HIGH |SPI_CON_ON;    // SPI configuration word
    //if(samp == 0)
        //config2 |= ;

    SpiChnOpen(chn, config, srcClkDiv);  
    SpiConfigPins();
    release_spi();
}

void SpiConfigPins()
{

    SYSKEY = 0x0; // Make sure SYSKEY is locked
    SYSKEY = 0xAA996655; // SYSKEY unlock sequence
    SYSKEY = 0x556699AA;

    //PPSInput(3,SDI2,RPA4); //MISO2 - P18
    //PPSOutput(2,RPA1,SDO2); //MOSI2 - P06

    SYSKEY = 0x0; // Relock SYSKEY

    TRISGbits.TRISG6 = 0; //SCK OUT
    TRISGbits.TRISG7 = 1; //MISO IN
    TRISGbits.TRISG8 = 0; //MOSI OUT
    TRISBbits.TRISB4 = 1; //CD IN
    TRISGbits.TRISG9 = 0; //CS OUT
}

unsigned char SpiTransfer(unsigned char byte)
{
    	SpiChnPutC(2,byte);
        while(SpiChnIsBusy(2) > 0);
        return SpiChnGetC(2);
}

void release_spi()
{
    CS_HIGH();
    SpiTransfer(0xff);
}

void CS_HIGH()
{
    LCD_CS = 1;
    SD_CS = 1;
}

void CS_LOW(char type)
{
    CS_HIGH();
    switch(type)
    {
        case LCD:
            LCD_CS = 0;
            break;
        case SD:
            SD_CS = 0;
            break;
    }
}<file_sep>/* 
 * File:   spi2.h
 * Author: Jason
 *
 * Created on March 1, 2014, 12:13 PM
 */

#ifndef SPI2_H
#define	SPI2_H

#include "fubsd.h"
#include <xc.h>
#include <plib.h>

//-----------------------
// SPEEDS
//-----------------------
#define SPI_SLOWEST 200     //400KHz
#define SPI_SLOW    20      //4MHz
#define SPI_MED     10      //8MHz
#define SPI_MED1    4       //20MHz
#define SPI_MED2    3       //26MHz
#define SPI_FAST    2       //40MHz
#define SPI_FASTEST 1       //80MHz
//-----------------------
// CHIP SELECTS
//-----------------------
#define SD_CS LATGbits.LATG9
#define SD_CS_LOW()       SD_CS = 0
#define SD_CS_HIGH()      SD_CS = 1


#define RFM_CSN FUB_P12
#define RFM_CSN_LOW()       PORTClearBits(RFM_CSN)
#define RFM_CSN_HIGH()      PORTSetBits(RFM_CSN)

#define RFM_CE  FUB_P13
#define RFM_CE_LOW()       PORTClearBits(RFM_CE)
#define RFM_CE_HIGH()      PORTSetBits(RFM_CE)
//-----------------------
// DATA PINS
//-----------------------
#define MOSI LATGbits.LATG8
#define MOSI_LOW()       MOSI = 0
#define MOSI_HIGH()      MOSI = 1

#define MISO PORTGbits.RG7
#define isMISO()          PORTGbits.RG7

#define SCK  LATGbits.LATG6
#define SCK_LOW()       SCK = 0
#define SCK_HIGH()      SCK = 1

//-----------------------
// MISC
//-----------------------
#define SD_CD PORTBbits.RB4
#define isCD()          SD_CD

//-----------------------
// CS TYPES
//-----------------------
#define LCD 0
#define SD 1

//-----------------------
// EXTERNAL FUNCTIONS
//-----------------------
void CS_LOW(char type);
void CS_HIGH();
void SpiInitDevice(int chn, int srcClkDiv, int samp);
void SpiConfigPins();
unsigned char SpiTransfer(unsigned char byte);
void release_spi();
unsigned char SpiTransfer2(unsigned char byte);

#endif	/* SPI2_H */

<file_sep>/* 
 * File:   main.h
 * Author: Jason
 *
 * Created on July 5, 2014, 8:11 PM
 */

#ifndef MAIN_H
#define	MAIN_H


void delay_us(int time);
void delay_ms(int time);

#endif	/* MAIN_H */

<file_sep>#include "fubsd.h"
#include <plib.h>
#include "i2c2.h"
#include "ft5206.h"


Touch ts_event;

void delayus(int time)
{
    delay_us(time);
}

void _nop_()
{
    Nop();
}

char SDA_IN(void)
{
    SDA_HIGH;
    delayus(5);
    return isSDA;
}
//IIC start
void TOUCH_Start(void)
{
	SDA_HIGH;
	_nop_();
	SCL_HIGH;
	delayus(2);
	SDA_LOW;
	delayus(2);
	SCL_LOW;
	_nop_();
}


//IIC stop
void TOUCH_Stop(void)
{
	SDA_LOW;
	_nop_();
	SCL_HIGH;
	delayus(2);
	SDA_HIGH;
	delayus(2);
	SCL_LOW;
	_nop_();
}


//Wait for an answer signal
unsigned char TOUCH_Wait_Ack(void)
{	unsigned char errtime=0;

	SDA_HIGH;
	delayus(1);
	SCL_HIGH;
	delayus(1);
  	while(isSDA == 1)
	{
	    errtime++;
	    if(errtime>250)
            {
              TOUCH_Stop();
              return 1;
            }
	}
	SCL_LOW;
	_nop_();
}




//Acknowledge
void TOUCH_Ack(void)
{	SCL_LOW;
	SDA_LOW;
	delayus(1);
	SCL_HIGH;
	delayus(1);
	SCL_LOW;
	_nop_();
}



//NO Acknowledge
void TOUCH_NAck(void)
{	SCL_LOW;
	SDA_HIGH;
	delayus(1);
	SCL_HIGH;
	delayus(1);
	SCL_LOW;
	_nop_();
}


//IIC send one byte
void TOUCH_Send_Byte(unsigned char data)
{	unsigned char t;

    for(t=0;t<8;t++)
    {
        SCL_LOW;

        if((data & 0x80) > 0)
            SDA_HIGH;
        else
            SDA_LOW;

        data <<=1;
        delayus(1);
        SCL_HIGH;
        delayus(1);
        SCL_LOW;
        delayus(1);
    }


}




/******************************************************************************************
*Function name£ºDraw_Big_Point(u16 x,u16 y)
* Parameter£ºUINT16_t x,UINT16_t y xy
* Return Value£ºvoid
* Function£ºDraw touch pen nib point 3 * 3
*********************************************************************************************/
void Draw_Big_Point(UINT x,UINT y,UINT colour)
{
    Write_Dir(0x40,0x00);//The drawing mode
    MemoryWrite_Position(x,y);//Memory write position
    LCD_CmdWrite(0x02);//Memory write Data
    LCD_DataWrite(colour);
    LCD_DataWrite(colour);
    MemoryWrite_Position(x,y+1);//Memory write position
    LCD_CmdWrite(0x02);////Memory write Data
    LCD_DataWrite(colour);
    LCD_DataWrite(colour);
}



//Read one byte£¬ack=0£¬Send Acknowledge£¬ack=1£¬NO Acknowledge
unsigned char TOUCH_Read_Byte(unsigned char ack)
{
    unsigned char t,receive=0;

    SCL_LOW;
    SDA_HIGH;
    for(t = 0; t < 8; t++)
    {
        _nop_();
        SCL_HIGH;
        _nop_();

        receive <<= 1;

        if(isSDA == 1)
            receive |= 0x01;

        delayus(1);
        SCL_LOW;
        delayus(1);
    }


    if (ack)  TOUCH_NAck();//NO Acknowledge
    else       TOUCH_Ack(); //Send Acknowledge

     return receive;
}


void TOUCH_Wr_Reg(unsigned char RegIndex,unsigned char RegValue1)
{
    TOUCH_Start();
    TOUCH_Send_Byte(WRITE_ADD);
    TOUCH_Wait_Ack();
    TOUCH_Send_Byte(RegIndex);
    TOUCH_Wait_Ack();

    TOUCH_Send_Byte(RegValue1);
    TOUCH_Wait_Ack();

    TOUCH_Stop();
    delayus(100);
}


void TOUCH_RdParFrPCTPFun(unsigned char *PCTP_Par,unsigned char ValFlag)
{	unsigned char k;

    TOUCH_Start();
    TOUCH_Send_Byte(READ_ADD);
    TOUCH_Wait_Ack();
    for(k=0;k<ValFlag;k++)
    {
        if(k == (ValFlag-1))
            *PCTP_Par++ = TOUCH_Read_Byte(1);
        else
            *PCTP_Par++ = TOUCH_Read_Byte(0);
    }
    TOUCH_Stop();
}


unsigned char TOUCH_Read_Reg(unsigned char RegIndex)
{
    unsigned char receive=0;

    TOUCH_Start();
    TOUCH_Send_Byte(WRITE_ADD);
    TOUCH_Wait_Ack();
    TOUCH_Send_Byte(RegIndex);
    TOUCH_Wait_Ack();

    TOUCH_Start();
    TOUCH_Send_Byte(READ_ADD);
    TOUCH_Wait_Ack();
    receive=TOUCH_Read_Byte(1);
    TOUCH_Stop();

    return receive;
}


void ft5x0x_i2c_txdata(unsigned char *txdata, unsigned char length)
{
    unsigned char ret =0;	UINT num;

    TOUCH_Start();
    TOUCH_Send_Byte(WRITE_ADD);
    TOUCH_Wait_Ack();
    for(num=0;num<length;num++)
    {
        TOUCH_Send_Byte(*txdata++);
        TOUCH_Wait_Ack();
    }
    TOUCH_Stop();
    Delay1ms(2);
}

unsigned char ft5x0x_i2c_rxdata(unsigned char *rxdata, unsigned char length)
{
    unsigned char num;

    TOUCH_Start();
    TOUCH_Send_Byte(READ_ADD);
    TOUCH_Wait_Ack();
    for(num=0;num<length;num++)
    {
        if(num==(length-1))
            *rxdata++ = TOUCH_Read_Byte(0);
        else
            *rxdata++ = TOUCH_Read_Byte(1);
    }

    TOUCH_Stop();

    return num;
}

unsigned char ft5x0x_read_data(void)
{
    unsigned char buf[32] = {0}; unsigned char ret = 0;

    #ifdef CONFIG_FT5X0X_MULTITOUCH
        TOUCH_RdParFrPCTPFun(buf, 31);
    #else
        TOUCH_RdParFrPCTPFun(buf, 7);
    #endif

    ts_event.touch_point = buf[2] & 0xf;

    if (ts_event.touch_point == 0)
    {
        return 0;
    }

    #ifdef CONFIG_FT5X0X_MULTITOUCH
    switch (ts_event.touch_point)
    {
        case 5:
        ts_event.x5 = (UINT)(buf[0x1b] & 0x0F)<<8 | (UINT)buf[0x1c];
        ts_event.y5 = (UINT)(buf[0x1d] & 0x0F)<<8 | (UINT)buf[0x1e];

        case 4:
        ts_event.x4 = (UINT)(buf[0x15] & 0x0F)<<8 | (UINT)buf[0x16];
        ts_event.y4 = (UINT)(buf[0x17] & 0x0F)<<8 | (UINT)buf[0x18];

        case 3:
        ts_event.x3 = (UINT)(buf[0x0f] & 0x0F)<<8 | (UINT)buf[0x10];
        ts_event.y3 = (UINT)(buf[0x11] & 0x0F)<<8 | (UINT)buf[0x12];

        case 2:
        ts_event.x2 = (UINT)(buf[9] & 0x0F)<<8 | (UINT)buf[10];
        ts_event.y2 = (UINT)(buf[11] & 0x0F)<<8 | (UINT)buf[12];

        case 1:
        ts_event.x1 = (UINT)(buf[3] & 0x0F)<<8 | (UINT)buf[4];
        ts_event.y1 = (UINT)(buf[5] & 0x0F)<<8 | (UINT)buf[6];

        break;
        default:
        return 0;
    }
    #else

    if (ts_event.touch_point == 1)
    {
        ts_event.x1 = (UINT)(buf[3] & 0x0F)<<8 | (UINT)buf[4];
        ts_event.y1 = (UINT)(buf[5] & 0x0F)<<8 | (UINT)buf[6];
        ret = 1;
    }
    else
    {
        ts_event.x1 = 0xFFFF;
        ts_event.y1 = 0xFFFF;
        ret = 0;
    }
    #endif


    return ret;
}


void inttostr(UINT value,unsigned char *str)
{
    *str++ = (value/100)+48;
    *str++ = ((value%100)/10)+48;
    *str   = ((value%100)%10)+48;
}


////////////////////////////////////

char CheckPen(void)
{
    if(isPEN()==0)										//Detect the occurrence of an interrupt
    {
        ts_event.Key_Sta=Key_Down;
        return 0;
    }
    return 1;
}

void TOUCH_Init(void)
{
    SDA_HIGH;
    SCL_HIGH;

/*
        TOUCH_Start();
    TOUCH_Send_Byte(WRITE_ADD);
    TOUCH_Wait_Ack();
    TOUCH_Send_Byte(FT5206_DEVICE_MODE);
    TOUCH_Wait_Ack();
    TOUCH_Send_Byte(0);
    TOUCH_Wait_Ack();

    TOUCH_Stop();
    delayus(100);
*/
    //RESET_LOW();
    //Delay1ms(10);
    //RESET_HIGH();
    //Delay1ms(10);
}

/*
void TPTEST(void)
{
	unsigned char ss[3];
	 IT0=1;        //Falling edge trigger
	 EX0=1;
	 EA=1;

	 TOUCH_Init();

        Active_Window(0,799,0,479);//Set the working window size
        Text_Foreground_Color1(color_white);//Set the foreground color
        Text_Background_Color1(color_black);//Set the background color
        Write_Dir(0X8E,0X80);//Began to clear the screen (display window)
        Chk_Busy();


	while(next)
	{
            Write_Dir(0x40,0x80);//Set the character mode
            Write_Dir(0x21,0x10);//Select the internal CGROM  ISO/IEC 8859-1.
            Write_Dir(0x22,0x00);//Full alignment is disable.The text background color . Text don't rotation. 2x zoom
            FontWrite_Position(40,200);
            String("Touch to display the coordinate");

            if(isPEN() == 0)        //The touch screen is pressed
            {
                    EX0=0;//Close interrupt
                    do
                    {
                            ft5x0x_read_data(ts_event);
                            ts_event.Key_Sta=Key_Up;


                            inttostr(ts_event.x1,ss);

                            FontWrite_Position(100,60);   //Set the display position
                            LCD_CmdWrite(0x02);
                            String("X = ");
                            LCD_DataWrite(ss[0]);
                            //Delay1ms(1);
                            LCD_DataWrite(ss[1]);
                            //Delay1ms(1);
                            LCD_DataWrite(ss[2]);
                            //Delay1ms(1);

                            inttostr(ts_event.y1,ss);
                            FontWrite_Position(100, 140);   //Set the display position
                            LCD_CmdWrite(0x02);
                            String("Y = ");
                            LCD_DataWrite(ss[0]);
                            //Delay1ms(1);
                            LCD_DataWrite(ss[1]);
                            //Delay1ms(1);
                            LCD_DataWrite(ss[2]);
                            //Delay1ms(1);



                            Draw_Big_Point(ts_event.x1,ts_event.y1,color_red);
                            Draw_Big_Point(ts_event.x2,ts_event.y2,color_green);
                            Draw_Big_Point(ts_event.x3,ts_event.y3,color_blue);
                            Draw_Big_Point(ts_event.x4,ts_event.y4,color_cyan);
                            Draw_Big_Point(ts_event.x5,ts_event.y5,color_purple);


                    }while(PEN==0);
                    EX0=1;
            }



    }



}
 * */
<file_sep>RA8875, PIC32MX795F512H
8080 Mode (Parallel 8 BIT)
Includes Capacitive Touch code / Test

Working!

-7/8/2014... Added FatFS and Keyboard
