OLED_1in5.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*****************************************************************************
  2. * | File : OLED_1in5.c
  3. * | Author : Waveshare team
  4. * | Function : 1.5inch OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-15
  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_1in5.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(0x15); // set column address
  76. OLED_WriteReg(0x00); // start column 0
  77. OLED_WriteReg(0x7f); // end column 127
  78. OLED_WriteReg(0x75); // set row address
  79. OLED_WriteReg(0x00); // start row 0
  80. OLED_WriteReg(0x7f); // end row 127
  81. OLED_WriteReg(0x81); // set contrast control
  82. OLED_WriteReg(0x80);
  83. OLED_WriteReg(0xa0); // gment remap
  84. OLED_WriteReg(0x51); //51
  85. OLED_WriteReg(0xa1); // start line
  86. OLED_WriteReg(0x00);
  87. OLED_WriteReg(0xa2); // display offset
  88. OLED_WriteReg(0x00);
  89. OLED_WriteReg(0xa4); // rmal display
  90. OLED_WriteReg(0xa8); // set multiplex ratio
  91. OLED_WriteReg(0x7f);
  92. OLED_WriteReg(0xb1); // set phase leghth
  93. OLED_WriteReg(0xf1);
  94. OLED_WriteReg(0xb3); // set dclk
  95. OLED_WriteReg(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
  96. OLED_WriteReg(0xab); //
  97. OLED_WriteReg(0x01); //
  98. OLED_WriteReg(0xb6); // set phase leghth
  99. OLED_WriteReg(0x0f);
  100. OLED_WriteReg(0xbe);
  101. OLED_WriteReg(0x0f);
  102. OLED_WriteReg(0xbc);
  103. OLED_WriteReg(0x08);
  104. OLED_WriteReg(0xd5);
  105. OLED_WriteReg(0x62);
  106. OLED_WriteReg(0xfd);
  107. OLED_WriteReg(0x12);
  108. }
  109. /********************************************************************************
  110. function:
  111. initialization
  112. ********************************************************************************/
  113. void OLED_1in5_Init(void)
  114. {
  115. //Hardware reset
  116. OLED_Reset();
  117. //Set the initialization register
  118. OLED_InitReg();
  119. DEV_Delay_ms(200);
  120. //Turn on the OLED display
  121. OLED_WriteReg(0xAF);
  122. }
  123. /********************************************************************************
  124. function: Set the display Window(Xstart, Ystart, Xend, Yend)
  125. parameter:
  126. xStart : X direction Start coordinates
  127. Ystart : Y direction Start coordinates
  128. Xend : X direction end coordinates
  129. Yend : Y direction end coordinates
  130. ********************************************************************************/
  131. static void OLED_SetWindow(UBYTE Xstart, UBYTE Ystart, UBYTE Xend, UBYTE Yend)
  132. {
  133. if((Xstart > OLED_1in5_WIDTH) || (Ystart > OLED_1in5_HEIGHT) ||
  134. (Xend > OLED_1in5_WIDTH) || (Yend > OLED_1in5_HEIGHT))
  135. return;
  136. OLED_WriteReg(0x15);
  137. OLED_WriteReg(Xstart/2);
  138. OLED_WriteReg(Xend/2 - 1);
  139. OLED_WriteReg(0x75);
  140. OLED_WriteReg(Ystart);
  141. OLED_WriteReg(Yend - 1);
  142. }
  143. /********************************************************************************
  144. function:
  145. Clear screen
  146. ********************************************************************************/
  147. void OLED_1in5_Clear(void)
  148. {
  149. UWORD i;
  150. OLED_SetWindow(0, 0, 128, 128);
  151. for(i=0; i<OLED_1in5_WIDTH*OLED_1in5_HEIGHT/2; i++){
  152. OLED_WriteData(0x00);
  153. }
  154. }
  155. /********************************************************************************
  156. function: Update all memory to OLED
  157. ********************************************************************************/
  158. void OLED_1in5_Display(UBYTE *Image)
  159. {
  160. UWORD i, j, temp;
  161. OLED_SetWindow(0, 0, 128, 128);
  162. for(i=0; i<OLED_1in5_HEIGHT; i++)
  163. for(j=0; j<OLED_1in5_WIDTH/2; j++)
  164. {
  165. temp = Image[j + i*64];
  166. OLED_WriteData(temp);
  167. }
  168. }