OLED_Driver.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.32inch OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-03-03
  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. #elif USE_IIC
  58. I2C_Write_Byte(Reg,IIC_CMD);
  59. #endif
  60. }
  61. static void OLED_WriteData(uint8_t Data)
  62. {
  63. #if USE_SPI_4W
  64. OLED_DC_1;
  65. OLED_CS_0;
  66. SPI4W_Write_Byte(Data);
  67. OLED_CS_1;
  68. #elif USE_IIC
  69. I2C_Write_Byte(Data,IIC_RAM);
  70. #endif
  71. }
  72. /*******************************************************************************
  73. function:
  74. Common register initialization
  75. *******************************************************************************/
  76. static void OLED_InitReg(void)
  77. {
  78. OLED_WriteReg(0xae); // turn off oled panel
  79. OLED_WriteReg(0xa0); // set re-map
  80. OLED_WriteReg(0x51);
  81. OLED_WriteReg(0xa1); // set display start line
  82. OLED_WriteReg(0x00);
  83. OLED_WriteReg(0xa2); // set display offset
  84. OLED_WriteReg(0x20);
  85. OLED_WriteReg(0xa4); // normal display
  86. OLED_WriteReg(0xa8); // set multiplex ratio
  87. OLED_WriteReg(0x5f);
  88. OLED_WriteReg(0xab); // function selection A
  89. OLED_WriteReg(0x01); // enable internal VDD regulator
  90. OLED_WriteReg(0x81); // set contrast
  91. OLED_WriteReg(0x77);
  92. OLED_WriteReg(0xb1); // set phase length
  93. OLED_WriteReg(0x31);
  94. OLED_WriteReg(0xb3); // set front clock divider/oscillator frequency
  95. OLED_WriteReg(0xb1);
  96. OLED_WriteReg(0xb4); // For brightness enhancement
  97. OLED_WriteReg(0xb5);
  98. OLED_WriteReg(0xb6); // Set Second pre-charge Period
  99. OLED_WriteReg(0X0d);
  100. OLED_WriteReg(0xbc); // Set Pre-charge voltage
  101. OLED_WriteReg(0x07);
  102. OLED_WriteReg(0xbe); // set vcomh
  103. OLED_WriteReg(0x07);
  104. OLED_WriteReg(0xd5); // Function Selection B
  105. OLED_WriteReg(0x02); // Enable second pre-charge
  106. }
  107. /********************************************************************************
  108. function:
  109. initialization
  110. ********************************************************************************/
  111. void OLED_1in32_Init(void)
  112. {
  113. //Hardware reset
  114. OLED_Reset();
  115. //Set the initialization register
  116. OLED_InitReg();
  117. Driver_Delay_ms(200);
  118. //Turn on the OLED display
  119. OLED_WriteReg(0xAF);
  120. }
  121. /********************************************************************************
  122. function: Set the display Window(Xstart, Ystart, Xend, Yend)
  123. parameter:
  124. xStart : X direction Start coordinates
  125. Ystart : Y direction Start coordinates
  126. Xend : X direction end coordinates
  127. Yend : Y direction end coordinates
  128. ********************************************************************************/
  129. static void OLED_SetWindow(UBYTE Xstart, UBYTE Ystart, UBYTE Xend, UBYTE Yend)
  130. {
  131. if((Xstart > OLED_1in32_WIDTH) || (Ystart > OLED_1in32_HEIGHT) ||
  132. (Xend > OLED_1in32_WIDTH) || (Yend > OLED_1in32_HEIGHT))
  133. return;
  134. OLED_WriteReg(0x15);
  135. OLED_WriteReg(Xstart/2);
  136. OLED_WriteReg(Xend/2 - 1);
  137. OLED_WriteReg(0x75);
  138. OLED_WriteReg(Ystart);
  139. OLED_WriteReg(Yend - 1);
  140. }
  141. /********************************************************************************
  142. function:
  143. Clear screen
  144. ********************************************************************************/
  145. void OLED_1in32_Clear(UBYTE color)
  146. {
  147. UWORD i;
  148. OLED_SetWindow(0, 0, 128, 96);
  149. for(i=0; i<OLED_1in32_WIDTH*OLED_1in32_HEIGHT/2; i++){
  150. OLED_WriteData(color | (color << 2) | (color << 4) | (color << 6));
  151. }
  152. }
  153. /********************************************************************************
  154. function: Update all memory to OLED
  155. ********************************************************************************/
  156. void OLED_1in32_Display(const UBYTE *Image)
  157. {
  158. UWORD i, j, temp;
  159. OLED_SetWindow(0, 0, 128, 96);
  160. for(i=0; i<OLED_1in32_HEIGHT; i++)
  161. for(j=0; j<OLED_1in32_WIDTH/2; j++)
  162. {
  163. temp = pgm_read_byte(&Image[j + i*64]);
  164. OLED_WriteData(temp);
  165. }
  166. }
  167. /********************************************************************************
  168. function: Update all memory to OLED
  169. ********************************************************************************/
  170. void OLED_1in32_Display_Part(const UBYTE *Image, UBYTE Xstart, UBYTE Ystart, UBYTE Xend, UBYTE Yend)
  171. {
  172. UWORD i, j, temp;
  173. OLED_SetWindow(Xstart, Ystart, Xend, Yend);
  174. for(i=0; i<Yend-Ystart; i++)
  175. for(j=0; j<(Xend-Xstart)/2; j++)
  176. {
  177. temp = Image[j + i*(Xend-Xstart)/2];
  178. OLED_WriteData(temp);
  179. }
  180. }