#include <24FJ64GA002.h> #fuses FRC,NOJTAG,OSCIO,NOWDT,NOPROTECT//内部クロック #use delay(internal=8000000) //クロック8MHz #pin_select U1TX=PIN_B7 #pin_select U1RX=PIN_B6 #use RS232(BAUD=9600,xmit=PIN_B7,rcv=PIN_B6) /* #pin_select U1TX=PIN_B7 #pin_select U1RX=PIN_B6 #use RS232(BAUD=9600,xmit=PIN_B7,rcv=PIN_B6) */ #define WT 5 #define CSc PIN_B10 #define DIc PIN_B13//SDATA #define CLKc PIN_B11 #define RESc PIN_B12 #define CS PIN_B5 #define DI PIN_B4//SDCのDIに繋がってる #define CLK PIN_B3 #define SDO PIN_A2//SDCのDOに繋がってる #define DISON 0xaf // 1. DIcsplay on P0 #define DISOFF 0xae // 2. DIcsplay off P0 #define DISINV 0xa7 // 4. Inverse DIcsplay P0 #define COMSCN 0xbb // 5. Common scan DIcrection P1 #define DISCTL 0xca // 6. DIcsplay control P4 (データシートp.30のP3は間違い) #define SLPOUT 0x94 // 8. Sleep out P0 #define PASET 0x75 // 9. Page AddREScs set P2 #define CASET 0x15 // 10.Column AddREScs set P2 #define DATCTL 0xbc // 11.Data scan DIcrection, etc. P3 #define RAMWR 0x5c // 13.Writing to memory P:data #define OSCON 0xd1 // 21.Internal oscillation on P0 #define PWRCTR 0x20 // 23.Power control P1 #define VOLCTR 0x81 // 24.Electronic colume control P2 //int8 cmd_list[8]={0,1,16,17,24}; // 0リセット 1初期化 16ブロック長 17リード 24ライト] int8 indata[512]; int8 file[10][32]; void w_spi8(int8 cmd){ int j; output_low(CS); for(j=0;j<8;j++){ if(cmd&0x80) output_high(DI); else output_low(DI); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); cmd<<=1; } //output_high(CS); } void dum8(){ int j; output_high(CS); for(j=0;j<8;j++){ output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); } output_low(CS); } void w_spi(int data){//16bitのデータ送信 int j; output_low(CS); for(j=0;j<16;j++){ if(data&0x8000) output_high(DI); else output_low(DI); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); data<<=1; } //output_high(CS); } void w_spi32(long data){//32bitのデータ送信 int j; output_low(CS); for(j=0;j<32;j++){ if(data&0x80000000) output_high(DI); else output_low(DI); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); data<<=1; } //output_high(CS); } int8 get_spi(){//spiデータを得る int j; int8 data; data=0; output_low(CS);//CSアサートにして output_high(DI); /* while(input(SDO)==1){//0がくるまで待ち output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); }*/ for(j=0;j<7;j++){ if(input(SDO)==0)data=data|0; else data=data|1; //data=data|input(SDO); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); data<<=1; } data=data|input(SDO); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); //output_high(CS); return data; } void s_read(long address){ int8 res; int j; res=0xff; get_spi();//8クロック流す get_spi();//念のためもう一回 get_spi();//8クロック流す get_spi();//念のためもう一回 res=0xff;dum8(); w_spi8(0x51);//CMD17 w_spi32(address*512); w_spi8(0); while(res==0xff) res=get_spi(); //============================================================= //確認用 res=0xff;dum8(); //============================================================= while(res==0xff) res=get_spi(); for(j=0;j<512;j++){ indata[j]=get_spi(); } } void s_write(long address,int8 data[]){//シングルスレッドライト int8 res=0xff; int j; get_spi();//8クロック流す get_spi();//念のためもう一回 res=0xff;dum8(); w_spi8(0b01011000);//CMD24 w_spi32(address); w_spi8(0); while(res==0xff) res=get_spi();//レスポンス得る //====================================================== printf("test:%x\n\r",res); delay_ms(200);res=0xff; //====================================================== get_spi();//8bitクロック output_low(CS); output_low(DI); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); for(j=0;j<512;j++){ w_spi8(data[j]); } res=0xff; while(res==0xff) res=get_spi();//レスポンス得る //====================================================== //====================================================== while(res!=0xff) res=get_spi();//busy待ち get_spi();//8bitクロック get_spi();//念のためもう一回 } int8 get_spi2(){//spiデータを得る int j; int8 data; data=0; output_low(CS);//CSアサートにして output_high(DI); for(j=0;j<16;j++){ output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); } for(j=0;j<7;j++){ if(input(SDO)==0)data=data|0; else data=data|1; //data=data|input(SDO); output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); data<<=1; } data=data|input(SDO); //output_high(CS); return data; } void init(){ int8 makedata[512]; int8 crc; int j,i; long a1,a2,a3; int8 res,res2=1; crc=0;res=0xff; delay_ms(200);//電源安定待ち for(j=0;j<512;j++) makedata[j]=0; for(j=0;j<20;j++) makedata[j]=0x63; output_high(DI); output_high(CS); for(j=0;j<90;j++){//74クロック以上入れる output_high(CLK); delay_us(WT); output_low(CLK); delay_us(WT); } output_low(CS); w_spi8(0x40);//CMD0 w_spi32(0); w_spi8(0x95);//CRC while(res==0xff) res=get_spi();//レスポンス受ける //================================================== //確認用 //printf("%x\n\r",res); // delay_ms(200); //================================================== res=0; while(1){res=0xff; dum8(); w_spi8(0x41);//CMD1 w_spi(0); w_spi(0); //w_spi8(0b11111001); //crc++; while(res==0xff) res=get_spi();//レスポンス受ける res2<<=1; if(res==0) break; //============================================================= //確認用 // printf("%x::%x\n\r",res,res2); // delay_ms(200); //============================================================= } //================================================ //確認用 //output_high(PIN_B11); //=============================================== res=0xff;dum8(); w_spi8(0x50);//CMD16 w_spi(0x0000); //512byte受信に設定 w_spi(0x0200); w_spi8(0); while(res==0xff) res=get_spi(); //============================================================= //確認用 //============================================================= res=0xff;dum8(); w_spi8(0x51);//CMD17 w_spi(0x0000); //受信モードにセット w_spi(0x0000); w_spi8(0); while(res==0xff) res=get_spi(); res=0xff; //============================================================= //確認用 printf("CMD17:%x:\n\r",res); delay_ms(200);res=0xff;dum8(); //============================================================= while(res==0xff) res=get_spi(); for(j=0;j<512;j++){ indata[j]=get_spi(); } output_high(DI); // for(j=446;j<461;j++){ // printf("%x ",indata[j]); // delay_ms(200); // } //delay_ms(200); a1=indata[454]&0xff; get_spi();//8クロック流す get_spi();//念のためもう一回 res=0xff;dum8(); w_spi8(0x51);//CMD17 w_spi32(a1*512); w_spi8(0); while(res==0xff) res=get_spi(); //============================================================= //確認用 printf("CMD17:%x:\n\r",res); delay_ms(200); res=0xff;dum8(); //============================================================= while(res==0xff) res=get_spi(); for(j=0;j<512;j++){ indata[j]=get_spi(); } a2=indata[22]&0xff; get_spi();//8クロック流す get_spi();//念のためもう一回 res=0xff;dum8(); w_spi8(0x51);//CMD17 w_spi32((0x02e0)*512); w_spi8(0); while(res==0xff) res=get_spi(); res=0xff; //============================================================= //確認用 printf("%x\n\r",a2); delay_ms(200); //============================================================= while(res==0xff) res=get_spi(); for(i=0;i<10;i++){ for(j=0;j<32;j++){ file[i][j]=get_spi(); } } for(i=0;i<512-320;i++) get_spi(); for(i=0;i<10;i++){ if(file[i][0]!=0xe5&&file[i][0]!=0){ a3=file[i][27]&0xff; a3<<=8; a3=a3|(file[i][26]&0xff); } } s_read(0x300); for(j=0;j<30;j++){ printf("%x:",indata[j]); delay_ms(200); } //s_write((a1+a2+a2+33)*512,makedata); } void s_read2(long address){ int8 res; int j; res=0xff; get_spi();//8クロック流す get_spi();//念のためもう一回 get_spi();//8クロック流す get_spi();//念のためもう一回 while(1){ res=0xff;dum8(); w_spi8(0x51);//CMD17 w_spi32(address*512); w_spi8(0); while(res==0xff) res=get_spi(); if(res==0) break; } res=0xff;dum8(); //============================================================= while(res==0xff) res=get_spi(); /* for(j=0;j<1563;j++){ indata3[j]=get_spi(); }*/ } void s_change(){ int8 res; int j; res=0xff; get_spi();//8クロック流す get_spi();//念のためもう一回 get_spi();//8クロック流す get_spi();//念のためもう一回 while(1){ res=0xff;dum8(); w_spi8(0x50);//CMD16 w_spi32(512*3); w_spi8(0); while(res==0xff) res=get_spi(); if(res==0) break; printf("change:%x\n\r",res); delay_ms(200); } res=0xff;dum8(); //============================================================= while(res==0xff) res=get_spi(); // for(j=0;j<512;j++){ // indata[j]=get_spi(); // } } void w_spi8c(int8 cmd){ int j; output_low(CSc); output_low(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); for(j=0;j<8;j++){ if(cmd&0x80) output_high(DIc); else output_low(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); cmd<<=1; } output_high(CSc); } void w_spi82c(int8 cmd){ int j; output_low(CSc); output_high(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); for(j=0;j<8;j++){ if(cmd&0x80) output_high(DIc); else output_low(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); cmd<<=1; } output_high(CSc); } void w_spic(int data){//16bitのデータ送信 int j; output_low(CSc); output_low(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); for(j=0;j<16;j++){ if(data&0x8000) output_high(DIc); else output_low(DIc); output_high(CLKc); delay_us(WT); output_low(CLKc); delay_us(WT); data<<=1; } //output_high(CSc); } void window(int8 x,int8 y,int8 x1,int8 y1){ w_spi8c(CASET); w_spi82c(x); w_spi82c(x1); w_spi8c(PASET); w_spi82c(y+2); w_spi82c(y1+2); } void box(int8 x,int8 y,int8 x1,int8 y1,int color){ int j; //color2=color>>8; window(x*8,y*8,x1*8,y1*8); w_spi8c(RAMWR); for(j=0;j<(x1*8-x*8)*(y1*8-y*8);j++){ w_spi82c(color>>8); w_spi82c(color); } } void image(int8 x,int8 y,int8 x1,int8 y1){ int j; int j2; long add; int8 g; int test=0; window(x*8,y*8,x1*8,y1*8); printf("image%x\n\r",g); delay_ms(200); // s_change();//1536byteづつに変える printf("mage2:%x\n\r",g); delay_ms(200); w_spi8c(RAMWR); add=0x340; s_read2(add);add++; for(j2=0;j2<54;j2++) get_spi();//54流す test=54; for(j=0;j<128*128;j++){ if(j%128==0){ w_spi82c(0x00); w_spi82c(0x00); } w_spi82c(0x0f&(get_spi()>>4));test++; if(test%512==0){ s_read2(add);add++; } g=0xf0&get_spi();test++; if(test%512==0){ s_read2(add);add++; } w_spi82c(g|(0x0f&(get_spi()>>4)));test++; if(test%512==0){ s_read2(add);add++; } }/* for(add=0x343;add<0x3a0;add+=3){ s_read2(add); for(j=0;j<512;j++){ w_spi82c(0x0f&(get_spi()>>4)); g=0xf0&get_spi(); w_spi82c(g|(0x0f&(get_spi()>>4))); //test++; printf("%x\n\r",g); delay_ms(200); } }*/ } void initc(){ long test=0,test2=0; int j=0,i=0; long add; int8 g; output_high(CSc); output_low(RESc); output_high(DIc); delay_us(100); output_high(RESc); delay_us(100); w_spi8c(DISCTL); w_spi82c(3); w_spi82c(32); w_spi82c(121); w_spi82c(0); w_spi8c(COMSCN); w_spi82c(1); w_spi8c(OSCON); w_spi8c(SLPOUT); w_spi8c(PWRCTR); w_spi82c(15); w_spi8c(DISINV); w_spi8c(DATCTL); w_spi82c(0); w_spi82c(0); w_spi82c(4); w_spi8c(VOLCTR); w_spi82c(33); w_spi8c(1); w_spi8c(CASET); w_spi82c(0); w_spi82c(131); w_spi8c(PASET); w_spi82c(0); w_spi82c(131); w_spi8c(RAMWR); for(j=0;j<132*132;j++){ w_spi82c(0x09); w_spi82c(0x87); } /* add=0x340; s_read(0x340); for(j=54;j<510;j++){ w_spi82c(0x0f&(indata[j++]>>4)); g=0xf0&indata[j++]; w_spi82c(g|(0x0f&(indata[j]>>4))); test++; }add++; for(add=0x341;add<0x3a0;add++){ s_read(add); for(j=0;j<510;j++){ w_spi82c(0x0f&(indata[j++]>>4)); g=0xf0&indata[j++]; w_spi82c(g|(0x0f&(indata[j]>>4))); test++; } test2++; test=0; } s_read(add); for(j=0;j<1040;j++){ w_spi82c(0x0f); w_spi82c(0xff); test++; }*/ delay_ms(10); w_spi8c(DISON); } void main(){ init(); initc(); while(1){ image(0,0,16,16); } }