OLED_Driver.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*****************************************************************************
  2. * | File : OLED_Driver.c
  3. * | Author : Waveshare team
  4. * | Function : 0.91inch 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_IIC
  53. I2C_Write_Byte(Reg,IIC_CMD);
  54. #endif
  55. }
  56. static void OLED_WriteData(uint8_t Data)
  57. {
  58. #if USE_IIC
  59. I2C_Write_Byte(Data,IIC_RAM);
  60. #endif
  61. }
  62. /*******************************************************************************
  63. function:
  64. Common register initialization
  65. *******************************************************************************/
  66. static void OLED_InitReg(void)
  67. {
  68. OLED_WriteReg(0xAE);
  69. OLED_WriteReg(0x40);//---set low column address
  70. OLED_WriteReg(0xB0);//---set high column address
  71. OLED_WriteReg(0xC8);//-not offset
  72. OLED_WriteReg(0x81);
  73. OLED_WriteReg(0xff);
  74. OLED_WriteReg(0xa1);
  75. OLED_WriteReg(0xa6);
  76. OLED_WriteReg(0xa8);
  77. OLED_WriteReg(0x1f);
  78. OLED_WriteReg(0xd3);
  79. OLED_WriteReg(0x00);
  80. OLED_WriteReg(0xd5);
  81. OLED_WriteReg(0xf0);
  82. OLED_WriteReg(0xd9);
  83. OLED_WriteReg(0x22);
  84. OLED_WriteReg(0xda);
  85. OLED_WriteReg(0x02);
  86. OLED_WriteReg(0xdb);
  87. OLED_WriteReg(0x49);
  88. OLED_WriteReg(0x8d);
  89. OLED_WriteReg(0x14);
  90. }
  91. /********************************************************************************
  92. function:
  93. initialization
  94. ********************************************************************************/
  95. void OLED_0in91_Init()
  96. {
  97. //Hardware reset
  98. OLED_Reset();
  99. //Set the initialization register
  100. OLED_InitReg();
  101. Driver_Delay_ms(200);
  102. //Turn on the OLED display
  103. OLED_WriteReg(0xaf);
  104. }
  105. /********************************************************************************
  106. function:
  107. Clear screen
  108. ********************************************************************************/
  109. void OLED_0in91_Clear()
  110. {
  111. UBYTE Column,Page;
  112. for(Page = 0; Page < OLED_0in91_HEIGHT/8; Page++) {
  113. OLED_WriteReg(0xb0 + Page); //Set page address
  114. OLED_WriteReg(0x00); //Set display position - column low address
  115. OLED_WriteReg(0x10); //Set display position - column high address
  116. for(Column = 0; Column < OLED_0in91_WIDTH; Column++)
  117. OLED_WriteData(0x00);
  118. }
  119. }
  120. /********************************************************************************
  121. function:
  122. Update all memory to OLED
  123. ********************************************************************************/
  124. void OLED_0in91_Display(const UBYTE *Image)
  125. {
  126. UBYTE Column,Page;
  127. UWORD temp;
  128. for(Page = 0; Page < OLED_0in91_HEIGHT/8; Page++) {
  129. OLED_WriteReg(0xb0 + Page);
  130. OLED_WriteReg(0x00);
  131. OLED_WriteReg(0x10);
  132. for(Column = 0; Column < OLED_0in91_WIDTH; Column++) {
  133. temp = Image[(3-Page) + Column*4];
  134. OLED_WriteData(temp);
  135. }
  136. }
  137. }
  138. /********************************************************************************
  139. function:
  140. Update all memory to OLED
  141. ********************************************************************************/
  142. void OLED_0in91_Display_Array(const UBYTE *Image)
  143. {
  144. UBYTE Column,Page;
  145. UWORD temp;
  146. for(Page = 0; Page < OLED_0in91_HEIGHT/8; Page++) {
  147. OLED_WriteReg(0xb0 + Page);
  148. OLED_WriteReg(0x00);
  149. OLED_WriteReg(0x10);
  150. for(Column = 0; Column < OLED_0in91_WIDTH; Column++) {
  151. temp = pgm_read_byte(&Image[(3-Page) + Column*4]);
  152. OLED_WriteData(temp);
  153. }
  154. }
  155. }