Natthanicha Jamroonpan / LCD4884

Dependents:   10A_ClassProject_MegnaticGame

Fork of LCD4884 by Dan Ghiciulescu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD4884.cpp Source File

LCD4884.cpp

00001 /*
00002 Modified by COX
00003 version 0.1
00004 
00005 Editor     : COX
00006 Date       : 06.03.2013
00007 
00008 *
00009 * Update DFRobot source to work on FRDM KL25Z
00010 *
00011 */
00012 
00013 #include "LCD4884.h"
00014 #include "font_6x8.h"
00015 #include "font_big.h"
00016 
00017 DigitalOut SpiClk(SPI_SCK);    //2- Serial Clock(Master Output)
00018 DigitalOut SpiMosi(SPI_MOSI);  //3- Master Output,Slave Input
00019 DigitalOut LcdDC(LCD_DC);      //4- Data/Command(command active low)
00020 DigitalOut SpiCS(SPI_CS);      //5- Chip Select,Slave Transmit Enable(active low,Master Output)
00021 DigitalOut LcdRst(LCD_RST);    //6- One Reset button
00022 PwmOut     LcdBl(LCD_BL);      //7- LCD backlight
00023 
00024 LCD4884::LCD4884()
00025 {};
00026 
00027 /******************************************************************/
00028 void LCD4884::backlight(float dat)
00029 {
00030     LcdBl = dat;
00031 }
00032 
00033 /******************************************************************/
00034 void LCD4884::LCD_init(void)
00035 {
00036     /* pin intializer */
00037     SpiClk = LOW;
00038     SpiMosi = LOW;
00039     SpiCS = LOW;
00040     LcdDC = LOW;
00041     LcdBl = LOW;
00042 
00043     LcdRst = LOW;
00044     wait(ONE_US);
00045     LcdRst = HIGH;
00046     
00047     SpiCS = LOW;  //Chip Select, Slave Transmit Enable(active low, Master Output)
00048     wait(ONE_US);
00049     SpiCS = HIGH;
00050     wait(ONE_US);
00051     LcdBl = LCD_INITIAL_BRIGHTNESS;
00052     
00053     //data_type=0, all are command bytes
00054     LCD_write_byte(0x21, 0); //Function Set:0 0 1 0 0 PD V H=0010 0001;PD=0,V=0,H=1;
00055     LCD_write_byte(0xc0, 0); //Set Vop:1 Vop6 Vop5 Vop4 Vop3 Vop2 Vop1 Vop0=1100 0000
00056     LCD_write_byte(0x06, 0); //Set Temperature Coefficient:0 0 0 0 0 1 Tc1 Tc0=0000 0110;Tc1=1,Tc0=0(Vlcd temperature coefficient 2)
00057     LCD_write_byte(0x13, 0); //Set Bias System (BSx):0 0 0 1 0 BS2 BS1 BS0=0001 0011;BS2=0, BS1=1, BS0=1==>N=4,MUX RATE=1:48
00058 
00059     LCD_write_byte(0x20, 0);//Function Set:0 0 1 0 0 PD V H=0010 0000;PD=0,V=0,H=0;
00060     LCD_clear();
00061     LCD_write_byte(0x0c, 0);//Display Control: 0 0 0 0 1 D 0 E=0000 1100 ;D=1,E=0:normal mode
00062 
00063     SpiCS = LOW;
00064 }
00065 
00066 /******************************************************************/
00067 void LCD4884::LCD_write_byte(unsigned char dat, unsigned char dat_type)
00068 {
00069     unsigned int i;
00070     SpiCS = LOW; //Chip Enable:Active LOW
00071 
00072     if (dat_type == 0)
00073         LcdDC = LOW; // D/C=0:the current data byte is interpreted as command byte
00074     else
00075         LcdDC = HIGH; // D/C=1:write data to display RAM
00076 
00077     for(i=0;i<8;i++)
00078     {
00079         if(dat&0x80) //1000 0000
00080         {
00081             SpiMosi = HIGH;
00082         }
00083         else
00084         {
00085             SpiMosi = LOW;
00086         }
00087         SpiClk = LOW;
00088         dat = dat << 1;
00089         SpiClk = HIGH;
00090     }
00091     SpiCS = HIGH;
00092 }
00093 
00094 /******************************************************************/
00095 void LCD4884::LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map,
00096                   unsigned char Pix_x,unsigned char Pix_y)
00097 {
00098     unsigned int i,n;
00099     unsigned char row;
00100 
00101     if (Pix_y%8==0)
00102         row=Pix_y/8; //row from 1 to 6;Pix_y from R0 to R47
00103     else
00104         row=Pix_y/8+1; //Quotient+1
00105     
00106     for (n=0;n<row;n++)
00107     {
00108         LCD_set_XY(X,Y);
00109         for(i=0; i<Pix_x; i++)
00110         {
00111             LCD_write_byte(map[i+n*Pix_x], 1); // D/C=1:write data to display RAM
00112         }
00113         Y++;
00114     }
00115 }
00116 
00117 /**************************************************************************************/
00118 void LCD4884::LCD_write_string(unsigned char X,unsigned char Y,char *s, char mode)
00119 {
00120     
00121     LCD_set_XY(X,Y);
00122     while (*s)
00123     {
00124         LCD_write_char(*s, mode);
00125         s++;
00126     }
00127 }
00128 
00129 /**************************************************************************************/
00130 void LCD4884::LCD_prop_write_string(unsigned char X,unsigned char Y,char *s, char mode)
00131 {
00132     LCD_set_XY(X,Y);
00133     while (*s)
00134     {
00135         LCD_prop_write_char(*s, mode);
00136         s++;
00137     }
00138 }
00139 
00140 /*************************************************************************************/
00141 void LCD4884::LCD_write_chinese(unsigned char X, unsigned char Y,unsigned char *c,unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row)
00142 {
00143     unsigned char i,n;
00144     LCD_set_XY(X,Y);                             
00145     for (i=0;i<num;)
00146     {
00147         for (n=0; n<ch_with*2; n++)
00148         {
00149             if (n==ch_with)
00150             {
00151                 if (i==0)
00152                     LCD_set_XY(X,Y+1);
00153                 else
00154                   LCD_set_XY((X+(ch_with+row)*i),Y+1);
00155             }
00156             LCD_write_byte(c[(i*ch_with*2)+n],1);
00157         }
00158         i++; 
00159         LCD_set_XY((X+(ch_with+row)*i),Y); 
00160     }
00161 }
00162 
00163 
00164 /******************************************************************/
00165 void LCD4884::LCD_write_string_big ( unsigned char X,unsigned char Y, char *string, char mode )
00166 {
00167     while ( *string )
00168     {
00169         LCD_write_char_big( X, Y, *string , mode );
00170         
00171         if(*string++ == '.')
00172             X += 5;
00173         else
00174             X += 12;
00175     }
00176 }
00177 
00178 /******************************************************************/
00179 /* write char in big font */
00180 void LCD4884::LCD_write_char_big (unsigned char X,unsigned char Y, unsigned char ch, char mode)
00181 {
00182    unsigned char i, j;
00183    unsigned char *pFont;
00184    unsigned char ch_dat;
00185    
00186    pFont = (unsigned char *) big_number;
00187    
00188    if(ch == '.')
00189         ch = 10;
00190    else if (ch == '+')
00191         ch = 11;
00192    else if (ch == '-')
00193         ch = 12;
00194    else
00195         ch = ch & 0x0f;
00196 
00197    for(i=0;i<3;i++)
00198    {
00199         LCD_set_XY ( X, Y+i);
00200 
00201         for(j=0; j<16; j++)
00202         {
00203             ch_dat = *(pFont+ch*48 + i*16 +j);
00204             LCD_write_byte( (mode == MENU_NORMAL)? ch_dat : (ch_dat^0xff), 1);
00205         }
00206    }
00207 }
00208   
00209 /******************************************************************/
00210 void LCD4884::LCD_write_char(unsigned char c, char mode)
00211 {
00212     unsigned char line;
00213     unsigned char *pFont;
00214     unsigned char ch; 
00215     
00216     pFont = (unsigned char *)font6_8; //pointer *pFont points at font6_8[][6]
00217     c -= 32; // the ASCII of "SP" is 32
00218 
00219     for (line=0; line<6; line++)
00220     {
00221         ch = *(pFont+c*6+line); //read c from the font6_8[][6] (the detail information is in the "font6x8.h")
00222         LCD_write_byte( (mode==MENU_NORMAL)? ch: (ch^ 0xff) , 1); //MENU_NORMAL=0,True:return ch;False:return ch
00223     }
00224 }
00225 
00226 /*******************************************************************/
00227 unsigned char LCD4884::LCD_prop_write_char(unsigned char c, char mode)
00228 {
00229     int line, line_s=0, line_e=2;
00230     unsigned char *pFont;
00231     unsigned char ch;
00232 
00233     pFont = (unsigned char *)font6_8;
00234     if (c -= 32)
00235     {
00236         for (line_s=0; line_s<6; line_s++)
00237         {
00238             if(*(pFont+c*6+line_s))
00239                 break;
00240         }
00241         for (line_e=5; line_e<0; line_e--)
00242         {
00243             if(*(pFont+c*6+line_e))
00244                 break;
00245         }
00246     }
00247     for (line=line_s; line<line_e+1; line++)
00248     {
00249         ch = *(pFont+c*6+line);
00250         LCD_write_byte( (mode==MENU_NORMAL)? ch: (ch^ 0xff) , 1);
00251     }
00252     LCD_write_byte( (mode==MENU_NORMAL)? 0:0xff, 1);
00253     return ((unsigned char)(line_e+2 - line_s));
00254 }
00255 
00256 /******************************************************************/
00257 void LCD4884::LCD_set_XY(unsigned char X, unsigned char Y)
00258 {
00259     LCD_write_byte(0x40 | Y, 0);        // column
00260     LCD_write_byte(0x80 | X, 0);        // row
00261 }
00262 
00263 /******************************************************************/
00264 void LCD4884::LCD_clear(void)
00265 {
00266     unsigned int i;
00267 
00268     LCD_write_byte(0x0c, 0);
00269     LCD_write_byte(0x80, 0);
00270 
00271     for (i=0; i<504; i++)  //6*84
00272     {
00273         LCD_write_byte(0, 1);
00274     }
00275 }
00276