OLED_Driver.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*****************************************************************************
  2. * | File : OLED_Driver.cpp
  3. * | Author : Waveshare team
  4. * | Function : 1.5inch RGB OLED Module Drive function
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-08-20
  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_Driver.h"
  32. #include "stdio.h"
  33. /*******************************************************************************
  34. function:
  35. Hardware reset
  36. *******************************************************************************/
  37. static void OLED_Reset(void)
  38. {
  39. OLED_RST_1;
  40. Driver_Delay_ms(100);
  41. OLED_RST_0;
  42. Driver_Delay_ms(100);
  43. OLED_RST_1;
  44. Driver_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_4W
  53. OLED_DC_0;
  54. OLED_CS_0;
  55. SPI4W_Write_Byte(Reg);
  56. OLED_CS_1;
  57. #endif
  58. }
  59. static void OLED_WriteData(uint8_t Data)
  60. {
  61. #if USE_SPI_4W
  62. OLED_DC_1;
  63. OLED_CS_0;
  64. SPI4W_Write_Byte(Data);
  65. OLED_CS_1;
  66. #endif
  67. }
  68. /*******************************************************************************
  69. function:
  70. Common register initialization
  71. *******************************************************************************/
  72. static void OLED_InitReg(void)
  73. {
  74. OLED_WriteReg(0xfd); // command lock
  75. OLED_WriteData(0x12);
  76. OLED_WriteReg(0xfd); // command lock
  77. OLED_WriteData(0xB1);
  78. OLED_WriteReg(0xae); // display off
  79. OLED_WriteReg(0xa4); // Normal Display mode
  80. OLED_WriteReg(0x15); //set column address
  81. OLED_WriteData(0x00); //column address start 00
  82. OLED_WriteData(0x7f); //column address end 127
  83. OLED_WriteReg(0x75); //set row address
  84. OLED_WriteData(0x00); //row address start 00
  85. OLED_WriteData(0x7f); //row address end 127
  86. OLED_WriteReg(0xB3);
  87. OLED_WriteData(0xF1);
  88. OLED_WriteReg(0xCA);
  89. OLED_WriteData(0x7F);
  90. OLED_WriteReg(0xa0); //set re-map & data format
  91. OLED_WriteData(0x74); //Horizontal address increment
  92. OLED_WriteReg(0xa1); //set display start line
  93. OLED_WriteData(0x00); //start 00 line
  94. OLED_WriteReg(0xa2); //set display offset
  95. OLED_WriteData(0x00);
  96. OLED_WriteReg(0xAB);
  97. OLED_WriteReg(0x01);
  98. OLED_WriteReg(0xB4);
  99. OLED_WriteData(0xA0);
  100. OLED_WriteData(0xB5);
  101. OLED_WriteData(0x55);
  102. OLED_WriteReg(0xC1);
  103. OLED_WriteData(0xC8);
  104. OLED_WriteData(0x80);
  105. OLED_WriteData(0xC0);
  106. OLED_WriteReg(0xC7);
  107. OLED_WriteData(0x0F);
  108. OLED_WriteReg(0xB1);
  109. OLED_WriteData(0x32);
  110. OLED_WriteReg(0xB2);
  111. OLED_WriteData(0xA4);
  112. OLED_WriteData(0x00);
  113. OLED_WriteData(0x00);
  114. OLED_WriteReg(0xBB);
  115. OLED_WriteData(0x17);
  116. OLED_WriteReg(0xB6);
  117. OLED_WriteData(0x01);
  118. OLED_WriteReg(0xBE);
  119. OLED_WriteData(0x05);
  120. OLED_WriteReg(0xA6);
  121. }
  122. /********************************************************************************
  123. function:
  124. initialization
  125. ********************************************************************************/
  126. void OLED_1in5_rgb_Init(void)
  127. {
  128. //Hardware reset
  129. OLED_Reset();
  130. //Set the initialization register
  131. OLED_InitReg();
  132. Driver_Delay_ms(200);
  133. //Turn on the OLED display
  134. OLED_WriteReg(0xAF);
  135. }
  136. /********************************************************************************
  137. function:
  138. Clear screen
  139. ********************************************************************************/
  140. void OLED_1in5_rgb_Clear(void)
  141. {
  142. UWORD i;
  143. OLED_WriteReg(0x15);
  144. OLED_WriteData(0);
  145. OLED_WriteData(127);
  146. OLED_WriteReg(0x75);
  147. OLED_WriteData(0);
  148. OLED_WriteData(127);
  149. // fill!
  150. OLED_WriteReg(0x5C);
  151. for(i=0; i<OLED_1in5_RGB_WIDTH*OLED_1in5_RGB_HEIGHT*2; i++){
  152. OLED_WriteData(0x00);
  153. }
  154. }
  155. /********************************************************************************
  156. function: Draw a point
  157. ********************************************************************************/
  158. void OLED_1in5_rgb_Set_Point(UBYTE Xpoint, UBYTE Ypoint, UWORD Color)
  159. {
  160. OLED_WriteReg(0x15);
  161. OLED_WriteData(Xpoint);
  162. OLED_WriteData(Xpoint);
  163. OLED_WriteReg(0x75);
  164. OLED_WriteData(Ypoint);
  165. OLED_WriteData(Ypoint);
  166. // fill!
  167. OLED_WriteReg(0x5C);
  168. OLED_WriteData(Color>>8);
  169. OLED_WriteData(Color);
  170. }
  171. /********************************************************************************
  172. function: Update all memory to OLED
  173. ********************************************************************************/
  174. void OLED_1in5_rgb_Display(const UBYTE *Image)
  175. {
  176. UWORD i, j, temp;
  177. OLED_WriteReg(0x15);
  178. OLED_WriteData(0);
  179. OLED_WriteData(127);
  180. OLED_WriteReg(0x75);
  181. OLED_WriteData(0);
  182. OLED_WriteData(127);
  183. // fill!
  184. OLED_WriteReg(0x5C);
  185. for(i=0; i<OLED_1in5_RGB_HEIGHT; i++)
  186. for(j=0; j<OLED_1in5_RGB_WIDTH*2; j++)
  187. {
  188. temp = Image[j + i*256];
  189. OLED_WriteData(temp);
  190. }
  191. }
  192. /********************************************************************************
  193. function: Update all memory to OLED
  194. ********************************************************************************/
  195. void OLED_1in5_rgb_Display_Part(const UBYTE *Image, UBYTE Xstart, UBYTE Ystart, UBYTE Xend, UBYTE Yend)
  196. {
  197. UWORD i, j, temp;
  198. OLED_WriteReg(0x15);
  199. OLED_WriteData(Xstart);
  200. OLED_WriteData(Xend-1);
  201. OLED_WriteReg(0x75);
  202. OLED_WriteData(Ystart);
  203. OLED_WriteData(Yend-1);
  204. // fill!
  205. OLED_WriteReg(0x5C);
  206. for(i=0; i<Yend-Ystart; i++)
  207. for(j=0; j<(Xend-Xstart)*2; j++)
  208. {
  209. temp = pgm_read_byte(&Image[j + i*(Xend-Xstart)*2]);
  210. OLED_WriteData(temp);
  211. }
  212. }