OLED_Driver.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.3inch OLED Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.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. #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(0x02);//---set low column address
  80. OLED_WriteReg(0x10);//---set high column address
  81. OLED_WriteReg(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
  82. OLED_WriteReg(0x81);//--set contrast control register
  83. OLED_WriteReg(0xA0);//--Set SEG/Column Mapping a0/a1
  84. OLED_WriteReg(0xC0);//Set COM/Row Scan Direction
  85. OLED_WriteReg(0xA6);//--set normal display a6/a7
  86. OLED_WriteReg(0xA8);//--set multiplex ratio(1 to 64)
  87. OLED_WriteReg(0x3F);//--1/64 duty
  88. OLED_WriteReg(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
  89. OLED_WriteReg(0x00);//-not offset
  90. OLED_WriteReg(0xd5);//--set display clock divide ratio/oscillator frequency
  91. OLED_WriteReg(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  92. OLED_WriteReg(0xD9);//--set pre-charge period
  93. OLED_WriteReg(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  94. OLED_WriteReg(0xDA);//--set com pins hardware configuration
  95. OLED_WriteReg(0x12);
  96. OLED_WriteReg(0xDB);//--set vcomh
  97. OLED_WriteReg(0x40);//Set VCOM Deselect Level
  98. OLED_WriteReg(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  99. OLED_WriteReg(0x02);//
  100. OLED_WriteReg(0xA4);// Disable Entire Display On (0xa4/0xa5)
  101. OLED_WriteReg(0xA6);// Disable Inverse Display On (0xa6/a7)
  102. }
  103. /********************************************************************************
  104. function:
  105. initialization
  106. ********************************************************************************/
  107. void OLED_1IN3_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_1IN3_Clear()
  122. {
  123. // UWORD Width, Height;
  124. UWORD i, j;
  125. // Width = (OLED_1IN3_WIDTH % 8 == 0)? (OLED_1IN3_WIDTH / 8 ): (OLED_1IN3_WIDTH / 8 + 1);
  126. // Height = OLED_1IN3_HEIGHT;
  127. for (i=0; i<8; i++) {
  128. /* set page address */
  129. OLED_WriteReg(0xB0 + i);
  130. /* set low column address */
  131. OLED_WriteReg(0x02);
  132. /* set high column address */
  133. OLED_WriteReg(0x10);
  134. for(j=0; j<128; j++) {
  135. /* write data */
  136. OLED_WriteData(0x00);
  137. }
  138. }
  139. }
  140. /********************************************************************************
  141. function: Update memory to OLED
  142. ********************************************************************************/
  143. void OLED_1IN3_Display(const UBYTE *Image)
  144. {
  145. UWORD page, column, temp;
  146. for (page=0; page<8; page++) {
  147. /* set page address */
  148. OLED_WriteReg(0xB0 + page);
  149. /* set low column address */
  150. OLED_WriteReg(0x02);
  151. /* set high column address */
  152. OLED_WriteReg(0x10);
  153. /* write data */
  154. for(column=0; column<128; column++) {
  155. temp = Image[(7-page) + column*8];
  156. OLED_WriteData(temp);
  157. }
  158. }
  159. }
  160. /********************************************************************************
  161. function: Update array data to OLED
  162. ********************************************************************************/
  163. void OLED_1IN3_Display_Array(const UBYTE *Image)
  164. {
  165. UWORD page, column, temp;
  166. for (page=0; page<8; page++) {
  167. /* set page address */
  168. OLED_WriteReg(0xB0 + page);
  169. /* set low column address */
  170. OLED_WriteReg(0x02);
  171. /* set high column address */
  172. OLED_WriteReg(0x10);
  173. /* write data */
  174. for(column=0; column<128; column++) {
  175. temp = pgm_read_byte(&Image[(7-page) + column*8]);
  176. OLED_WriteData(temp);
  177. }
  178. }
  179. }