OLED_0in49.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*****************************************************************************
  2. * | File : OLED_0in49.c
  3. * | Author : Waveshare team
  4. * | Function : 0.49inch OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-17
  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. /*******************************************************************************
  34. function:
  35. Hardware reset
  36. *******************************************************************************/
  37. static void OLED_Reset(void)
  38. {
  39. OLED_RST_1;
  40. DEV_Delay_ms(100);
  41. OLED_RST_0;
  42. DEV_Delay_ms(100);
  43. OLED_RST_1;
  44. DEV_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); /*display off*/
  69. OLED_WriteReg(0x00); /*set lower column address*/
  70. OLED_WriteReg(0x12); /*set higher column address*/
  71. OLED_WriteReg(0x00); /*set display start line*/
  72. OLED_WriteReg(0xB0); /*set page address*/
  73. OLED_WriteReg(0x81); /*contract control*/
  74. OLED_WriteReg(0x4f); /*128*/
  75. OLED_WriteReg(0xA0); /*set segment remap*/
  76. OLED_WriteReg(0xA6); /*normal / reverse*/
  77. OLED_WriteReg(0xA8); /*multiplex ratio*/
  78. OLED_WriteReg(0x1F); /*duty = 1/32*/
  79. OLED_WriteReg(0xC8); /*Com scan direction*/
  80. OLED_WriteReg(0xD3); /*set display offset*/
  81. OLED_WriteReg(0x00);
  82. OLED_WriteReg(0x20);
  83. OLED_WriteReg(0x01); /*set Vertical Addressing Mode*/
  84. OLED_WriteReg(0xD5); /*set osc division*/
  85. OLED_WriteReg(0x80);
  86. OLED_WriteReg(0xD9); /*set pre-charge period*/
  87. OLED_WriteReg(0xf1);
  88. OLED_WriteReg(0xDA); /*set COM pins*/
  89. OLED_WriteReg(0x12);
  90. OLED_WriteReg(0xdb); /*set vcomh*/
  91. OLED_WriteReg(0x40);
  92. OLED_WriteReg(0x8d); /*set charge pump enable*/
  93. OLED_WriteReg(0x14);
  94. OLED_WriteReg(0xAF); /*display ON*/
  95. }
  96. /********************************************************************************
  97. function:
  98. initialization
  99. ********************************************************************************/
  100. void OLED_0in49_Init()
  101. {
  102. //Hardware reset
  103. OLED_Reset();
  104. //Set the initialization register
  105. OLED_InitReg();
  106. DEV_Delay_ms(200);
  107. //Turn on the OLED display
  108. OLED_WriteReg(0xaf);
  109. }
  110. /********************************************************************************
  111. function:
  112. Clear screen
  113. ********************************************************************************/
  114. void OLED_0in49_Clear(void)
  115. {
  116. UWORD i;
  117. OLED_WriteReg(0x22); // 设置页面地址
  118. OLED_WriteReg(0x00);
  119. OLED_WriteReg(0x03);
  120. OLED_WriteReg(0x21); // 设置列地址
  121. OLED_WriteReg(0x20);
  122. OLED_WriteReg(0x5f);
  123. for (i = 0; i < 256; i++)
  124. {
  125. OLED_WriteData(0XFF);
  126. }
  127. }
  128. /********************************************************************************
  129. function:
  130. reverse a byte data
  131. ********************************************************************************/
  132. static UBYTE reverse(UBYTE temp)
  133. {
  134. temp = ((temp & 0x55) << 1) | ((temp & 0xaa) >> 1);
  135. temp = ((temp & 0x33) << 2) | ((temp & 0xcc) >> 2);
  136. temp = ((temp & 0x0f) << 4) | ((temp & 0xf0) >> 4);
  137. return temp;
  138. }
  139. /********************************************************************************
  140. function:
  141. Update all memory to OLED
  142. ********************************************************************************/
  143. void OLED_0in49_Display(const UBYTE *Image)
  144. {
  145. UBYTE temp;
  146. UWORD i;
  147. OLED_WriteReg(0x22);
  148. OLED_WriteReg(0x00);
  149. OLED_WriteReg(0x03);
  150. OLED_WriteReg(0x21);
  151. OLED_WriteReg(0x20);
  152. OLED_WriteReg(0x5f);
  153. for (i = 0; i < 256; i++)
  154. {
  155. temp = Image[i];
  156. temp = reverse(temp); // reverse the buffer
  157. OLED_WriteData(temp);
  158. }
  159. }