OLED_SSD1306_ATY.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @file OLED_SSD1306_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2025 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Base functions of GP22 for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220901 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __OLED_SSD1306_ATY_C
  27. #define __OLED_SSD1306_ATY_C
  28. #include "OLED_SSD1306_ATY.h"
  29. void OLED_WriteData(uint8_t oledData)
  30. {
  31. I2C_Start();
  32. I2C_WriteByte(0x78); //Slave address,SA0=0
  33. I2C_WriteByte(0x40);
  34. I2C_WriteByte(oledData);
  35. I2C_Stop();
  36. }
  37. void OLED_WriteCmd(uint8_t oledCmd)
  38. {
  39. I2C_Start();
  40. I2C_WriteByte(0x78); //Slave address,SA0=0
  41. I2C_WriteByte(0x00);
  42. I2C_WriteByte(oledCmd);
  43. I2C_Stop();
  44. }
  45. void OLED_SetPos(uint8_t x, uint8_t y)
  46. {
  47. OLED_WriteCmd(0xb0 + y);
  48. OLED_WriteCmd(((x & 0xf0) >> 4) | 0x10);
  49. OLED_WriteCmd((x & 0x0f) | 0x01);
  50. }
  51. void OLED_FillScreen(uint8_t fillData)
  52. {
  53. uint8_t y, x;
  54. for(y = 0; y < 8; y++)
  55. {
  56. OLED_WriteCmd(0xb0 + y);
  57. OLED_WriteCmd(0x01);
  58. OLED_WriteCmd(0x10);
  59. for(x = 0; x < X_WIDTH; x++)
  60. OLED_WriteData(fillData);
  61. }
  62. }
  63. void OLED_Init(void)
  64. {
  65. // uint16_t i = 0;
  66. // for(i = 0; i < 5000; i++){}
  67. // DelayNms(50);
  68. // OLED_WriteCmd(0xae); // Turn off oled panel
  69. // OLED_WriteCmd(0x00); // Set low column address
  70. // OLED_WriteCmd(0x10); // Set high column address
  71. // OLED_WriteCmd(0x40); // Set start line address, Set Mapping RAM Display Start Line (0x00~0x3F)
  72. // OLED_WriteCmd(0x81); // Set contrast control register
  73. // OLED_WriteCmd(BRIGHTNESS); // Set SEG Output Current Brightness
  74. // OLED_WriteCmd(0xa1); // Set SEG/Column Mapping, 0xa0 reverse x, 0xa1 normal x
  75. // OLED_WriteCmd(0xc8); // Set COM/Row Scan Direction, 0xc0 reverse y, 0xc8 normal y
  76. // OLED_WriteCmd(0xa6); // Set normal display
  77. // OLED_WriteCmd(0xa8); // Set multiplex ratio(1 to 64)
  78. // OLED_WriteCmd(0x3f); // 1/64 duty
  79. // OLED_WriteCmd(0xd3); // Set display offset Shift Mapping RAM Counter (0x00~0x3F)
  80. // OLED_WriteCmd(0x00); // Not offset
  81. // OLED_WriteCmd(0xd5); // Set display clock divide ratio/oscillator frequency
  82. // OLED_WriteCmd(0x80); // Set divide ratio, Set Clock as 100 Frames/Sec
  83. // OLED_WriteCmd(0xd9); // Set pre-charge period
  84. // OLED_WriteCmd(0xf1); // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  85. // OLED_WriteCmd(0xda); // Set com pins hardware configuration
  86. // OLED_WriteCmd(0x12);
  87. // OLED_WriteCmd(0xdb); // Set vcomh
  88. // OLED_WriteCmd(0x40); // Set VCOM Deselect Level
  89. // OLED_WriteCmd(0x20); // Set Page Addressing Mode (0x00/0x01/0x02)
  90. // OLED_WriteCmd(0x02); //
  91. // OLED_WriteCmd(0x8d); // Set Charge Pump enable/disable
  92. // OLED_WriteCmd(0x14); // Set(0x10) disable
  93. // OLED_WriteCmd(0xa4); // Disable Entire Display On (0xa4/0xa5)
  94. // OLED_WriteCmd(0xa6); // Disable Inverse Display On (0xa6/a7)
  95. // OLED_WriteCmd(0xaf); // Turn on oled panel
  96. // OLED_FillScreen(0x00); // Clear screen
  97. // OLED_SetPos(0, 0); // Reset origin
  98. // 0.91
  99. OLED_WriteCmd(0xAE);
  100. OLED_WriteCmd(0x20);
  101. OLED_WriteCmd(0x00);
  102. OLED_WriteCmd(0xb0);
  103. OLED_WriteCmd(0xc8);
  104. OLED_WriteCmd(0x00);
  105. OLED_WriteCmd(0x10);
  106. OLED_WriteCmd(0x40);
  107. OLED_WriteCmd(0x81);
  108. OLED_WriteCmd(0xdf);
  109. OLED_WriteCmd(0xa1);
  110. OLED_WriteCmd(0xa6);
  111. OLED_WriteCmd(0xa8);
  112. OLED_WriteCmd(0x1F);
  113. OLED_WriteCmd(0xa4);
  114. OLED_WriteCmd(0xd3);
  115. OLED_WriteCmd(0x00);
  116. OLED_WriteCmd(0xd5);
  117. OLED_WriteCmd(0xf0);
  118. OLED_WriteCmd(0xd9);
  119. OLED_WriteCmd(0x22);
  120. OLED_WriteCmd(0xda);
  121. OLED_WriteCmd(0x12);
  122. OLED_WriteCmd(0xdb);
  123. OLED_WriteCmd(0x20);
  124. OLED_WriteCmd(0x8d);
  125. OLED_WriteCmd(0x14);
  126. OLED_WriteCmd(0xDA);
  127. OLED_WriteCmd(0x02);
  128. OLED_WriteCmd(0xaf);
  129. OLED_FillScreen(0x00); // Clear screen
  130. OLED_SetPos(0, 0); // Reset origin
  131. }
  132. void OLED_DrawChar8x16(uint8_t x, uint8_t y, uint8_t symbol[])
  133. {
  134. uint8_t i = 0;
  135. if(x > 120) // If x large than max width, then move to next line begining
  136. {
  137. x = 0;
  138. y += 2;
  139. }
  140. OLED_SetPos(x, y);
  141. for(i = 0; i < 8; i++)
  142. {
  143. OLED_WriteData(symbol[i]);
  144. }
  145. OLED_SetPos(x, y + 1);
  146. for(i = 0; i < 8; i++)
  147. {
  148. OLED_WriteData(symbol[i + 8]);
  149. }
  150. }
  151. void OLED_DrawChar16x16(uint8_t x, uint8_t y, uint8_t symbol[])
  152. {
  153. uint8_t i = 0;
  154. if(x > 120) // If x large than max width, then move to next line begining
  155. {
  156. x = 0;
  157. y += 2;
  158. }
  159. OLED_SetPos(x, y);
  160. for(i = 0; i < 16; i++)
  161. {
  162. OLED_WriteData(symbol[i]);
  163. }
  164. OLED_SetPos(x, y + 1);
  165. for(i = 0; i < 16; i++)
  166. {
  167. OLED_WriteData(symbol[i + 16]);
  168. }
  169. }
  170. // void OLED_DrawString8x16(uint8_t x, uint8_t y, uint8_t* str, uint8_t len, uint8_t* font)
  171. // {
  172. // uint8_t i = 0;
  173. // for(i = 0; i < len; i++)
  174. // {
  175. // if(x > 120)
  176. // {
  177. // x = 0;
  178. // y += 2;
  179. // }
  180. // if(y > 6)
  181. // break;
  182. // OLED_DrawChar8x16(x, y, font + ((str[i] - 32) * 16));
  183. // x += 8;
  184. // }
  185. // }
  186. uint8_t NumToChar(uint8_t num)
  187. {
  188. if(num >= 0 && num <= 9)
  189. return num + 32 + 16;
  190. return num; // Not a number between 0-9
  191. }
  192. void OLED_DisNumber(uint8_t x, uint8_t y, uint8_t disData)
  193. {
  194. OLED_SetPos(x, y);
  195. OLED_DrawChar8x16(x, y, FontSongTi8x16 + ((NumToChar(disData) - 32) * 16));
  196. }
  197. void OLED_DisValue(uint8_t x, uint8_t y, uint8_t* disData, uint8_t disLength)
  198. {
  199. uint8_t i = 0;
  200. for(i = 0; i < disLength; i++)
  201. {
  202. OLED_SetPos(x, y);
  203. OLED_DrawChar8x16(x, y, FontSongTi8x16 + ((NumToChar(disData[i]) - 32) * 16));
  204. x += 8;
  205. }
  206. }
  207. void OLED_Dis_Welcom(void)
  208. {
  209. uint8_t beginX = 28, beginY = 2;
  210. OLED_DrawString8x16(0, 0, "I. Astrol", FontSongTi8x16);
  211. // OLED_DrawString8x16(0, 16, "HT", 2, FontSongTi8x16);
  212. }
  213. // todo: not tested
  214. #endif /* __OLED_SSD1306_ATY_C */