OLED_0in96.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*****************************************************************************
  2. * | File : OLED_0in96.c
  3. * | Author : Waveshare team
  4. * | Function : OLED_0in96 OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-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_0in96.h"
  32. #include "stdio.h"
  33. #define vccstate SSD1306_SWITCHCAPVCC
  34. /*******************************************************************************
  35. function:
  36. Hardware reset
  37. *******************************************************************************/
  38. static void OLED_Reset(void)
  39. {
  40. OLED_RST_1;
  41. DEV_Delay_ms(100);
  42. OLED_RST_0;
  43. DEV_Delay_ms(100);
  44. OLED_RST_1;
  45. DEV_Delay_ms(100);
  46. }
  47. /*******************************************************************************
  48. function:
  49. Write register address and data
  50. *******************************************************************************/
  51. static void OLED_WriteReg(uint8_t Reg)
  52. {
  53. #if USE_SPI
  54. OLED_DC_0;
  55. DEV_SPI_WriteByte(Reg);
  56. #elif USE_IIC
  57. I2C_Write_Byte(Reg,IIC_CMD);
  58. #endif
  59. }
  60. /*******************************************************************************
  61. function:
  62. Common register initialization
  63. *******************************************************************************/
  64. static void OLED_WriteData(uint8_t Data)
  65. {
  66. #if USE_SPI
  67. OLED_DC_1;
  68. DEV_SPI_WriteByte(Data);
  69. #elif USE_IIC
  70. I2C_Write_Byte(Data,IIC_RAM);
  71. #endif
  72. }
  73. static void OLED_InitReg()
  74. {
  75. OLED_WriteReg(SSD1306_DISPLAYOFF);
  76. OLED_WriteReg(SSD1306_SETDISPLAYCLOCKDIV);
  77. OLED_WriteReg(0x80); // the suggested ratio 0x80
  78. OLED_WriteReg(SSD1306_SETMULTIPLEX);
  79. OLED_WriteReg(0x3F);
  80. OLED_WriteReg(SSD1306_SETDISPLAYOFFSET);
  81. OLED_WriteReg(0x0); // no offset
  82. OLED_WriteReg(SSD1306_SETSTARTLINE | 0x0); // line #0
  83. OLED_WriteReg(SSD1306_CHARGEPUMP);
  84. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
  85. OLED_WriteReg(SSD1306_MEMORYMODE);
  86. OLED_WriteReg(0x00); // 0x0 act like ks0108
  87. OLED_WriteReg(SSD1306_SEGREMAP | 0x1);
  88. OLED_WriteReg(SSD1306_COMSCANDEC);
  89. OLED_WriteReg(SSD1306_SETCOMPINS);
  90. OLED_WriteReg(0x12); // TODO - calculate based on _rawHieght ?
  91. OLED_WriteReg(SSD1306_SETCONTRAST);
  92. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF);
  93. OLED_WriteReg(SSD1306_SETPRECHARGE);
  94. OLED_WriteReg((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
  95. OLED_WriteReg(SSD1306_SETVCOMDETECT);
  96. OLED_WriteReg(0x40);
  97. OLED_WriteReg(SSD1306_DISPLAYALLON_RESUME);
  98. OLED_WriteReg(SSD1306_NORMALDISPLAY);
  99. }
  100. /********************************************************************************
  101. function:
  102. initialization
  103. ********************************************************************************/
  104. void OLED_0in96_Init()
  105. {
  106. //Hardware reset
  107. OLED_Reset();
  108. //Set the initialization register
  109. OLED_InitReg();
  110. DEV_Delay_ms(200);
  111. //Turn on the OLED display
  112. OLED_WriteReg(0xaf);
  113. }
  114. /********************************************************************************
  115. function:
  116. Clear screen
  117. ********************************************************************************/
  118. void OLED_0in96_clear()
  119. {
  120. UWORD j;
  121. OLED_WriteReg(SSD1306_COLUMNADDR);
  122. OLED_WriteReg(0); //cloumn start address
  123. OLED_WriteReg(OLED_0in96_HEIGHT -1); //cloumn end address
  124. OLED_WriteReg(SSD1306_PAGEADDR);
  125. OLED_WriteReg(0); //page atart address
  126. OLED_WriteReg(OLED_0in96_WIDTH/8 -1); //page end address
  127. for (j = 0; j < 1024; j++) {
  128. OLED_WriteData(0x00);
  129. }
  130. }
  131. /********************************************************************************
  132. function:
  133. Update all memory to OLED
  134. ********************************************************************************/
  135. void OLED_0in96_display(UBYTE *Image)
  136. {
  137. UWORD j, i, temp;
  138. OLED_WriteReg(SSD1306_COLUMNADDR);
  139. OLED_WriteReg(0); //cloumn start address
  140. OLED_WriteReg(OLED_0in96_HEIGHT -1); //cloumn end address
  141. OLED_WriteReg(SSD1306_PAGEADDR);
  142. OLED_WriteReg(0); //page atart address
  143. OLED_WriteReg(OLED_0in96_WIDTH/8 -1); //page end address
  144. for (j = 0; j < 8; j++) {
  145. for(i = 0; i < 128; i++) {
  146. temp = Image[7-j + i*8];
  147. OLED_WriteData(temp);
  148. }
  149. }
  150. }