数码之家

 找回密码
 立即注册
搜索
查看: 1558|回复: 22

[STM] LCD 12864 驱动有问题,谁能看出时啥问题啊

[复制链接]
发表于 2022-11-12 22:33:55 | 显示全部楼层 |阅读模式
100家元
买的lcd 12864 网上下载的驱动,显示是这样的,不知道哪里错了,哪位大佬能指点下
微信图片_20221112223026.jpg

  1. /******************************************************************************
  2. **
  3. * \file        st7567.h
  4. * \author      IOsetting | iosetting@outlook.com
  5. * \date        
  6. * \brief       Library of ST7567 LCD on W806
  7. * \note        
  8. * \version     v0.1
  9. * \ingroup     demo
  10. * \remarks     test-board: HLK-W806-KIT-V1.0
  11. *              
  12. ******************************************************************************/

  13. #ifndef __ST7567_H_
  14. #define __ST7567_H_

  15. #include "stm32f4xx_hal.h"
  16. #include "ascii_fonts.h"
  17. #include "main.h"

  18. #define ST7567_BUF_SIZE         1024
  19. #define ST7567_HARDWARE_SPI     1

  20. // CS: B4, B14
  21. #define ST7567_CS_PORT      GPIOB
  22. #define ST7567_CS_PIN       ST7567_CS_Pin
  23. // SCK: B1, B2, B15, B24
  24. #define ST7567_SCK_PORT     GPIOB
  25. #define ST7567_SCK_PIN      GPIO_PIN_10
  26. // MOSI: B5, B17, B26, PA7
  27. #define ST7567_MOSI_PORT    GPIOB
  28. #define ST7567_MOSI_PIN     GPIO_PIN_15
  29. // MISO: B0, B3, B16, B25

  30. #define ST7567_RES_PORT     GPIOB
  31. #define ST7567_RES_PIN      ST7567_RST_Pin
  32. #define ST7567_DC_PORT      GPIOB
  33. #define ST7567_DC_PIN       ST7567_DC_Pin
  34. #define ST7567_BL_PORT      GPIOC
  35. #define ST7567_BL_PIN       LED_Pin

  36. #if ST7567_HARDWARE_SPI
  37.     extern SPI_HandleTypeDef    hspi;
  38.     #define ST7567_CS_LOW       HAL_GPIO_WritePin(GPIOB, ST7567_CS_Pin, GPIO_PIN_RESET)
  39.     #define ST7567_CS_HIGH      HAL_GPIO_WritePin(GPIOB, ST7567_CS_Pin, GPIO_PIN_SET)
  40. #else
  41.     #define ST7567_SCK_LOW      HAL_GPIO_WritePin(ST7567_SCK_PORT, ST7567_SCK_PIN, GPIO_PIN_RESET)
  42.     #define ST7567_SCK_HIGH     HAL_GPIO_WritePin(ST7567_SCK_PORT, ST7567_SCK_PIN, GPIO_PIN_SET)
  43.     #define ST7567_CS_LOW       HAL_GPIO_WritePin(ST7567_CS_PORT, ST7567_CS_PIN, GPIO_PIN_RESET)
  44.     #define ST7567_CS_HIGH      HAL_GPIO_WritePin(ST7567_CS_PORT, ST7567_CS_PIN, GPIO_PIN_SET)
  45.     #define ST7567_MOSI_LOW     HAL_GPIO_WritePin(ST7567_MOSI_PORT, ST7567_MOSI_PIN, GPIO_PIN_RESET)
  46.     #define ST7567_MOSI_HIGH    HAL_GPIO_WritePin(ST7567_MOSI_PORT, ST7567_MOSI_PIN, GPIO_PIN_SET)
  47. #endif

  48. #define ST7567_BL_LOW           HAL_GPIO_WritePin(ST7567_BL_PORT, ST7567_BL_PIN, GPIO_PIN_RESET)
  49. #define ST7567_BL_HIGH          HAL_GPIO_WritePin(ST7567_BL_PORT, ST7567_BL_PIN, GPIO_PIN_SET)
  50. #define ST7567_DC_LOW           HAL_GPIO_WritePin(ST7567_DC_PORT, ST7567_DC_PIN, GPIO_PIN_RESET)
  51. #define ST7567_DC_HIGH          HAL_GPIO_WritePin(ST7567_DC_PORT, ST7567_DC_PIN, GPIO_PIN_SET)
  52. #define ST7567_RESET_LOW        HAL_GPIO_WritePin(ST7567_RES_PORT, ST7567_RES_PIN, GPIO_PIN_RESET)
  53. #define ST7567_RESET_HIGH       HAL_GPIO_WritePin(ST7567_RES_PORT, ST7567_RES_PIN, GPIO_PIN_SET)

  54. // X width
  55. #define ST7567_WIDTH  128
  56. // Y height
  57. #define ST7567_HEIGHT 64
  58. // Additional bytes in each row
  59. #define ST7567_SEG_EXPAND 4
  60. // X orientation
  61. #define ST7567_X_ORIENT ST7567_SEG_DIRECTION_REVERSE
  62. // Y orientation
  63. #define ST7567_Y_ORIENT ST7567_COM_DIRECTION_NORMAL


  64. /* ST7567 commands definitions */
  65. #define ST7567_DISPLAY_OFF                   0xAE /* 0xae: Display OFF (sleep mode) */
  66. #define ST7567_DISPLAY_ON                    0xAF /* 0xaf: Display ON in normal mode */

  67. #define ST7567_SET_START_LINE                0x40 /* 0x40-7f: Set display start line */
  68. #define  ST7567_SET_START_LINE_MASK          0x3f

  69. #define ST7567_SET_PAGE_ADDRESS              0xB0 /* 0xb0-b7: Set page start address */
  70. #define  ST7567_SET_PAGE_ADDRESS_MASK        0x07

  71. #define ST7567_SET_COLUMN_ADDRESS_MSB        0x10 /* 0x10-0x1f: Set higher column address */
  72. #define ST7567_SET_COLUMN_ADDRESS_MSB_MASK   0x0f

  73. #define ST7567_SET_COLUMN_ADDRESS_LSB        0x00 /* 0x00-0x0f: Set lower column address */
  74. #define  ST7567_SET_COLUMN_ADDRESS_LSB_MASK  0x0F

  75. /**
  76. * SEG: 0 - 131
  77. */
  78. #define ST7567_SEG_DIRECTION_NORMAL          0xA0 /* 0xa0: Column address 0 is mapped to SEG0 */
  79. #define ST7567_SEG_DIRECTION_REVERSE         0xA1 /* 0xa1: Column address 128 is mapped to SEG0 */

  80. /**
  81. * COM: 0 - 63
  82. */
  83. #define ST7567_COM_DIRECTION_NORMAL          0xC0 /* 0xc0: Set COM output direction, normal mode */
  84. #define ST7567_COM_DIRECTION_REVERSE         0xC8 /* 0xc8: Set COM output direction, reverse mode */

  85. #define ST7567_INVERSE_DISPLAY_OFF           0xA6 /* 0xa6: Normal display */
  86. #define ST7567_INVERSE_DISPLAY_ON            0xA7 /* 0xa7: Inverse display */

  87. #define ST7567_ALL_PIXEL_ON                  0xA5 /* 0xa5: Entire display ON */
  88. #define ST7567_ALL_PIXEL_NORMAL              0xA4 /* 0xa4: Resume to RAM content display */

  89. #define ST7567_BIAS_1_9                      0xA2 /* 0xa2: Select BIAS setting 1/9 */
  90. #define ST7567_BIAS_1_7                      0xA3 /* 0xa3: Select BIAS setting 1/7 */

  91. #define ST7567_READ_MODIFY_WRITE_START       0xE0 /* 0xe0: Enter the Read Modify Write mode */
  92. #define ST7567_READ_MODIFY_WRITE_END         0xEE /* 0xee: Leave the Read Modify Write mode */
  93. #define ST7567_RESET                         0xE2 /* 0xe2: Software RESET */

  94. /**
  95. * This instruction controls the built-in power circuits.
  96. * Typically, these 3 flags are turned ON at the same time.
  97. */
  98. #define ST7567_POWER_CONTROL                 0x28
  99. #define ST7567_POWER_CONTROL_VF              0x01
  100. #define ST7567_POWER_CONTROL_VR              0x02
  101. #define ST7567_POWER_CONTROL_VB              0x04

  102. /**
  103. * The operation voltage (V0) calculation formula is shown below:
  104. * (RR comes from Regulation Ratio, EV comes from EV[5:0])
  105. * V0 = RR X [ 1 – (63 – EV) / 162 ] X 2.1, or
  106. * V0 = RR X [ ( 99 + EV ) / 162 ] X 2.1
  107. */
  108. #define ST7567_REGULATION_RATIO              0x20
  109. #define ST7567_REGULATION_RATIO_3_0          0x00
  110. #define ST7567_REGULATION_RATIO_3_5          0x01
  111. #define ST7567_REGULATION_RATIO_4_0          0x02
  112. #define ST7567_REGULATION_RATIO_4_5          0x03
  113. #define ST7567_REGULATION_RATIO_5_0          0x04 /* Default */
  114. #define ST7567_REGULATION_RATIO_5_5          0x05
  115. #define ST7567_REGULATION_RATIO_6_0          0x06
  116. #define ST7567_REGULATION_RATIO_6_5          0x07

  117. /**
  118. * This is double byte instruction. The first byte set ST7567 into EV
  119. * adjust mode and the following instruction will change the EV setting.
  120. * That means these 2 bytes must be used together. They control the electronic
  121. * volume to adjust a suitable V0 voltage for the LCD.
  122. */
  123. #define ST7567_SET_EV                        0x81
  124. #define ST7567_SET_EV_MASK                   0x3F

  125. #define ST7567_SET_BOOSTER                   0xF8 /* Set booster level */
  126. #define ST7567_SET_BOOSTER_4X                0x00
  127. #define ST7567_SET_BOOSTER_5X                0x01

  128. #define ST7567_NOP                           0xE3
  129. #define ST7567_TEST                          0xFE

  130. #ifndef ST7567_TIMEOUT
  131. #define ST7567_TIMEOUT                                        20000
  132. #endif

  133. /** Background color */
  134. #define ST7567_COLOR_BACK 0x00
  135. /** Front color */
  136. #define ST7567_COLOR_FRONT 0x01

  137. /**
  138. * [url=home.php?mod=space&uid=650075]@brief[/url]  Initializes ST7567 LCD
  139. * @param  None
  140. * @retval None
  141. */
  142. void ST7567_Init(void);

  143. /**
  144. * @brief  Hardware reset ST7567 LCD
  145. * @param  None
  146. * @retval None
  147. */
  148. void ST7567_Reset(void);

  149. /**
  150. * @brief  Turn ST7567 LCD backlight on
  151. * @param  None
  152. * @retval None
  153. */
  154. void ST7567_BackLight_On(void);

  155. /**
  156. * @brief  Turn ST7567 LCD backlight off
  157. * @param  None
  158. * @retval None
  159. */
  160. void ST7567_BackLight_Off(void);

  161. /**
  162. * @brief  Turn ST7567 LCD backlight off
  163. * @param  val: value between 0x00 ~ 0x3F
  164. * @retval None
  165. */
  166. void ST7567_SetContrast(uint8_t val);

  167. /**
  168. * @brief  Updates buffer from internal RAM to LCD
  169. * @note   This function must be called each time you do some changes to LCD, to update buffer from RAM to LCD
  170. * @param  None
  171. * @retval None
  172. */
  173. void ST7567_UpdateScreen(void);

  174. /**
  175. * @brief  Toggles pixels invertion inside internal RAM
  176. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  177. * @param  None
  178. * @retval None
  179. */
  180. void ST7567_ToggleInvert(void);

  181. /**
  182. * @brief  Fills entire LCD with desired color
  183. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  184. * @param  Color: Color to be used for screen fill. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  185. * @retval None
  186. */
  187. void ST7567_Fill(uint8_t Color);

  188. /**
  189. * @brief  Draws pixel at desired location
  190. * @note   @ref ST7567_UpdateScreen() must called after that in order to see updated LCD screen
  191. * @param  x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
  192. * @param  y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
  193. * @param  color: Color to be used for screen fill. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  194. * @retval None
  195. */
  196. void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color);

  197. /**
  198. * @brief  Sets cursor pointer to desired location for strings
  199. * @param  x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
  200. * @param  y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
  201. * @retval None
  202. */
  203. void ST7567_GotoXY(uint16_t x, uint16_t y);

  204. /**
  205. * @brief  Puts character to internal RAM
  206. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  207. * @param  ch: Character to be written
  208. * @param  *Font: Pointer to @ref FontDef_t structure with used font
  209. * @param  color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  210. * @retval Character written
  211. */
  212. char ST7567_Putc(char ch, FontDef_t* Font, uint8_t color);

  213. /**
  214. * @brief  Puts string to internal RAM
  215. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  216. * @param  *str: String to be written
  217. * @param  *Font: Pointer to @ref FontDef_t structure with used font
  218. * @param  color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  219. * @retval Zero on success or character value when function failed
  220. */
  221. char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color);

  222. /**
  223. * @brief  Draws line on LCD
  224. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  225. * @param  x0: Line X start point. Valid input is 0 to ST7567_WIDTH - 1
  226. * @param  y0: Line Y start point. Valid input is 0 to ST7567_HEIGHT - 1
  227. * @param  x1: Line X end point. Valid input is 0 to ST7567_WIDTH - 1
  228. * @param  y1: Line Y end point. Valid input is 0 to ST7567_HEIGHT - 1
  229. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  230. * @retval None
  231. */
  232. void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c);

  233. /**
  234. * @brief  Draws rectangle on LCD
  235. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  236. * @param  x: Top left X start point. Valid input is 0 to ST7567_WIDTH - 1
  237. * @param  y: Top left Y start point. Valid input is 0 to ST7567_HEIGHT - 1
  238. * @param  w: Rectangle width in units of pixels
  239. * @param  h: Rectangle height in units of pixels
  240. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  241. * @retval None
  242. */
  243. void ST7567_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c);

  244. /**
  245. * @brief  Draws filled rectangle on LCD
  246. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  247. * @param  x: Top left X start point. Valid input is 0 to ST7567_WIDTH - 1
  248. * @param  y: Top left Y start point. Valid input is 0 to ST7567_HEIGHT - 1
  249. * @param  w: Rectangle width in units of pixels
  250. * @param  h: Rectangle height in units of pixels
  251. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  252. * @retval None
  253. */
  254. void ST7567_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c);

  255. /**
  256. * @brief  Draws triangle on LCD
  257. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  258. * @param  x1: First coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
  259. * @param  y1: First coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
  260. * @param  x2: Second coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
  261. * @param  y2: Second coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
  262. * @param  x3: Third coordinate X location. Valid input is 0 to ST7567_WIDTH - 1
  263. * @param  y3: Third coordinate Y location. Valid input is 0 to ST7567_HEIGHT - 1
  264. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  265. * @retval None
  266. */
  267. void ST7567_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color);

  268. /**
  269. * @brief  Draws circle to STM buffer
  270. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  271. * @param  x: X location for center of circle. Valid input is 0 to ST7567_WIDTH - 1
  272. * @param  y: Y location for center of circle. Valid input is 0 to ST7567_HEIGHT - 1
  273. * @param  r: Circle radius in units of pixels
  274. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  275. * @retval None
  276. */
  277. void ST7567_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c);

  278. /**
  279. * @brief  Draws filled circle to STM buffer
  280. * @note   @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  281. * @param  x: X location for center of circle. Valid input is 0 to ST7567_WIDTH - 1
  282. * @param  y: Y location for center of circle. Valid input is 0 to ST7567_HEIGHT - 1
  283. * @param  r: Circle radius in units of pixels
  284. * @param  c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  285. * @retval None
  286. */
  287. void ST7567_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c);

  288. /**
  289. * @brief  Writes multi bytes to slave
  290. * @param  *I2Cx: I2C used
  291. * @param  address: 7 bit slave address, left aligned, bits 7:1 are used, LSB bit is not used
  292. * @param  reg: register to write to
  293. * @param  *data: pointer to data array to write it to slave
  294. * @param  count: how many bytes will be written
  295. * @retval None
  296. */
  297. void ST7567_Image(uint8_t *img, uint8_t frame, uint8_t x, uint8_t y);

  298. /**
  299. * @brief  Writes single byte command to slave
  300. * @param  command: command to be written
  301. * @retval None
  302. */
  303. void ST7567_WriteCommand(uint8_t command);

  304. /**
  305. * @brief  Writes single byte data to slave
  306. * @param  data: data to be written
  307. * @retval None
  308. */<div class="blockcode"><blockquote>/******************************************************************************
  309. **
  310. * \file        st7567.c
  311. * \author      IOsetting | iosetting@outlook.com
  312. * \date        
  313. * \brief       Library of ST7567 LCD on W806
  314. * \note        
  315. * \version     v0.1
  316. * \ingroup     demo
  317. * \remarks     test-board: HLK-W806-KIT-V1.0
  318. *
  319. *              B10   -> RES, RESET
  320. *              B11   -> DC, A0
  321. *              B14   -> CSB, Chip Select
  322. *              B15   -> SCK, SCL, CLK, Clock
  323. *              B16   -> LED-A, Backlight
  324. *              B17   -> MOSI, SDA
  325. *              GND   -> GND
  326. *              3.3V  -> VCC
  327. *
  328. *              ST7567_HARDWARE_SPI - 0: Software SPI, 1: Hardware SPI
  329. *
  330. ******************************************************************************/

  331. #include "st7567.h"

  332. /* Absolute value */
  333. #define ABS(x)   ((x) > 0 ? (x) : -(x))

  334. #if ST7567_X_ORIENT == ST7567_SEG_DIRECTION_REVERSE
  335.     #define ST7567_X_OFFSET  ST7567_SEG_EXPAND
  336. #else
  337.     #define ST7567_X_OFFSET  0
  338. #endif

  339. /**
  340. * In datasheet, it says "the column address is increased (+1) after each display
  341. * data access (read/write). This allows MPU accessing DDRAM content continuously.
  342. * This feature stops at the end of each page (Column Address “83h”) because the
  343. * Column Address and Page Address circuits are independent. For example, both Page
  344. * Address and Column Address should be assigned for changing the DDRAM pointer
  345. * from (Page-0, Column-83h) to (Page-1, Column-0)."
  346. * In actual test the Page Address will grow automatically.
  347. */
  348. /* ST7567 data buffer */
  349. static uint8_t ST7567_Buffer_all[(ST7567_WIDTH + ST7567_SEG_EXPAND) * ST7567_HEIGHT / 8];

  350. /* Private ST7567 structure */
  351. typedef struct {
  352.     uint16_t CurrentX;
  353.     uint16_t CurrentY;
  354.     uint8_t Inverted;
  355.     uint8_t Initialized;
  356. } ST7567_t;

  357. /* Private variable */
  358. static ST7567_t ST7567;

  359. extern SPI_HandleTypeDef hspi2;


  360. static void ST7567_TransmitByte(uint8_t dat)
  361. {
  362. #if ST7567_HARDWARE_SPI
  363.     ST7567_CS_LOW;
  364.     HAL_SPI_Transmit(&hspi2, &dat, 1, ST7567_TIMEOUT);
  365.     ST7567_CS_HIGH;
  366. #else
  367.     uint8_t i;
  368.     ST7567_CS_LOW;
  369.     for (i = 0; i < 8; i++)
  370.     {
  371.         ST7567_SCK_LOW;
  372.         if (dat & 0x80)
  373.         {
  374.             ST7567_MOSI_HIGH;
  375.         }
  376.         else
  377.         {
  378.             ST7567_MOSI_LOW;
  379.         }
  380.         ST7567_SCK_HIGH;
  381.         dat <<= 1;
  382.     }
  383.     ST7567_CS_HIGH;
  384. #endif
  385. }

  386. static void ST7567_Transmit(uint8_t *pData, uint32_t Size, uint32_t Timeout)
  387. {
  388. #if ST7567_HARDWARE_SPI
  389.     ST7567_CS_LOW;
  390.     HAL_SPI_Transmit(&hspi2, pData, Size, Timeout);
  391.     ST7567_CS_HIGH;
  392. #else
  393.     while (Size-- > 0)
  394.     {
  395.         ST7567_TransmitByte(*(pData++));
  396.     }
  397. #endif
  398. }

  399. void ST7567_WriteCommand(uint8_t command)
  400. {
  401.     ST7567_DC_LOW;
  402.     ST7567_TransmitByte(command);
  403.     ST7567_DC_HIGH;
  404. }

  405. void ST7567_WriteData(uint8_t data)
  406. {
  407.     ST7567_TransmitByte(data);
  408. }

  409. void ST7567_Init(void)
  410. {
  411.     ST7567_Reset();
  412.     ST7567_BackLight_On();

  413.     ST7567_WriteCommand(ST7567_RESET);
  414.     ST7567_WriteCommand(ST7567_POWER_CONTROL
  415.         |ST7567_POWER_CONTROL_VB
  416.         |ST7567_POWER_CONTROL_VR
  417.         |ST7567_POWER_CONTROL_VF);
  418.     ST7567_WriteCommand(ST7567_SET_EV);
  419.     ST7567_WriteCommand(ST7567_SET_EV_MASK & 0x20);
  420.     ST7567_WriteCommand(ST7567_BIAS_1_9);
  421.     ST7567_WriteCommand(ST7567_X_ORIENT);
  422.     ST7567_WriteCommand(ST7567_Y_ORIENT);
  423.     ST7567_WriteCommand(ST7567_REGULATION_RATIO | ST7567_REGULATION_RATIO_5_0);
  424.     ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_OFF);
  425.     ST7567_WriteCommand(ST7567_DISPLAY_ON);
  426.     ST7567_WriteCommand(ST7567_ALL_PIXEL_NORMAL);

  427.     ST7567_WriteCommand(ST7567_SET_START_LINE | (0x00 & ST7567_SET_START_LINE_MASK));
  428.     ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS | (0x00 & ST7567_SET_PAGE_ADDRESS_MASK));
  429.     ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB);
  430.     ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB);
  431. }

  432. void ST7567_Reset(void)
  433. {
  434.     ST7567_RESET_LOW;
  435.     HAL_Delay(5);
  436.     ST7567_RESET_HIGH;
  437. }


  438. void ST7567_BackLight_On(void)
  439. {
  440.     ST7567_BL_HIGH;
  441. }

  442. void ST7567_BackLight_Off(void)
  443. {
  444.     ST7567_BL_LOW;
  445. }

  446. void ST7567_SetContrast(uint8_t val)
  447. {
  448.     ST7567_WriteCommand(ST7567_SET_EV);
  449.     ST7567_WriteCommand(ST7567_SET_EV_MASK & val);
  450. }

  451. void ST7567_UpdateScreen(void)
  452. {
  453.     ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS | (0x00 & ST7567_SET_PAGE_ADDRESS_MASK));
  454.     ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB);
  455.     ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB);
  456.     ST7567_Transmit(ST7567_Buffer_all, sizeof(ST7567_Buffer_all), ST7567_TIMEOUT);
  457. }

  458. void ST7567_ToggleInvert(void)
  459. {
  460.     /* Toggle invert */
  461.     ST7567.Inverted = !ST7567.Inverted;
  462.     if (ST7567.Inverted)
  463.     {
  464.         ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_ON);
  465.     }
  466.     else
  467.     {
  468.         ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_OFF);
  469.     }
  470. }

  471. void ST7567_Fill(uint8_t color)
  472. {
  473.     /* Set memory */
  474.     memset((void*)ST7567_Buffer_all, (color == ST7567_COLOR_BACK) ? 0x00 : 0xFF, sizeof(ST7567_Buffer_all));
  475. }

  476. void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
  477. {
  478.     if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
  479.     {
  480.         /* Error */
  481.         return;
  482.     }

  483.     if (color == ST7567_COLOR_FRONT)
  484.     {
  485.         ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] |= 1 << (y % 8);
  486.     }
  487.     else
  488.     {
  489.         ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] &= ~(1 << (y % 8));
  490.     }
  491. }

  492. void ST7567_GotoXY(uint16_t x, uint16_t y)
  493. {
  494.     /* Set write pointers */
  495.     ST7567.CurrentX = x;
  496.     ST7567.CurrentY = y;
  497. }

  498. char ST7567_Putc(char ch, FontDef_t* font, uint8_t color)
  499. {
  500.     uint32_t i, b, j, k;

  501.     for (i = 0; i < font->height; i++)
  502.     {
  503.         for (j = 0; j < font->bytes; j++)
  504.         {
  505.             b = font->data[((ch - 32) * font->height + i) * font->bytes + j];
  506.             if (font->order == 0)
  507.             {
  508.                 for (k = 0; k < 8 && k < font->width - j * 8; k++)
  509.                 {
  510.                     if ((b << k) & 0x80)
  511.                     {
  512.                         ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color);
  513.                     }
  514.                     else
  515.                     {
  516.                         ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) !color);
  517.                     }
  518.                 }
  519.             }
  520.             else
  521.             {
  522.                 for (k = 0; k < 8 && k < font->width - j * 8; k++)
  523.                 {
  524.                     if (b & (0x0001 << k))
  525.                     {
  526.                         ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) color);
  527.                     }
  528.                     else
  529.                     {
  530.                         ST7567_DrawPixel(ST7567.CurrentX + (j * 8) + k, (ST7567.CurrentY + i), (uint8_t) !color);
  531.                     }
  532.                 }
  533.             }
  534.         }
  535.     }

  536.     /* Increase pointer */
  537.     ST7567.CurrentX += font->width;

  538.     /* Return character written */
  539.     return ch;
  540. }

  541. char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color)
  542. {
  543.     /* Write characters */
  544.     while (*str)
  545.     {
  546.         /* Write character by character */
  547.         if (ST7567_Putc(*str, Font, color) != *str)
  548.         {
  549.             /* Return error */
  550.             return *str;
  551.         }
  552.         
  553.         /* Increase string pointer */
  554.         str++;
  555.     }
  556.    
  557.     /* Everything OK, zero should be returned */
  558.     return *str;
  559. }


  560. void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c)
  561. {
  562.     int16_t dx, dy, sx, sy, err, e2, i, tmp;
  563.    
  564.     /* Check for overflow */
  565.     if (x0 >= ST7567_WIDTH)
  566.     {
  567.         x0 = ST7567_WIDTH - 1;
  568.     }
  569.     if (x1 >= ST7567_WIDTH)
  570.     {
  571.         x1 = ST7567_WIDTH - 1;
  572.     }
  573.     if (y0 >= ST7567_HEIGHT)
  574.     {
  575.         y0 = ST7567_HEIGHT - 1;
  576.     }
  577.     if (y1 >= ST7567_HEIGHT)
  578.     {
  579.         y1 = ST7567_HEIGHT - 1;
  580.     }
  581.    
  582.     dx = (x0 < x1) ? (x1 - x0) : (x0 - x1);
  583.     dy = (y0 < y1) ? (y1 - y0) : (y0 - y1);
  584.     sx = (x0 < x1) ? 1 : -1;
  585.     sy = (y0 < y1) ? 1 : -1;
  586.     err = ((dx > dy) ? dx : -dy) / 2;

  587.     if (dx == 0)
  588.     {
  589.         if (y1 < y0)
  590.         {
  591.             tmp = y1;
  592.             y1 = y0;
  593.             y0 = tmp;
  594.         }
  595.         
  596.         if (x1 < x0)
  597.         {
  598.             tmp = x1;
  599.             x1 = x0;
  600.             x0 = tmp;
  601.         }
  602.         
  603.         /* Vertical line */
  604.         for (i = y0; i <= y1; i++)
  605.         {
  606.             ST7567_DrawPixel(x0, i, c);
  607.         }
  608.         
  609.         /* Return from function */
  610.         return;
  611.     }
  612.    
  613.     if (dy == 0)
  614.     {
  615.         if (y1 < y0)
  616.         {
  617.             tmp = y1;
  618.             y1 = y0;
  619.             y0 = tmp;
  620.         }
  621.         
  622.         if (x1 < x0)
  623.         {
  624.             tmp = x1;
  625.             x1 = x0;
  626.             x0 = tmp;
  627.         }
  628.         
  629.         /* Horizontal line */
  630.         for (i = x0; i <= x1; i++)
  631.         {
  632.             ST7567_DrawPixel(i, y0, c);
  633.         }
  634.         
  635.         /* Return from function */
  636.         return;
  637.     }

  638.     while (1)
  639.     {
  640.         ST7567_DrawPixel(x0, y0, c);
  641.         if (x0 == x1 && y0 == y1)
  642.         {
  643.             break;
  644.         }
  645.         e2 = err;
  646.         if (e2 > -dx)
  647.         {
  648.             err -= dy;
  649.             x0 += sx;
  650.         }
  651.         if (e2 < dy)
  652.         {
  653.             err += dx;
  654.             y0 += sy;
  655.         }
  656.     }
  657. }

  658. void ST7567_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c)
  659. {
  660.     /* Check input parameters */
  661.     if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
  662.     {
  663.         /* Return error */
  664.         return;
  665.     }

  666.     /* Check width and height */
  667.     if ((x + w) >= ST7567_WIDTH)
  668.     {
  669.         w = ST7567_WIDTH - x;
  670.     }
  671.     if ((y + h) >= ST7567_HEIGHT)
  672.     {
  673.         h = ST7567_HEIGHT - y;
  674.     }

  675.     /* Draw 4 lines */
  676.     ST7567_DrawLine(x, y, x + w, y, c);         /* Top line */
  677.     ST7567_DrawLine(x, y + h, x + w, y + h, c); /* Bottom line */
  678.     ST7567_DrawLine(x, y, x, y + h, c);         /* Left line */
  679.     ST7567_DrawLine(x + w, y, x + w, y + h, c); /* Right line */
  680. }

  681. void ST7567_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t c)
  682. {
  683.     uint8_t i;
  684.    
  685.     /* Check input parameters */
  686.     if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
  687.     {
  688.         /* Return error */
  689.         return;
  690.     }

  691.     /* Check width and height */
  692.     if ((x + w) >= ST7567_WIDTH)
  693.     {
  694.         w = ST7567_WIDTH - x;
  695.     }
  696.     if ((y + h) >= ST7567_HEIGHT)
  697.     {
  698.         h = ST7567_HEIGHT - y;
  699.     }

  700.     /* Draw lines */
  701.     for (i = 0; i <= h; i++)
  702.     {
  703.         /* Draw lines */
  704.         ST7567_DrawLine(x, y + i, x + w, y + i, c);
  705.     }
  706. }

  707. void ST7567_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color)
  708. {
  709.     /* Draw lines */
  710.     ST7567_DrawLine(x1, y1, x2, y2, color);
  711.     ST7567_DrawLine(x2, y2, x3, y3, color);
  712.     ST7567_DrawLine(x3, y3, x1, y1, color);
  713. }


  714. void ST7567_DrawFilledTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint8_t color)
  715. {
  716.     int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  717.     yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  718.     curpixel = 0;
  719.    
  720.     deltax = ABS(x2 - x1);
  721.     deltay = ABS(y2 - y1);
  722.     x = x1;
  723.     y = y1;

  724.     if (x2 >= x1)
  725.     {
  726.         xinc1 = 1;
  727.         xinc2 = 1;
  728.     }
  729.     else
  730.     {
  731.         xinc1 = -1;
  732.         xinc2 = -1;
  733.     }

  734.     if (y2 >= y1)
  735.     {
  736.         yinc1 = 1;
  737.         yinc2 = 1;
  738.     }
  739.     else
  740.     {
  741.         yinc1 = -1;
  742.         yinc2 = -1;
  743.     }

  744.     if (deltax >= deltay)
  745.     {
  746.         xinc1 = 0;
  747.         yinc2 = 0;
  748.         den = deltax;
  749.         num = deltax / 2;
  750.         numadd = deltay;
  751.         numpixels = deltax;
  752.     }
  753.     else
  754.     {
  755.         xinc2 = 0;
  756.         yinc1 = 0;
  757.         den = deltay;
  758.         num = deltay / 2;
  759.         numadd = deltax;
  760.         numpixels = deltay;
  761.     }

  762.     for (curpixel = 0; curpixel <= numpixels; curpixel++)
  763.     {
  764.         ST7567_DrawLine(x, y, x3, y3, color);

  765.         num += numadd;
  766.         if (num >= den)
  767.         {
  768.             num -= den;
  769.             x += xinc1;
  770.             y += yinc1;
  771.         }
  772.         x += xinc2;
  773.         y += yinc2;
  774.     }
  775. }

  776. void ST7567_DrawCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c)
  777. {
  778.     int16_t f = 1 - r;
  779.     int16_t ddF_x = 1;
  780.     int16_t ddF_y = -2 * r;
  781.     int16_t x = 0;
  782.     int16_t y = r;

  783.     ST7567_DrawPixel(x0, y0 + r, c);
  784.     ST7567_DrawPixel(x0, y0 - r, c);
  785.     ST7567_DrawPixel(x0 + r, y0, c);
  786.     ST7567_DrawPixel(x0 - r, y0, c);

  787.     while (x < y)
  788.     {
  789.         if (f >= 0)
  790.         {
  791.             y--;
  792.             ddF_y += 2;
  793.             f += ddF_y;
  794.         }
  795.         x++;
  796.         ddF_x += 2;
  797.         f += ddF_x;

  798.         ST7567_DrawPixel(x0 + x, y0 + y, c);
  799.         ST7567_DrawPixel(x0 - x, y0 + y, c);
  800.         ST7567_DrawPixel(x0 + x, y0 - y, c);
  801.         ST7567_DrawPixel(x0 - x, y0 - y, c);

  802.         ST7567_DrawPixel(x0 + y, y0 + x, c);
  803.         ST7567_DrawPixel(x0 - y, y0 + x, c);
  804.         ST7567_DrawPixel(x0 + y, y0 - x, c);
  805.         ST7567_DrawPixel(x0 - y, y0 - x, c);
  806.     }
  807. }

  808. void ST7567_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, uint8_t c)
  809. {
  810.     int16_t f = 1 - r;
  811.     int16_t ddF_x = 1;
  812.     int16_t ddF_y = -2 * r;
  813.     int16_t x = 0;
  814.     int16_t y = r;

  815.     ST7567_DrawPixel(x0, y0 + r, c);
  816.     ST7567_DrawPixel(x0, y0 - r, c);
  817.     ST7567_DrawPixel(x0 + r, y0, c);
  818.     ST7567_DrawPixel(x0 - r, y0, c);
  819.     ST7567_DrawLine(x0 - r, y0, x0 + r, y0, c);

  820.     while (x < y)
  821.     {
  822.         if (f >= 0)
  823.         {
  824.             y--;
  825.             ddF_y += 2;
  826.             f += ddF_y;
  827.         }
  828.         x++;
  829.         ddF_x += 2;
  830.         f += ddF_x;

  831.         ST7567_DrawLine(x0 - x, y0 + y, x0 + x, y0 + y, c);
  832.         ST7567_DrawLine(x0 + x, y0 - y, x0 - x, y0 - y, c);

  833.         ST7567_DrawLine(x0 + y, y0 + x, x0 - y, y0 + x, c);
  834.         ST7567_DrawLine(x0 + y, y0 - x, x0 - y, y0 - x, c);
  835.     }
  836. }

  837. void ST7567_Image(uint8_t *img, uint8_t frame, uint8_t x, uint8_t y)
  838. {
  839.     uint32_t i, b, j;

  840.     b = 0;
  841.     if(frame >= img[2])
  842.         return;
  843.     uint32_t start = (frame * (img[3] + (img[4] << 8)));
  844.    
  845.     /* Go through font */
  846.     for (i = 0; i < img[1]; i++) {
  847.         for (j = 0; j < img[0]; j++) {
  848.             ST7567_DrawPixel(x + j, (y + i), (uint8_t) (img[b/8 + 5 + start] >> (b%8)) & 1);
  849.             b++;
  850.         }
  851.     }
  852. }

  853. void ST7567_TestDisplayRAM(void)
  854. {
  855.     uint16_t x, y, pos = 0;
  856.     for (y = 0; y < 2; y++)
  857.     {
  858.         for (x = 0; x < ST7567_WIDTH; x++)
  859.         {
  860.             ST7567_Buffer_all[pos++] = x;
  861.         }
  862.         ST7567_Buffer_all[pos++] = 0x11;
  863.         ST7567_Buffer_all[pos++] = 0x11;
  864.         ST7567_Buffer_all[pos++] = 0x11;
  865.         ST7567_Buffer_all[pos++] = 0x11;
  866.         
  867.     }
  868.     ST7567_Transmit(ST7567_Buffer_all, sizeof(ST7567_Buffer_all), ST7567_TIMEOUT);
  869.                
  870. }

复制代码

void ST7567_WriteData(uint8_t data);

/**
* @brief  Test ST7567 LCD Display RAM
* @param  None
* @retval None
*/
void ST7567_TestDisplayRAM(void);

#endif // __ST7567_H_
  1. ST7567_DrawLine(0, 0, 127, 127, ST7567_COLOR_FRONT);
  2.                 //ST7567_Fill(ENABLE?ST7567_COLOR_BACK:ST7567_COLOR_FRONT);
  3.                 ST7567_UpdateScreen();
复制代码


发表于 2022-11-12 23:09:30 来自手机浏览器 | 显示全部楼层
我记得这样是初始化不对,可能是线定义不对,也有可能是程序故意搞出个BUG。
回复

使用道具 举报

发表于 2022-11-13 00:18:41 来自手机浏览器 | 显示全部楼层
估计是程序问题吧,st75的,我自己改写的基本也不会用六百多行,问卖家要程序吧,要不就自己看文档写,很费劲但是很有成就感。
回复

使用道具 举报

发表于 2022-11-13 00:23:38 来自手机浏览器 | 显示全部楼层
屏幕已经显示了 初始化已经成功  用指令清屏  然后写需要的数据就可以了
回复

使用道具 举报

发表于 2022-11-13 08:38:42 | 显示全部楼层
是否少了两个升压电容?
回复

使用道具 举报

发表于 2022-11-13 09:20:02 | 显示全部楼层
几百行不注释?
回复

使用道具 举报

发表于 2022-11-13 13:11:36 来自手机浏览器 | 显示全部楼层
电路正常。初始化正常检查清屏命令有没有运行到
回复

使用道具 举报

发表于 2022-11-13 15:58:01 来自手机浏览器 | 显示全部楼层
ST7567程序需要这么多么?io模拟spi,不超50行,就能完成初始化和清屏。你这都是啥程序,太复杂了不想看。。。
回复

使用道具 举报

发表于 2022-11-13 16:07:33 | 显示全部楼层
楼主应该分步测试仪,先将初始化及清屏功能完成,剩下的显示内容就容易的解决了。
回复

使用道具 举报

发表于 2022-11-13 16:47:26 | 显示全部楼层
这个屏如果用51单片机驱动就很简单了。
回复

使用道具 举报

发表于 2022-11-13 16:53:42 | 显示全部楼层

每人累计最高8次奖励
回复

使用道具 举报

发表于 2022-11-13 23:27:56 | 显示全部楼层


根据以前出现过相似画面,猜测是取模和显示不一致————当然,也有可能是初始化指令没对。
回复

使用道具 举报

发表于 2022-11-14 10:23:16 | 显示全部楼层
stm的库不知道,avr的简单
回复

使用道具 举报

发表于 2022-11-14 10:35:41 | 显示全部楼层
测试清屏 打点,打点不正常的话就是初始化命令或者write函数有问题,打点正常的话就是字体取模有问题
回复

使用道具 举报

发表于 2022-11-14 11:07:28 | 显示全部楼层
LCD 12864 驱动有问题,谁能看出时啥问题啊
回复

使用道具 举报

发表于 2022-11-14 11:53:52 | 显示全部楼层
赞成4楼的说法,感觉屏已经初始化成功了,后面调用清屏函数ST7567_Fill(),然后写Ram,比如打点或画图形,最后要调用那个更新Ram到LCD的函数 ST7567_UpdateScreen(void)。
回复

使用道具 举报

发表于 2022-11-14 17:05:07 | 显示全部楼层
代码太长,懒得看
回复

使用道具 举报

发表于 2022-11-16 12:45:01 | 显示全部楼层
感觉是工作模式不对吧,清屏完成后,如果是点阵模式(图形模式)与字符模式搞错容易出现这个现象,太久没玩了。
回复

使用道具 举报

发表于 2022-11-17 21:28:40 来自手机浏览器 | 显示全部楼层
一个简简单单的st7567,写成这样,我是服了
回复

使用道具 举报

发表于 2022-11-18 07:51:32 来自手机浏览器 | 显示全部楼层
wzmwhf 发表于 2022-11-17 21:28
一个简简单单的st7567,写成这样,我是服了

你也不看看写了什么功能,别看程序长就一脸鄙视。这段程序能不能工作不说,从编程规范、风格上说,比很多人写的好。
回复

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-30 12:36 , Processed in 0.140401 second(s), 12 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表