st7567.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef __FW_ST7567__
  15. #define __FW_ST7567__
  16. #include "fw_hal.h"
  17. /**
  18. * ST7565, ST7567
  19. *
  20. * Display Data RAM has a matrix of 65 by 132 bits. The address ranges are:
  21. * X=0~131(83H) (column address), Y=0~8 (page address)
  22. *
  23. */
  24. #define ST7567_MODEL ST7567_MODEL_ST7567
  25. #define ST7567_CS P35
  26. #define ST7567_MOSI P34
  27. #define ST7567_SCK P32
  28. #define ST7567_RES P37
  29. #define ST7567_DC P36
  30. #define ST7567_BL P12
  31. // X width
  32. #define ST7567_WIDTH 128
  33. // Y height
  34. #define ST7567_HEIGHT 64
  35. // Display RAM Columns
  36. #define ST7567_COLUMN_EXPAND 4
  37. // Display RAM Pages (8x8bit + 1bit)
  38. #define ST7567_PAGES 8
  39. // X orientation
  40. #define ST7567_X_ORIENT ST7567_SEG_DIRECTION_NORMAL
  41. // Y orientation
  42. #define ST7567_Y_ORIENT ST7567_COM_DIRECTION_NORMAL
  43. #define ST7567_MODEL_ST7565 0
  44. #define ST7567_MODEL_ST7567 1
  45. /* ST7567 commands definitions */
  46. #define ST7567_DISPLAY_OFF 0xAE /* 0xae: Display OFF (sleep mode), default */
  47. #define ST7567_DISPLAY_ON 0xAF /* 0xaf: Display ON in normal mode */
  48. #define ST7567_SET_START_LINE 0x40 /* 0x40-7f: Set display start line */
  49. #define ST7567_SET_START_LINE_MASK 0x3f
  50. #define ST7567_SET_PAGE_ADDRESS 0xB0 /* 0xb0-b7: Set page start address, 0 - 8 */
  51. #define ST7567_SET_PAGE_ADDRESS_MASK 0x07
  52. #define ST7567_SET_COLUMN_ADDRESS_MSB 0x10 /* 0x10-0x1f: Set 8bit column address - high 4 bits of 0 - 131 */
  53. #define ST7567_SET_COLUMN_ADDRESS_MSB_MASK 0x0F
  54. #define ST7567_SET_COLUMN_ADDRESS_LSB 0x00 /* 0x00-0x0f: Set 8bit column address - low 4 bits of 0 - 131 */
  55. #define ST7567_SET_COLUMN_ADDRESS_LSB_MASK 0x0F
  56. #define ST7567_SEG_DIRECTION_NORMAL 0xA0 /* 0xa0: Column address 0 is mapped to SEG0, default*/
  57. #define ST7567_SEG_DIRECTION_REVERSE 0xA1 /* 0xa1: Column address 83H(131) is mapped to SEG0 */
  58. #define ST7567_COM_DIRECTION_NORMAL 0xC0 /* 0xc0: Set COM output direction, normal mode */
  59. #define ST7567_COM_DIRECTION_REVERSE 0xC8 /* 0xc8: Set COM output direction, reverse mode */
  60. #define ST7567_INVERSE_DISPLAY_OFF 0xA6 /* 0xa6: Normal display */
  61. #define ST7567_INVERSE_DISPLAY_ON 0xA7 /* 0xa7: Inverse display */
  62. #define ST7567_ALL_PIXEL_ON 0xA5 /* 0xa5: Entire display ON */
  63. #define ST7567_ALL_PIXEL_NORMAL 0xA4 /* 0xa4: Resume to RAM content display */
  64. /* LCD bias set */
  65. #define ST7567_BIAS_1_9 0xA2 /* 0xa2: Select BIAS setting 1/9 */
  66. #define ST7567_BIAS_1_7 0xA3 /* 0xa3: Select BIAS setting 1/7 */
  67. #define ST7567_READ_MODIFY_WRITE_START 0xE0 /* 0xe0: Enter the Read Modify Write mode */
  68. #define ST7567_READ_MODIFY_WRITE_END 0xEE /* 0xee: Leave the Read Modify Write mode */
  69. #define ST7567_RESET 0xE2 /* 0xe2: Software RESET */
  70. /**
  71. * This instruction controls the built-in power circuits.
  72. * Typically, these 3 flags are turned ON at the same time.
  73. */
  74. #define ST7567_POWER_CONTROL 0x28
  75. #define ST7567_POWER_CONTROL_VF 0x01
  76. #define ST7567_POWER_CONTROL_VR 0x02
  77. #define ST7567_POWER_CONTROL_VB 0x04
  78. /**
  79. * The operation voltage (V0) calculation formula is shown below:
  80. * (RR comes from Regulation Ratio, EV comes from EV[5:0])
  81. * V0 = RR X [ 1 – (63 – EV) / 162 ] X 2.1, or
  82. * V0 = RR X [ ( 99 + EV ) / 162 ] X 2.1
  83. */
  84. #define ST7567_REGULATION_RATIO 0x20
  85. #define ST7567_REGULATION_RATIO_3_0 0x00
  86. #define ST7567_REGULATION_RATIO_3_5 0x01
  87. #define ST7567_REGULATION_RATIO_4_0 0x02
  88. #define ST7567_REGULATION_RATIO_4_5 0x03
  89. #define ST7567_REGULATION_RATIO_5_0 0x04 /* Default */
  90. #define ST7567_REGULATION_RATIO_5_5 0x05
  91. #define ST7567_REGULATION_RATIO_6_0 0x06
  92. #define ST7567_REGULATION_RATIO_6_5 0x07
  93. /**
  94. * This is double byte instruction. The first byte set ST7567 into EV
  95. * adjust mode and the following instruction will change the EV setting.
  96. * That means these 2 bytes must be used together. They control the electronic
  97. * volume to adjust a suitable V0 voltage for the LCD.
  98. */
  99. #define ST7567_SET_EV 0x81
  100. #define ST7567_SET_EV_MASK 0x3F
  101. #if (ST7567_MODEL == ST7567_MODEL_ST7565)
  102. #define ST7567_MODE_SLEEP 0xAC
  103. #define ST7567_MODE_NORMAL 0xAD
  104. #endif
  105. #define ST7567_SET_BOOSTER 0xF8 /* Set booster level */
  106. #define ST7567_SET_BOOSTER_4X 0x00
  107. #define ST7567_SET_BOOSTER_5X 0x01
  108. #define ST7567_SET_BOOSTER_6X 0x11
  109. #define ST7567_NOP 0xE3
  110. #define ST7567_TEST 0xFE
  111. #ifndef ST7567_TIMEOUT
  112. #define ST7567_TIMEOUT 20000
  113. #endif
  114. /** Front color */
  115. #define ST7567_COLOR_FRONT 1U
  116. /** Background color */
  117. #define ST7567_COLOR_BACK 0U
  118. typedef struct {
  119. uint8_t width;
  120. uint8_t height;
  121. uint8_t order;
  122. uint8_t bytes;
  123. const uint8_t *dat;
  124. } FontDef_t;
  125. extern __CODE FontDef_t Font_3x5;
  126. extern __CODE FontDef_t Font_5x7;
  127. /**
  128. * @brief Write a single byte data to ST7567
  129. * @param dat: data
  130. * @retval None
  131. */
  132. void ST7567_WriteData(uint8_t dat);
  133. void ST7567_WriteSameData(uint8_t dat, uint32_t size);
  134. /**
  135. * @brief Write a single byte command to ST7567
  136. * @param command: command
  137. * @retval None
  138. */
  139. void ST7567_WriteCommand(uint8_t command);
  140. /**
  141. * @brief Hardware reset ST7567 LCD
  142. * @param None
  143. * @retval None
  144. */
  145. void ST7567_Reset(void);
  146. /**
  147. * @brief Initializes ST7567 LCD
  148. * @param None
  149. * @retval None
  150. */
  151. void ST7567_Init(void);
  152. /**
  153. * @brief Powersave mode control
  154. * @param state HAL_State_ON:powersave mode, HAL_State_OFF:work mode
  155. * @retval None
  156. */
  157. void ST7567_SetPowerSaveMode(HAL_State_t state);
  158. /**
  159. * @brief Turn ST7567 LCD backlight on or off
  160. * @param state HAL_State_ON:on, HAL_State_OFF:off
  161. * @retval None
  162. */
  163. void ST7567_SetBackLightState(HAL_State_t state);
  164. /**
  165. * @brief Turn ST7567 LCD backlight off
  166. * @param val: value between 0x00 ~ 0x3F
  167. * @retval None
  168. */
  169. void ST7567_SetContrast(uint8_t val);
  170. /**
  171. * @brief Update LCD display with buffer changes
  172. * @note Call this function each time when display is changed
  173. * @param None
  174. * @retval None
  175. */
  176. void ST7567_UpdateScreen(void);
  177. /**
  178. * @brief Toggles pixels invertion inside internal RAM
  179. * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  180. * @param None
  181. * @retval None
  182. */
  183. void ST7567_ToggleInvert(void);
  184. /**
  185. * @brief Fills entire LCD with specified color
  186. * @note @ref ST7567_UpdateScreen() must be called after that in order to see updates
  187. * @param Color: Color to be used for screen fill, ST7567_COLOR_FRONT or ST7567_COLOR_BACK
  188. * @retval None
  189. */
  190. void ST7567_Fill(uint8_t Color);
  191. /**
  192. * @brief Draws pixel at desired location
  193. * @note @ref ST7567_UpdateScreen() must called after that in order to see updates
  194. * @param x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
  195. * @param y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
  196. * @param color: Color to be used for screen fill. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  197. * @retval None
  198. */
  199. void ST7567_DrawPixel(uint8_t x, uint8_t y, uint8_t color);
  200. /**
  201. * @brief Sets cursor pointer to desired location for strings
  202. * @param x: X location. This parameter can be a value between 0 and ST7567_WIDTH - 1
  203. * @param y: Y location. This parameter can be a value between 0 and ST7567_HEIGHT - 1
  204. * @retval None
  205. */
  206. void ST7567_GotoXY(uint16_t x, uint16_t y);
  207. /**
  208. * @brief Puts character to internal RAM
  209. * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  210. * @param ch: Character to be written
  211. * @param *Font: Pointer to @ref FontDef_t structure with used font
  212. * @param color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  213. * @retval Character written
  214. */
  215. char ST7567_Putc(char ch, FontDef_t* Font, uint8_t color);
  216. /**
  217. * @brief Puts string to internal RAM
  218. * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  219. * @param *str: String to be written
  220. * @param *Font: Pointer to @ref FontDef_t structure with used font
  221. * @param color: Color used for drawing. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  222. * @retval Zero on success or character value when function failed
  223. */
  224. char ST7567_Puts(char* str, FontDef_t* Font, uint8_t color);
  225. /**
  226. * @brief Draws line on LCD
  227. * @note @ref ST7567_UpdateScreen() must be called after that in order to see updated LCD screen
  228. * @param x0: Line X start point. Valid input is 0 to ST7567_WIDTH - 1
  229. * @param y0: Line Y start point. Valid input is 0 to ST7567_HEIGHT - 1
  230. * @param x1: Line X end point. Valid input is 0 to ST7567_WIDTH - 1
  231. * @param y1: Line Y end point. Valid input is 0 to ST7567_HEIGHT - 1
  232. * @param c: Color to be used. This parameter can be a value of @ref ST7567_COLOR_t enumeration
  233. * @retval None
  234. */
  235. void ST7567_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c);
  236. #endif // __ST7567_H_