Aitendo 1.44inch TFT ZY-FGD1442701V1(ST7735S)を駆動させる. BusOutで接続. Bitmap画像をSDカードから読み込み. Connected with BusOut. Bitmap data read from SD(SDHC) card.

Dependencies:   FatFileSystemCpp SDHC_FileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDHCFileSystem.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 SDFileSystem sd(p5, p6, p7, p8, "sd");
00006 
00007 DigitalOut CS0(p21);
00008 DigitalOut CD(p22);
00009 DigitalOut RD(p23);
00010 DigitalOut WR(p24);
00011 DigitalOut RSTB(p25);
00012 
00013 BusOut Bout(p17, p16, p15, p14, p13, p12, p11, p10);
00014 
00015 Timer t;
00016 
00017 void LCD_Write_CMD(unsigned char cmd)
00018 {
00019     CS0=0;
00020     RD=1;
00021     CD=0;
00022     Bout=cmd;
00023     WR=0;
00024     WR=1;
00025     CS0=1;
00026 }
00027 
00028 void LCD_Write_Data(unsigned char d)
00029 {
00030     CS0=0;
00031     RD=1;
00032     CD=1;
00033     Bout=d;
00034     WR=0;
00035     WR=1;
00036     CS0=1;
00037 }
00038 
00039 void LCD_Reset(void)
00040 {
00041     RSTB=1;
00042     wait_ms(1);
00043     RSTB=0;
00044     wait_ms(1);
00045     RSTB=1;
00046     wait_ms(120);
00047     LCD_Write_CMD(0x11); // Sleep Out and Booster On
00048     wait_ms(120);
00049 }
00050 
00051 void LCD_Init(void)
00052 {
00053     LCD_Reset();
00054 
00055     LCD_Write_CMD(0xB1); //In Normal Mode(Full Colors) Frame rate 80Hz
00056     LCD_Write_Data(0x02);
00057     LCD_Write_Data(0x35);
00058     LCD_Write_Data(0x36);
00059 
00060     LCD_Write_CMD(0xB2); //In Idle Mode(8-colors)
00061     LCD_Write_Data(0x02);
00062     LCD_Write_Data(0x35);
00063     LCD_Write_Data(0x36);
00064 
00065     LCD_Write_CMD(0xB3); //In Partial Mode + Full Colors
00066     LCD_Write_Data(0x02);
00067     LCD_Write_Data(0x35);
00068     LCD_Write_Data(0x36);
00069     LCD_Write_Data(0x02);
00070     LCD_Write_Data(0x35);
00071     LCD_Write_Data(0x36);
00072 
00073     LCD_Write_CMD(0xB4); //Dot Inversion Control
00074     LCD_Write_Data(0x03); // Inversion setting in Idle mode and full Colors partial mode
00075 
00076     LCD_Write_CMD(0xC0); // Power Control Setting (GVDD Voltage)
00077     LCD_Write_Data(0xA2); // AVDD=5, GVDD=4.6
00078     LCD_Write_Data(0x02); // VGCL=-4.6
00079     LCD_Write_Data(0x84); // MODE=AUTO
00080 
00081     LCD_Write_CMD(0xC1); // Power Control Setting (VGH/VGL Voltage)
00082     LCD_Write_Data(0xC5); // VGH and VGL supply power level = 2.4, VGL=-10, VGH=3*AVDD-0.5
00083 
00084     LCD_Write_CMD(0xC2); // In Normal Mode(Full Colors) APA/DCA
00085     LCD_Write_Data(0x0D); // SAP=Small, AP=Large
00086     LCD_Write_Data(0x00); // Clock frequency for Booster circuit/1,/3,/1,/1,/1
00087 
00088     LCD_Write_CMD(0xC3); // In Idle Mode(8-colors) APA/DCA
00089     LCD_Write_Data(0x8D);
00090     LCD_Write_Data(0xEA);
00091 
00092     LCD_Write_CMD(0xC4); // In Partial Mode(Full Colors) APA/DCA
00093     LCD_Write_Data(0x8D);
00094     LCD_Write_Data(0xEE);
00095 
00096     LCD_Write_CMD(0xC5); // VCOM
00097     LCD_Write_Data(0x05); // -0.55
00098 
00099     LCD_Write_CMD(0x36); // Memory Data Access Control
00100     LCD_Write_Data(0x48); // MX, RGB mode (Row Address Order, RGB color filter panel)
00101 
00102     LCD_Write_CMD(0xE0); // Gamma Adjustment (+Polarity)
00103     LCD_Write_Data(0x03);
00104     LCD_Write_Data(0x1B);
00105     LCD_Write_Data(0x09);
00106     LCD_Write_Data(0x0E);
00107     LCD_Write_Data(0x32);
00108     LCD_Write_Data(0x2D);
00109     LCD_Write_Data(0x28);
00110     LCD_Write_Data(0x2C);
00111     LCD_Write_Data(0x2B);
00112     LCD_Write_Data(0x29);
00113     LCD_Write_Data(0x30);
00114     LCD_Write_Data(0x3B);
00115     LCD_Write_Data(0x00);
00116     LCD_Write_Data(0x01);
00117     LCD_Write_Data(0x02);
00118     LCD_Write_Data(0x10);
00119 
00120     LCD_Write_CMD(0xE1); // Gamma Adjustment (-Polarity)
00121     LCD_Write_Data(0x03);
00122     LCD_Write_Data(0x1B);
00123     LCD_Write_Data(0x09);
00124     LCD_Write_Data(0x0E);
00125     LCD_Write_Data(0x32);
00126     LCD_Write_Data(0x2E);
00127     LCD_Write_Data(0x28);
00128     LCD_Write_Data(0x2C);
00129     LCD_Write_Data(0x2B);
00130     LCD_Write_Data(0x28);
00131     LCD_Write_Data(0x31);
00132     LCD_Write_Data(0x3C);
00133     LCD_Write_Data(0x00);
00134     LCD_Write_Data(0x00);
00135     LCD_Write_Data(0x02);
00136     LCD_Write_Data(0x10);
00137 
00138     LCD_Write_CMD(0x3A); // Interface Pixel Format
00139     LCD_Write_Data(0x05); // 16-bit/pixel
00140 
00141     LCD_Write_CMD(0x2A); // Column Address Set
00142     LCD_Write_Data(0x00);
00143     LCD_Write_Data(0x02);
00144     LCD_Write_Data(0x00);
00145     LCD_Write_Data(0x81);
00146 
00147     LCD_Write_CMD(0x2B); // Row Address Set
00148     LCD_Write_Data(0x00);
00149     LCD_Write_Data(0x01);
00150     LCD_Write_Data(0x00);
00151     LCD_Write_Data(0x80);
00152 
00153     wait_ms(100);
00154     LCD_Write_CMD(0x2C); // Memory Write
00155 
00156 }
00157 
00158 void LCD_Enter_Sleep(void)
00159 {
00160     LCD_Write_CMD(0x10); //Sleep in
00161     wait_ms(120);
00162 }
00163 void LCD_Exit_Sleep(void)
00164 {
00165     LCD_Write_CMD(0x11); //Sleep out
00166     wait_ms(120);
00167 }
00168 
00169 void LCD_Fill_Black(void)
00170 {
00171     t.start();
00172     LCD_Write_CMD(0x2C); // Memory Write
00173     for (int i=0; i<128; i++) {
00174         for (int j=0; j<128*2; j++) {
00175             LCD_Write_Data(0x00);
00176         }
00177     }
00178     LCD_Write_CMD(0x29); // Display On
00179     t.stop();
00180     pc.printf("Fill Black %dms\r\n",t.read_ms());
00181     t.reset();
00182 }
00183 
00184 void LCD_Fill_White(void)
00185 {
00186     t.start();
00187     LCD_Write_CMD(0x2C); // Memory Write
00188     for (int i=0; i<128; i++) {
00189         for (int j=0; j<128*2; j++) {
00190             LCD_Write_Data(0xFF);
00191         }
00192     }
00193     LCD_Write_CMD(0x29); // Display On
00194     t.stop();
00195     pc.printf("Fill White %dms\r\n",t.read_ms());
00196     t.reset();
00197 }
00198 
00199 void LCD_Print_Bitmap(char path[32])
00200 {
00201     t.start();
00202     FILE *fp;
00203     int i,j;
00204     unsigned char offset;
00205     unsigned short pixel[128];
00206     
00207     if ((fp=fopen(path, "rb")) == NULL) {
00208         LCD_Fill_Black();
00209         exit(1);
00210     }
00211     
00212     //LCD_Write_CMD(0x28); // Display Off
00213     
00214     fseek(fp, 0x0a, SEEK_SET);
00215     fread(&offset, 1, 1, fp);
00216     fseek(fp, offset, SEEK_SET);
00217     
00218     LCD_Write_CMD(0x2C); // Memory Write
00219     
00220     for (i=0; i<128; i++) {
00221         fread(&pixel, 2, 128, fp);
00222         for(j=0;j<128;j++){
00223             LCD_Write_Data((unsigned char)((pixel[j] & 0xFFFF) >> 8));
00224             LCD_Write_Data((unsigned char)pixel[j]);
00225         }
00226     }
00227     
00228     LCD_Write_CMD(0x29); // Display On
00229     
00230     fclose(fp);
00231     t.stop();
00232     pc.printf("Print Bitmap %dms\r\n",t.read_ms());
00233     t.reset();
00234 }
00235 
00236 int main()
00237 {
00238     LCD_Init();
00239     pc.baud(115200);
00240 
00241     if (sd.disk_initialize()){
00242          pc.printf("error\r\n");
00243          LCD_Fill_Black();
00244          exit(1);
00245     }
00246 
00247     while(1) {
00248         LCD_Print_Bitmap("/sd/sample.bmp");
00249         wait(5);
00250         LCD_Print_Bitmap("/sd/sample2.bmp");
00251         wait(5);
00252         LCD_Print_Bitmap("/sd/sample3.bmp");
00253         wait(5);
00254     }
00255 }