[其他ST产品] ST7735和ST7789驱动

[复制链接]
 楼主| caoyunhj2301 发表于 2021-12-28 23:54 | 显示全部楼层 |阅读模式
ST, CST, TE, LCD, CD
  1. /* Define to prevent recursive inclusion -------------------------------------*/
  2. #ifndef __LCD_H
  3. #define __LCD_H

  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif

  7. /* Includes ------------------------------------------------------------------*/
  8. #include <stdint.h>
  9.    
  10. /** @addtogroup BSP
  11.   * @{
  12.   */

  13. /** @addtogroup Components
  14.   * @{
  15.   */

  16. /** @addtogroup LCD
  17.   * @{
  18.   */

  19. /** @defgroup LCD_Exported_Types
  20.   * @{
  21.   */

  22. /** @defgroup LCD_Driver_structure  LCD Driver structure
  23.   * @{
  24.   */
  25. typedef struct
  26. {
  27.   void     (*Init)(void);
  28.   uint16_t (*ReadID)(void);
  29.   void     (*DisplayOn)(void);
  30.   void     (*DisplayOff)(void);
  31.   void     (*SetCursor)(uint16_t, uint16_t);
  32.   void     (*WritePixel)(uint16_t, uint16_t, uint16_t);
  33.   uint16_t (*ReadPixel)(uint16_t, uint16_t);
  34.   
  35.    /* Optimized operation */
  36.   void     (*SetDisplayWindow)(uint16_t, uint16_t, uint16_t, uint16_t);
  37.   void     (*DrawHLine)(uint16_t, uint16_t, uint16_t, uint16_t);
  38.   void     (*DrawVLine)(uint16_t, uint16_t, uint16_t, uint16_t);
  39.   
  40.   uint16_t (*GetLcdPixelWidth)(void);
  41.   uint16_t (*GetLcdPixelHeight)(void);
  42.   void     (*DrawBitmap)(uint16_t, uint16_t, uint8_t*);
  43.   void     (*DrawRGBImage)(uint16_t, uint16_t, uint16_t, uint16_t, uint8_t*);
  44. }LCD_DrvTypeDef;   
  45. /**
  46.   * @}
  47.   */

  48. /**
  49.   * @}
  50.   */

  51. /**
  52.   * @}
  53.   */

  54. /**
  55.   * @}
  56.   */

  57. /**
  58.   * @}
  59.   */

  60. #ifdef __cplusplus
  61. }
  62. #endif

  63. #endif /* __LCD_H */


  64. /* Define to prevent recursive inclusion -------------------------------------*/
  65. #ifndef __st7789v_H
  66. #define __st7789v_H

  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif

  70. /* Includes ------------------------------------------------------------------*/
  71. #include "lcd.h"

  72. /** @addtogroup BSP
  73.   * @{
  74.   */

  75. /** @addtogroup Components
  76.   * @{
  77.   */
  78.   
  79. /** @addtogroup st7789v
  80.   * @{
  81.   */

  82. /** @defgroup st7789v_Exported_Types
  83.   * @{
  84.   */
  85.    
  86. /**
  87.   * @}
  88.   */

  89. /** @defgroup st7789v_Exported_Constants
  90.   * @{
  91.   */
  92.       
  93. /**
  94.   * [url=home.php?mod=space&uid=247401]@brief[/url]  st7789v Size  
  95.   */  
  96. #define  st7789v_LCD_PIXEL_WIDTH    ((uint16_t)128)
  97. #define  st7789v_LCD_PIXEL_HEIGHT   ((uint16_t)160)


  98. /**
  99.   * @brief  LCD Lines depending on the chosen fonts.  
  100.   */
  101. #define LCD_LINE_0               LINE(0)
  102. #define LCD_LINE_1               LINE(1)
  103. #define LCD_LINE_2               LINE(2)
  104. #define LCD_LINE_3               LINE(3)
  105. #define LCD_LINE_4               LINE(4)
  106. #define LCD_LINE_5               LINE(5)
  107. #define LCD_LINE_6               LINE(6)
  108. #define LCD_LINE_7               LINE(7)
  109. #define LCD_LINE_8               LINE(8)
  110. #define LCD_LINE_9               LINE(9)
  111. #define LCD_LINE_10              LINE(10)
  112. #define LCD_LINE_11              LINE(11)
  113. #define LCD_LINE_12              LINE(12)
  114. #define LCD_LINE_13              LINE(13)
  115. #define LCD_LINE_14              LINE(14)
  116. #define LCD_LINE_15              LINE(15)
  117. #define LCD_LINE_16              LINE(16)
  118. #define LCD_LINE_17              LINE(17)
  119. #define LCD_LINE_18              LINE(18)
  120. #define LCD_LINE_19              LINE(19)
  121.    
  122. /**
  123.   * @}
  124.   */

  125. /** @defgroup ADAFRUIT_SPI_LCD_Exported_Functions
  126.   * @{
  127.   */
  128. void     st7789v_Init(void);
  129. uint16_t st7789v_ReadID(void);

  130. void     st7789v_DisplayOn(void);
  131. void     st7789v_DisplayOff(void);
  132. void     st7789v_SetCursor(uint16_t Xpos, uint16_t Ypos);
  133. void     st7789v_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode);
  134. void     st7789v_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue);
  135. uint8_t  st7789v_ReadReg(uint8_t LCDReg);

  136. void     st7789v_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height);
  137. void     st7789v_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);
  138. void     st7789v_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);

  139. uint16_t st7789v_GetLcdPixelWidth(void);
  140. uint16_t st7789v_GetLcdPixelHeight(void);
  141. void     st7789v_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp);

  142. /* LCD driver structure */
  143. extern LCD_DrvTypeDef   st7789v_drv;

  144. /* LCD IO functions */
  145. void     LCD_IO_Init(void);
  146. void     LCD_IO_WriteMultipleData(uint8_t *pData, uint32_t Size);
  147. void     LCD_IO_WriteReg(uint8_t Reg);
  148. void     LCD_Delay(uint32_t delay);
  149. /**
  150.   * @}
  151.   */

  152. #ifdef __cplusplus
  153. }
  154. #endif

  155. #endif /* __st7789v_H */

  156. /**
  157.   * @}
  158.   */

  159. /**
  160.   * @}
  161.   */

  162. /**
  163.   * @}
  164.   */


  165. /* Includes ------------------------------------------------------------------*/
  166. #include "st7789v.h"

  167. /** @addtogroup BSP
  168.   * @{
  169.   */

  170. /** @addtogroup Components
  171.   * @{
  172.   */

  173. /** @addtogroup st7789v
  174.   * @brief      This file provides a set of functions needed to drive the
  175.   *             st7789v LCD.
  176.   * @{
  177.   */

  178. /** @defgroup st7789v_Private_TypesDefinitions
  179.   * @{
  180.   */

  181. /**
  182.   * @}
  183.   */

  184. /** @defgroup st7789v_Private_Defines
  185.   * @{
  186.   */

  187. /**
  188.   * @}
  189.   */

  190. /** @defgroup st7789v_Private_Macros
  191.   * @{
  192.   */

  193. /**
  194.   * @}
  195.   */  

  196. /** @defgroup st7789v_Private_Variables
  197.   * @{
  198.   */

  199. LCD_DrvTypeDef   st7789v_drv =
  200. {
  201.   st7789v_Init,
  202.   0,
  203.   st7789v_DisplayOn,
  204.   st7789v_DisplayOff,
  205.   st7789v_SetCursor,
  206.   st7789v_WritePixel,
  207.   0,
  208.   st7789v_SetDisplayWindow,
  209.   st7789v_DrawHLine,
  210.   st7789v_DrawVLine,
  211.   st7789v_GetLcdPixelWidth,
  212.   st7789v_GetLcdPixelHeight,
  213.   st7789v_DrawBitmap,
  214. };

  215. static uint16_t ArrayRGB[320] = {0};

  216. /**
  217. * @}
  218. */

  219. /** @defgroup st7789v_Private_FunctionPrototypes
  220.   * @{
  221.   */

  222. /**
  223. * @}
  224. */

  225. /** @defgroup st7789v_Private_Functions
  226.   * @{
  227.   */
  228.   
  229. /**
  230.   * @brief  Writes to the selected LCD register.
  231.   * @param  LCDReg: Address of the selected register.
  232.   * @param  LCDRegValue: value to write to the selected register.
  233.   * @retval None
  234.   */
  235. void st7789v_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue)
  236. {
  237.   LCD_IO_WriteReg(LCDReg);
  238.   LCD_IO_WriteMultipleData(&LCDRegValue, 1);
  239. }

  240. /**
  241.   * @brief  Initialize the st7789v LCD Component.
  242.   * @param  None
  243.   * @retval None
  244.   */
  245. void st7789v_Init(void)
  246. {   
  247.   
  248.   /* Initialize st7789v low level bus layer -----------------------------------*/
  249.   LCD_IO_Init();
  250.   /* Out of sleep mode, 0 args, no delay */
  251.   st7789v_WriteReg(0x11, 0x00);
  252.   /**/   
  253.    
  254.   st7789v_WriteReg(0x36, 0x00);

  255.     st7789v_WriteReg(0x3A, 0x06);


  256.     st7789v_WriteReg(0xB2, 0x0C);
  257.     st7789v_WriteReg(0xB2, 0x0C);
  258.     st7789v_WriteReg(0xB2, 0x00);   
  259.     st7789v_WriteReg(0xB2, 0x33);   
  260.     st7789v_WriteReg(0xB2, 0x33);   

  261.     st7789v_WriteReg(0xB7, 0x35);   //VGH=13.26V, VGL=-10.43V

  262.     st7789v_WriteReg(0xBB, 0x38);   //VCOM

  263.     st7789v_WriteReg(0xC0, 0x2C);   

  264.     st7789v_WriteReg(0xC2, 0x01);   

  265.     st7789v_WriteReg(0xC3, 0x1A); //VAP  //5V

  266.     st7789v_WriteReg(0xC4, 0x20);   

  267.     st7789v_WriteReg(0xC6, 0x0F);   

  268.     st7789v_WriteReg(0xD0, 0xA4);   
  269.     st7789v_WriteReg(0xD0, 0xA1);   

  270.     st7789v_WriteReg(0xE0 , 0xD0);   
  271.     st7789v_WriteReg(0xE0, 0x1E);   
  272.     st7789v_WriteReg(0xE0, 0x24);   
  273.     st7789v_WriteReg(0xE0, 0x0A);   
  274.     st7789v_WriteReg(0xE0 , 0x0A);   
  275.     st7789v_WriteReg(0xE0 , 0x26);   
  276.     st7789v_WriteReg(0xE0, 0x44);   
  277.     st7789v_WriteReg(0xE0, 0x44);   
  278.     st7789v_WriteReg(0xE0, 0x57);   
  279.     st7789v_WriteReg(0xE0, 0x29);   
  280.     st7789v_WriteReg(0xE0 , 0x14);   
  281.     st7789v_WriteReg(0xE0, 0x14);   
  282.     st7789v_WriteReg(0xE0 , 0x30);   
  283.     st7789v_WriteReg(0xE0 , 0x34);   


  284.     st7789v_WriteReg(0xE1, 0xD0);   
  285.     st7789v_WriteReg(0xE1 , 0x1E);   
  286.     st7789v_WriteReg(0xE1, 0x23);   
  287.     st7789v_WriteReg(0xE1 , 0x0A);   
  288.     st7789v_WriteReg(0xE1, 0x09);   
  289.     st7789v_WriteReg(0xE1, 0x25);   
  290.     st7789v_WriteReg(0xE1 , 0x43);   
  291.     st7789v_WriteReg(0xE1, 0x33);   
  292.     st7789v_WriteReg(0xE1, 0x56);   
  293.     st7789v_WriteReg(0xE1 , 0x28);   
  294.     st7789v_WriteReg(0xE1 , 0x14);   
  295.     st7789v_WriteReg(0xE1, 0x14);   
  296.     st7789v_WriteReg(0xE1, 0x2F);   
  297.     st7789v_WriteReg(0xE1 , 0x34);   

  298.     st7789v_WriteReg(0x29,0x00);   

  299.     st7789v_WriteReg(0x21, 0x00);  

  300.     st7789v_WriteReg(0x2A, 0x00);   
  301.     st7789v_WriteReg(0x2A , 0x00);   
  302.     st7789v_WriteReg(0x2A , 0x00);   
  303.     st7789v_WriteReg(0x2A , 0xEF);   
  304.      
  305.     st7789v_WriteReg(0x2B, 0x00);   
  306.     st7789v_WriteReg(0x2B , 0x00);   
  307.     st7789v_WriteReg(0x2B , 0x00);   
  308.     st7789v_WriteReg(0x2B , 0xEF);   
  309. }

  310. /**
  311.   * @brief  Enables the Display.
  312.   * @param  None
  313.   * @retval None
  314.   */
  315. void st7789v_DisplayOn(void)
  316. {
  317.   uint8_t data = 0;
  318.   LCD_IO_WriteReg(0x13);/* Partial off (Normal): NORON */
  319.   LCD_Delay(10);
  320.   LCD_IO_WriteReg(0x29);/* Display on: DISPON */
  321.   LCD_Delay(10);
  322.   LCD_IO_WriteReg(0x36);/* Memory data access control: MADCTL */
  323.   data = 0xC0;
  324.   LCD_IO_WriteMultipleData(&data, 1);
  325. }

  326. /**
  327.   * @brief  Disables the Display.
  328.   * @param  None
  329.   * @retval None
  330.   */
  331. void st7789v_DisplayOff(void)
  332. {
  333.   uint8_t data = 0;
  334.   LCD_IO_WriteReg(0x13);/* Partial off (Normal): NORON */
  335.   LCD_Delay(10);
  336.   LCD_IO_WriteReg(0x28);/* Display off: DISPOFF */
  337.   LCD_Delay(10);
  338.   LCD_IO_WriteReg(0x36);/* Memory data access control: MADCTL */
  339.   data = 0xC0;
  340.   LCD_IO_WriteMultipleData(&data, 1);
  341. }

  342. /**
  343.   * @brief  Sets Cursor position.
  344.   * @param  Xpos: specifies the X position.
  345.   * @param  Ypos: specifies the Y position.
  346.   * @retval None
  347.   */
  348. void st7789v_SetCursor(uint16_t Xpos, uint16_t Ypos)
  349. {
  350.   uint8_t data = 0;
  351.   LCD_IO_WriteReg(0x2A);/* Column address set: CASET */
  352.   data = (Xpos) >> 8;
  353.   LCD_IO_WriteMultipleData(&data, 1);
  354.   data = (Xpos) & 0xFF;
  355.   LCD_IO_WriteMultipleData(&data, 1);
  356.   LCD_IO_WriteReg(0x2B);/* Row address set: RASET */
  357.   data = (Ypos) >> 8;
  358.   LCD_IO_WriteMultipleData(&data, 1);
  359.   data = (Ypos) & 0xFF;
  360.   LCD_IO_WriteMultipleData(&data, 1);
  361.   LCD_IO_WriteReg(0x2C);/* Memory write: RAMWR */
  362. }

  363. /**
  364.   * @brief  Writes pixel.   
  365.   * @param  Xpos: specifies the X position.
  366.   * @param  Ypos: specifies the Y position.
  367.   * @param  RGBCode: the RGB pixel color
  368.   * @retval None
  369.   */
  370. void st7789v_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  371. {
  372.   uint8_t data = 0;
  373.   if((Xpos >= st7789v_LCD_PIXEL_WIDTH) || (Ypos >= st7789v_LCD_PIXEL_HEIGHT))
  374.   {
  375.     return;
  376.   }
  377.   
  378.   /* Set Cursor */
  379.   st7789v_SetCursor(Xpos, Ypos);
  380.   
  381.   data = RGBCode >> 8;
  382.   LCD_IO_WriteMultipleData(&data, 1);
  383.   data = RGBCode;
  384.   LCD_IO_WriteMultipleData(&data, 1);
  385. }  




  386. /**
  387.   * @brief  Sets a display window
  388.   * @param  Xpos:   specifies the X bottom left position.
  389.   * @param  Ypos:   specifies the Y bottom left position.
  390.   * @param  Height: display window height.
  391.   * @param  Width:  display window width.
  392.   * @retval None
  393.   */
  394. void st7789v_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  395. {
  396.   uint8_t data = 0;
  397.   /* Column addr set, 4 args, no delay: XSTART = Xpos, XEND = (Xpos + Width - 1) */
  398.   LCD_IO_WriteReg(0x2A);/* Column address set: CASET */
  399.   data = (Xpos) >> 8;
  400.   LCD_IO_WriteMultipleData(&data, 1);
  401.   data = (Xpos) & 0xFF;
  402.   LCD_IO_WriteMultipleData(&data, 1);
  403.   data = (Xpos + Width - 1) >> 8;
  404.   LCD_IO_WriteMultipleData(&data, 1);
  405.   data = (Xpos + Width - 1) & 0xFF;
  406.   LCD_IO_WriteMultipleData(&data, 1);
  407.   /* Row addr set, 4 args, no delay: YSTART = Ypos, YEND = (Ypos + Height - 1) */
  408.   LCD_IO_WriteReg(0x2B);/* Row address set: RASET */
  409.   data = (Ypos) >> 8;
  410.   LCD_IO_WriteMultipleData(&data, 1);
  411.   data = (Ypos) & 0xFF;
  412.   LCD_IO_WriteMultipleData(&data, 1);
  413.   data = (Ypos + Height - 1) >> 8;
  414.   LCD_IO_WriteMultipleData(&data, 1);
  415.   data = (Ypos + Height - 1) & 0xFF;
  416.   LCD_IO_WriteMultipleData(&data, 1);
  417. }

  418. /**
  419.   * @brief  Draws horizontal line.
  420.   * @param  RGBCode: Specifies the RGB color   
  421.   * @param  Xpos: specifies the X position.
  422.   * @param  Ypos: specifies the Y position.
  423.   * @param  Length: specifies the line length.  
  424.   * @retval None
  425.   */
  426. void st7789v_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  427. {
  428.   uint8_t counter = 0;
  429.   
  430.   if(Xpos + Length > st7789v_LCD_PIXEL_WIDTH) return;
  431.   
  432.   /* Set Cursor */
  433.   st7789v_SetCursor(Xpos, Ypos);
  434.   
  435.   for(counter = 0; counter < Length; counter++)
  436.   {
  437.     ArrayRGB[counter] = RGBCode;
  438.   }
  439.   LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  440. }

  441. /**
  442.   * @brief  Draws vertical line.
  443.   * @param  RGBCode: Specifies the RGB color   
  444.   * @param  Xpos: specifies the X position.
  445.   * @param  Ypos: specifies the Y position.
  446.   * @param  Length: specifies the line length.  
  447.   * @retval None
  448.   */
  449. void st7789v_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  450. {
  451.   uint8_t counter = 0;
  452.   
  453.   if(Ypos + Length > st7789v_LCD_PIXEL_HEIGHT) return;
  454.   for(counter = 0; counter < Length; counter++)
  455.   {
  456.     st7789v_WritePixel(Xpos, Ypos + counter, RGBCode);
  457.   }   
  458. }

  459. /**
  460.   * @brief  Gets the LCD pixel Width.
  461.   * @param  None
  462.   * @retval The Lcd Pixel Width
  463.   */
  464. uint16_t st7789v_GetLcdPixelWidth(void)
  465. {
  466.   return st7789v_LCD_PIXEL_WIDTH;
  467. }

  468. /**
  469.   * @brief  Gets the LCD pixel Height.
  470.   * @param  None
  471.   * @retval The Lcd Pixel Height
  472.   */
  473. uint16_t st7789v_GetLcdPixelHeight(void)
  474. {                          
  475.   return st7789v_LCD_PIXEL_HEIGHT;
  476. }

  477. /**
  478.   * @brief  Displays a bitmap picture loaded in the internal Flash.
  479.   * @param  BmpAddress: Bmp picture address in the internal Flash.
  480.   * @retval None
  481.   */
  482. void st7789v_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  483. {
  484.   uint32_t index = 0, size = 0;
  485.   
  486.   /* Read bitmap size */
  487.   size = *(volatile uint16_t *) (pbmp + 2);
  488.   size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  489.   /* Get bitmap data address offset */
  490.   index = *(volatile uint16_t *) (pbmp + 10);
  491.   index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  492.   size = (size - index)/2;
  493.   pbmp += index;
  494.   
  495.   /* Set GRAM write direction and BGR = 0 */
  496.   /* Memory access control: MY = 0, MX = 1, MV = 0, ML = 0 */
  497.   st7789v_WriteReg(0x36, 0x40);// LCD_REG_54              0x36 /* Memory data access control: MADCTL */

  498.   /* Set Cursor */
  499.   st7789v_SetCursor(Xpos, Ypos);  

  500.   LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);

  501.   /* Set GRAM write direction and BGR = 0 */
  502.   /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  503.   st7789v_WriteReg(0x36, 0xC0);/* Memory data access control: MADCTL */
  504. }

  505. /**
  506. * @}
  507. */

  508. /**
  509. * @}
  510. */

  511. /**
  512. * @}
  513. */

  514. /**
  515. * @}
  516. */


  517. /* Includes ------------------------------------------------------------------*/
  518. #include "st7735.h"

  519. /** @addtogroup BSP
  520.   * @{
  521.   */

  522. /** @addtogroup Components
  523.   * @{
  524.   */

  525. /** @addtogroup ST7735
  526.   * @brief      This file provides a set of functions needed to drive the
  527.   *             ST7735 LCD.
  528.   * @{
  529.   */

  530. /** @defgroup ST7735_Private_TypesDefinitions
  531.   * @{
  532.   */

  533. /**
  534.   * @}
  535.   */

  536. /** @defgroup ST7735_Private_Defines
  537.   * @{
  538.   */

  539. /**
  540.   * @}
  541.   */

  542. /** @defgroup ST7735_Private_Macros
  543.   * @{
  544.   */

  545. /**
  546.   * @}
  547.   */  

  548. /** @defgroup ST7735_Private_Variables
  549.   * @{
  550.   */


  551. LCD_DrvTypeDef   st7735_drv =
  552. {
  553.   st7735_Init,
  554.   0,
  555.   st7735_DisplayOn,
  556.   st7735_DisplayOff,
  557.   st7735_SetCursor,
  558.   st7735_WritePixel,
  559.   0,
  560.   st7735_SetDisplayWindow,
  561.   st7735_DrawHLine,
  562.   st7735_DrawVLine,
  563.   st7735_GetLcdPixelWidth,
  564.   st7735_GetLcdPixelHeight,
  565.   st7735_DrawBitmap,
  566. };

  567. static uint16_t ArrayRGB[320] = {0};

  568. /**
  569. * @}
  570. */

  571. /** @defgroup ST7735_Private_FunctionPrototypes
  572.   * @{
  573.   */

  574. /**
  575. * @}
  576. */

  577. /** @defgroup ST7735_Private_Functions
  578.   * @{
  579.   */

  580. /**
  581.   * @brief  Initialize the ST7735 LCD Component.
  582.   * @param  None
  583.   * @retval None
  584.   */
  585. void st7735_Init(void)
  586. {   
  587.   
  588.   /* Initialize ST7735 low level bus layer -----------------------------------*/
  589.   LCD_IO_Init();
  590.    
  591.   LCD_HW_RESET();
  592.    
  593.   /* Out of sleep mode*/
  594.   LCD_IO_WriteReg(0x11);
  595.   LCD_Delay(120);//120ms
  596.   /* Frame rate ctrl - normal mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D)*/
  597.   LCD_IO_WriteReg(0xB1);
  598.   LCD_IO_WriteData(0x01);
  599.   LCD_IO_WriteData(0x2C);
  600.   LCD_IO_WriteData(0x2D);
  601.   /* Frame rate control - idle mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D) */
  602.   LCD_IO_WriteReg(0xB2);
  603.   LCD_IO_WriteData(0x01);
  604.   LCD_IO_WriteData(0x2C);
  605.   LCD_IO_WriteData(0x2D);
  606.   /* Frame rate ctrl - partial mode, 6 args: Dot inversion mode, Line inversion mode */
  607.   LCD_IO_WriteReg(0xB3);
  608.   LCD_IO_WriteData(0x01);
  609.   LCD_IO_WriteData(0x2C);
  610.   LCD_IO_WriteData(0x2D);
  611.   LCD_IO_WriteData(0x01);
  612.   LCD_IO_WriteData(0x2C);
  613.   LCD_IO_WriteData(0x2D);
  614.   /* Display inversion ctrl, 1 arg, no delay: No inversion */
  615.   st7735_WriteReg(0xB4, 0x07);
  616.   /* Power control, 3 args, no delay: -4.6V , AUTO mode */
  617.   LCD_IO_WriteReg(0xC0);
  618.   LCD_IO_WriteData(0xA2);
  619.   LCD_IO_WriteData(0x02);
  620.   LCD_IO_WriteData(0x84);
  621.   /* Power control, 1 arg, no delay: VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD */
  622.   st7735_WriteReg(0xC1, 0xC5);
  623.   /* Power control, 2 args, no delay: Opamp current small, Boost frequency */
  624.   LCD_IO_WriteReg(0xC2);
  625.   LCD_IO_WriteData(0x0A);
  626.   LCD_IO_WriteData(0x00);
  627.   /* Power control, 2 args, no delay: BCLK/2, Opamp current small & Medium low */  
  628.   LCD_IO_WriteReg(0xC3);
  629.   LCD_IO_WriteData(0x8A);
  630.   LCD_IO_WriteData(0x2A);
  631.   /* Power control, 2 args, no delay */
  632.    LCD_IO_WriteReg(0xC4);
  633.   LCD_IO_WriteData(0x8A);
  634.   LCD_IO_WriteData(0xEE);
  635.   /* Power control, 1 arg, no delay */
  636.   st7735_WriteReg(0xC5, 0x0E);
  637.   /* Don't invert display, no args, no delay */
  638.   LCD_IO_WriteReg(0x20);
  639.   /* Set color mode, 1 arg, no delay: 16-bit color */
  640.   st7735_WriteReg(0x3A, 0x05);
  641.   /* Column addr set, 4 args, no delay: XSTART = 0, XEND = 127 */
  642.   LCD_IO_WriteReg(0x2A);
  643.   LCD_IO_WriteData(0x00);
  644.   LCD_IO_WriteData(0x00);
  645.   LCD_IO_WriteData(0x00);
  646.   LCD_IO_WriteData(0x7F);

  647.   /* Row addr set, 4 args, no delay: YSTART = 0, YEND = 159 */
  648.   LCD_IO_WriteReg(0x2B);
  649.   LCD_IO_WriteData(0x00);
  650.   LCD_IO_WriteData(0x00);
  651.   LCD_IO_WriteData(0x00);
  652.   LCD_IO_WriteData(0x9F);

  653.   /* Magical unicorn dust, 16 args, no delay */
  654.   LCD_IO_WriteReg(0xE0);
  655.   LCD_IO_WriteData(0x02);
  656.   LCD_IO_WriteData(0x1c);  
  657.   LCD_IO_WriteData(0x07);
  658.   LCD_IO_WriteData(0x12);
  659.   LCD_IO_WriteData(0x37);  
  660.   LCD_IO_WriteData(0x32);  
  661.   LCD_IO_WriteData(0x29);  
  662.   LCD_IO_WriteData(0x2d);
  663.   LCD_IO_WriteData(0x29);  
  664.   LCD_IO_WriteData(0x25);  
  665.   LCD_IO_WriteData(0x2B);  
  666.   LCD_IO_WriteData(0x39);  
  667.   LCD_IO_WriteData(0x00);  
  668.   LCD_IO_WriteData(0x01);  
  669.   LCD_IO_WriteData(0x03);  
  670.   LCD_IO_WriteData(0x10);
  671.   /* Sparkles and rainbows, 16 args, no delay */
  672.   LCD_IO_WriteReg(0xE0);
  673.   LCD_IO_WriteData(0x03);
  674.   LCD_IO_WriteData(0x1d);  
  675.   LCD_IO_WriteData(0x07);  
  676.   LCD_IO_WriteData(0x06);
  677.   LCD_IO_WriteData(0x2E);  
  678.   LCD_IO_WriteData(0x2C);  
  679.   LCD_IO_WriteData(0x29);  
  680.   LCD_IO_WriteData(0x2D);
  681.   LCD_IO_WriteData(0x2E);  
  682.   LCD_IO_WriteData(0x2E);  
  683.   LCD_IO_WriteData(0x37);  
  684.   LCD_IO_WriteData(0x3F);  
  685.   LCD_IO_WriteData(0x00);  
  686.   LCD_IO_WriteData(0x00);  
  687.   LCD_IO_WriteData(0x02);  
  688.   LCD_IO_WriteData(0x10);
  689.   /* Normal display on, no args, no delay */
  690.   st7735_WriteReg(0x13, 0x00);
  691.   /* Main screen turn on, no delay */
  692.   st7735_WriteReg(0x29, 0x00);
  693.   /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  694.   st7735_WriteReg(0x36, 0xC0);
  695. }

  696. /**
  697.   * @brief  Enables the Display.
  698.   * @param  None
  699.   * @retval None
  700.   */
  701. void st7735_DisplayOn(void)
  702. {
  703.   LCD_IO_WriteReg(0x13);
  704.   LCD_Delay(10);
  705.   LCD_IO_WriteReg(0x29);
  706.   LCD_Delay(10);
  707.   LCD_IO_WriteReg(0x36);
  708.   LCD_IO_WriteData(0xC0);
  709. }

  710. /**
  711.   * @brief  Disables the Display.
  712.   * @param  None
  713.   * @retval None
  714.   */
  715. void st7735_DisplayOff(void)
  716. {
  717.   LCD_IO_WriteReg(0x13);
  718.   LCD_Delay(10);
  719.   LCD_IO_WriteReg(0x28);
  720.   LCD_Delay(10);
  721.   LCD_IO_WriteReg(0x36);
  722.   LCD_IO_WriteData(0xC0);
  723. }

  724. /**
  725.   * @brief  Sets Cursor position.
  726.   * @param  Xpos: specifies the X position.
  727.   * @param  Ypos: specifies the Y position.
  728.   * @retval None
  729.   */
  730. void st7735_SetCursor(uint16_t Xpos, uint16_t Ypos)
  731. {
  732.   LCD_IO_WriteReg(0x2A);  
  733.   LCD_IO_WriteData((Xpos) >> 8);
  734.   LCD_IO_WriteData((Xpos) & 0xFF);
  735.   LCD_IO_WriteReg(0x2B);
  736.   LCD_IO_WriteData((Ypos) >> 8);
  737.   LCD_IO_WriteData((Ypos) & 0xFF);
  738.   LCD_IO_WriteReg(0x2C);
  739. }

  740. /**
  741.   * @brief  Writes pixel.   
  742.   * @param  Xpos: specifies the X position.
  743.   * @param  Ypos: specifies the Y position.
  744.   * @param  RGBCode: the RGB pixel color
  745.   * @retval None
  746.   */
  747. void st7735_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  748. {

  749.   if((Xpos >= ST7735_LCD_PIXEL_WIDTH) || (Ypos >= ST7735_LCD_PIXEL_HEIGHT))
  750.   {
  751.     return;
  752.   }
  753.   
  754.   /* Set Cursor */
  755.   st7735_SetCursor(Xpos, Ypos);  
  756.   LCD_IO_WriteData(RGBCode >> 8);
  757.   LCD_IO_WriteData(RGBCode);

  758. }  


  759. /**
  760.   * @brief  Writes to the selected LCD register.
  761.   * @param  LCDReg: Address of the selected register.
  762.   * @param  LCDRegValue: value to write to the selected register.
  763.   * @retval None
  764.   */
  765. void st7735_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue)
  766. {
  767.   LCD_IO_WriteReg(LCDReg);
  768.   LCD_IO_WriteData(LCDRegValue);
  769. }

  770. /**
  771.   * @brief  Sets a display window
  772.   * @param  Xpos:   specifies the X bottom left position.
  773.   * @param  Ypos:   specifies the Y bottom left position.
  774.   * @param  Height: display window height.
  775.   * @param  Width:  display window width.
  776.   * @retval None
  777.   */
  778. void st7735_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  779. {

  780.   /* Column addr set, 4 args, no delay: XSTART = Xpos, XEND = (Xpos + Width - 1) */
  781.   LCD_IO_WriteReg(0x2A);
  782.   LCD_IO_WriteData((Xpos) >> 8);
  783.   LCD_IO_WriteData((Xpos) & 0xFF);
  784.   LCD_IO_WriteData((Xpos + Width - 1) >> 8);
  785.   LCD_IO_WriteData((Xpos + Width - 1) & 0xFF);
  786.   /* Row addr set, 4 args, no delay: YSTART = Ypos, YEND = (Ypos + Height - 1) */
  787.   LCD_IO_WriteReg(0x2B);
  788.   LCD_IO_WriteData((Ypos) >> 8 );
  789.   LCD_IO_WriteData((Ypos) & 0xFF );
  790.   LCD_IO_WriteData((Ypos + Height - 1) >> 8 );
  791.   LCD_IO_WriteData((Ypos + Height - 1) & 0xFF );
  792. }

  793. /**
  794.   * @brief  Draws horizontal line.
  795.   * @param  RGBCode: Specifies the RGB color   
  796.   * @param  Xpos: specifies the X position.
  797.   * @param  Ypos: specifies the Y position.
  798.   * @param  Length: specifies the line length.  
  799.   * @retval None
  800.   */
  801. void st7735_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  802. {
  803.   uint8_t counter = 0;
  804.   
  805.   if(Xpos + Length > ST7735_LCD_PIXEL_WIDTH) return;
  806.   
  807.   /* Set Cursor */
  808.   st7735_SetCursor(Xpos, Ypos);
  809.   
  810.   for(counter = 0; counter < Length; counter++)
  811.   {
  812.     ArrayRGB[counter] = RGBCode;
  813.   }
  814.   LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  815. }

  816. /**
  817.   * @brief  Draws vertical line.
  818.   * @param  RGBCode: Specifies the RGB color   
  819.   * @param  Xpos: specifies the X position.
  820.   * @param  Ypos: specifies the Y position.
  821.   * @param  Length: specifies the line length.  
  822.   * @retval None
  823.   */
  824. void st7735_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  825. {
  826.   uint8_t counter = 0;
  827.   
  828.   if(Ypos + Length > ST7735_LCD_PIXEL_HEIGHT) return;
  829.   for(counter = 0; counter < Length; counter++)
  830.   {
  831.     st7735_WritePixel(Xpos, Ypos + counter, RGBCode);
  832.   }   
  833. }

  834. /**
  835.   * @brief  Gets the LCD pixel Width.
  836.   * @param  None
  837.   * @retval The Lcd Pixel Width
  838.   */
  839. uint16_t st7735_GetLcdPixelWidth(void)
  840. {
  841.   return ST7735_LCD_PIXEL_WIDTH;
  842. }

  843. /**
  844.   * @brief  Gets the LCD pixel Height.
  845.   * @param  None
  846.   * @retval The Lcd Pixel Height
  847.   */
  848. uint16_t st7735_GetLcdPixelHeight(void)
  849. {                          
  850.   return ST7735_LCD_PIXEL_HEIGHT;
  851. }

  852. /**
  853.   * @brief  Displays a bitmap picture loaded in the internal Flash.
  854.   * @param  BmpAddress: Bmp picture address in the internal Flash.
  855.   * @retval None
  856.   */
  857. void st7735_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  858. {
  859.   uint32_t index = 0, size = 0;
  860.   
  861.   /* Read bitmap size */
  862.   size = *(volatile uint16_t *) (pbmp + 2);
  863.   size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  864.   /* Get bitmap data address offset */
  865.   index = *(volatile uint16_t *) (pbmp + 10);
  866.   index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  867.   size = (size - index)/2;
  868.   pbmp += index;
  869.   
  870.   /* Set GRAM write direction and BGR = 0 */
  871.   /* Memory access control: MY = 0, MX = 1, MV = 0, ML = 0 */
  872.   st7735_WriteReg(0x36, 0x40);

  873.   /* Set Cursor */
  874.   st7735_SetCursor(Xpos, Ypos);  

  875.   LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);

  876.   /* Set GRAM write direction and BGR = 0 */
  877.   /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  878.   st7735_WriteReg(0x36, 0xC0);
  879. }
  880. #ifndef __ST7735_H
  881. #define __ST7735_H

  882. #ifdef __cplusplus
  883. extern "C" {
  884. #endif

  885. /* Includes ------------------------------------------------------------------*/
  886. #include "../Common/lcd.h"
  887. #include "stm32f1xx_board.h"
  888. /** @addtogroup BSP
  889.   * @{
  890.   */

  891. /** @addtogroup Components
  892.   * @{
  893.   */
  894.   
  895. /** @addtogroup ST7735
  896.   * @{
  897.   */

  898. /** @defgroup ST7735_Exported_Types
  899.   * @{
  900.   */
  901.    
  902. /**
  903.   * @}
  904.   */

  905. /** @defgroup ST7735_Exported_Constants
  906.   * @{
  907.   */
  908.       
  909. /**
  910.   * @brief  ST7735 Size  
  911.   */  
  912. #define  ST7735_LCD_PIXEL_WIDTH    ((uint16_t)128)
  913. #define  ST7735_LCD_PIXEL_HEIGHT   ((uint16_t)160)





  914.                         


  915. /**
  916.   * @brief  LCD Lines depending on the chosen fonts.  
  917.   */
  918. #define LCD_LINE_0               LINE(0)
  919. #define LCD_LINE_1               LINE(1)
  920. #define LCD_LINE_2               LINE(2)
  921. #define LCD_LINE_3               LINE(3)
  922. #define LCD_LINE_4               LINE(4)
  923. #define LCD_LINE_5               LINE(5)
  924. #define LCD_LINE_6               LINE(6)
  925. #define LCD_LINE_7               LINE(7)
  926. #define LCD_LINE_8               LINE(8)
  927. #define LCD_LINE_9               LINE(9)
  928. #define LCD_LINE_10              LINE(10)
  929. #define LCD_LINE_11              LINE(11)
  930. #define LCD_LINE_12              LINE(12)
  931. #define LCD_LINE_13              LINE(13)
  932. #define LCD_LINE_14              LINE(14)
  933. #define LCD_LINE_15              LINE(15)
  934. #define LCD_LINE_16              LINE(16)
  935. #define LCD_LINE_17              LINE(17)
  936. #define LCD_LINE_18              LINE(18)
  937. #define LCD_LINE_19              LINE(19)
  938.    
  939. /**
  940.   * @}
  941.   */

  942. /** @defgroup ADAFRUIT_SPI_LCD_Exported_Functions
  943.   * @{
  944.   */
  945. void     st7735_Init(void);
  946. uint16_t st7735_ReadID(void);

  947. void     st7735_DisplayOn(void);
  948. void     st7735_DisplayOff(void);
  949. void     st7735_SetCursor(uint16_t Xpos, uint16_t Ypos);
  950. void     st7735_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode);
  951. void     st7735_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue);
  952. uint8_t  st7735_ReadReg(uint8_t LCDReg);

  953. void     st7735_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height);
  954. void     st7735_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);
  955. void     st7735_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length);

  956. uint16_t st7735_GetLcdPixelWidth(void);
  957. uint16_t st7735_GetLcdPixelHeight(void);
  958. void     st7735_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp);

  959. /* LCD driver structure */
  960. extern LCD_DrvTypeDef   st7735_drv;

  961. /**
  962.   * @}
  963.   */

  964. #ifdef __cplusplus
  965. }
  966. #endif

  967. #endif /* __ST7735_H */
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

298

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部