OLED_Driver.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.3inch OLED Module (C) Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-19
  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. /*******************************************************************************
  33. function:
  34. Hardware reset
  35. *******************************************************************************/
  36. static void OLED_Reset(void)
  37. {
  38. OLED_RST_1;
  39. Driver_Delay_ms(100);
  40. OLED_RST_0;
  41. Driver_Delay_ms(100);
  42. OLED_RST_1;
  43. Driver_Delay_ms(100);
  44. }
  45. /*******************************************************************************
  46. function:
  47. Write register address and data
  48. *******************************************************************************/
  49. void OLED_WriteReg(uint8_t Reg)
  50. {
  51. #if USE_SPI_4W
  52. OLED_DC_0;
  53. OLED_CS_0;
  54. SPI4W_Write_Byte(Reg);
  55. OLED_CS_1;
  56. #elif USE_IIC
  57. I2C_Write_Byte(Reg,IIC_CMD);
  58. #endif
  59. }
  60. void OLED_WriteData(uint8_t Data)
  61. {
  62. #if USE_SPI_4W
  63. OLED_DC_1;
  64. OLED_CS_0;
  65. SPI4W_Write_Byte(Data);
  66. OLED_CS_1;
  67. #elif USE_IIC
  68. I2C_Write_Byte(Data,IIC_RAM);
  69. #endif
  70. }
  71. /*******************************************************************************
  72. function:
  73. Common register initialization
  74. *******************************************************************************/
  75. static void OLED_InitReg(void)
  76. {
  77. OLED_WriteReg(0xae); /*turn off OLED display*/
  78. OLED_WriteReg(0x00); /*set lower column address*/
  79. OLED_WriteReg(0x10); /*set higher column address*/
  80. OLED_WriteReg(0xB0); /*set page address*/
  81. OLED_WriteReg(0xdc); /*set display start line*/
  82. OLED_WriteReg(0x00);
  83. OLED_WriteReg(0x81); /*contract control*/
  84. OLED_WriteReg(0x6f); /*128*/
  85. OLED_WriteReg(0x21); /* Set Memory addressing mode (0x20/0x21) */
  86. OLED_WriteReg(0xa0); /*set segment remap*/
  87. OLED_WriteReg(0xc0); /*Com scan direction*/
  88. OLED_WriteReg(0xa4); /*Disable Entire Display On (0xA4/0xA5)*/
  89. OLED_WriteReg(0xa6); /*normal / reverse*/
  90. OLED_WriteReg(0xa8); /*multiplex ratio*/
  91. OLED_WriteReg(0x3f); /*duty = 1/64*/
  92. OLED_WriteReg(0xd3); /*set display offset*/
  93. OLED_WriteReg(0x60);
  94. OLED_WriteReg(0xd5); /*set osc division*/
  95. OLED_WriteReg(0x41);
  96. OLED_WriteReg(0xd9); /*set pre-charge period*/
  97. OLED_WriteReg(0x22);
  98. OLED_WriteReg(0xdb); /*set vcomh*/
  99. OLED_WriteReg(0x35);
  100. OLED_WriteReg(0xad); /*set charge pump enable*/
  101. OLED_WriteReg(0x8a); /*Set DC-DC enable (a=0:disable; a=1:enable) */
  102. }
  103. /********************************************************************************
  104. function:
  105. initialization
  106. ********************************************************************************/
  107. void OLED_Init()
  108. {
  109. //Hardware reset
  110. OLED_Reset();
  111. //Set the initialization register
  112. OLED_InitReg();
  113. Driver_Delay_ms(200);
  114. //Turn on the OLED display
  115. OLED_WriteReg(0xaf);
  116. }
  117. /********************************************************************************
  118. function:
  119. Clear screen
  120. ********************************************************************************/
  121. void OLED_Clear()
  122. {
  123. UWORD Width, Height, column;
  124. Width = (OLED_WIDTH % 8 == 0)? (OLED_WIDTH / 8 ): (OLED_WIDTH / 8 + 1);
  125. Height = OLED_HEIGHT;
  126. OLED_WriteReg(0xb0); //Set the row start address
  127. for (UWORD j = 0; j < Height; j++) {
  128. column = 63 - j;
  129. OLED_WriteReg(0x00 + (column & 0x0f)); //Set column low start address
  130. OLED_WriteReg(0x10 + (column >> 4)); //Set column higt start address
  131. for (UWORD i = 0; i < Width; i++) {
  132. OLED_WriteData(0x00);
  133. }
  134. }
  135. }
  136. /********************************************************************************
  137. function:
  138. Update all memory to OLED
  139. ********************************************************************************/
  140. void OLED_Display(const UBYTE *Image)
  141. {
  142. UWORD Width, Height, column, temp;
  143. Width = (OLED_WIDTH % 8 == 0)? (OLED_WIDTH / 8 ): (OLED_WIDTH / 8 + 1);
  144. Height = OLED_HEIGHT;
  145. OLED_WriteReg(0xb0); //Set the row start address
  146. for (UWORD j = 0; j < Height; j++) {
  147. column = 63 - j;
  148. OLED_WriteReg(0x00 + (column & 0x0f)); //Set column low start address
  149. OLED_WriteReg(0x10 + (column >> 4)); //Set column higt start address
  150. for (UWORD i = 0; i < Width; i++) {
  151. temp = Image[i + j * Width];
  152. temp = reverse(temp); //reverse the buffer
  153. OLED_WriteData(temp);
  154. }
  155. }
  156. }
  157. /********************************************************************************
  158. function:
  159. Update all Array to OLED
  160. ********************************************************************************/
  161. void OLED_Display_Array(const UBYTE *Image)
  162. {
  163. UWORD Width, Height, column, temp;
  164. Width = (OLED_WIDTH % 8 == 0)? (OLED_WIDTH / 8 ): (OLED_WIDTH / 8 + 1);
  165. Height = OLED_HEIGHT;
  166. OLED_WriteReg(0xb0); //Set the row start address
  167. for (UWORD j = 0; j < Height; j++) {
  168. column = 63 - j;
  169. OLED_WriteReg(0x00 + (column & 0x0f)); //Set column low start address
  170. OLED_WriteReg(0x10 + (column >> 4)); //Set column higt start address
  171. for (UWORD i = 0; i < Width; i++) {
  172. temp = pgm_read_byte(&Image[i + j * Width]);
  173. temp = reverse(temp); //reverse the buffer
  174. OLED_WriteData(temp);
  175. }
  176. }
  177. }
  178. /********************************************************************************
  179. function:
  180. reverse a byte
  181. ********************************************************************************/
  182. UBYTE reverse(UBYTE temp)
  183. {
  184. temp = ((temp & 0x55) << 1) | ((temp & 0xaa) >> 1);
  185. temp = ((temp & 0x33) << 2) | ((temp & 0xcc) >> 2);
  186. temp = ((temp & 0x0f) << 4) | ((temp & 0xf0) >> 4);
  187. return temp;
  188. }