ssd1306.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include "ssd1306.h"
  15. /* Absolute value */
  16. #define ABS(x) ((x) > 0 ? (x) : -(x))
  17. /* SSD1306 data buffer */
  18. static __XDATA uint8_t SSD1306_Buffer_all[SSD1306_WIDTH * SSD1306_HEIGHT / 8];
  19. /* Private SSD1306 structure */
  20. typedef struct {
  21. uint16_t CurrentX;
  22. uint16_t CurrentY;
  23. uint8_t Inverted;
  24. uint8_t Initialized;
  25. } SSD1306_t;
  26. /* Private variable */
  27. static SSD1306_t SSD1306;
  28. void SSD1306_WriteCommand(uint8_t command)
  29. {
  30. I2C_Write(SSD1306_I2C_ADDR, 0x00, &command, 1);
  31. }
  32. void SSD1306_WriteData(uint8_t dat)
  33. {
  34. I2C_Write(SSD1306_I2C_ADDR, 0x40, &dat, 1);
  35. }
  36. void SSD1306_Init(void)
  37. {
  38. SYS_Delay(100);
  39. /* Init LCD */
  40. SSD1306_WriteCommand(0xAE); //display off
  41. /**
  42. * Set the lower start column address of pointer by command 00h~0Fh.
  43. * Set the upper start column address of pointer by command 10h~1Fh.
  44. */
  45. SSD1306_WriteCommand(0x00); //---set low column address
  46. SSD1306_WriteCommand(0x10); //---set high column address
  47. /** set contrast control register, 2 bytes, 0x00 - 0xFF */
  48. SSD1306_WriteCommand(0x81);
  49. SSD1306_WriteCommand(0x7F);
  50. /** 0xA4,Output follows RAM content
  51. * 0xA5,Output ignores RAM content */
  52. SSD1306_WriteCommand(0xA4);
  53. /** 0xA6, Normal display (RESET)
  54. * 0xA7, Inverse display */
  55. SSD1306_WriteCommand(0xA6);
  56. /* 0x20,Set Memory Addressing Mode, 2 bytes,
  57. * 0x00,Horizontal Addressing Mode (slide horizontally and goto next page)
  58. * 0x01,Vertical Addressing Mode (slide vertically and goto next column)
  59. * 0x02,Page Addressing Mode (RESET) (slide horizontally and remain in the same page)
  60. * 0x03,Invalid
  61. */
  62. SSD1306_WriteCommand(0x20);
  63. SSD1306_WriteCommand(0x00);
  64. /**
  65. * Set the page start address of the target display location by command B0h to B7h
  66. * For Page Addressing Mode only
  67. */
  68. SSD1306_WriteCommand(0xB0);
  69. /**
  70. * Set Page Address, 3 bytes
  71. * For Horizontal and Vertical Addressing Mode only
  72. */
  73. SSD1306_WriteCommand(0x22);
  74. SSD1306_WriteCommand(0x00); // From Page 0
  75. SSD1306_WriteCommand(0x07); // To Page 7
  76. /**
  77. * COM Output Scan Direction
  78. * 0xC0: normal mode (RESET) Scan from COM0 to COM[N –1]
  79. * 0xC8: remapped mode. Scan from COM[N-1] to COM0 */
  80. SSD1306_WriteCommand(0xC8); //Set COM Output Scan Direction
  81. /**
  82. * Set display RAM display start line register from 0-63 */
  83. SSD1306_WriteCommand(0x40);
  84. /**
  85. * Segment Re-map
  86. * 0xA0: column address 0 is mapped to SEG0 (RESET),
  87. * 0xA1: column address 127 is mapped to SEG0 */
  88. SSD1306_WriteCommand(0xA1);
  89. /**
  90. * Set MUX ratio to N+1 MUX
  91. * N=A[5:0]: from 16MUX to 64MUX, RESET=111111b (i.e. 63d, 64MUX)
  92. * A[5:0] from 0 to 14 are invalid entry.*/
  93. SSD1306_WriteCommand(0xA8);
  94. SSD1306_WriteCommand(0x3F);
  95. /**
  96. * Set Display Offset, Set vertical shift by COM from 0d~63d
  97. * The value is reset to 00h after RESET */
  98. SSD1306_WriteCommand(0xD3);
  99. SSD1306_WriteCommand(0x00); // offset in vertical
  100. /**
  101. * Set COM Pins Hardware Configuration
  102. * A[4]=0b, Sequential COM pin configuration
  103. * A[4]=1b(RESET), Alternative COM pin configuration
  104. * A[5]=0b(RESET), Disable COM Left/Right remap
  105. * A[5]=1b, Enable COM Left/Right remap */
  106. SSD1306_WriteCommand(0xDA);
  107. SSD1306_WriteCommand(0x12); // A[4]=0, A[5]=1
  108. /**
  109. * Set Display Divide Ratio/Oscillator Frequency
  110. * */
  111. SSD1306_WriteCommand(0xD5);
  112. SSD1306_WriteCommand(0xF0); // divide ratio
  113. /**
  114. * Set Pre-charge Period */
  115. SSD1306_WriteCommand(0xD9);
  116. SSD1306_WriteCommand(0x22);
  117. /**
  118. * Set V COMH Deselect Level
  119. * 0x00: 0.65 * Vcc
  120. * 0x10: 0.77 * Vcc (RESET)
  121. * 0x11: 0.83 * Vcc
  122. * */
  123. SSD1306_WriteCommand(0xDB);
  124. SSD1306_WriteCommand(0x10);
  125. /** charge pump setting
  126. * 0x10: Disable charge pump(RESET)
  127. * 0x14: Enable charge pump during display on
  128. */
  129. SSD1306_WriteCommand(0x8D);
  130. SSD1306_WriteCommand(0x14);
  131. /** 0xAE, Display OFF (sleep mode),
  132. * 0xAF, Display ON in normal mode */
  133. SSD1306_WriteCommand(0xAF);
  134. /* Clear screen */
  135. SSD1306_Fill(SSD1306_COLOR_BLACK);
  136. /* Update screen */
  137. SSD1306_UpdateScreen();
  138. /* Set default values */
  139. SSD1306.CurrentX = 0;
  140. SSD1306.CurrentY = 0;
  141. /* Initialized OK */
  142. SSD1306.Initialized = 1;
  143. }
  144. void SSD1306_UpdateScreen(void)
  145. {
  146. I2C_Write(SSD1306_I2C_ADDR, 0x40, SSD1306_Buffer_all, SSD1306_WIDTH * SSD1306_HEIGHT / 8);
  147. }
  148. void SSD1306_ToggleInvert(void)
  149. {
  150. uint16_t i;
  151. /* Toggle invert */
  152. SSD1306.Inverted = !SSD1306.Inverted;
  153. /* Do memory toggle */
  154. for (i = 0; i < sizeof(SSD1306_Buffer_all); i++)
  155. {
  156. SSD1306_Buffer_all[i] = ~SSD1306_Buffer_all[i];
  157. }
  158. }
  159. void SSD1306_Fill(uint8_t color)
  160. {
  161. if (SSD1306.Inverted)
  162. {
  163. color = (uint8_t)!color;
  164. }
  165. /* Set memory */
  166. memset(SSD1306_Buffer_all, (color == SSD1306_COLOR_BLACK) ? 0x00 : 0xFF, SSD1306_WIDTH * SSD1306_HEIGHT / 8);
  167. }
  168. void SSD1306_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
  169. {
  170. if (x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT)
  171. {
  172. /* Error */
  173. return;
  174. }
  175. /* Check if pixels are inverted */
  176. if (SSD1306.Inverted)
  177. {
  178. color = (uint8_t)!color;
  179. }
  180. /* Set color */
  181. if (color == SSD1306_COLOR_WHITE)
  182. {
  183. SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8);
  184. }
  185. else
  186. {
  187. SSD1306_Buffer_all[x + (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8));
  188. }
  189. }
  190. void SSD1306_GotoXY(uint16_t x, uint16_t y)
  191. {
  192. /* Set write pointers */
  193. SSD1306.CurrentX = x;
  194. SSD1306.CurrentY = y;
  195. }
  196. void SSD1306_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t c)
  197. {
  198. int16_t dx, dy, sx, sy, err, e2, i, tmp;
  199. /* Check for overflow */
  200. if (x0 >= SSD1306_WIDTH)
  201. {
  202. x0 = SSD1306_WIDTH - 1;
  203. }
  204. if (x1 >= SSD1306_WIDTH)
  205. {
  206. x1 = SSD1306_WIDTH - 1;
  207. }
  208. if (y0 >= SSD1306_HEIGHT)
  209. {
  210. y0 = SSD1306_HEIGHT - 1;
  211. }
  212. if (y1 >= SSD1306_HEIGHT)
  213. {
  214. y1 = SSD1306_HEIGHT - 1;
  215. }
  216. dx = (x0 < x1) ? (x1 - x0) : (x0 - x1);
  217. dy = (y0 < y1) ? (y1 - y0) : (y0 - y1);
  218. sx = (x0 < x1) ? 1 : -1;
  219. sy = (y0 < y1) ? 1 : -1;
  220. err = ((dx > dy) ? dx : -dy) / 2;
  221. if (dx == 0)
  222. {
  223. if (y1 < y0)
  224. {
  225. tmp = y1;
  226. y1 = y0;
  227. y0 = tmp;
  228. }
  229. if (x1 < x0)
  230. {
  231. tmp = x1;
  232. x1 = x0;
  233. x0 = tmp;
  234. }
  235. /* Vertical line */
  236. for (i = y0; i <= y1; i++)
  237. {
  238. SSD1306_DrawPixel(x0, i, c);
  239. }
  240. /* Return from function */
  241. return;
  242. }
  243. if (dy == 0)
  244. {
  245. if (y1 < y0)
  246. {
  247. tmp = y1;
  248. y1 = y0;
  249. y0 = tmp;
  250. }
  251. if (x1 < x0)
  252. {
  253. tmp = x1;
  254. x1 = x0;
  255. x0 = tmp;
  256. }
  257. /* Horizontal line */
  258. for (i = x0; i <= x1; i++)
  259. {
  260. SSD1306_DrawPixel(i, y0, c);
  261. }
  262. /* Return from function */
  263. return;
  264. }
  265. while (1)
  266. {
  267. SSD1306_DrawPixel(x0, y0, c);
  268. if (x0 == x1 && y0 == y1)
  269. {
  270. break;
  271. }
  272. e2 = err;
  273. if (e2 > -dx)
  274. {
  275. err -= dy;
  276. x0 += sx;
  277. }
  278. if (e2 < dy)
  279. {
  280. err += dx;
  281. y0 += sy;
  282. }
  283. }
  284. }
  285. void SSD1306_ON(void)
  286. {
  287. SSD1306_WriteCommand(0x8D);
  288. SSD1306_WriteCommand(0x14);
  289. SSD1306_WriteCommand(0xAF);
  290. }
  291. void SSD1306_OFF(void)
  292. {
  293. SSD1306_WriteCommand(0x8D);
  294. SSD1306_WriteCommand(0x10);
  295. SSD1306_WriteCommand(0xAE);
  296. }