Dimiter K / Mbed OS MAX32620FTHR_GPS_Tracker

Dependencies:   MAX17055 MAX32620FTHR MAX77650 SerialGPS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD_Driver.cpp Source File

LCD_Driver.cpp

00001 
00002 /***********************************************************************************************************************
00003 * | file        :   LCD_Driver.c
00004 * | version     :   V1.0
00005 * | date        :   2017-10-16
00006 * | function    :   On the ST7735S chip driver and clear screen, drawing lines, drawing, writing
00007                     and other functions to achieve
00008 ***********************************************************************************************************************/
00009 
00010 #include "LCD_Driver.h"
00011 
00012 #include <stdlib.h>     //itoa()
00013 #include <stdio.h>
00014 #include "fonts.h"
00015 
00016 LCD_DIS sLCD_DIS;
00017 
00018 /***********************************************************************************************************************
00019             ------------------------------------------------------------------------
00020             |\\\                                                                ///|
00021             |\\\                        Drive layer                             ///|
00022             ------------------------------------------------------------------------
00023 ***********************************************************************************************************************/
00024 /*******************************************************************************
00025 function:
00026             Hardware reset
00027 *******************************************************************************/
00028 SPI spi0(P0_5, P0_6, P0_4); // mosi, miso, sclk
00029 
00030 DigitalOut cs0(P0_7);
00031 DigitalOut rst(P1_5);
00032 DigitalOut dc(P1_4);
00033 
00034 static void LCD_Reset(void)
00035 {
00036     RST_HIGH();
00037     wait_ms(100);
00038     RST_LOW();
00039     wait_ms(100);
00040     RST_HIGH();
00041     wait_ms(100);
00042 }
00043 
00044 /*******************************************************************************
00045 function:
00046         Write register address and data
00047 *******************************************************************************/
00048 static void LCD_WriteReg(uint8_t Reg)
00049 {
00050     DC_LOW();
00051     cs0 = 0;    
00052     spi0.write(Reg);    
00053     cs0 = 1;
00054 }
00055 
00056 static void LCD_WriteData_8Bit(uint8_t Data)
00057 {
00058     DC_HIGH();
00059     cs0 = 0;    
00060     spi0.write(Data);
00061     cs0 = 1;
00062 }
00063 
00064 
00065 static void LCD_WriteData_NLen16Bit(uint16_t Data,uint32_t DataLen)
00066 {
00067     uint32_t i;
00068     DC_HIGH();
00069     cs0 = 0;    
00070     for(i = 0; i < DataLen; i++) {
00071         spi0.write( (uint8_t)(Data >> 8) );
00072         spi0.write((uint8_t)(Data & 0XFF) );
00073     }
00074     cs0 = 1;
00075 }
00076 
00077 /*******************************************************************************
00078 function:
00079         Common register initialization
00080 *******************************************************************************/
00081 static void LCD_InitReg(void)
00082 {
00083     spi0.frequency(4000000);
00084     cs0 = 1;
00085     
00086     //ST7735R Frame Rate
00087     LCD_WriteReg(0xB1);
00088     LCD_WriteData_8Bit(0x01);
00089     LCD_WriteData_8Bit(0x2C);
00090     LCD_WriteData_8Bit(0x2D);
00091 
00092     LCD_WriteReg(0xB2);
00093     LCD_WriteData_8Bit(0x01);
00094     LCD_WriteData_8Bit(0x2C);
00095     LCD_WriteData_8Bit(0x2D);
00096 
00097     LCD_WriteReg(0xB3);
00098     LCD_WriteData_8Bit(0x01);
00099     LCD_WriteData_8Bit(0x2C);
00100     LCD_WriteData_8Bit(0x2D);
00101     LCD_WriteData_8Bit(0x01);
00102     LCD_WriteData_8Bit(0x2C);
00103     LCD_WriteData_8Bit(0x2D);
00104 
00105     LCD_WriteReg(0xB4); //Column inversion
00106     LCD_WriteData_8Bit(0x07);
00107 
00108     //ST7735R Power Sequence
00109     LCD_WriteReg(0xC0);
00110     LCD_WriteData_8Bit(0xA2);
00111     LCD_WriteData_8Bit(0x02);
00112     LCD_WriteData_8Bit(0x84);
00113     LCD_WriteReg(0xC1);
00114     LCD_WriteData_8Bit(0xC5);
00115 
00116     LCD_WriteReg(0xC2);
00117     LCD_WriteData_8Bit(0x0A);
00118     LCD_WriteData_8Bit(0x00);
00119 
00120     LCD_WriteReg(0xC3);
00121     LCD_WriteData_8Bit(0x8A);
00122     LCD_WriteData_8Bit(0x2A);
00123     LCD_WriteReg(0xC4);
00124     LCD_WriteData_8Bit(0x8A);
00125     LCD_WriteData_8Bit(0xEE);
00126 
00127     LCD_WriteReg(0xC5); //VCOM
00128     LCD_WriteData_8Bit(0x0E);
00129 
00130     //ST7735R Gamma Sequence
00131     LCD_WriteReg(0xe0);
00132     LCD_WriteData_8Bit(0x0f);
00133     LCD_WriteData_8Bit(0x1a);
00134     LCD_WriteData_8Bit(0x0f);
00135     LCD_WriteData_8Bit(0x18);
00136     LCD_WriteData_8Bit(0x2f);
00137     LCD_WriteData_8Bit(0x28);
00138     LCD_WriteData_8Bit(0x20);
00139     LCD_WriteData_8Bit(0x22);
00140     LCD_WriteData_8Bit(0x1f);
00141     LCD_WriteData_8Bit(0x1b);
00142     LCD_WriteData_8Bit(0x23);
00143     LCD_WriteData_8Bit(0x37);
00144     LCD_WriteData_8Bit(0x00);
00145     LCD_WriteData_8Bit(0x07);
00146     LCD_WriteData_8Bit(0x02);
00147     LCD_WriteData_8Bit(0x10);
00148 
00149     LCD_WriteReg(0xe1);
00150     LCD_WriteData_8Bit(0x0f);
00151     LCD_WriteData_8Bit(0x1b);
00152     LCD_WriteData_8Bit(0x0f);
00153     LCD_WriteData_8Bit(0x17);
00154     LCD_WriteData_8Bit(0x33);
00155     LCD_WriteData_8Bit(0x2c);
00156     LCD_WriteData_8Bit(0x29);
00157     LCD_WriteData_8Bit(0x2e);
00158     LCD_WriteData_8Bit(0x30);
00159     LCD_WriteData_8Bit(0x30);
00160     LCD_WriteData_8Bit(0x39);
00161     LCD_WriteData_8Bit(0x3f);
00162     LCD_WriteData_8Bit(0x00);
00163     LCD_WriteData_8Bit(0x07);
00164     LCD_WriteData_8Bit(0x03);
00165     LCD_WriteData_8Bit(0x10);
00166 
00167     LCD_WriteReg(0xF0); //Enable test command
00168     LCD_WriteData_8Bit(0x01);
00169 
00170     LCD_WriteReg(0xF6); //Disable ram power save mode
00171     LCD_WriteData_8Bit(0x00);
00172 
00173     LCD_WriteReg(0x3A); //65k mode
00174     LCD_WriteData_8Bit(0x05);
00175 
00176 }
00177 
00178 
00179 /********************************************************************************
00180 function:   Set the display scan and color transfer modes
00181 parameter:
00182         Scan_dir   :   Scan direction
00183         Colorchose :   RGB or GBR color format
00184 ********************************************************************************/
00185 void LCD_SetGramScanWay(LCD_SCAN_DIR Scan_dir)
00186 {
00187     //Get the screen scan direction
00188     sLCD_DIS.LCD_Scan_Dir = Scan_dir;
00189 
00190     //Get GRAM and LCD width and height
00191     if(Scan_dir == L2R_U2D || Scan_dir == L2R_D2U || Scan_dir == R2L_U2D || Scan_dir == R2L_D2U) {
00192         sLCD_DIS.LCD_Dis_Column = LCD_HEIGHT ;
00193         sLCD_DIS.LCD_Dis_Page = LCD_WIDTH ;
00194     } else {
00195         sLCD_DIS.LCD_Dis_Column = LCD_WIDTH ;
00196         sLCD_DIS.LCD_Dis_Page = LCD_HEIGHT ;
00197     }
00198 
00199     // Gets the scan direction of GRAM
00200     uint16_t MemoryAccessReg_Data=0;  //0x36
00201     switch (Scan_dir) {
00202     case L2R_U2D:
00203         MemoryAccessReg_Data = 0X00 | 0x00;//x Scan direction | y Scan direction
00204         break;
00205     case L2R_D2U:
00206         MemoryAccessReg_Data = 0x00 | 0x80;//0xC8 | 0X10
00207         break;
00208     case R2L_U2D:// 0X4
00209         MemoryAccessReg_Data = 0x40 | 0x00;
00210         break;
00211     case R2L_D2U:// 0XC
00212         MemoryAccessReg_Data = 0x40 | 0x80;
00213         break;
00214     case U2D_L2R://0X2
00215         MemoryAccessReg_Data = 0X00 | 0X00 | 0x20;
00216         break;
00217     case U2D_R2L://0X6
00218         MemoryAccessReg_Data = 0x00 | 0X40 | 0x20;
00219         break;
00220     case D2U_L2R://0XA
00221         MemoryAccessReg_Data = 0x80 | 0x00 | 0x20;
00222         break;
00223     case D2U_R2L://0XE
00224         MemoryAccessReg_Data = 0x40 | 0x80 | 0x20;
00225         break;
00226     }
00227 
00228     //please set (MemoryAccessReg_Data & 0x10) != 1
00229     if((MemoryAccessReg_Data && 0x20) != 1) {
00230         sLCD_DIS.LCD_X_Adjust = LCD_X;
00231         sLCD_DIS.LCD_Y_Adjust = LCD_Y;
00232     } else {
00233         sLCD_DIS.LCD_X_Adjust = LCD_Y;
00234         sLCD_DIS.LCD_Y_Adjust = LCD_X;
00235     }
00236 
00237     // Set the read / write scan direction of the frame memory
00238     LCD_WriteReg(0x36); //MX, MY, RGB mode
00239 #if defined(LCD_1IN44)
00240     LCD_WriteData_8Bit( MemoryAccessReg_Data | 0x08);   //0x08 set RGB
00241 #elif defined(LCD_1IN8)
00242     LCD_WriteData_8Bit( MemoryAccessReg_Data & 0xf7);   //RGB color filter panel
00243 #endif
00244 
00245 }
00246 
00247 /***********************************************************************************************************************
00248             ------------------------------------------------------------------------
00249             |\\\                                                                ///|
00250             |\\\                        App layer                               ///|
00251             ------------------------------------------------------------------------
00252 ***********************************************************************************************************************/
00253 
00254 /********************************************************************************
00255 function:
00256             initialization
00257 ********************************************************************************/
00258 void LCD_Init( LCD_SCAN_DIR Lcd_ScanDir )
00259 {
00260 
00261     //Hardware reset
00262     LCD_Reset();
00263 
00264     //Set the initialization register
00265     LCD_InitReg();
00266 
00267     //Set the display scan and color transfer modes
00268     LCD_SetGramScanWay( Lcd_ScanDir );
00269     wait_ms(200);
00270 
00271     //sleep out
00272     LCD_WriteReg(0x11);
00273     wait_ms(120);
00274 
00275     //Turn on the LCD display
00276     LCD_WriteReg(0x29);
00277 }
00278 
00279 /********************************************************************************
00280 function:   Sets the start position and size of the display area
00281 parameter:
00282         Xstart  :   X direction Start coordinates
00283         Ystart  :   Y direction Start coordinates
00284         Xend    :   X direction end coordinates
00285         Yend    :   Y direction end coordinates
00286 ********************************************************************************/
00287 void LCD_SetWindows( POINT Xstart, POINT Ystart, POINT Xend, POINT Yend )
00288 {
00289 
00290     //set the X coordinates
00291     LCD_WriteReg ( 0x2A );
00292     LCD_WriteData_8Bit ( 0x00 );                        //Set the horizontal starting point to the high octet
00293     LCD_WriteData_8Bit ( (Xstart & 0xff) + sLCD_DIS.LCD_X_Adjust);          //Set the horizontal starting point to the low octet
00294     LCD_WriteData_8Bit ( 0x00 );                //Set the horizontal end to the high octet
00295     LCD_WriteData_8Bit ( (( Xend - 1 ) & 0xff) + sLCD_DIS.LCD_X_Adjust);    //Set the horizontal end to the low octet
00296 
00297     //set the Y coordinates
00298     LCD_WriteReg ( 0x2B );
00299     LCD_WriteData_8Bit ( 0x00 );
00300     LCD_WriteData_8Bit ( (Ystart & 0xff) + sLCD_DIS.LCD_Y_Adjust);
00301     LCD_WriteData_8Bit ( 0x00 );
00302     LCD_WriteData_8Bit ( ( (Yend - 1) & 0xff )+ sLCD_DIS.LCD_Y_Adjust);
00303 
00304     LCD_WriteReg(0x2C);
00305 
00306 }
00307 
00308 /********************************************************************************
00309 function:   Set the display point (Xpoint, Ypoint)
00310 parameter:
00311         xStart :   X direction Start coordinates
00312         xEnd   :   X direction end coordinates
00313 ********************************************************************************/
00314 void LCD_SetCursor ( POINT Xpoint, POINT Ypoint )
00315 {
00316     LCD_SetWindows ( Xpoint, Ypoint, Xpoint , Ypoint );
00317 }
00318 
00319 /********************************************************************************
00320 function:   Set show color
00321 parameter:
00322         Color  :   Set show color
00323 ********************************************************************************/
00324 void LCD_SetColor( COLOR Color ,POINT Xpoint, POINT Ypoint)
00325 {
00326     LCD_WriteData_NLen16Bit(Color ,(uint32_t)Xpoint * (uint32_t)Ypoint);
00327 }
00328 
00329 /********************************************************************************
00330 function:   Point (Xpoint, Ypoint) Fill the color
00331 parameter:
00332         Xpoint :   The x coordinate of the point
00333         Ypoint :   The y coordinate of the point
00334         Color  :   Set the color
00335 ********************************************************************************/
00336 void LCD_SetPointlColor ( POINT Xpoint, POINT Ypoint, COLOR Color )
00337 {
00338     if ( ( Xpoint <= sLCD_DIS.LCD_Dis_Column ) && ( Ypoint <= sLCD_DIS.LCD_Dis_Page ) ) {
00339         LCD_SetCursor (Xpoint, Ypoint);
00340         LCD_SetColor ( Color , 1 , 1);
00341     }
00342 }
00343 
00344 /********************************************************************************
00345 function:   Fill the area with the color
00346 parameter:
00347         Xstart :   Start point x coordinate
00348         Ystart :   Start point y coordinate
00349         Xend   :   End point coordinates
00350         Yend   :   End point coordinates
00351         Color  :   Set the color
00352 ********************************************************************************/
00353 void LCD_SetArealColor (POINT Xstart, POINT Ystart, POINT Xend, POINT Yend, COLOR  Color)
00354 {
00355     if((Xend > Xstart) && (Yend > Ystart)) {
00356         LCD_SetWindows( Xstart , Ystart , Xend , Yend  );
00357         LCD_SetColor ( Color ,Xend - Xstart , Yend - Ystart );
00358     }
00359 }
00360 
00361 /********************************************************************************
00362 function:
00363             Clear screen
00364 ********************************************************************************/
00365 void LCD_Clear(COLOR  Color)
00366 {
00367     LCD_SetArealColor(0,0, sLCD_DIS.LCD_Dis_Column , sLCD_DIS.LCD_Dis_Page, Color);
00368 }
00369 
00370 /********************************************************************************************/
00371 
00372 void DC_LOW(void)
00373 {
00374     dc = 0;
00375 }
00376 void DC_HIGH(void)
00377 {
00378     dc= 1;
00379 }
00380 
00381 void RST_LOW(void)
00382 {
00383     rst = 0;
00384 }
00385 void RST_HIGH(void)
00386 {
00387     rst = 1;
00388 }
00389 
00390