OLED_1in54.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*****************************************************************************
  2. * | File : OLED_1in54.c
  3. * | Author : Waveshare team
  4. * | Function : 1.54inch OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2022-07-14
  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_1in54.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_SPI
  53. OLED_DC_0;
  54. DEV_SPI_WriteByte(Reg);
  55. #elif USE_IIC
  56. I2C_Write_Byte(Reg,IIC_CMD);
  57. #endif
  58. }
  59. static void OLED_WriteData(uint8_t Data)
  60. {
  61. #if USE_SPI
  62. OLED_DC_1;
  63. DEV_SPI_WriteByte(Data);
  64. #elif USE_IIC
  65. I2C_Write_Byte(Data,IIC_RAM);
  66. #endif
  67. }
  68. /*******************************************************************************
  69. function:
  70. Common register initialization
  71. *******************************************************************************/
  72. static void OLED_InitReg(void)
  73. {
  74. OLED_WriteReg(0xAE);//--turn off oled panel
  75. OLED_WriteReg(0x00);//---set low column address
  76. OLED_WriteReg(0x10);//---set high column address
  77. OLED_WriteReg(0x20);
  78. OLED_WriteReg(0x00);
  79. OLED_WriteReg(0xFF);
  80. OLED_WriteReg(0xA6);
  81. OLED_WriteReg(0xA8);
  82. OLED_WriteReg(0x3F);
  83. OLED_WriteReg(0xD3);
  84. OLED_WriteReg(0x00);
  85. OLED_WriteReg(0xD5);
  86. OLED_WriteReg(0x80);
  87. OLED_WriteReg(0xD9);
  88. OLED_WriteReg(0x22);
  89. OLED_WriteReg(0xDA);
  90. OLED_WriteReg(0x12);
  91. OLED_WriteReg(0xDB);
  92. OLED_WriteReg(0x40);
  93. }
  94. /********************************************************************************
  95. function:
  96. initialization
  97. ********************************************************************************/
  98. void OLED_1in54_Init(void)
  99. {
  100. //Hardware reset
  101. OLED_Reset();
  102. //Set the initialization register
  103. OLED_InitReg();
  104. DEV_Delay_ms(200);
  105. //Turn on the OLED display
  106. OLED_WriteReg(0xAF);
  107. }
  108. /********************************************************************************
  109. function:
  110. Clear screen
  111. ********************************************************************************/
  112. void OLED_1in54_Clear(void)
  113. {
  114. // UWORD Width, Height;
  115. UWORD i, j;
  116. // Width = (OLED_1IN3_WIDTH % 8 == 0)? (OLED_1IN3_WIDTH / 8 ): (OLED_1IN3_WIDTH / 8 + 1);
  117. // Height = OLED_1IN3_HEIGHT;
  118. for (i=0; i<8; i++) {
  119. /* set page address */
  120. OLED_WriteReg(0xB0 + i);
  121. /* set low column address */
  122. OLED_WriteReg(0x00);
  123. /* set high column address */
  124. OLED_WriteReg(0x10);
  125. for(j=0; j<128; j++) {
  126. /* write data */
  127. OLED_WriteData(0x00);
  128. }
  129. }
  130. }
  131. /********************************************************************************
  132. function: Update all memory to OLED
  133. ********************************************************************************/
  134. void OLED_1in54_Display(UBYTE *Image)
  135. {
  136. UWORD page, column, temp;
  137. for (page=0; page<8; page++) {
  138. /* set page address */
  139. OLED_WriteReg(0xB0 + page);
  140. /* set low column address */
  141. OLED_WriteReg(0x00);
  142. /* set high column address */
  143. OLED_WriteReg(0x10);
  144. /* write data */
  145. for(column=0; column<128; column++) {
  146. temp = Image[(7-page) + column*8];
  147. OLED_WriteData(temp);
  148. }
  149. }
  150. }