[PIC®/AVR®/dsPIC®产品] ST7735 用于PIC微控制器的TFT库

[复制链接]
1604|15
 楼主| 598330983 发表于 2024-7-19 21:39 | 显示全部楼层 |阅读模式

  1. // --------------------------------------------------------------------------
  2. // ST7735-library (declaration)
  3. //
  4. // Author: Bernhard Bablok
  5. //
  6. // The code is based on work from Gavin Lyons, see
  7. // https://github.com/gavinlyonsrepo/pic_16F18346_projects
  8. //
  9. // https://github.com/bablokb/pic-st7735
  10. // --------------------------------------------------------------------------

  11. #ifndef ST7735_TFT_H
  12. #define ST7735_TFT_H

  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <string.h>

  16. #if defined _PTC
  17.   #include "picconfig.h"
  18. #endif

  19. #if defined TFT_ENABLE_FONTS
  20.   #if !defined TFT_ENABLE_TEXT
  21.     #define TFT_ENABLE_TEXT
  22.   #endif
  23.   #if !defined PROGMEM
  24.     #define PROGMEM
  25.   #endif
  26. #endif

  27. #if defined TFT_ENABLE_ALL
  28.   #if !defined TFT_ENABLE_SHAPES
  29.     #define TFT_ENABLE_SHAPES
  30.   #endif
  31.   #if !defined TFT_ENABLE_SCROLL
  32.     #define TFT_ENABLE_SCROLL
  33.   #endif
  34.   #if !defined TFT_ENABLE_TEXT
  35.     #define TFT_ENABLE_TEXT
  36.   #endif
  37.   #if !defined TFT_ENABLE_ROTATE
  38.     #define TFT_ENABLE_ROTATE
  39.   #endif
  40. #endif

  41. extern uint8_t tft_width, tft_height;

  42. #define _swap(a, b) { int16_t t; t = a; a = b; b = t;}
  43. #define ST7735_NOP     0x00
  44. #define ST7735_SWRESET 0x01
  45. #define ST7735_RDDID   0x04
  46. #define ST7735_RDDST   0x09
  47. #define ST7735_SLPIN   0x10
  48. #define ST7735_SLPOUT  0x11
  49. #define ST7735_PTLON   0x12
  50. #define ST7735_NORON   0x13
  51. #define ST7735_INVOFF  0x20
  52. #define ST7735_INVON   0x21
  53. #define ST7735_DISPOFF 0x28
  54. #define ST7735_DISPON  0x29
  55. #define ST7735_CASET   0x2A
  56. #define ST7735_RASET   0x2B
  57. #define ST7735_RAMWR   0x2C
  58. #define ST7735_RAMRD   0x2E
  59. #define ST7735_PTLAR   0x30
  60. #define ST7735_VSCRDEF 0x33
  61. #define ST7735_COLMOD  0x3A
  62. #define ST7735_MADCTL  0x36
  63. #define ST7735_MADCTL_MY 0x80
  64. #define ST7735_MADCTL_MX 0x40
  65. #define ST7735_MADCTL_MV 0x20
  66. #define ST7735_MADCTL_ML 0x10
  67. #define ST7735_MADCTL_RGB 0x00
  68. #define ST7735_VSCRSADD 0x37
  69. #define ST7735_FRMCTR1 0xB1
  70. #define ST7735_FRMCTR2 0xB2
  71. #define ST7735_FRMCTR3 0xB3
  72. #define ST7735_INVCTR  0xB4
  73. #define ST7735_DISSET5 0xB6
  74. #define ST7735_PWCTR1  0xC0
  75. #define ST7735_PWCTR2  0xC1
  76. #define ST7735_PWCTR3  0xC2
  77. #define ST7735_PWCTR4  0xC3
  78. #define ST7735_PWCTR5  0xC4
  79. #define ST7735_VMCTR1  0xC5
  80. #define ST7735_RDID1   0xDA
  81. #define ST7735_RDID2   0xDB
  82. #define ST7735_RDID3   0xDC
  83. #define ST7735_RDID4   0xDD
  84. #define ST7735_PWCTR6  0xFC
  85. #define ST7735_GMCTRP1 0xE0
  86. #define ST7735_GMCTRN1 0xE1

  87. // Color definitions
  88. #define   ST7735_BLACK   0x0000
  89. #define   ST7735_BLUE    0x001F
  90. #define   ST7735_RED     0xF800
  91. #define   ST7735_GREEN   0x07E0
  92. #define   ST7735_CYAN    0x07FF
  93. #define   ST7735_MAGENTA 0xF81F
  94. #define   ST7735_YELLOW  0xFFE0
  95. #define   ST7735_WHITE   0xFFFF

  96. #define LCD_ASCII_OFFSET 0x20 //0x20, ASCII character for Space, The font table starts with this character


  97. // ******** FUNCTION  PROTOTYPES ************

  98. // SPI
  99. void write_command(uint8_t );
  100. void write_data(uint8_t );

  101. // Init
  102. #if defined TFT_ENABLE_GREEN
  103. void TFT_GreenTab_Initialize(void);
  104. void Rcmd2green();
  105. #endif

  106. #if defined TFT_ENABLE_RED
  107. void TFT_RedTab_Initialize(void);
  108. #endif

  109. #if defined TFT_ENABLE_BLACK
  110. void TFT_BlackTab_Initialize(void);
  111. #endif

  112. #if defined(TFT_ENABLE_RED) || defined(TFT_ENABLE_BLACK)
  113. void Rcmd2red();
  114. #endif

  115. #if defined TFT_ENABLE_GENERIC
  116. void TFT_ST7735B_Initialize(void);
  117. void Bcmd();
  118. #endif

  119. #if defined TFT_ENABLE_RESET
  120. void TFT_ResetPIN(void);
  121. #endif

  122. void Rcmd1();
  123. void Rcmd3();

  124. // Misc + Screen related
  125. void setAddrWindow(uint8_t , uint8_t , uint8_t , uint8_t );
  126. void fillScreen(uint16_t color);
  127. void drawFastVLine(uint8_t x, uint8_t y, uint8_t h, uint16_t color);
  128. void drawFastHLine(uint8_t x, uint8_t y, uint8_t w, uint16_t color);
  129. void drawPixel(uint8_t , uint8_t , uint16_t );
  130. void fillRectangle(uint8_t , uint8_t , uint8_t , uint8_t , uint16_t );
  131. void fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);

  132. void invertDisplay(bool i);
  133. void NormalDisplay(void);
  134. void pushColor(uint16_t color);

  135. //Scroll
  136. #if defined TFT_ENABLE_SCROLL
  137. void setScrollDefinition(uint8_t top_fix_height, uint8_t bottom_fix_height, bool _scroll_direction);
  138. void VerticalScroll(uint8_t _vsp);
  139. #endif

  140. // Shapes
  141. #if defined TFT_ENABLE_SHAPES
  142. void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
  143. void drawRectWH(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color);
  144. void drawRoundRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t r, uint16_t color);
  145. void fillRoundRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t r, uint16_t color);
  146. void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
  147. void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
  148. void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
  149. void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
  150. void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
  151. void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
  152. #endif

  153. // Text
  154. #if defined(TFT_ENABLE_TEXT)
  155. void drawChar(uint8_t x, uint8_t y, uint8_t c, uint16_t color, uint16_t bg,  uint8_t size);
  156. void setTextWrap(bool w);
  157. void drawText(uint8_t x, uint8_t y, const char *_text, uint16_t color, uint16_t bg, uint8_t size);
  158. #endif

  159. #if defined TFT_ENABLE_ROTATE
  160. void setRotation(uint8_t m);
  161. #endif

  162. #if defined TFT_ENABLE_FONTS
  163. /// Font data stored PER GLYPH
  164. typedef struct {
  165.   uint16_t bitmapOffset; ///< Pointer into GFXfont->bitmap
  166.   uint8_t width;         ///< Bitmap dimensions in pixels
  167.   uint8_t height;        ///< Bitmap dimensions in pixels
  168.   uint8_t xAdvance;      ///< Distance to advance cursor (x axis)
  169.   int8_t xOffset;        ///< X dist from cursor pos to UL corner
  170.   int8_t yOffset;        ///< Y dist from cursor pos to UL corner
  171. } GFXglyph;

  172. /// Data stored for FONT AS A WHOLE
  173. typedef struct {
  174.   uint8_t *bitmap;     ///< Glyph bitmaps, concatenated
  175.   GFXglyph *glyph;     ///< Glyph array
  176.   uint16_t first;      ///< ASCII extents (first char)
  177.   uint16_t last;       ///< ASCII extents (last char)
  178.   uint8_t yAdvance;    ///< Newline distance (y axis)
  179.   const char *subset;  ///< subset of chars in the font
  180. } GFXfont;

  181. extern GFXfont *_gfxFont;
  182. void setFont(const GFXfont *f);
  183. #endif

  184. // ------ not functional yet ----------------------------------------------
  185. // needs fat-library

  186. // BMP processing from SD-card
  187. #if defined TFT_ENABLE_BMP
  188. bool bmpDraw(int8_t x, int8_t y, int8_t *bmpname);
  189. #endif

  190. #endif


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
huahuagg 发表于 2024-7-20 12:26 | 显示全部楼层
看不太明白。
huahuagg 发表于 2024-7-20 12:27 | 显示全部楼层
一般是都做好函数,只需要实现一个SPI的发送函数即可实现使用。
liu96jp 发表于 2024-8-6 14:29 | 显示全部楼层
其实所谓的库就是代码写的比较立正点吧
g0d5xs 发表于 2024-8-6 15:36 | 显示全部楼层
一般来说,这种库函数还是比较好用的,只要底层接入成功,基本驱动都是OK的
ex7s4 发表于 2024-8-6 16:38 | 显示全部楼层
其实我觉得不如用LVGL这种库比较好用
zhizia4f 发表于 2024-8-6 17:43 | 显示全部楼层
其实还有用u8g2的,这个也是不错的选择
y1n9an 发表于 2024-8-6 18:49 | 显示全部楼层
一般开源的库比较多,其实可以随便用哪款都可以的
su1yirg 发表于 2024-8-6 20:52 | 显示全部楼层
这个PIC是不是还能用arduino开发啊?直接可以用C++的库吧
l1uyn9b 发表于 2024-8-7 08:30 | 显示全部楼层
你这个代码都是头文件啊?压缩包里是C文件吗?
lamanius 发表于 2024-8-7 09:13 | 显示全部楼层
看这个例程好像有点熟悉,有点中景园的感觉
tax2r6c 发表于 2024-8-7 11:00 | 显示全部楼层
基本函数都有了,可以的,有机会下载来玩玩
suw12q 发表于 2024-8-7 13:18 | 显示全部楼层
这种屏的分辨率是多少?没看出来呢?
呐咯密密 发表于 2024-8-15 14:51 | 显示全部楼层
SPI的接口是软件模拟的吗
旧年胶片 发表于 2025-9-20 16:28 | 显示全部楼层
ST7735 的 PIC 微控制器 TFT 库,提供初始化、绘图、文本显示等函数,通过 SPI 通信控制屏幕。可设置像素、画图形、显示字符,支持颜色配置。需配置 SPI 引脚及时序,适配 PIC 的 I/O 口。常见库有 GitHub 上的开源实现,需根据 PIC 型号修改引脚定义,方便快速驱动 1.8 英寸等 TFT 屏
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

5575

帖子

22

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