OLED_Driver.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. * | File : OLED_Driver.c
  3. * | Author : Waveshare team
  4. * | Function : OLED_0in96 OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-20
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "OLED_Driver.h"
  32. #include "stdio.h"
  33. #define vccstate SSD1306_SWITCHCAPVCC
  34. /*******************************************************************************
  35. function:
  36. Hardware reset
  37. *******************************************************************************/
  38. static void OLED_Reset(void)
  39. {
  40. OLED_RST_1;
  41. Driver_Delay_ms(100);
  42. OLED_RST_0;
  43. Driver_Delay_ms(100);
  44. OLED_RST_1;
  45. Driver_Delay_ms(100);
  46. }
  47. /*******************************************************************************
  48. function:
  49. Write register address and data
  50. *******************************************************************************/
  51. static void OLED_WriteReg(uint8_t Reg)
  52. {
  53. #if USE_SPI_4W
  54. OLED_DC_0;
  55. OLED_CS_0;
  56. SPI4W_Write_Byte(Reg);
  57. OLED_CS_1;
  58. #elif USE_IIC
  59. I2C_Write_Byte(Reg,IIC_CMD);
  60. #endif
  61. }
  62. /*******************************************************************************
  63. function:
  64. Common register initialization
  65. *******************************************************************************/
  66. static void OLED_WriteData(uint8_t Data)
  67. {
  68. #if USE_SPI_4W
  69. OLED_DC_1;
  70. OLED_CS_0;
  71. SPI4W_Write_Byte(Data);
  72. OLED_CS_1;
  73. #elif USE_IIC
  74. I2C_Write_Byte(Data,IIC_RAM);
  75. #endif
  76. }
  77. static void OLED_InitReg()
  78. {
  79. OLED_WriteReg(SSD1306_DISPLAYOFF);
  80. OLED_WriteReg(SSD1306_SETDISPLAYCLOCKDIV);
  81. OLED_WriteReg(0x80); // the suggested ratio 0x80
  82. OLED_WriteReg(SSD1306_SETMULTIPLEX);
  83. OLED_WriteReg(0x3F);
  84. OLED_WriteReg(SSD1306_SETDISPLAYOFFSET);
  85. OLED_WriteReg(0x0); // no offset
  86. OLED_WriteReg(SSD1306_SETSTARTLINE | 0x0); // line #0
  87. OLED_WriteReg(SSD1306_CHARGEPUMP);
  88. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
  89. OLED_WriteReg(SSD1306_MEMORYMODE);
  90. OLED_WriteReg(0x00); // 0x0 act like ks0108
  91. OLED_WriteReg(SSD1306_SEGREMAP | 0x1);
  92. OLED_WriteReg(SSD1306_COMSCANDEC);
  93. OLED_WriteReg(SSD1306_SETCOMPINS);
  94. OLED_WriteReg(0x12); // TODO - calculate based on _rawHieght ?
  95. OLED_WriteReg(SSD1306_SETCONTRAST);
  96. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF);
  97. OLED_WriteReg(SSD1306_SETPRECHARGE);
  98. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
  99. OLED_WriteReg(SSD1306_SETVCOMDETECT);
  100. OLED_WriteReg(0x40);
  101. OLED_WriteReg(SSD1306_DISPLAYALLON_RESUME);
  102. OLED_WriteReg(SSD1306_NORMALDISPLAY);
  103. }
  104. /********************************************************************************
  105. function:
  106. initialization
  107. ********************************************************************************/
  108. void OLED_0in96_Init()
  109. {
  110. //Hardware reset
  111. OLED_Reset();
  112. //Set the initialization register
  113. OLED_InitReg();
  114. Driver_Delay_ms(200);
  115. //Turn on the OLED display
  116. OLED_WriteReg(0xaf);
  117. }
  118. /********************************************************************************
  119. function:
  120. Clear screen
  121. ********************************************************************************/
  122. void OLED_0in96_clear()
  123. {
  124. UWORD j;
  125. OLED_WriteReg(SSD1306_COLUMNADDR);
  126. OLED_WriteReg(0); //cloumn start address
  127. OLED_WriteReg(OLED_0in96_HEIGHT -1); //cloumn end address
  128. OLED_WriteReg(SSD1306_PAGEADDR);
  129. OLED_WriteReg(0); //page atart address
  130. OLED_WriteReg(OLED_0in96_WIDTH/8 -1); //page end address
  131. for (j = 0; j < 1024; j++) {
  132. OLED_WriteData(0x00);
  133. }
  134. }
  135. /********************************************************************************
  136. function:
  137. Update all memory to OLED
  138. ********************************************************************************/
  139. void OLED_0in96_display(const UBYTE *Image)
  140. {
  141. UWORD j, i, temp;
  142. OLED_WriteReg(SSD1306_COLUMNADDR);
  143. OLED_WriteReg(0); //cloumn start address
  144. OLED_WriteReg(OLED_0in96_HEIGHT -1); //cloumn end address
  145. OLED_WriteReg(SSD1306_PAGEADDR);
  146. OLED_WriteReg(0); //page atart address
  147. OLED_WriteReg(OLED_0in96_WIDTH/8 -1); //page end address
  148. for (j = 0; j < 8; j++) {
  149. for(i = 0; i < 128; i++) {
  150. temp = Image[7-j + i*8];
  151. OLED_WriteData(temp);
  152. }
  153. }
  154. }
  155. /********************************************************************************
  156. function:
  157. Update all array data to OLED
  158. ********************************************************************************/
  159. void OLED_0in96_display_Array(const UBYTE *Image)
  160. {
  161. UWORD j, i, temp;
  162. OLED_WriteReg(SSD1306_COLUMNADDR);
  163. OLED_WriteReg(0); //cloumn start address
  164. OLED_WriteReg(OLED_0in96_HEIGHT -1); //cloumn end address
  165. OLED_WriteReg(SSD1306_PAGEADDR);
  166. OLED_WriteReg(0); //page atart address
  167. OLED_WriteReg(OLED_0in96_WIDTH/8 -1); //page end address
  168. for (j = 0; j < 8; j++) {
  169. for(i = 0; i < 128; i++) {
  170. temp = pgm_read_byte(&Image[7-j + i*8]);
  171. OLED_WriteData(temp);
  172. }
  173. }
  174. }