OLED_1in3.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*****************************************************************************
  2. * | File : OLED_1in3.c
  3. * | Author : Waveshare team
  4. * | Function : 1.3inch OLED Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-08-13
  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_1in3.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. // OLED_CS_0;
  55. DEV_SPI_WriteByte(Reg);
  56. // printf("WriteReg\r\n");
  57. // OLED_CS_1;
  58. #elif USE_IIC
  59. I2C_Write_Byte(Reg,IIC_CMD);
  60. #endif
  61. }
  62. static void OLED_WriteData(uint8_t Data)
  63. {
  64. #if USE_SPI
  65. OLED_DC_1;
  66. // OLED_CS_0;
  67. DEV_SPI_WriteByte(Data);
  68. // printf("WriteData\r\n");
  69. // OLED_CS_1;
  70. #elif USE_IIC
  71. I2C_Write_Byte(Data,IIC_RAM);
  72. #endif
  73. }
  74. /*******************************************************************************
  75. function:
  76. Common register initialization
  77. *******************************************************************************/
  78. static void OLED_InitReg(void)
  79. {
  80. OLED_WriteReg(0xAE);//--turn off oled panel
  81. OLED_WriteReg(0x02);//---set low column address
  82. OLED_WriteReg(0x10);//---set high column address
  83. OLED_WriteReg(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
  84. OLED_WriteReg(0x81);//--set contrast control register
  85. OLED_WriteReg(0xA0);//--Set SEG/Column Mapping a0/a1
  86. OLED_WriteReg(0xC0);//Set COM/Row Scan Direction
  87. OLED_WriteReg(0xA6);//--set normal display a6/a7
  88. OLED_WriteReg(0xA8);//--set multiplex ratio(1 to 64)
  89. OLED_WriteReg(0x3F);//--1/64 duty
  90. OLED_WriteReg(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
  91. OLED_WriteReg(0x00);//-not offset
  92. OLED_WriteReg(0xd5);//--set display clock divide ratio/oscillator frequency
  93. OLED_WriteReg(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  94. OLED_WriteReg(0xD9);//--set pre-charge period
  95. OLED_WriteReg(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  96. OLED_WriteReg(0xDA);//--set com pins hardware configuration
  97. OLED_WriteReg(0x12);
  98. OLED_WriteReg(0xDB);//--set vcomh
  99. OLED_WriteReg(0x40);//Set VCOM Deselect Level
  100. OLED_WriteReg(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  101. OLED_WriteReg(0x02);//
  102. OLED_WriteReg(0xA4);// Disable Entire Display On (0xa4/0xa5)
  103. OLED_WriteReg(0xA6);// Disable Inverse Display On (0xa6/a7)
  104. }
  105. /********************************************************************************
  106. function:
  107. initialization
  108. ********************************************************************************/
  109. void OLED_1IN3_Init()
  110. {
  111. //Hardware reset
  112. OLED_Reset();
  113. //Set the initialization register
  114. OLED_InitReg();
  115. DEV_Delay_ms(200);
  116. //Turn on the OLED display
  117. OLED_WriteReg(0xaf);
  118. }
  119. /********************************************************************************
  120. function:
  121. Clear screen
  122. ********************************************************************************/
  123. void OLED_1IN3_Clear()
  124. {
  125. // UWORD Width, Height;
  126. UWORD i, j;
  127. // Width = (OLED_1IN3_WIDTH % 8 == 0)? (OLED_1IN3_WIDTH / 8 ): (OLED_1IN3_WIDTH / 8 + 1);
  128. // Height = OLED_1IN3_HEIGHT;
  129. for (i=0; i<8; i++) {
  130. /* set page address */
  131. OLED_WriteReg(0xB0 + i);
  132. /* set low column address */
  133. OLED_WriteReg(0x02);
  134. /* set high column address */
  135. OLED_WriteReg(0x10);
  136. for(j=0; j<128; j++) {
  137. /* write data */
  138. OLED_WriteData(0x00);
  139. }
  140. }
  141. }
  142. /********************************************************************************
  143. function: Update memory to OLED
  144. ********************************************************************************/
  145. void OLED_1IN3_Display(UBYTE *Image)
  146. {
  147. UWORD page, column, temp;
  148. for (page=0; page<8; page++) {
  149. /* set page address */
  150. OLED_WriteReg(0xB0 + page);
  151. /* set low column address */
  152. OLED_WriteReg(0x02);
  153. /* set high column address */
  154. OLED_WriteReg(0x10);
  155. /* write data */
  156. for(column=0; column<128; column++) {
  157. temp = Image[(7-page) + column*8];
  158. OLED_WriteData(temp);
  159. }
  160. }
  161. }