Takeshi Ueno / Mbed 2 deprecated MyLCDJapaneseTest

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // AD-12864-SPI test program
00002 // About AD-12864-SPI, see http://www.aitendo.co.jp/product/1622.
00003 
00004 // Pin allocation
00005 // 1 p21 #CS1 with 10k ohm pull-up
00006 // 2 p22 #RESET with 10k ohm pull-up
00007 // 3 p23 A0 ... 0:command 1:data
00008 // 4 p13 SCK
00009 // 5 p11 MOSI
00010 // 6     Vdd
00011 // 7     Vss
00012 // 8 NC  LED_A
00013 
00014 // Japanese Character Version:
00015 // UTF Font from e-font http://openlab.ring.gr.jp/efont/unicode/
00016 // and row-col convert with information from:
00017 // http://todotani.cocolog-nifty.com/blog/2009/07/arduino-d98c.html
00018 // Font data file based on
00019 // http://todotani.cocolog-nifty.com/blog/files/glcdfont_12_ucs2.zip
00020 // make binary and split binary 
00021 
00022 #include "mbed.h"
00023 
00024 DigitalOut cs(p21);
00025 DigitalOut rst(p22);
00026 DigitalOut a0(p23);
00027 SPI spi(p11, p12, p13); // mosi, miso, sclk
00028 
00029 LocalFileSystem local("local");
00030 
00031 #ifdef DEBUG
00032 Serial pc(USBTX, USBRX); // tx, rx
00033 #endif
00034 
00035 void regwrite(unsigned char c) {
00036     cs = a0 = 0;
00037     spi.write(c);
00038     cs = 1;
00039 }
00040 
00041 void datawrite(unsigned char c) {
00042     cs = 0;
00043     a0 = 1;
00044     spi.write(c);
00045     cs = 1;
00046 }
00047 
00048 // set position (x, 8*y)
00049 void locate(int x, int y) {
00050     regwrite(0xb0 | (y & 0x0f)); // Page Address Set (see 2.4.3)
00051     regwrite(0x10 | (x >> 4 & 0x0f)); // Column Address Set (see 2.4.4)
00052     regwrite(x & 0x0f);
00053 }
00054 
00055 void cls(void) {
00056     int x, y;
00057     for (y = 0; y < 8; y++) {
00058         locate(0, y);
00059         for (x = 0; x < 128; x++) datawrite(0x00);
00060     }
00061 }
00062 
00063 void plot(int x, int y) {
00064     locate(x, y >> 3);
00065     datawrite(1 << (y & 7));
00066 }
00067 
00068 void init() {
00069     spi.format(8,0); // nazo
00070     spi.frequency(10000000); // modify later
00071 
00072     // reset
00073     wait_ms(200);
00074     rst = 0;
00075     wait_ms(200);
00076     rst = 1;
00077 
00078     // initialize sequence
00079     regwrite(0xaf);    // display on (see 2.4.1)
00080     regwrite(0x2f);    // power control set (see 2.4.16)
00081     regwrite(0x81);    // set electronic volume mode (see 2.4.18)
00082 //    regwrite(0x1f);    // electronic volume data 00-3f
00083     regwrite(0x00);    // electronic volume data 00-3f
00084     regwrite(0x27);    // V5 Volatge Regulator Internal Resister Ratio Set (see 2.4.17)
00085     regwrite(0xa2);    // LCD Bias Set ... 1/9 bias (see 2.4.11)
00086     regwrite(0xc8);    // Common Output Mode Select ... Reverse (see 2.4.15)
00087     regwrite(0xa0);    // ADC Select ... Normal (see 2.4.8)
00088     regwrite(0xa4);    // Display All Points ON/OFF ... normal (see 2.4.10)
00089     regwrite(0xa6);    // Display Normal/Reverse ... normal (see 2.4.9)
00090     regwrite(0xac);    // Static Indicator ... off (see 2.4.19)
00091     regwrite(0x00);    // off
00092     regwrite(0x40);    // Display Strat Line Set ... 0 (see 2.4.2)
00093     regwrite(0xe0);    // Write Mode Set
00094 }
00095 
00096 void drawchar24(int x, int y, unsigned int c) {
00097     unsigned char buf[24];
00098     char fname[20];
00099     int width,base;
00100 
00101     if((c >= 0x0020) && (c <= 0x00FE)) {
00102       width=12;
00103       base=0x0020;
00104       sprintf(fname,"/local/002000FE.dat");
00105     }
00106     else if((c >= 0x0100) && (c <= 0x05F3)) {
00107       width=12;
00108       base=0x0100;
00109       sprintf(fname,"/local/010005F3.dat");
00110     }
00111     else if((c >= 0x1E00) && (c <= 0x1F72)) {
00112       width=12;
00113       base=0x1E00;
00114       sprintf(fname,"/local/1E001F72.dat");
00115     }
00116     else if((c >= 0x2010) && (c <= 0x28FE)) {
00117       width=12;
00118       base=0x2010;
00119       sprintf(fname,"/local/201028FE.dat");
00120     }
00121     else if((c >= 0x3000) && (c <= 0x33DD)) {
00122       width=24;
00123       base=0x3000;
00124       sprintf(fname,"/local/300033DD.dat");
00125     }
00126     else if((c >= 0x4E00) && (c <= 0x9FA4)) {
00127       width=24;
00128       base=0x4E00;
00129       sprintf(fname,"/local/4E009FA4.dat");
00130     }
00131     else if((c >= 0xF900) && (c <= 0xFA26)) {
00132       width=24;
00133       base=0xF900;
00134       sprintf(fname,"/local/F900FA26.dat");
00135     }
00136     else if((c >= 0xFF00) && (c <= 0xFF5E)) {
00137       width=24;
00138       base=0xFF00;
00139       sprintf(fname,"/local/FF00FF5E.dat");
00140     }
00141     else if((c >= 0xFF60) && (c <= 0xFF9F)) {
00142       width=12;
00143       base=0xFF60;
00144       sprintf(fname,"/local/FF60FF9F.dat");
00145     }
00146     else if((c >= 0xFFE0) && (c <= 0xFFE6)) {
00147       width=24;
00148       base=0xFFE0;
00149       sprintf(fname,"/local/FFE0FFE6.dat");
00150     }
00151     else {
00152       return;
00153     }
00154     
00155     FILE *fp = fopen(fname,"rb");
00156     fseek(fp,(c-base) * width,SEEK_SET);
00157     fread(buf, sizeof(unsigned char), width, fp);
00158     fclose(fp);
00159 
00160     #ifdef DEBUG
00161     int i;
00162     pc.printf("DEBUG:%s:%04.4x -> ",fname,c);
00163     for(i=0;i<width;i++) {
00164         pc.printf("%02.2x ",buf[i]);
00165     }
00166     pc.printf("\r\n");
00167     #endif
00168         
00169     if(width == 24) {
00170       locate(x,y);
00171       datawrite(buf[0]); datawrite(buf[1]); datawrite(buf[2]);
00172       datawrite(buf[3]); datawrite(buf[4]); datawrite(buf[5]);
00173       datawrite(buf[6]); datawrite(buf[7]); datawrite(buf[8]);
00174       datawrite(buf[9]); datawrite(buf[10]); datawrite(buf[11]);
00175       locate(x,y+1);
00176       datawrite(buf[12]>>4); datawrite(buf[13]>>4); datawrite(buf[14]>>4);
00177       datawrite(buf[15]>>4); datawrite(buf[16]>>4); datawrite(buf[17]>>4);
00178       datawrite(buf[18]>>4); datawrite(buf[19]>>4); datawrite(buf[20]>>4);
00179       datawrite(buf[21]>>4); datawrite(buf[22]>>4); datawrite(buf[23]>>4);
00180     }
00181     else if(width == 12) {
00182       locate(x,y);
00183       datawrite(buf[0]); datawrite(buf[1]); datawrite(buf[2]);
00184       datawrite(buf[3]); datawrite(buf[4]); datawrite(buf[5]);
00185       locate(x,y+1);
00186       datawrite(buf[6]); datawrite(buf[7]); datawrite(buf[8]);
00187       datawrite(buf[9]); datawrite(buf[10]); datawrite(buf[11]);
00188     }
00189 }
00190 /*
00191 void drawtext(const char *s) {
00192     unsigned char c;
00193     while ((c = *s++) != '\0') drawchar(c);
00194 }
00195 */
00196 
00197 int main() {
00198     init();
00199     cls();
00200     locate(0, 0);
00201     // http://www.unicode.org/charts/PDF/U3040.pdf
00202     // http://ash.jp/code/unitbl21.htm
00203     drawchar24(0,0,0x3053);  // KO on UCS2
00204     drawchar24(12,0,0x3093); // N
00205     drawchar24(24,0,0x306b); // NI
00206     drawchar24(36,0,0x3061); // CHI
00207     drawchar24(48,0,0x306F); // HA
00208     drawchar24(0,2,0x65E5);  // NI
00209     drawchar24(12,2,0x672C); // HON
00210     drawchar24(24,2,0x8A9E); // GO
00211     drawchar24(36,2,0x8868); // HYO
00212     drawchar24(48,2,0x793A); // JI
00213     drawchar24(60,2,0x306E); // NO
00214     drawchar24(72,2,0x30C6); // TE
00215     drawchar24(84,2,0x30B9); // SU
00216     drawchar24(96,2,0x30C8); // TO
00217     drawchar24(108,2,0x3002);// MARU
00218     drawchar24(0,4,0xFF21);   // zenkaku-A
00219     drawchar24(12,4,0xFF22);  // zenkaku-B
00220     drawchar24(24,4,0xFF23);  // zenkaku-C
00221     drawchar24(36,4,0xFF24);  // zenkaku-D
00222     drawchar24(48,4,0xFF25);  // zenkaku-E
00223     drawchar24(60,4,0xFF26);  // zenkaku-F
00224     drawchar24(72,4,0xFF27);  // zenkaku-G
00225     drawchar24(84,4,0xFF28);  // zenkaku-H
00226     drawchar24(96,4,0xFF29);  // zenkaku-I
00227     drawchar24(108,4,0xFF2A); // zenkaku-J
00228 
00229     while (1) {}
00230 }