ST7735S.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /****************************************Copyright (c)**************************************************
  2. * File Name: ST7735S.c
  3. * Function Describe: LCD Driver of ST7735S
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2016-1-8
  7. ********************************************************************************************************/
  8. //-------------------------------------------------------
  9. #include "includes.h"
  10. //-------------------------------------------------------
  11. #define LCD_CS_PIN GPIO_Pin_12
  12. #define LCD_CS_PORT GPIOB
  13. #define LCD_CS_HIGH LCD_CS_PORT->BSRR = LCD_CS_PIN
  14. #define LCD_CS_LOW LCD_CS_PORT->BRR = LCD_CS_PIN
  15. #define LCD_SCK_PIN GPIO_Pin_13
  16. #define LCD_SCK_PORT GPIOB
  17. #define LCD_SCK_HIGH LCD_SCK_PORT->BSRR = LCD_SCK_PIN
  18. #define LCD_SCK_LOW LCD_SCK_PORT->BRR = LCD_SCK_PIN
  19. #define LCD_DCX_PIN GPIO_Pin_14
  20. #define LCD_DCX_PORT GPIOB
  21. #define LCD_DCX_HIGH LCD_DCX_PORT->BSRR = LCD_DCX_PIN
  22. #define LCD_DCX_LOW LCD_DCX_PORT->BRR = LCD_DCX_PIN
  23. #define LCD_SDA_PIN GPIO_Pin_15
  24. #define LCD_SDA_PORT GPIOB
  25. #define LCD_SDA_HIGH LCD_SDA_PORT->BSRR = LCD_SDA_PIN
  26. #define LCD_SDA_LOW LCD_SDA_PORT->BRR = LCD_SDA_PIN
  27. #define LCD_RESET_PIN GPIO_Pin_6
  28. #define LCD_RESET_PORT GPIOC
  29. #define LCD_RESET_HIGH LCD_RESET_PORT->BSRR = LCD_RESET_PIN
  30. #define LCD_RESET_LOW LCD_RESET_PORT->BRR = LCD_RESET_PIN
  31. #define LCD_BL_PIN GPIO_Pin_7
  32. #define LCD_BL_PORT GPIOC
  33. #define LCD_BL_HIGH LCD_BL_PORT->BSRR = LCD_BL_PIN
  34. #define LCD_BL_LOW LCD_BL_PORT->BRR = LCD_BL_PIN
  35. /*******************************************************************************
  36. ST7735PortInit
  37. *******************************************************************************/
  38. static void ST7735PortInit(void)
  39. {
  40. GPIO_InitTypeDef GPIO_InitStructure;
  41. /* Enable GPIOD and GPIOE clocks */
  42. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  45. LCD_CS_HIGH;
  46. LCD_BL_HIGH;
  47. GPIO_InitStructure.GPIO_Pin = (LCD_CS_PIN|LCD_SCK_PIN|LCD_DCX_PIN|LCD_SDA_PIN);
  48. GPIO_Init(GPIOB, &GPIO_InitStructure);
  49. LCD_CS_HIGH;
  50. LCD_SCK_HIGH;
  51. LCD_SDA_HIGH;
  52. LCD_DCX_HIGH;
  53. LCD_RESET_HIGH;
  54. LCD_BL_HIGH;
  55. GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN);
  56. GPIO_Init(GPIOC, &GPIO_InitStructure);
  57. LCD_RESET_HIGH;
  58. LCD_BL_HIGH;
  59. }
  60. /*******************************************************************************
  61. ST7735SendCmd
  62. *******************************************************************************/
  63. void ST7735SendCmd(unsigned char Cmd)
  64. {
  65. unsigned char i,j;
  66. LCD_CS_LOW;
  67. LCD_DCX_LOW;
  68. for(i=0;i<8;i++){
  69. j=0x80>>i;
  70. LCD_SCK_LOW;
  71. if(Cmd&j)LCD_SDA_HIGH;
  72. else LCD_SDA_LOW;
  73. LCD_SCK_HIGH;
  74. }
  75. LCD_CS_HIGH;
  76. }
  77. /*******************************************************************************
  78. ST7735SendData
  79. *******************************************************************************/
  80. void ST7735SendData(unsigned char Data)
  81. {
  82. unsigned char i,j;
  83. LCD_CS_LOW;
  84. LCD_DCX_HIGH;
  85. for(i=0;i<8;i++){
  86. j=0x80>>i;
  87. LCD_SCK_LOW;
  88. if(Data & j)LCD_SDA_HIGH;
  89. else LCD_SDA_LOW;
  90. LCD_SCK_HIGH;
  91. }
  92. LCD_CS_HIGH;
  93. }
  94. void ST7735Init(void)
  95. {
  96. LcdPortInit();
  97. //reset
  98. LCD_RESET_HIGH;
  99. DelayMs(1);
  100. LCD_RESET_LOW;
  101. DelayMs(1);
  102. LCD_RESET_HIGH;
  103. DelayMs(120);
  104. //-----------------------------------ST7735S Set REG---------------------------------------------//
  105. ST7735SendCmd(0x11); //Sleep out
  106. DelayMs(120); //Delay 120ms
  107. //------------------------------------ST7735S Frame Rate-----------------------------------------//
  108. ST7735SendCmd(0xB1);
  109. ST7735SendData(0x05);
  110. ST7735SendData(0x3C);
  111. ST7735SendData(0x3C);
  112. ST7735SendCmd(0xB2);
  113. ST7735SendData(0x05);
  114. ST7735SendData(0x3C);
  115. ST7735SendData(0x3C);
  116. ST7735SendCmd(0xB3);
  117. ST7735SendData(0x05);
  118. ST7735SendData(0x3C);
  119. ST7735SendData(0x3C);
  120. ST7735SendData(0x05);
  121. ST7735SendData(0x3C);
  122. ST7735SendData(0x3C);
  123. //------------------------------------End ST7735S Frame Rate-----------------------------------------//
  124. ST7735SendCmd(0xB4); //Dot inversion
  125. ST7735SendData(0x03);
  126. //------------------------------------ST7735S Power Sequence-----------------------------------------//
  127. ST7735SendCmd(0xC0);
  128. ST7735SendData(0x28);
  129. ST7735SendData(0x08);
  130. ST7735SendData(0x04);
  131. ST7735SendCmd(0xC1);
  132. ST7735SendData(0XC0);
  133. ST7735SendCmd(0xC2);
  134. ST7735SendData(0x0D);
  135. ST7735SendData(0x00);
  136. ST7735SendCmd(0xC3);
  137. ST7735SendData(0x8D);
  138. ST7735SendData(0x2A);
  139. ST7735SendCmd(0xC4);
  140. ST7735SendData(0x8D);
  141. ST7735SendData(0xEE);
  142. //---------------------------------End ST7735S Power Sequence-------------------------------------//
  143. ST7735SendCmd(0xC5); //VCOM
  144. ST7735SendData(0x1A);
  145. ST7735SendCmd(0x36); //MX, MY, RGB mode
  146. ST7735SendData(0x60);//-----------这个值非常重要,决定了内部显存和显示区域的关系,以及RGB颜色顺序
  147. //------------------------------------ST7735S Gamma Sequence-----------------------------------------//
  148. ST7735SendCmd(0xE0);
  149. ST7735SendData(0x04);
  150. ST7735SendData(0x22);
  151. ST7735SendData(0x07);
  152. ST7735SendData(0x0A);
  153. ST7735SendData(0x2E);
  154. ST7735SendData(0x30);
  155. ST7735SendData(0x25);
  156. ST7735SendData(0x2A);
  157. ST7735SendData(0x28);
  158. ST7735SendData(0x26);
  159. ST7735SendData(0x2E);
  160. ST7735SendData(0x3A);
  161. ST7735SendData(0x00);
  162. ST7735SendData(0x01);
  163. ST7735SendData(0x03);
  164. ST7735SendData(0x13);
  165. ST7735SendCmd(0xE1);
  166. ST7735SendData(0x04);
  167. ST7735SendData(0x16);
  168. ST7735SendData(0x06);
  169. ST7735SendData(0x0D);
  170. ST7735SendData(0x2D);
  171. ST7735SendData(0x26);
  172. ST7735SendData(0x23);
  173. ST7735SendData(0x27);
  174. ST7735SendData(0x27);
  175. ST7735SendData(0x25);
  176. ST7735SendData(0x2D);
  177. ST7735SendData(0x3B);
  178. ST7735SendData(0x00);
  179. ST7735SendData(0x01);
  180. ST7735SendData(0x04);
  181. ST7735SendData(0x13);
  182. //------------------------------------End ST7735S Gamma Sequence-----------------------------------------//
  183. ST7735SendCmd(0x3A); //65k mode
  184. ST7735SendData(0x05);
  185. ST7735SendCmd(0x29); //Display on
  186. }
  187. /**********************************************************************************************
  188. 设置一个写地址区域
  189. ************************************************************************************************/
  190. void SetBlockWrite(u16 x1,u16 y1,u16 x2,u16 y2)
  191. {
  192. u16 Xstart,Xend,Ystart,Yend;
  193. Xstart=x1+1;
  194. Xend=x2+1;
  195. Ystart=y1+2;
  196. Yend=y2+2;
  197. ST7735SendCmd(0x2a);
  198. ST7735SendData(Xstart>>8);
  199. ST7735SendData(Xstart&0xff);
  200. ST7735SendData(Xend>>8);
  201. ST7735SendData(Xend&0xff);
  202. ST7735SendCmd(0x2b);
  203. ST7735SendData(Ystart>>8);
  204. ST7735SendData(Ystart&0xff);
  205. ST7735SendData(Yend>>8);
  206. ST7735SendData(Yend&0xff);
  207. ST7735SendCmd(0x2c);
  208. }
  209. /****************************************************************************
  210. * 名 称:void SetPoint(u16 x,u16 y,u16 color)
  211. * 功 能:指定坐标点
  212. * 入口参数:x轴y轴
  213. * 出口参数:无
  214. * 说 明:2012.4.27 hxm v1.0
  215. * 调用方法:无
  216. ****************************************************************************/
  217. void SetPoint(u16 x,u16 y,u16 color)
  218. {
  219. ST7735SendCmd(0x2a);
  220. ST7735SendData(x>>8);
  221. ST7735SendData(x&0x00ff);
  222. st7735_CMD(0x2b);
  223. ST7735SendData(y>>8);
  224. ST7735SendData(y&0x00ff);
  225. st7735_CMD(0X2C);
  226. st7735_DATA(color>>8);
  227. st7735_DATA(color&0x00ff);
  228. }
  229. /*********************************************************************************************************
  230. END FILE
  231. *********************************************************************************************************/