Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SG12864A.cpp
00001 // 00002 // 00003 // 00004 00005 #include "SG12864A.h" 00006 #include "font.h" 00007 00008 SG12864A::SG12864A() : DI_OUT(DI), RW_OUT(RW), E_OUT(E), DB_OUT(DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7), 00009 CS1_OUT(CS1),CS2_OUT(CS2),RST_OUT(RST) 00010 { DI_OUT = 0x01;RW_OUT = 0x01; E_OUT = 0x00; 00011 DB_OUT = 0x00; 00012 DB_OUT.output(); 00013 CS1_OUT= 0x01; CS2_OUT=0x01; RST_OUT=0x01; 00014 } 00015 //Data output function 00016 void SG12864A::lcd_Write(char cs, char code, char DIflag){ 00017 int data; 00018 DB_OUT.output(); 00019 RW_OUT = 0; 00020 if(cs==1) 00021 CS1_OUT=0; 00022 else 00023 CS2_OUT=0; 00024 data = (int)code; 00025 DB_OUT = data & 0x00FF; 00026 if(DIflag == 0) 00027 DI_OUT = 1; 00028 else 00029 DI_OUT = 0; 00030 wait_us(1); 00031 E_OUT = 1; 00032 wait_us(1); 00033 E_OUT = 0; 00034 CS1_OUT = 1; 00035 CS2_OUT = 1; 00036 RW_OUT = 1; 00037 wait_us(5); 00038 } 00039 00040 // Data Read Function 00041 char SG12864A::lcd_Read(char cs){ 00042 int data; 00043 DB_OUT.input(); 00044 RW_OUT = 1; 00045 if(cs==1) 00046 CS1_OUT = 0; 00047 else 00048 CS2_OUT = 0; 00049 DI_OUT = 1; 00050 wait_us(1); 00051 E_OUT = 1; 00052 wait_us(1); 00053 E_OUT = 0; 00054 wait_us(3); 00055 data = DB_OUT & 0x00FF; 00056 wait_us(1); 00057 CS1_OUT = 1; 00058 CS2_OUT = 1; 00059 DB_OUT.output(); 00060 return((char)data); 00061 } 00062 00063 // Data Read Function 00064 char SG12864A::lcd_StatusRead(char cs){ 00065 int data; 00066 DB_OUT.input(); 00067 RW_OUT = 1; 00068 if(cs==1) 00069 CS1_OUT = 0; 00070 else 00071 CS2_OUT = 0; 00072 DI_OUT = 0; 00073 wait_us(1); 00074 E_OUT = 1; 00075 wait_us(1); 00076 E_OUT = 0; 00077 wait_us(3); 00078 data = DB_OUT & 0x00FF; 00079 wait_us(1); 00080 CS1_OUT = 1; 00081 CS2_OUT = 1; 00082 DB_OUT.output(); 00083 return((char)data); 00084 } 00085 00086 // Screen Clear Function 00087 void SG12864A::lcd_Clear(char data){ 00088 char page, colum; 00089 00090 for(page=0; page<8;page++){ //repeat 8 page 00091 lcd_Write(1,0xB8+page,1); //page set 00092 lcd_Write(1,0x40,1); //column reset 00093 lcd_Write(2,0xB8+page,1); //page set 00094 lcd_Write(2,0x40,1); //column reset 00095 for(colum=0;colum<64;colum++){ //repeat 64column 00096 lcd_Write(1,data,0); //fill data 00097 lcd_Write(2,data,0); //fill data 00098 } 00099 } 00100 00101 lcd_Write(1,0xC0,1); //reset start line 00102 lcd_Write(2,0xC0,1); 00103 } 00104 00105 // Initialize Function 00106 void SG12864A::lcd_Init(void){ 00107 RST_OUT = 0; 00108 wait_ms(1000); 00109 RST_OUT = 1; 00110 wait_ms(10); 00111 lcd_Write(1, 0x3F, 1); // Display On 00112 lcd_Write(2, 0x3F, 1); // Display On 00113 lcd_Clear(0); 00114 } 00115 00116 // Draw Pixel Function 00117 void SG12864A::lcd_Pixel(int Xpos, int Ypos, char On){ 00118 char cs, data, page, pos, count, i; 00119 00120 /* if colum >127 then do nothing */ 00121 if(Xpos<128){ 00122 if(Xpos>63){ // 64=<colum<=127? 00123 Xpos = Xpos-64; // shift 64 dot 00124 cs = 1; 00125 } 00126 else 00127 cs = 2; 00128 page = (char)(7-Ypos/8); // set page 00129 lcd_Write(cs, 0xB8+page, 1); 00130 lcd_Write(cs, 0x40+Xpos, 1); // set colum 00131 data = lcd_Read(cs); // get current data 00132 lcd_Write(cs, 0x40+Xpos, 1); // set colum 00133 data = lcd_Read(cs); // get current data 00134 pos =1; // set bit position 00135 count = (char)(7-Ypos%8); // set bit 00136 for(i=0; i<count; i++) // caluculate 2^n 00137 pos *= 2; 00138 lcd_Write(cs, 0x40+Xpos, 1); // back address 00139 if(On==1) // set or reset bit 00140 lcd_Write(cs, data | pos, 0); // set 1 00141 else 00142 lcd_Write(cs, data & ~pos, 0); // set 0 00143 } 00144 } 00145 00146 //Draw Straight line Function 00147 #define abs(a) (((a)>0) ? (a) : -(a)) 00148 void SG12864A::lcd_Line(int x0, int y0, int x1, int y1){ 00149 int steep, t; 00150 int deltax, deltay, error; 00151 int x, y; 00152 int ystep; 00153 00154 steep = (abs(y1 - y0) > abs(x1 - x0)); 00155 00156 if(steep){ 00157 t = x0; x0 = y0; y0 = t; 00158 t = x1; x1 = y1; y1 = t; 00159 } 00160 if(x0 > x1) { 00161 t = x0; x0 = x1; x1 = t; 00162 t = y0; y0 = y1; y1 = t; 00163 } 00164 deltax = x1 - x0; 00165 deltay = abs(y1 - y0); 00166 error = 0; 00167 y = y0; 00168 00169 if(y0 < y1) ystep = 1; else ystep = -1; 00170 for(x=x0; x<x1; x++) { 00171 if(steep) lcd_Pixel(y,x,1); else lcd_Pixel(x,y,1); 00172 error += deltay; 00173 if((error << 1) >= deltax) { 00174 y += ystep; 00175 error -= deltax; 00176 } 00177 } 00178 } 00179 00180 00181 //Display Character Function 00182 void SG12864A::lcd_Char(char line, char colum, int letter){ 00183 char cs, i; 00184 int pos; 00185 00186 if(colum < 16){ 00187 if(colum >7){ 00188 pos = (colum-8)*8; 00189 cs = 1; 00190 } 00191 else{ 00192 pos = colum*8; 00193 cs = 2; 00194 } 00195 lcd_Write(cs, 0xB8+line,1); 00196 lcd_Write(cs, 0x40+pos,1); 00197 for(i=0; i<5; i++) 00198 lcd_Write(cs,Font[letter-0x20][i],0); 00199 lcd_Write(cs,0,0); 00200 lcd_Write(cs,0,0); 00201 lcd_Write(cs,0,0); 00202 } 00203 } 00204 00205 // Display Character Function2 00206 void SG12864A::lcd_Char1(char line, char colum, int letter){ 00207 char cs, i; 00208 int pos; 00209 00210 if(colum < 18){ 00211 if(colum > 8){ 00212 pos = (colum- 9) * 7; 00213 cs = 1; 00214 } 00215 else{ 00216 pos = colum * 7; 00217 cs = 2; 00218 } 00219 lcd_Write(cs, 0xB8+line, 1); // set page 00220 lcd_Write(cs, 0x40+pos, 1); // set colum 00221 for(i=0; i<5; i++) 00222 lcd_Write(cs, Font[letter-0x20][i], 0); 00223 lcd_Write(cs, 0, 0); 00224 lcd_Write(cs, 0, 0); 00225 } 00226 } 00227 00228 // Display Char Srings Function 00229 void SG12864A::lcd_Str(char line, char colum, char *s){ 00230 while(*s) 00231 SG12864A::lcd_Char1(line, colum++, *s++); 00232 } 00233 00234 // Display Image Function 00235 void SG12864A::lcd_Image(char *ptr){ 00236 char cs, Xpos; 00237 int page, colum; 00238 00239 for(page=0; page<8; page++){ 00240 for(colum=0; colum<128; colum++){ 00241 if(colum > 63){ 00242 Xpos=colum-64; 00243 cs = 1; 00244 } 00245 else{ 00246 Xpos = colum; 00247 cs = 2; 00248 } 00249 lcd_Write(cs, 0xB8+page, 1); 00250 lcd_Write(cs, 0x40+Xpos, 1); 00251 lcd_Write(cs, *ptr++, 0); 00252 } 00253 } 00254 } 00255 00256 //Scroll Function 00257 void SG12864A::lcd_Scroll(int delay){ 00258 int i; 00259 00260 for(i=0; i<64; i++){ 00261 lcd_Write(1, 0xC0+i,1); 00262 lcd_Write(2, 0xC0+i,1); 00263 wait_ms(delay); 00264 } 00265 }
Generated on Tue Aug 2 2022 22:01:40 by
