WILLY BAYOT / stm32_adafruit

Dependents:   TDEMNucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers st7735.c Source File

st7735.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    st7735.c
00004   * @author  MCD Application Team
00005   * @version V1.0.0
00006   * @date    22-April-2014
00007   * @brief   This file includes the driver for ST7735 LCD mounted on the Adafruit
00008   *          1.8" TFT LCD shield (reference ID 802).
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "st7735.h"
00041 
00042 /** @addtogroup BSP
00043   * @{
00044   */ 
00045 
00046 /** @addtogroup Components
00047   * @{
00048   */ 
00049 
00050 /** @addtogroup ST7735
00051   * @brief      This file provides a set of functions needed to drive the
00052   *             ST7735 LCD.
00053   * @{
00054   */
00055 
00056 /** @defgroup ST7735_Private_TypesDefinitions
00057   * @{
00058   */ 
00059 
00060 /**
00061   * @}
00062   */ 
00063 
00064 /** @defgroup ST7735_Private_Defines
00065   * @{
00066   */
00067 
00068 /**
00069   * @}
00070   */ 
00071 
00072 /** @defgroup ST7735_Private_Macros
00073   * @{
00074   */
00075 
00076 /**
00077   * @}
00078   */  
00079 
00080 /** @defgroup ST7735_Private_Variables
00081   * @{
00082   */ 
00083 
00084 
00085 LCD_DrvTypeDef   st7735_drv = 
00086 {
00087   st7735_Init,
00088   0,
00089   st7735_DisplayOn,
00090   st7735_DisplayOff,
00091   st7735_SetCursor,
00092   st7735_WritePixel,
00093   0,
00094   0,
00095   st7735_DrawHLine,
00096   st7735_DrawVLine,
00097   st7735_GetLcdPixelWidth,
00098   st7735_GetLcdPixelHeight,
00099   0,
00100 };
00101 
00102 /**
00103 * @}
00104 */ 
00105 
00106 /** @defgroup ST7735_Private_FunctionPrototypes
00107   * @{
00108   */
00109 
00110 /**
00111 * @}
00112 */ 
00113 
00114 /** @defgroup ST7735_Private_Functions
00115   * @{
00116   */
00117 
00118 /**
00119   * @brief  Initialize the ST7735 LCD Component.
00120   * @param  None
00121   * @retval None
00122   */
00123 void st7735_Init(void)
00124 {    
00125   /* Initialize ST7735 low level bus layer -----------------------------------*/
00126   LCD_IO_Init();
00127   /* Out of sleep mode, 0 args, no delay */
00128   st7735_WriteReg(LCD_REG_17, 0x00); 
00129   /* Frame rate ctrl - normal mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D)*/
00130   LCD_IO_WriteReg(LCD_REG_177);
00131   LCD_IO_WriteData(0x01);
00132   LCD_IO_WriteData(0x2C);
00133   LCD_IO_WriteData(0x2D);
00134   /* Frame rate control - idle mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D) */    
00135   st7735_WriteReg(LCD_REG_178, 0x01);
00136   st7735_WriteReg(LCD_REG_178, 0x2C);
00137   st7735_WriteReg(LCD_REG_178, 0x2D);
00138   /* Frame rate ctrl - partial mode, 6 args: Dot inversion mode, Line inversion mode */ 
00139   st7735_WriteReg(LCD_REG_179, 0x01);
00140   st7735_WriteReg(LCD_REG_179, 0x2C);
00141   st7735_WriteReg(LCD_REG_179, 0x2D);
00142   st7735_WriteReg(LCD_REG_179, 0x01);
00143   st7735_WriteReg(LCD_REG_179, 0x2C);
00144   st7735_WriteReg(LCD_REG_179, 0x2D);
00145   /* Display inversion ctrl, 1 arg, no delay: No inversion */
00146   st7735_WriteReg(LCD_REG_180, 0x07);
00147   /* Power control, 3 args, no delay: -4.6V , AUTO mode */
00148   st7735_WriteReg(LCD_REG_192, 0xA2);
00149   st7735_WriteReg(LCD_REG_192, 0x02);
00150   st7735_WriteReg(LCD_REG_192, 0x84);
00151   /* Power control, 1 arg, no delay: VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD */
00152   st7735_WriteReg(LCD_REG_193, 0xC5);
00153   /* Power control, 2 args, no delay: Opamp current small, Boost frequency */ 
00154   st7735_WriteReg(LCD_REG_194, 0x0A);
00155   st7735_WriteReg(LCD_REG_194, 0x00);
00156   /* Power control, 2 args, no delay: BCLK/2, Opamp current small & Medium low */  
00157   st7735_WriteReg(LCD_REG_195, 0x8A);
00158   st7735_WriteReg(LCD_REG_195, 0x2A);
00159   /* Power control, 2 args, no delay */
00160   st7735_WriteReg(LCD_REG_196, 0x8A);
00161   st7735_WriteReg(LCD_REG_196, 0xEE);
00162   /* Power control, 1 arg, no delay */
00163   st7735_WriteReg(LCD_REG_197, 0x0E);
00164   /* Don't invert display, no args, no delay */
00165   LCD_IO_WriteReg(LCD_REG_32);
00166   /* Memory access control (directions), 1 arg: row addr/col addr, bottom to top refresh */
00167   st7735_WriteReg(LCD_REG_54, 0xC8);
00168   /* Set color mode, 1 arg, no delay: 16-bit color */
00169   st7735_WriteReg(LCD_REG_58, 0x05);
00170   /* Column addr set, 4 args, no delay: XSTART = 0, XEND = 127 */
00171   st7735_WriteReg(LCD_REG_42, 0x00);
00172   st7735_WriteReg(LCD_REG_42, 0x00);
00173   st7735_WriteReg(LCD_REG_42, 0x00);
00174   st7735_WriteReg(LCD_REG_42, 0x7F);
00175   /* Row addr set, 4 args, no delay: YSTART = 0, YEND = 159 */
00176   st7735_WriteReg(LCD_REG_43, 0x00);
00177   st7735_WriteReg(LCD_REG_43, 0x00);
00178   st7735_WriteReg(LCD_REG_43, 0x00);
00179   st7735_WriteReg(LCD_REG_43, 0x9F);
00180   /* Magical unicorn dust, 16 args, no delay */
00181   st7735_WriteReg(LCD_REG_224, 0x02); 
00182   st7735_WriteReg(LCD_REG_224, 0x1c);  
00183   st7735_WriteReg(LCD_REG_224, 0x07); 
00184   st7735_WriteReg(LCD_REG_224, 0x12);
00185   st7735_WriteReg(LCD_REG_224, 0x37);  
00186   st7735_WriteReg(LCD_REG_224, 0x32);  
00187   st7735_WriteReg(LCD_REG_224, 0x29);  
00188   st7735_WriteReg(LCD_REG_224, 0x2d);
00189   st7735_WriteReg(LCD_REG_224, 0x29);  
00190   st7735_WriteReg(LCD_REG_224, 0x25);  
00191   st7735_WriteReg(LCD_REG_224, 0x2B);  
00192   st7735_WriteReg(LCD_REG_224, 0x39);  
00193   st7735_WriteReg(LCD_REG_224, 0x00);  
00194   st7735_WriteReg(LCD_REG_224, 0x01);  
00195   st7735_WriteReg(LCD_REG_224, 0x03);  
00196   st7735_WriteReg(LCD_REG_224, 0x10);
00197   /* Sparkles and rainbows, 16 args, no delay */
00198   st7735_WriteReg(LCD_REG_225, 0x03);
00199   st7735_WriteReg(LCD_REG_225, 0x1d);  
00200   st7735_WriteReg(LCD_REG_225, 0x07);  
00201   st7735_WriteReg(LCD_REG_225, 0x06);
00202   st7735_WriteReg(LCD_REG_225, 0x2E);  
00203   st7735_WriteReg(LCD_REG_225, 0x2C);  
00204   st7735_WriteReg(LCD_REG_225, 0x29);  
00205   st7735_WriteReg(LCD_REG_225, 0x2D);
00206   st7735_WriteReg(LCD_REG_225, 0x2E);  
00207   st7735_WriteReg(LCD_REG_225, 0x2E);  
00208   st7735_WriteReg(LCD_REG_225, 0x37);  
00209   st7735_WriteReg(LCD_REG_225, 0x3F);  
00210   st7735_WriteReg(LCD_REG_225, 0x00);  
00211   st7735_WriteReg(LCD_REG_225, 0x00);  
00212   st7735_WriteReg(LCD_REG_225, 0x02);  
00213   st7735_WriteReg(LCD_REG_225, 0x10);
00214   /* Normal display on, no args, no delay */
00215   st7735_WriteReg(LCD_REG_19, 0x00);
00216   /* Main screen turn on, no delay */
00217   st7735_WriteReg(LCD_REG_41, 0x00);
00218   st7735_WriteReg(LCD_REG_54, 0xC0);
00219 }
00220 
00221 /**
00222   * @brief  Enables the Display.
00223   * @param  None
00224   * @retval None
00225   */
00226 void st7735_DisplayOn(void)
00227 {
00228   LCD_IO_WriteReg(LCD_REG_19);
00229   LCD_Delay(10);
00230   LCD_IO_WriteReg(LCD_REG_41);
00231   LCD_Delay(10);
00232   LCD_IO_WriteReg(LCD_REG_54);
00233   LCD_IO_WriteData(0xC0);
00234 }
00235 
00236 /**
00237   * @brief  Disables the Display.
00238   * @param  None
00239   * @retval None
00240   */
00241 void st7735_DisplayOff(void)
00242 {
00243   LCD_IO_WriteReg(LCD_REG_19);
00244   LCD_Delay(10);
00245   LCD_IO_WriteReg(LCD_REG_40);
00246   LCD_Delay(10);
00247   LCD_IO_WriteReg(LCD_REG_54);
00248   LCD_IO_WriteData(0xC0);
00249 }
00250 
00251 /**
00252   * @brief  Sets Cursor position.
00253   * @param  Xpos: specifies the X position.
00254   * @param  Ypos: specifies the Y position.
00255   * @retval None
00256   */
00257 void st7735_SetCursor(uint16_t Xpos, uint16_t Ypos)
00258 {
00259   LCD_IO_WriteReg(LCD_REG_42);
00260   LCD_IO_WriteData(Xpos);
00261   LCD_IO_WriteData(Xpos);
00262   LCD_IO_WriteReg(LCD_REG_43); 
00263   LCD_IO_WriteData(Ypos);
00264   LCD_IO_WriteData(Ypos);
00265   LCD_IO_WriteReg(LCD_REG_44);
00266 }
00267 
00268 /**
00269   * @brief  Writes pixel.   
00270   * @param  Xpos: specifies the X position.
00271   * @param  Ypos: specifies the Y position.
00272   * @param  RGBCode: the RGB pixel color
00273   * @retval None
00274   */
00275 void st7735_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGB_Code)
00276 {
00277   if((Xpos >= ST7735_LCD_PIXEL_WIDTH) || (Ypos >= ST7735_LCD_PIXEL_HEIGHT)) 
00278   {
00279     return;
00280   }
00281   
00282   /* Set Cursor */
00283   st7735_SetCursor(Xpos, Ypos);
00284   
00285   LCD_IO_WriteData(RGB_Code >> 8);
00286   LCD_IO_WriteData(RGB_Code);
00287 }  
00288 
00289 
00290 /**
00291   * @brief  Writes to the selected LCD register.
00292   * @param  LCDReg: Address of the selected register.
00293   * @param  LCDRegValue: value to write to the selected register.
00294   * @retval None
00295   */
00296 void st7735_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue)
00297 {
00298   LCD_IO_WriteReg(LCDReg);
00299   
00300   LCD_IO_WriteData(LCDRegValue);
00301 }
00302 
00303 /**
00304   * @brief  Draws horizontal line.
00305   * @param  RGBCode: Specifies the RGB color   
00306   * @param  Xpos: specifies the X position.
00307   * @param  Ypos: specifies the Y position.
00308   * @param  Length: specifies the line length.  
00309   * @retval None
00310   */
00311 void st7735_DrawHLine(uint16_t RGB_Code, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
00312 {
00313   uint8_t counter = 0;
00314   
00315   if(Xpos + Length > ST7735_LCD_PIXEL_WIDTH) return;
00316   
00317   for(counter = 0; counter < Length; counter++)
00318   {
00319     st7735_WritePixel(Xpos + counter, Ypos, RGB_Code);
00320   }
00321 }
00322 
00323 /**
00324   * @brief  Draws vertical line.
00325   * @param  RGBCode: Specifies the RGB color   
00326   * @param  Xpos: specifies the X position.
00327   * @param  Ypos: specifies the Y position.
00328   * @param  Length: specifies the line length.  
00329   * @retval None
00330   */
00331 void st7735_DrawVLine(uint16_t RGB_Code, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
00332 {
00333   uint8_t counter = 0;
00334   
00335   if(Ypos + Length > ST7735_LCD_PIXEL_HEIGHT) return;
00336   for(counter = 0; counter < Length; counter++)
00337   {
00338     st7735_WritePixel(Xpos, Ypos + counter, RGB_Code);
00339   }   
00340 }
00341 
00342 /**
00343   * @brief  Gets the LCD pixel Width.
00344   * @param  None
00345   * @retval The Lcd Pixel Width
00346   */
00347 uint16_t st7735_GetLcdPixelWidth(void)
00348 {
00349   return ST7735_LCD_PIXEL_WIDTH;
00350 }
00351 
00352 /**
00353   * @brief  Gets the LCD pixel Height.
00354   * @param  None
00355   * @retval The Lcd Pixel Height
00356   */
00357 uint16_t st7735_GetLcdPixelHeight(void)
00358 {                          
00359   return ST7735_LCD_PIXEL_HEIGHT;
00360 }
00361 
00362 /**
00363 * @}
00364 */ 
00365 
00366 /**
00367 * @}
00368 */ 
00369 
00370 /**
00371 * @}
00372 */ 
00373 
00374 /**
00375 * @}
00376 */
00377 
00378 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00379