OLED_Driver.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 0.95inch RGB 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. /*******************************************************************************
  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(DISPLAY_OFF); //Display Off
  75. OLED_WriteReg(SET_CONTRAST_A); //Set contrast for color A
  76. OLED_WriteReg(0xFF); //145 0x91
  77. OLED_WriteReg(SET_CONTRAST_B); //Set contrast for color B
  78. OLED_WriteReg(0xFF); //80 0x50
  79. OLED_WriteReg(SET_CONTRAST_C); //Set contrast for color C
  80. OLED_WriteReg(0xFF); //125 0x7D
  81. OLED_WriteReg(MASTER_CURRENT_CONTROL);//master current control
  82. OLED_WriteReg(0x06); //6
  83. OLED_WriteReg(SET_PRECHARGE_SPEED_A);//Set Second Pre-change Speed For ColorA
  84. OLED_WriteReg(0x64); //100
  85. OLED_WriteReg(SET_PRECHARGE_SPEED_B);//Set Second Pre-change Speed For ColorB
  86. OLED_WriteReg(0x78); //120
  87. OLED_WriteReg(SET_PRECHARGE_SPEED_C);//Set Second Pre-change Speed For ColorC
  88. OLED_WriteReg(0x64); //100
  89. OLED_WriteReg(SET_REMAP); //set remap & data format
  90. OLED_WriteReg(0x72); //normal 0x72 180 0x60
  91. OLED_WriteReg(SET_DISPLAY_START_LINE);//Set display Start Line
  92. OLED_WriteReg(0x0);
  93. OLED_WriteReg(SET_DISPLAY_OFFSET); //Set display offset
  94. OLED_WriteReg(0x0);
  95. OLED_WriteReg(NORMAL_DISPLAY); //Set display mode
  96. OLED_WriteReg(SET_MULTIPLEX_RATIO); //Set multiplex ratio
  97. OLED_WriteReg(0x3F);
  98. OLED_WriteReg(SET_MASTER_CONFIGURE); //Set master configuration
  99. OLED_WriteReg(0x8E);
  100. OLED_WriteReg(POWER_SAVE_MODE); //Set Power Save Mode
  101. OLED_WriteReg(0x00); //0x00
  102. OLED_WriteReg(PHASE_PERIOD_ADJUSTMENT);//phase 1 and 2 period adjustment
  103. OLED_WriteReg(0x31); //0x31
  104. OLED_WriteReg(DISPLAY_CLOCK_DIV); //display clock divider/oscillator frequency
  105. OLED_WriteReg(0xF0);
  106. OLED_WriteReg(SET_PRECHARGE_VOLTAGE);//Set Pre-Change Level
  107. OLED_WriteReg(0x3A);
  108. OLED_WriteReg(SET_V_VOLTAGE); //Set vcomH
  109. OLED_WriteReg(0x3E);
  110. OLED_WriteReg(DEACTIVE_SCROLLING); //disable scrolling
  111. }
  112. /********************************************************************************
  113. function:
  114. initialization
  115. ********************************************************************************/
  116. void OLED_0in95_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_0in95_rgb_Clear(void)
  131. {
  132. UWORD i;
  133. OLED_WriteReg(SET_COLUMN_ADDRESS);
  134. OLED_WriteReg(0); //cloumn start address
  135. OLED_WriteReg(OLED_0in95_RGB_WIDTH - 1); //cloumn end address
  136. OLED_WriteReg(SET_ROW_ADDRESS);
  137. OLED_WriteReg(0); //page atart address
  138. OLED_WriteReg(OLED_0in95_RGB_HEIGHT - 1); //page end address
  139. for(i=0; i<OLED_0in95_RGB_WIDTH*OLED_0in95_RGB_HEIGHT*2; i++){
  140. OLED_WriteData(0x00);
  141. }
  142. }
  143. /********************************************************************************
  144. function: Draw a point
  145. ********************************************************************************/
  146. void OLED_0in95_rgb_Set_Point(UBYTE Xpoint, UBYTE Ypoint, UWORD Color)
  147. {
  148. OLED_WriteReg(SET_COLUMN_ADDRESS);
  149. OLED_WriteReg(Xpoint);
  150. OLED_WriteReg(Xpoint);
  151. OLED_WriteReg(SET_ROW_ADDRESS);
  152. OLED_WriteReg(Ypoint);
  153. OLED_WriteReg(Ypoint);
  154. OLED_WriteData(Color>>8);
  155. OLED_WriteData(Color);
  156. }
  157. /********************************************************************************
  158. function: Update all memory to OLED
  159. ********************************************************************************/
  160. void OLED_0in95_rgb_Display(const UBYTE *Image)
  161. {
  162. UWORD i, j, temp;
  163. OLED_WriteReg(SET_COLUMN_ADDRESS);
  164. OLED_WriteReg(0); //cloumn start address
  165. OLED_WriteReg(OLED_0in95_RGB_WIDTH - 1); //cloumn end address
  166. OLED_WriteReg(SET_ROW_ADDRESS);
  167. OLED_WriteReg(0); //page atart address
  168. OLED_WriteReg(OLED_0in95_RGB_HEIGHT - 1); //page end address
  169. for(i=0; i<OLED_0in95_RGB_HEIGHT; i++)
  170. for(j=0; j<OLED_0in95_RGB_WIDTH*2; j++)
  171. {
  172. temp = pgm_read_byte(&Image[j + i*OLED_0in95_RGB_WIDTH*2]);
  173. OLED_WriteData(temp);
  174. }
  175. }