    /**********************************************
* Photo Fram V0.91 2023/07/27
* PIC24FJ128GB106 family 
* 
* 480x320 TFT LCD display program with SD card driver ST9776S
*
 
     * 
***********************************************/
// PIC24FJ128GB106 Configuration Bit Settings

// 'C' source line config statements
/*
// CONFIG3
#pragma config WPFP = WPFP511           // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
#pragma config WPDIS = WPDIS            // Segment Write Protection Disable bit (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS         // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
#pragma config WPEND = WPENDMEM         // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)

// CONFIG2
#pragma config POSCMOD = NONE           // Primary Oscillator Select (Primary oscillator disabled)
#pragma config DISUVREG = OFF           // Internal USB 3.3V Regulator Disable bit (Regulator is disabled)
#pragma config IOL1WAY = OFF            // IOLOCK One-Way Set Enable bit (Unlimited Writes To RP Registers)
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
#pragma config FNOSC = FRCPLL           // Oscillator Select (Fast RC oscillator with Postscaler and PLL module (FRCPLL))
#pragma config PLL_96MHZ = ON           // 96MHz PLL Disable (Enabled)
#pragma config PLLDIV = DIV2            // USB 96 MHz PLL Prescaler Select bits (Oscillator input divided by 2 (8MHz input))
#pragma config IESO = OFF               // Internal External Switch Over Mode (IESO mode (Two-speed start-up)disabled)

// CONFIG1
#pragma config WDTPS = PS32768          // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128            // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = OFF             // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (Watchdog Timer is disabled)
#pragma config ICS = PGx1               // Comm Channel Select (Emulator functions are shared with PGEC1/PGED1)
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF                // General Code Segment Code Protect (Code protection is disabled)
//#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG port is disabled)
*/
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <string.h>

#define FCY 16000000UL// libpic30.hの前にFosc/2を定義　__delay関数用
#include <libpic30.h> 
#include "Font7.h"
#include "SDC_main.h"
#include "BMP_display_system.h"

 



#define LCD_CS LATEbits.LATE6
#define LCD_DC LATEbits.LATE7
#define LCD_RES LATGbits.LATG6
#define GLED LATBbits.LATB8
#define TEST LATDbits.LATD2
#define SD_CD PORTDbits.RD1
#define KDIR PORTBbits.RB12
#define KUP PORTBbits.RB13
#define KDOWN PORTBbits.RB14
#define KENTER PORTBbits.RB15
#define us unsigned

us char lastKDIR,lastKUP,lastKDOWN,lastKENTER;
us char mojicolor,backcolor;
us char No_media,SD_CK,lastSD_CD,CHATA;
unsigned int FX,FY;//フォント表示の開始座標
unsigned int GX,GY,GIFx,GIFy;//グラフ表示座標　ピクセルサイズ　x,y
unsigned int F_col,F_row;
us char lcd_digi[10];
//us char coment[]="happy new year";
us int lastmemory[64];
const __attribute__((section(".Data_Memory"), space (prog), address (0x9600) )) unsigned int _flash_datas[96*8];
int lastdispmax,lastdispmin;
us char str[32];

us char color[17][3];//18bit color data
us char readFname[32][256];
int maxFname,FnameIndex;
int openid,Hgif,Vgif,offset;//

us int xt,yt,st;
//uint8_t fname8[32];
//関数宣言
void disp_dir_fname(int idx);
void File_open(int index);
void INC_Fname_disp(int yd);
void DEC_Fname_disp(int yd);


unsigned char color0[16][3]= {//24bit color data RGB
	{0,0,0},//0 黒
	{255,0,0},//1　赤
	{0,255,0},//2　緑
	{0,100,255},//3　青
	{255,255,0},//4　黄
	{0,255,255},//5　シアン
	{255,0,255},//6　紫
	{255,255,255},//7　白
	{0,40,41},//8　紺
	{120,0,0},//9　茶
	{0,50,0},//10　
	{120,120,0},//11　
	{0,30,50},//12
	{120,0,120},//13
	{60,60,60},//14
	{255,70,0}//15
  
};

/////////////////////////////////////////////フラッシュメモリー読み書き
void Flash_Read(unsigned char LN) {
 unsigned int i=0;

    unsigned int page = __builtin_tblpage(&_flash_datas);
    unsigned int offset = __builtin_tbloffset(&_flash_datas);
      // フラッシュメモリー読み込み
    TBLPAG = page;
    for (i=0; i<=LN; i++) {
        lastmemory[i] = __builtin_tblrdl(offset + i*2);
    }
}

void Flash_Write(unsigned char LN) {
    unsigned int i=0;
    unsigned int page = __builtin_tblpage(&_flash_datas);
    unsigned int offset = __builtin_tbloffset(&_flash_datas);

    
    // ページ消去
    TBLPAG = page;
    __builtin_tblwtl(offset, 0x0000); // 
    NVMCON = 0x4042;
    asm volatile ("disi #5");
    __builtin_write_NVM();          //erase 8line = 64*8
    while(NVMCONbits.WR);
 
    // ラストメモリー書き込み
    NVMCON = 0x4003;
    TBLPAG = page;
    for (i=0; i<=LN; i++) {
        __builtin_tblwtl(offset + i*2, lastmemory[i]);  // *2 偶数
        __builtin_tblwth(offset + i*2, 0xFF);
   
    asm volatile ("disi #5");
    __builtin_write_NVM();
    while(NVMCONbits.WR);
    }
}
/////////////////////////////////////////////フラッシュメモリー読み書きEND


void lcd_CMD(unsigned char CM) {

    LCD_DC = 0;    
	LCD_CS = 0;
	
	SPI2BUF = CM;
    __delay_us(2) ;
	LCD_CS = 1;
}
void lcd_DATA(unsigned char DA) {

    LCD_DC = 1;
	LCD_CS = 0;
	
	SPI2BUF = DA;
    __delay_us(2) ;
	LCD_CS = 1;

}
void init_LCD() {
	LCD_RES = 0;
	__delay_ms(120) ;

	LCD_RES = 1;
	__delay_ms(120) ;

    lcd_CMD(0x01);
   
	__delay_ms(120) ;
    
	lcd_CMD(0x11);//Sleep Out
    lcd_CMD(0x28);// display off
	__delay_ms(120) ;
	lcd_CMD(0x36);// MADCTL Memoru Data Access Control
     
	lcd_DATA(0b01001000);
    lcd_CMD(0x3A);// COLMOD Interface Pixel Format
	lcd_DATA(0x66);

	lcd_CMD(0x2A);// CASET Column Address Set
	lcd_DATA(0x00);
	lcd_DATA(0x00);
	lcd_DATA(0x01);//    01
	lcd_DATA(0xDF);//479 DF
	lcd_CMD(0x2B);// RASET Row Address Set
	lcd_DATA(0x00);
	lcd_DATA(0x00);
	lcd_DATA(0x01);//     01
	lcd_DATA(0x3F);//319  3F

	__delay_ms(200) ;
	
}



void itostring(unsigned char digit, unsigned long int data,unsigned  char *buffer) {
	unsigned char i;
	buffer += digit;
	*buffer =0;					// 最後の数字位置
	for(i=digit; i>0; i--) {			// 変換は下位から上位へ
            buffer--;					// ポインター１
            *buffer = (data % 10) + '0';	// ASCIIへ
            data = data / 10;				// 次の桁へ
	}
}
void set_col_range(unsigned int start, unsigned int end) {//行範囲の指定
	lcd_CMD(0x002A);
	lcd_DATA(start >> 8);
	lcd_DATA(start);
	lcd_DATA(end >> 8);
	lcd_DATA(end);
	}
void set_row_range(unsigned int start, unsigned int end) {//列範囲の指定
	lcd_CMD(0x002B);
	lcd_DATA(start >> 8);
	lcd_DATA(start);
	lcd_DATA(end >> 8);
	lcd_DATA(end);
	}

void set_LCDcolor(unsigned char C) {//24bitRGB?16bitRGB
	unsigned int c0;
    us int cr,cg,cb;
    c0 = 0;
	cr = color0[C][0];//赤
	cg = color0[C][1];//緑 
	cb = color0[C][2];//青
/*    cr =255- color0[C][0];//赤
	cg =255- color0[C][1];//緑 
	cb =255- color0[C][2];//青
    */
	color[C][0] = cr;
	color[C][1] = cg;
    color[C][2] = cb;
    
}

void makecolorList(){
	us char i;
	for (i = 0;i <= 15;i++){
		set_LCDcolor(i);
	}
}
void write_color(unsigned char C) {
	lcd_DATA(color[C][0]);
	lcd_DATA(color[C][1]);
    lcd_DATA(color[C][2]);
}
void select_font_mk2(unsigned int F) {
    switch (F) {
        case  7:F_col=6;F_row=9;break;//実際のフォントドットから-1した値
        case  6:F_col=5;F_row=9;break;
        case 12:F_col=11;F_row=21;break;
    }
}
void mojidisp_mk2(us char f,us char ch,us char spn) {//BLOCK文字表示
	int k;
    const unsigned char *fc;
	int x,y,ma,mj;

	set_col_range(FX,FX+F_col);
 	set_row_range(FY,FY+F_row);
 	lcd_CMD(0x2C);//色データ書き込み

    switch (f) {
        case 7:fc = Font7_table[ch];break;
//		case 6:fc = Font6_table[ch];break;
//        case 12:fc = Font12_table[ch];break;
     }

	mj = F_col;

 	for (y=0;y<=F_row;y++) {
 		mj = F_col;
 		for (x=0;x<=F_col;x++) {
 			ma = mj % 8;//ma:mjを8で割り算した余り
 			k=(*fc) & (1<< ma);
 				if (k>0) {
                    write_color(mojicolor);//文字色
 				} else {
 					write_color(backcolor);//背景色
					
 				}
                mj--;
                if (ma == 0) {if (x < F_col) {fc++;}}
 			}
                
 		fc++;
 	}
	FX=FX+F_col+spn;
    
}


void mojiretsudisp_mk2( us int x,us int y,us char fnt,  char *str,us char spn) {//  文字列表示
    unsigned char i;
    char len;
	FX = x; FY =y;
	select_font_mk2(fnt);
	len = strlen(str);
    
	for (i=0;i<len;i++) {
        mojidisp_mk2(fnt,str[i],spn);

	}
}


void write_dots(int x,int y,int d,char c) {//x:ｘ座標　y:y座標　d:線幅　c:色番号
	int i,j;
	set_col_range(x,x+d);
 	set_row_range(y,y+d);
	lcd_CMD(0x2C);//色データ書き込み
	for (i=x;i<x+d;i++) {
 		for (j=y;j<=y+d;j++) {
 			write_color(c);//文字色
 		}
	}
}


void integerdisp(us int x, us int y,us char fnt,us char degi, us long int num,us char spn) {
    FX=x;FY=y;
    itostring(degi,num,lcd_digi);//ASCIIに変換
    mojiretsudisp_mk2(x,y,fnt,lcd_digi,spn);
}


void write_line(unsigned int xs,unsigned int ys,unsigned int xe,unsigned int ye,unsigned char c) {
    unsigned int i,j;
    set_col_range(xs,xe);
    set_row_range(ys,ye);
    lcd_CMD(0x2C);//色データ書き込み
    for (i=ys;i<=ye;i++) {
        for (j=xs;j<=xe;j++) {
            write_color(c);
        }
    }
}




us char getKeySw(us char ls,us char No){
 	us char sw,last,cnt;
 	cnt=0;
 	last= ls; //Keyスイッチの状態
	while(cnt<CHATA){
		__delay_ms(5);
 
		switch(No) {
		case 0: sw = KDIR;break;
		case 1: sw = KUP;break;
		case 2: sw = KDOWN;break;
		case 3: sw = KENTER;break;
		}
 		if(last==sw){ // 前回の状態と比較
			cnt++; //安定状態
 			}else{
			cnt=0; //チャタリング発生中
 			}
 		last=sw; // 今の状態を前回の状態とする
 		}
	return sw;
 	}
 void selectKDIR() {
	unsigned char sw,id;
    id = 0;
	if (KDIR == 1) {lastKDIR = 1; return;}
	if (KDIR == lastKDIR) {return;}
	sw = getKeySw(KDIR,0);
	lastKDIR = sw;
	if ((sw == 0) && (KDIR == 0)) {
    
        __delay_ms(200) ;
		SD_CK = finnd_SD_card();

		if (SD_CK == 1){
			disp_dir_fname(0);
		}
	}
}
 void selectKUP() {
	int sw,ya,yd;
	if (KUP == 1) {lastKUP = 1; return;}
	if (KUP == lastKUP) {return;}
	sw = getKeySw(KUP,1);
	lastKUP = sw;
	if (sw == 0) {
        if(FnameIndex == 0){return;}

        ya = FnameIndex%20;
        FX=60;FY=25+(15*ya);
        strcpy(str,readFname[FnameIndex]);mojicolor=7;
        mojiretsudisp_mk2(FX,FY,7,str,2);// FX,FY,Font番号,文字スパン
        if (FnameIndex != 0){FnameIndex--;}
        if (FnameIndex < lastdispmin) {
            yd = lastdispmin -20;
            DEC_Fname_disp(yd);
/*            
FX =200;FY=25;integerdisp(FX, FY,7,3, FnameIndex, 2);//FX,FY,Font番号,桁数,数値,スパン
FX =200;FY=40;integerdisp(FX, FY,7,3, lastdispmin, 2);//FX,FY,Font番号,桁数,数値,スパン
FX =200;FY=55;integerdisp(FX, FY,7,3, lastdispmax, 2);//FX,FY,Font番号,桁数,数値,スパン*/
            return;
        }else{
        ya = FnameIndex%20;
        FX = 60;FY=25+(15*ya);
        strcpy(str,readFname[FnameIndex]);mojicolor=2;
        mojiretsudisp_mk2(FX,FY,7,str,2);// FX,FY,Font番号,文字スパン
        }
	}
}
void selectKDOWN() {
	int sw,ya;
	if (KDOWN == 1) {lastKDOWN = 1; return;}
	if (KDOWN == lastKDOWN) {return;}
	sw = getKeySw(KDOWN,2);
	lastKDOWN = sw;
	if (sw == 0) {

        if (FnameIndex == maxFname -1){return;}
        ya = FnameIndex%20;
        FX=60;FY=25+(15*ya);mojicolor=7;
        strcpy(str,readFname[FnameIndex]);
        mojiretsudisp_mk2(FX,FY,7,str,2);// FX,FY,Font番号,文字,スパン
        FnameIndex++;ya = FnameIndex%20;
   //     if (FnameIndex > maxFname){FnameIndex=0;return;}
        
        if (FnameIndex > lastdispmax) {
            INC_Fname_disp(FnameIndex);return;
        }else{
            FX=60;FY=25+(15*ya);
            strcpy(str,readFname[FnameIndex]);mojicolor=2;
            mojiretsudisp_mk2(FX,FY,7,str,2);// FX,FY,Font番号,文字,スパン
        }
	}
}
void selectKENTER() {
	unsigned char sw;
	if (KENTER == 1) {lastKENTER = 1; return;}
	if (KENTER == lastKENTER) {return;}
	sw = getKeySw(KENTER,3);
	lastKENTER = sw;
	if (sw == 0) {
        File_open(FnameIndex);
	}
}


 void FnameSort(unsigned char n) {
    int i, j;
    char temp[32];           //  作業用ポインタ 
	
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if ((strcmp(readFname[j], readFname[i])) > 0) {
                strcpy(temp, readFname[i]);                
                strcpy(readFname[i], readFname[j]);            
                strcpy(readFname[j], temp);               
            }
        }
      
    }
}

 void INC_Fname_disp(int yd){
    int ymax,y,yc;
    
    write_line(0,0,160,330,8);//Xs,Ys,Xe,Ye,Color 
    FnameIndex = yd;
    if (maxFname > yd +20){ymax = yd +20;}else{ymax = maxFname-yd;}
     for (y=0;y < ymax;y++) {
        
         find_fname_disp(y+FnameIndex); 
//         FnameIndex++; 
        }
    lastdispmax = yd + ymax - 1;
    lastdispmin = yd;
}
 
 void DEC_Fname_disp(int ys){
    int ymax,y,yd;
    yd = ys;
    write_line(0,0,160,330,8);//Xs,Ys,Xe,Ye,Color 
//    FnameIndex = yd;
//    ymax = yd;
//    if ((yd - 19) < 1){ys=0;}else {ys = yd -19;}
     for (y=0;y < 20;y++) {
        
         find_fname_disp(y+yd); 
//         FnameIndex++; 
        }
    lastdispmax = yd;
    lastdispmin = yd - 19;
//    FnameIndex = yd +19;
//    find_fname_disp(FnameIndex); 
}
 
void find_fname_disp(int yd) {//ydは表示するFnameIndex
    int xd,ya,yc;
    ya = yd%20;

    xd=15;yc = 25+(15*ya);
    write_line(0,yc-2,160,yc + 12,8);//Xs,Ys,Xe,Ye,Color 

     FX=15;
     FY=25+(15*ya);
     mojicolor=7;
     integerdisp(FX, FY,7,3, yd, 2);//FX,FY,Font番号,桁数,数値,スパン
     if (yd == FnameIndex){mojicolor=2;}else{mojicolor=7;}
     FX = 60;
      mojiretsudisp_mk2(FX,FY,7,readFname[yd],2);// FX,FY,Font番号,文字,スパン

}

void disp_dir_fname(int idx){
    int yd,ymax;
    lcd_CMD(0x36);
    lcd_DATA(0b01001000);

    ymax=maxFname;
    if (maxFname > 19) {ymax = 20;}else{ymax = maxFname;}
    FnameIndex= idx;

    write_line(0,0,120,25,8);//Xs,Ys,Xe,Ye,Color 

    for (yd=0;yd < ymax;yd++) {
        
         find_fname_disp(yd + FnameIndex); 
    //     FnameIndex++;
        }
    lastdispmax = ymax - 1;
    lastdispmin = 0;
    
    write_line(188,20,276,54,8);//Xs,Ys,Xe,Ye,Color 
    backcolor=8;mojicolor=7; 
    FX=200;FY=25;
    mojiretsudisp_mk2(FX,FY,7,"All Files",2);
    FX = 233;FY = 40;
    integerdisp(FX, FY,7,3, maxFname, 2);//FX,FY,Font番号,桁数,数値,スパン 
}
 
 

void Bmpdisplay(int grx, int gry, int col, int row,uint16_t *file) {
  	us char c1,c2,cr,cg,cb,cd;
	int G_col=col-1;
	int G_row=row-1;
	int x,y,xofset,yofset;
    lcd_CMD(0x36);
    lcd_DATA(0b11011000);
    xofset = 0;grx = 0;
    yofset = 0;gry = 0;
    set_col_range(0,319);
    set_row_range(0,479);

    if (col > 320){
        xofset = (col - 320)/2;
        
    }
    if (row > 480){
        yofset = (row - 480)/2;
    }
    if (col < 320){grx = (320-col)/2;
        write_line(0,0,grx,479,8);//Xs,Ys,Xe,Ye,Color 	
        write_line(319-grx,0,319,479,8);//Xs,Ys,Xe,Ye,Color
        set_col_range(grx,grx + G_col);
    }
    
    if (row < 480){gry = (480-row)/2;
        write_line(0,0,319,gry,8);//Xs,Ys,Xe,Ye,Color 	
        write_line(0,479-gry,319,479,8);//Xs,Ys,Xe,Ye,Color
        set_row_range(gry,gry + G_row);
    }

 	lcd_CMD(0x2C);//色データ書き込み
 	for (y=0;y<=G_row;y++) {
            for (x=0;x<=G_col;x++) {
 
                cb=FILEIO_GetChar(file);
                cg=FILEIO_GetChar(file);
                cr=FILEIO_GetChar(file);

                if (((x >= xofset) && (x <= 319+xofset)) && ((y >= yofset) && (y <=479 + yofset))){
                lcd_DATA(cr);
                lcd_DATA(cg);
                lcd_DATA(cb);
                }
            }
 	}
}


void mediacheck() {
    lcd_CMD(0x36);// MADCTL Memoru Data Access Control
	lcd_DATA(0b01001000);
	if (SD_CD != lastSD_CD) {
        backcolor=8;
        mojicolor=7;
        write_line(0,0,130,20,8);//Xs,Ys,Xe,Ye,Color 
        FX=15;FY=10;
        if (SD_CD == 1) {
            mojiretsudisp_mk2(FX,FY,7,"NO MEDIA   ",2);// FX,FY,Font番号,文字スパン
            No_media=1;
            } else {
            mojiretsudisp_mk2(FX,FY,7,"MEDIA READY",2);// FX,FY,Font番号,文字スパン
            No_media=0;
        }
		lastSD_CD = SD_CD;
	}
}
void fname16tofname8(uint16_t *fname){
    unsigned char i ;
    for (i=0;i<32;i++){
    str[i]=(unsigned char)fname[i];
    
    }
}


void set_dir_fname( us char *dir_fname,unsigned int yd){

    maxFname=yd;

    strcpy(readFname[yd],dir_fname);//SDカードより読み込んだファイル名をus charの2次元配列に記憶
    
}

void File_open(int index) {
     if (No_media == 1) {return;}
     strcpy(str,readFname[index]);
     mojicolor=4;
     strcpy(lastmemory,str);
//     mojiretsudisp_mk2(180,25,7,str,2);// FX,FY,Font番号,文字スパン
     Flash_Write(12);
       SD_CK=open_SD_card(readFname[index]);
}	


void testdisp( char *coment,signed int val,us char degi){
    FX=150;FY=40+15*st;
    mojiretsudisp_mk2(FX,FY,7,coment,2);// FX,FY,Font番号,文字スパン
    integerdisp(FX+20, FY,7,degi, val, 2);//FX,FY,Font番号,桁数,数値,スパン
    st++;
}
void 	analysGIFdata(){
//    uint8_t st[6];
    uint8_t i,BitCount,Comp;
    
/*    for(i=0;i<2;i++){
        st[i]=GIFdata[i];
    } 
    st[2]=0;*/
//    testdisp(st,0,1);
    Hgif = GIFdata[18] | GIFdata[19]<<8;
//    testdisp("Hgif",Hgif,3);
    Vgif = GIFdata[22] |GIFdata[23]<<8;
//    testdisp("Vgif",Vgif,3);
    offset = GIFdata[10] | GIFdata[11]<<8;
//    testdisp("Ofset",offset,3);
    BitCount = GIFdata[28] | GIFdata[29]<<8;
//    testdisp("BitCount",BitCount,2);
    Comp = GIFdata[0x1e] | GIFdata[0x1F];
//    testdisp("Comp",Comp,2);

    st = 0;
}
	


 int main()  {
 unsigned int i,j;
 OSCCON = 0b0001000110100001; //Fosc FRCPLL 32MHz

  CLKDIV = 0x0000;
 AD1PCFGL = 0b1111111111111111;//AN無効
 RPOR9bits.RP19R = 11;//RP19にSCK2を割り当て
 RPOR13bits.RP26R = 10;//RP26にSDO2を割り当て

 SPI2BUF = 0x0;//SP2バッファークリアー
 
 SPI2CON1 = 0b0000000001111011;//Active_Low SDO使用。8bit、マスターモード SP1/2 PP1/1
 SPI2CON2 = 0b000000000000000;//フレーム未使用

 SPI2STATbits.SPIEN = 1;//SPI2有効化


 
 PORTB  = 0b0000000000000000;  //ポートB　L出力
 TRISB  = 0b1111000000000000; //RB12-15 input その他全て出力設定
 PORTE   =0b0000000000000111; //ポートC　出力設定
 TRISE  = 0b0000000000000000;//ポートE全て出力	
 TRISF  = 0b0000000000000100; //RF2 U1RXに設定
 TRISC  = 0b0000000000000000;  //ポートC　出力
 TRISD  = 0b0000000000000011;  //ポートD　出力
 TRISG  = 0b0000000000000000;  //ポートG　6,7,8出力

 xt=180;yt=50;st=0;
CHATA = 8;
makecolorList();
init_LCD();//LCD初期設定

//***********************************画面クリアー（1番色(白)で全面塗りつぶし）
lcd_CMD(0x002C);//色データ書き込み

    lcd_CMD(0x29);//Display ON

	write_line(0,0,319,479,8);//Xs,Ys,Xe,Ye,Color 	
    mojicolor=7;backcolor = 8;

    mojicolor=4;

    lastSD_CD = 2;

//    fname16tofname8(searchname);
//    year=2025;
    Flash_Read(11);
    mojicolor=4;
//    strcpy(str,lastmemory);
//    mojiretsudisp_mk2(180,25,7,lastmemory,2);// FX,FY,Font番号,文字スパン
    
    mediacheck();
    SD_CK = finnd_SD_card();


    for (i=0;i <= maxFname;i++){//全ファイル名を読み込む
        
        if (strcmp(lastmemory,readFname[i]) == 0){
             SD_CK=open_SD_card(readFname[i]);
             break;
        }
    }
    while(1){

		mediacheck();
    	selectKDIR();
        selectKUP();
        selectKDOWN();
        selectKENTER();
        GLED = ~GLED;
    };
	

}
