OLED_0in49.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*****************************************************************************
  2. * | File : OLED_0in49.c
  3. * | Author : Waveshare team
  4. * | Function : 0.49inch OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-05-31
  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_0in49.h"
  32. #include "stdio.h"
  33. #include "DEV_Config.h"
  34. /*******************************************************************************
  35. function:
  36. Write register address and data
  37. *******************************************************************************/
  38. static void OLED_WriteReg(uint8_t Reg)
  39. {
  40. I2C_Write_Byte(Reg, IIC_CMD);
  41. }
  42. static void OLED_WriteData(uint8_t Data)
  43. {
  44. I2C_Write_Byte(Data, IIC_RAM);
  45. }
  46. /*******************************************************************************
  47. function:
  48. Common register initialization
  49. *******************************************************************************/
  50. static void OLED_InitReg(void)
  51. {
  52. OLED_WriteReg(0xAE); /*display off*/
  53. OLED_WriteReg(0x00); /*set lower column address*/
  54. OLED_WriteReg(0x12); /*set higher column address*/
  55. OLED_WriteReg(0x00); /*set display start line*/
  56. OLED_WriteReg(0xB0); /*set page address*/
  57. OLED_WriteReg(0x81); /*contract control*/
  58. OLED_WriteReg(0x4f); /*128*/
  59. OLED_WriteReg(0xA0); /*set segment remap*/
  60. OLED_WriteReg(0xA6); /*normal / reverse*/
  61. OLED_WriteReg(0xA8); /*multiplex ratio*/
  62. OLED_WriteReg(0x1F); /*duty = 1/32*/
  63. OLED_WriteReg(0xC8); /*Com scan direction*/
  64. OLED_WriteReg(0xD3); /*set display offset*/
  65. OLED_WriteReg(0x00);
  66. OLED_WriteReg(0x20);
  67. OLED_WriteReg(0x01); /*set Vertical Addressing Mode*/
  68. OLED_WriteReg(0xD5); /*set osc division*/
  69. OLED_WriteReg(0x80);
  70. OLED_WriteReg(0xD9); /*set pre-charge period*/
  71. OLED_WriteReg(0xf1);
  72. OLED_WriteReg(0xDA); /*set COM pins*/
  73. OLED_WriteReg(0x12);
  74. OLED_WriteReg(0xdb); /*set vcomh*/
  75. OLED_WriteReg(0x40);
  76. OLED_WriteReg(0x8d); /*set charge pump enable*/
  77. OLED_WriteReg(0x14);
  78. // OLED_WriteReg(0xAF); /*display ON*/
  79. }
  80. /********************************************************************************
  81. function:
  82. initialization
  83. ********************************************************************************/
  84. void OLED_0in49_Init()
  85. {
  86. System_Init();
  87. // Hardware reset
  88. OLED_InitReg();
  89. Driver_Delay_ms(200);
  90. // Turn on the OLED display
  91. OLED_WriteReg(0xaf);
  92. }
  93. /********************************************************************************
  94. function:
  95. Clear screen
  96. ********************************************************************************/
  97. void OLED_0in49_Clear(void)
  98. {
  99. UWORD i;
  100. OLED_WriteReg(0x22); // 设置页面地址
  101. OLED_WriteReg(0x00);
  102. OLED_WriteReg(0x03);
  103. OLED_WriteReg(0x21); // 设置列地址
  104. OLED_WriteReg(0x20);
  105. OLED_WriteReg(0x5f);
  106. for (i = 0; i < 256; i++)
  107. {
  108. OLED_WriteData(0x00);
  109. }
  110. }
  111. /********************************************************************************
  112. function:
  113. reverse a byte data
  114. ********************************************************************************/
  115. static UBYTE reverse(UBYTE temp)
  116. {
  117. temp = ((temp & 0x55) << 1) | ((temp & 0xaa) >> 1);
  118. temp = ((temp & 0x33) << 2) | ((temp & 0xcc) >> 2);
  119. temp = ((temp & 0x0f) << 4) | ((temp & 0xf0) >> 4);
  120. return temp;
  121. }
  122. /********************************************************************************
  123. function:
  124. Update all memory to OLED
  125. ********************************************************************************/
  126. void OLED_0in49_Display(const UBYTE *Image)
  127. {
  128. UBYTE temp;
  129. UWORD i;
  130. OLED_WriteReg(0x22);
  131. OLED_WriteReg(0x00);
  132. OLED_WriteReg(0x03);
  133. OLED_WriteReg(0x21);
  134. OLED_WriteReg(0x20);
  135. OLED_WriteReg(0x5f);
  136. for (i = 0; i < 256; i++)
  137. {
  138. temp = pgm_read_byte(&Image[i]);
  139. temp = reverse(temp); // reverse the buffer
  140. OLED_WriteData(~temp);
  141. }
  142. }
  143. /********************************************************************************
  144. function: Update RAM memory to OLED
  145. ********************************************************************************/
  146. void OLED_0in49_Display_RAM(const UBYTE *Image)
  147. {
  148. UBYTE temp;
  149. UWORD i;
  150. OLED_WriteReg(0x22);
  151. OLED_WriteReg(0x00);
  152. OLED_WriteReg(0x03);
  153. OLED_WriteReg(0x21);
  154. OLED_WriteReg(0x20);
  155. OLED_WriteReg(0x5f);
  156. for (i = 0; i < 256; i++)
  157. {
  158. temp = Image[i];
  159. temp = reverse(temp); // reverse the buffer
  160. OLED_WriteData(temp);
  161. }
  162. }