OLED_Driver.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 0.96inch RGB OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-06-16
  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. /*******************************************************************************
  34. function:
  35. Hardware reset
  36. *******************************************************************************/
  37. static void OLED_Reset(void)
  38. {
  39. OLED_RST_1;
  40. Driver_Delay_ms(100);
  41. OLED_RST_0;
  42. Driver_Delay_ms(100);
  43. OLED_RST_1;
  44. Driver_Delay_ms(100);
  45. }
  46. /*******************************************************************************
  47. function:
  48. Write register address and data
  49. *******************************************************************************/
  50. static void OLED_WriteReg(uint8_t Reg)
  51. {
  52. #if USE_SPI_4W
  53. OLED_DC_0;
  54. OLED_CS_0;
  55. SPI4W_Write_Byte(Reg);
  56. OLED_CS_1;
  57. #endif
  58. }
  59. static void OLED_WriteData(uint8_t Data)
  60. {
  61. #if USE_SPI_4W
  62. OLED_DC_1;
  63. OLED_CS_0;
  64. SPI4W_Write_Byte(Data);
  65. OLED_CS_1;
  66. #endif
  67. }
  68. /*******************************************************************************
  69. function:
  70. Common register initialization
  71. *******************************************************************************/
  72. static void OLED_InitReg(void)
  73. {
  74. OLED_WriteReg(0xfd); // command lock
  75. OLED_WriteReg(0x12);
  76. OLED_WriteReg(0xae); // display off
  77. // OLED_WriteReg(0xa4); // display off
  78. OLED_WriteReg(0x15); // set column address
  79. OLED_WriteReg(0x20); // column address start 00
  80. OLED_WriteReg(0x5f); // column address end 127
  81. OLED_WriteReg(0x75); // set row address
  82. OLED_WriteReg(0x00); // row address start 00
  83. OLED_WriteReg(0x7f); // row address end 95
  84. OLED_WriteReg(0xa0); // set re-map & data format
  85. OLED_WriteReg(0x60); // Horizontal address increment
  86. OLED_WriteReg(0x00);
  87. OLED_WriteReg(0xa1); // set display start line
  88. OLED_WriteReg(0x00); // start 0 line
  89. OLED_WriteReg(0xa2); // set display offset
  90. OLED_WriteReg(0x00);
  91. OLED_WriteReg(0xB1);
  92. OLED_WriteReg(0x84);
  93. OLED_WriteReg(0xB3);
  94. OLED_WriteReg(0x20);
  95. OLED_WriteReg(0xB6);
  96. OLED_WriteReg(0x01);
  97. OLED_WriteReg(0xBB);
  98. OLED_WriteReg(0x00);
  99. OLED_WriteReg(0xBE);
  100. OLED_WriteReg(0x07);
  101. OLED_WriteReg(0xC7);
  102. OLED_WriteReg(0x0F);
  103. OLED_WriteReg(0xC1);
  104. OLED_WriteReg(0x32);
  105. OLED_WriteReg(0x29);
  106. OLED_WriteReg(0x53);
  107. OLED_WriteReg(0xCA);
  108. OLED_WriteReg(0x7F);
  109. // Driver_Delay_ms(100);
  110. // OLED_WriteReg(0xAF); // turn on oled panel
  111. }
  112. /********************************************************************************
  113. function:
  114. initialization
  115. ********************************************************************************/
  116. void OLED_0in96_rgb_Init(void)
  117. {
  118. //Hardware reset
  119. OLED_Reset();
  120. //Set the initialization register
  121. OLED_InitReg();
  122. Driver_Delay_ms(200);
  123. //Turn on the OLED display
  124. OLED_WriteReg(0xAF);
  125. }
  126. /********************************************************************************
  127. function:
  128. Clear screen
  129. ********************************************************************************/
  130. void OLED_0in96_rgb_Clear(void)
  131. {
  132. UWORD i;
  133. OLED_WriteReg(0x15); // set column address
  134. OLED_WriteData(0x20); // column address start 00
  135. OLED_WriteData(0x5f); // column address end 63
  136. OLED_WriteReg(0x75); // set row address
  137. OLED_WriteData(0x00); // row address start 00
  138. OLED_WriteData(0x7f); // row address end 127
  139. OLED_WriteReg(0x5C);
  140. for(i=0; i<OLED_0in96_rgb_WIDTH*OLED_0in96_rgb_HEIGHT*2; i++){
  141. OLED_WriteData(0x00);
  142. }
  143. }
  144. void OLED_0in96_rgb_Clear_color(UWORD color)
  145. {
  146. UWORD i;
  147. UBYTE temp1,temp2;
  148. temp1 = (color >> 8) & 0xff;
  149. temp2 = color & 0xff;
  150. OLED_WriteReg(0x15); // set column address
  151. OLED_WriteData(0x20); // column address start 00
  152. OLED_WriteData(0x5f); // column address end 63
  153. OLED_WriteReg(0x75); // set row address
  154. OLED_WriteData(0x00); // row address start 00
  155. OLED_WriteData(0x7f); // row address end 127
  156. OLED_WriteReg(0x5C);
  157. for(i=0; i<OLED_0in96_rgb_WIDTH*OLED_0in96_rgb_HEIGHT; i++){
  158. OLED_WriteData(temp1);
  159. OLED_WriteData(temp2);
  160. }
  161. }
  162. /********************************************************************************
  163. function: Draw a point
  164. ********************************************************************************/
  165. void OLED_0in96_rgb_Set_Point(UBYTE Xpoint, UBYTE Ypoint, UWORD Color)
  166. {
  167. OLED_WriteReg(0x15);
  168. OLED_WriteReg(0x20 + Xpoint);
  169. OLED_WriteReg(0x20 + Xpoint);
  170. OLED_WriteReg(0x75);
  171. OLED_WriteReg(Ypoint);
  172. OLED_WriteReg(Ypoint);
  173. OLED_WriteReg(0x5C);
  174. OLED_WriteData(Color>>8 & 0xFF);
  175. OLED_WriteData(Color & 0xFF);
  176. }
  177. /********************************************************************************
  178. function: Update all memory to OLED
  179. ********************************************************************************/
  180. void OLED_0in96_rgb_Display(const UBYTE *Image)
  181. {
  182. UWORD i, j, temp;
  183. OLED_WriteReg(0x15); // set column address
  184. OLED_WriteData(0x20); // column address start 00
  185. OLED_WriteData(0x5f); // column address end 63
  186. OLED_WriteReg(0x75); // set row address
  187. OLED_WriteData(0x00); // row address start 00
  188. OLED_WriteData(0x7f); // row address end 127
  189. OLED_WriteReg(0x5C);
  190. for(i=0; i<OLED_0in96_rgb_HEIGHT; i++)
  191. for(j=0; j<OLED_0in96_rgb_WIDTH*2; j++)
  192. {
  193. temp = pgm_read_byte(&Image[j + i*OLED_0in96_rgb_WIDTH*2]);
  194. OLED_WriteData(temp);
  195. }
  196. }