st7735.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. ******************************************************************************
  3. * @file st7735.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 24-November-2014
  7. * @brief This file includes the driver for ST7735 LCD mounted on the Adafruit
  8. * 1.8" TFT LCD shield (reference ID 802).
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "st7735.h"
  40. /** @addtogroup BSP
  41. * @{
  42. */
  43. /** @addtogroup Components
  44. * @{
  45. */
  46. /** @addtogroup ST7735
  47. * @brief This file provides a set of functions needed to drive the
  48. * ST7735 LCD.
  49. * @{
  50. */
  51. /** @defgroup ST7735_Private_TypesDefinitions
  52. * @{
  53. */
  54. /**
  55. * @}
  56. */
  57. /** @defgroup ST7735_Private_Defines
  58. * @{
  59. */
  60. /**
  61. * @}
  62. */
  63. /** @defgroup ST7735_Private_Macros
  64. * @{
  65. */
  66. /**
  67. * @}
  68. */
  69. /** @defgroup ST7735_Private_Variables
  70. * @{
  71. */
  72. LCD_DrvTypeDef st7735_drv =
  73. {
  74. st7735_Init,
  75. 0,
  76. st7735_DisplayOn,
  77. st7735_DisplayOff,
  78. st7735_SetCursor,
  79. st7735_WritePixel,
  80. 0,
  81. st7735_SetDisplayWindow,
  82. st7735_DrawHLine,
  83. st7735_DrawVLine,
  84. st7735_GetLcdPixelWidth,
  85. st7735_GetLcdPixelHeight,
  86. st7735_DrawBitmap,
  87. };
  88. static uint16_t ArrayRGB[320] = {0};
  89. /**
  90. * @}
  91. */
  92. /** @defgroup ST7735_Private_FunctionPrototypes
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /** @defgroup ST7735_Private_Functions
  99. * @{
  100. */
  101. /**
  102. * @brief Initialize the ST7735 LCD Component.
  103. * @param None
  104. * @retval None
  105. */
  106. void st7735_Init(void)
  107. {
  108. uint8_t data = 0;
  109. /* Initialize ST7735 low level bus layer -----------------------------------*/
  110. LCD_IO_Init();
  111. /* Out of sleep mode, 0 args, no delay */
  112. st7735_WriteReg(LCD_REG_17, 0x00);
  113. /* Frame rate ctrl - normal mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D)*/
  114. LCD_IO_WriteReg(LCD_REG_177);
  115. data = 0x01;
  116. LCD_IO_WriteMultipleData(&data, 1);
  117. data = 0x2C;
  118. LCD_IO_WriteMultipleData(&data, 1);
  119. data = 0x2D;
  120. LCD_IO_WriteMultipleData(&data, 1);
  121. /* Frame rate control - idle mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D) */
  122. st7735_WriteReg(LCD_REG_178, 0x01);
  123. st7735_WriteReg(LCD_REG_178, 0x2C);
  124. st7735_WriteReg(LCD_REG_178, 0x2D);
  125. /* Frame rate ctrl - partial mode, 6 args: Dot inversion mode, Line inversion mode */
  126. st7735_WriteReg(LCD_REG_179, 0x01);
  127. st7735_WriteReg(LCD_REG_179, 0x2C);
  128. st7735_WriteReg(LCD_REG_179, 0x2D);
  129. st7735_WriteReg(LCD_REG_179, 0x01);
  130. st7735_WriteReg(LCD_REG_179, 0x2C);
  131. st7735_WriteReg(LCD_REG_179, 0x2D);
  132. /* Display inversion ctrl, 1 arg, no delay: No inversion */
  133. st7735_WriteReg(LCD_REG_180, 0x07);
  134. /* Power control, 3 args, no delay: -4.6V , AUTO mode */
  135. st7735_WriteReg(LCD_REG_192, 0xA2);
  136. st7735_WriteReg(LCD_REG_192, 0x02);
  137. st7735_WriteReg(LCD_REG_192, 0x84);
  138. /* Power control, 1 arg, no delay: VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD */
  139. st7735_WriteReg(LCD_REG_193, 0xC5);
  140. /* Power control, 2 args, no delay: Opamp current small, Boost frequency */
  141. st7735_WriteReg(LCD_REG_194, 0x0A);
  142. st7735_WriteReg(LCD_REG_194, 0x00);
  143. /* Power control, 2 args, no delay: BCLK/2, Opamp current small & Medium low */
  144. st7735_WriteReg(LCD_REG_195, 0x8A);
  145. st7735_WriteReg(LCD_REG_195, 0x2A);
  146. /* Power control, 2 args, no delay */
  147. st7735_WriteReg(LCD_REG_196, 0x8A);
  148. st7735_WriteReg(LCD_REG_196, 0xEE);
  149. /* Power control, 1 arg, no delay */
  150. st7735_WriteReg(LCD_REG_197, 0x0E);
  151. /* Don't invert display, no args, no delay */
  152. LCD_IO_WriteReg(LCD_REG_32);
  153. /* Set color mode, 1 arg, no delay: 16-bit color */
  154. st7735_WriteReg(LCD_REG_58, 0x05);
  155. /* Column addr set, 4 args, no delay: XSTART = 0, XEND = 127 */
  156. LCD_IO_WriteReg(LCD_REG_42);
  157. data = 0x00;
  158. LCD_IO_WriteMultipleData(&data, 1);
  159. LCD_IO_WriteMultipleData(&data, 1);
  160. LCD_IO_WriteMultipleData(&data, 1);
  161. data = 0x7F;
  162. LCD_IO_WriteMultipleData(&data, 1);
  163. /* Row addr set, 4 args, no delay: YSTART = 0, YEND = 159 */
  164. LCD_IO_WriteReg(LCD_REG_43);
  165. data = 0x00;
  166. LCD_IO_WriteMultipleData(&data, 1);
  167. LCD_IO_WriteMultipleData(&data, 1);
  168. LCD_IO_WriteMultipleData(&data, 1);
  169. data = 0x9F;
  170. LCD_IO_WriteMultipleData(&data, 1);
  171. /* Magical unicorn dust, 16 args, no delay */
  172. st7735_WriteReg(LCD_REG_224, 0x02);
  173. st7735_WriteReg(LCD_REG_224, 0x1c);
  174. st7735_WriteReg(LCD_REG_224, 0x07);
  175. st7735_WriteReg(LCD_REG_224, 0x12);
  176. st7735_WriteReg(LCD_REG_224, 0x37);
  177. st7735_WriteReg(LCD_REG_224, 0x32);
  178. st7735_WriteReg(LCD_REG_224, 0x29);
  179. st7735_WriteReg(LCD_REG_224, 0x2d);
  180. st7735_WriteReg(LCD_REG_224, 0x29);
  181. st7735_WriteReg(LCD_REG_224, 0x25);
  182. st7735_WriteReg(LCD_REG_224, 0x2B);
  183. st7735_WriteReg(LCD_REG_224, 0x39);
  184. st7735_WriteReg(LCD_REG_224, 0x00);
  185. st7735_WriteReg(LCD_REG_224, 0x01);
  186. st7735_WriteReg(LCD_REG_224, 0x03);
  187. st7735_WriteReg(LCD_REG_224, 0x10);
  188. /* Sparkles and rainbows, 16 args, no delay */
  189. st7735_WriteReg(LCD_REG_225, 0x03);
  190. st7735_WriteReg(LCD_REG_225, 0x1d);
  191. st7735_WriteReg(LCD_REG_225, 0x07);
  192. st7735_WriteReg(LCD_REG_225, 0x06);
  193. st7735_WriteReg(LCD_REG_225, 0x2E);
  194. st7735_WriteReg(LCD_REG_225, 0x2C);
  195. st7735_WriteReg(LCD_REG_225, 0x29);
  196. st7735_WriteReg(LCD_REG_225, 0x2D);
  197. st7735_WriteReg(LCD_REG_225, 0x2E);
  198. st7735_WriteReg(LCD_REG_225, 0x2E);
  199. st7735_WriteReg(LCD_REG_225, 0x37);
  200. st7735_WriteReg(LCD_REG_225, 0x3F);
  201. st7735_WriteReg(LCD_REG_225, 0x00);
  202. st7735_WriteReg(LCD_REG_225, 0x00);
  203. st7735_WriteReg(LCD_REG_225, 0x02);
  204. st7735_WriteReg(LCD_REG_225, 0x10);
  205. /* Normal display on, no args, no delay */
  206. st7735_WriteReg(LCD_REG_19, 0x00);
  207. /* Main screen turn on, no delay */
  208. st7735_WriteReg(LCD_REG_41, 0x00);
  209. /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  210. st7735_WriteReg(LCD_REG_54, 0xC0);
  211. }
  212. /**
  213. * @brief Enables the Display.
  214. * @param None
  215. * @retval None
  216. */
  217. void st7735_DisplayOn(void)
  218. {
  219. uint8_t data = 0;
  220. LCD_IO_WriteReg(LCD_REG_19);
  221. LCD_Delay(10);
  222. LCD_IO_WriteReg(LCD_REG_41);
  223. LCD_Delay(10);
  224. LCD_IO_WriteReg(LCD_REG_54);
  225. data = 0xC0;
  226. LCD_IO_WriteMultipleData(&data, 1);
  227. }
  228. /**
  229. * @brief Disables the Display.
  230. * @param None
  231. * @retval None
  232. */
  233. void st7735_DisplayOff(void)
  234. {
  235. uint8_t data = 0;
  236. LCD_IO_WriteReg(LCD_REG_19);
  237. LCD_Delay(10);
  238. LCD_IO_WriteReg(LCD_REG_40);
  239. LCD_Delay(10);
  240. LCD_IO_WriteReg(LCD_REG_54);
  241. data = 0xC0;
  242. LCD_IO_WriteMultipleData(&data, 1);
  243. }
  244. /**
  245. * @brief Sets Cursor position.
  246. * @param Xpos: specifies the X position.
  247. * @param Ypos: specifies the Y position.
  248. * @retval None
  249. */
  250. void st7735_SetCursor(uint16_t Xpos, uint16_t Ypos)
  251. {
  252. uint8_t data = 0;
  253. LCD_IO_WriteReg(LCD_REG_42);
  254. data = (Xpos) >> 8;
  255. LCD_IO_WriteMultipleData(&data, 1);
  256. data = (Xpos) & 0xFF;
  257. LCD_IO_WriteMultipleData(&data, 1);
  258. LCD_IO_WriteReg(LCD_REG_43);
  259. data = (Ypos) >> 8;
  260. LCD_IO_WriteMultipleData(&data, 1);
  261. data = (Ypos) & 0xFF;
  262. LCD_IO_WriteMultipleData(&data, 1);
  263. LCD_IO_WriteReg(LCD_REG_44);
  264. }
  265. /**
  266. * @brief Writes pixel.
  267. * @param Xpos: specifies the X position.
  268. * @param Ypos: specifies the Y position.
  269. * @param RGBCode: the RGB pixel color
  270. * @retval None
  271. */
  272. void st7735_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  273. {
  274. uint8_t data = 0;
  275. if((Xpos >= ST7735_LCD_PIXEL_WIDTH) || (Ypos >= ST7735_LCD_PIXEL_HEIGHT))
  276. {
  277. return;
  278. }
  279. /* Set Cursor */
  280. st7735_SetCursor(Xpos, Ypos);
  281. data = RGBCode >> 8;
  282. LCD_IO_WriteMultipleData(&data, 1);
  283. data = RGBCode;
  284. LCD_IO_WriteMultipleData(&data, 1);
  285. }
  286. /**
  287. * @brief Writes to the selected LCD register.
  288. * @param LCDReg: Address of the selected register.
  289. * @param LCDRegValue: value to write to the selected register.
  290. * @retval None
  291. */
  292. void st7735_WriteReg(uint8_t LCDReg, uint8_t LCDRegValue)
  293. {
  294. LCD_IO_WriteReg(LCDReg);
  295. LCD_IO_WriteMultipleData(&LCDRegValue, 1);
  296. }
  297. /**
  298. * @brief Sets a display window
  299. * @param Xpos: specifies the X bottom left position.
  300. * @param Ypos: specifies the Y bottom left position.
  301. * @param Height: display window height.
  302. * @param Width: display window width.
  303. * @retval None
  304. */
  305. void st7735_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  306. {
  307. uint8_t data = 0;
  308. /* Column addr set, 4 args, no delay: XSTART = Xpos, XEND = (Xpos + Width - 1) */
  309. LCD_IO_WriteReg(LCD_REG_42);
  310. data = (Xpos) >> 8;
  311. LCD_IO_WriteMultipleData(&data, 1);
  312. data = (Xpos) & 0xFF;
  313. LCD_IO_WriteMultipleData(&data, 1);
  314. data = (Xpos + Width - 1) >> 8;
  315. LCD_IO_WriteMultipleData(&data, 1);
  316. data = (Xpos + Width - 1) & 0xFF;
  317. LCD_IO_WriteMultipleData(&data, 1);
  318. /* Row addr set, 4 args, no delay: YSTART = Ypos, YEND = (Ypos + Height - 1) */
  319. LCD_IO_WriteReg(LCD_REG_43);
  320. data = (Ypos) >> 8;
  321. LCD_IO_WriteMultipleData(&data, 1);
  322. data = (Ypos) & 0xFF;
  323. LCD_IO_WriteMultipleData(&data, 1);
  324. data = (Ypos + Height - 1) >> 8;
  325. LCD_IO_WriteMultipleData(&data, 1);
  326. data = (Ypos + Height - 1) & 0xFF;
  327. LCD_IO_WriteMultipleData(&data, 1);
  328. }
  329. /**
  330. * @brief Draws horizontal line.
  331. * @param RGBCode: Specifies the RGB color
  332. * @param Xpos: specifies the X position.
  333. * @param Ypos: specifies the Y position.
  334. * @param Length: specifies the line length.
  335. * @retval None
  336. */
  337. void st7735_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  338. {
  339. uint8_t counter = 0;
  340. if(Xpos + Length > ST7735_LCD_PIXEL_WIDTH) return;
  341. /* Set Cursor */
  342. st7735_SetCursor(Xpos, Ypos);
  343. for(counter = 0; counter < Length; counter++)
  344. {
  345. ArrayRGB[counter] = RGBCode;
  346. }
  347. LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  348. }
  349. /**
  350. * @brief Draws vertical line.
  351. * @param RGBCode: Specifies the RGB color
  352. * @param Xpos: specifies the X position.
  353. * @param Ypos: specifies the Y position.
  354. * @param Length: specifies the line length.
  355. * @retval None
  356. */
  357. void st7735_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  358. {
  359. uint8_t counter = 0;
  360. if(Ypos + Length > ST7735_LCD_PIXEL_HEIGHT) return;
  361. for(counter = 0; counter < Length; counter++)
  362. {
  363. st7735_WritePixel(Xpos, Ypos + counter, RGBCode);
  364. }
  365. }
  366. /**
  367. * @brief Gets the LCD pixel Width.
  368. * @param None
  369. * @retval The Lcd Pixel Width
  370. */
  371. uint16_t st7735_GetLcdPixelWidth(void)
  372. {
  373. return ST7735_LCD_PIXEL_WIDTH;
  374. }
  375. /**
  376. * @brief Gets the LCD pixel Height.
  377. * @param None
  378. * @retval The Lcd Pixel Height
  379. */
  380. uint16_t st7735_GetLcdPixelHeight(void)
  381. {
  382. return ST7735_LCD_PIXEL_HEIGHT;
  383. }
  384. /**
  385. * @brief Displays a bitmap picture loaded in the internal Flash.
  386. * @param BmpAddress: Bmp picture address in the internal Flash.
  387. * @retval None
  388. */
  389. void st7735_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  390. {
  391. uint32_t index = 0, size = 0;
  392. /* Read bitmap size */
  393. size = *(volatile uint16_t *) (pbmp + 2);
  394. size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  395. /* Get bitmap data address offset */
  396. index = *(volatile uint16_t *) (pbmp + 10);
  397. index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  398. size = (size - index)/2;
  399. pbmp += index;
  400. /* Set GRAM write direction and BGR = 0 */
  401. /* Memory access control: MY = 0, MX = 1, MV = 0, ML = 0 */
  402. st7735_WriteReg(LCD_REG_54, 0x40);
  403. /* Set Cursor */
  404. st7735_SetCursor(Xpos, Ypos);
  405. LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);
  406. /* Set GRAM write direction and BGR = 0 */
  407. /* Memory access control: MY = 1, MX = 1, MV = 0, ML = 0 */
  408. st7735_WriteReg(LCD_REG_54, 0xC0);
  409. }
  410. /**
  411. * @}
  412. */
  413. /**
  414. * @}
  415. */
  416. /**
  417. * @}
  418. */
  419. /**
  420. * @}
  421. */
  422. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/