LCD.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "includes.h"
  2. static u16 susLcdBackColor; //背景颜色
  3. static u16 susLcdForeColor; //画笔颜色
  4. extern const unsigned char g_apucLetter16[];
  5. extern const unsigned char g_apucCLetter16[];
  6. extern const unsigned char g_apucLetter24[];
  7. extern const unsigned char g_apucCLetter24[];
  8. extern const unsigned char g_apucFonts16[];
  9. extern const unsigned char g_apucCFonts16[];
  10. extern const unsigned char g_apucFonts24[];
  11. extern const unsigned char g_apucCFonts24[];
  12. unsigned char showApn=1;
  13. unsigned char LastKeyLight=0;//熄屏首次电量屏幕
  14. unsigned char KeyLightFlag=-1;
  15. /***************************************************************************
  16. *LcdSetColor 设置画笔颜色和背景颜色
  17. ****************************************************************************/
  18. static void LcdPortInit(void)
  19. {
  20. GPIO_InitTypeDef GPIO_InitStructure;
  21. #if(LCD_BUS_TYPE==LCD_BUS_IO)
  22. /* Enable GPIOD and GPIOE clocks */
  23. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE);
  24. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  26. LCD_CS_HIGH;
  27. LCD_BL_HIGH;
  28. GPIO_InitStructure.GPIO_Pin = (LCD_CS_PIN|LCD_SCK_PIN|LCD_DCX_PIN|LCD_SDA_PIN);
  29. GPIO_Init(GPIOB, &GPIO_InitStructure);
  30. LCD_CS_HIGH;
  31. LCD_SCK_HIGH;
  32. LCD_DCX_HIGH;
  33. LCD_SDA_HIGH;
  34. LCD_RESET_HIGH;
  35. LCD_BL_HIGH;
  36. GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN);
  37. GPIO_Init(GPIOA, &GPIO_InitStructure);
  38. LCD_RESET_HIGH;
  39. LCD_BL_HIGH;
  40. #elif(LCD_BUS_TYPE==LCD_BUS_SPI)
  41. SPI_InitTypeDef SPI_InitStructure;
  42. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE);
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  45. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  46. GPIO_InitStructure.GPIO_Pin = LCD_DCX_PIN;
  47. GPIO_Init(LCD_DCX_PORT, &GPIO_InitStructure);
  48. LCD_DCX_HIGH;
  49. /* NS */
  50. GPIO_InitStructure.GPIO_Pin = LCD_SPI_NSS_PIN;
  51. GPIO_Init(LCD_SPI_PORT, &GPIO_InitStructure);
  52. /*SCK */
  53. GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
  54. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  55. GPIO_Init(LCD_SPI_PORT, &GPIO_InitStructure);
  56. /*MOSI */
  57. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
  58. GPIO_Init(LCD_SPI_PORT, &GPIO_InitStructure);
  59. LCD_SPI_NSS_HIGH;
  60. SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;//半双工,只发不读
  61. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  62. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  63. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  64. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  65. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  66. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //2分频
  67. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  68. SPI_InitStructure.SPI_CRCPolynomial = 7;
  69. SPI_Init(LCD_SPI_HANDLE, &SPI_InitStructure);
  70. SPI_Cmd(LCD_SPI_HANDLE, ENABLE);
  71. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  72. GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN);
  73. GPIO_Init(GPIOA, &GPIO_InitStructure);
  74. LCD_RESET_HIGH;
  75. LCD_BL_HIGH;
  76. #endif
  77. }
  78. #if(LCD_BUS_TYPE==LCD_BUS_IO)
  79. void LcdSendCommand(unsigned char Cmd)
  80. {
  81. unsigned char i,j;
  82. LCD_CS_LOW;
  83. LCD_DCX_LOW;
  84. for(i=0;i<8;i++){
  85. j=0x80>>i;
  86. LCD_SCK_LOW;
  87. if(Cmd&j)LCD_SDA_HIGH;
  88. else LCD_SDA_LOW;
  89. LCD_SCK_HIGH;
  90. }
  91. LCD_CS_HIGH;
  92. }
  93. void LcdSendData(unsigned char Data)
  94. {
  95. unsigned char i,j;
  96. unsigned char ret;
  97. LCD_CS_LOW;
  98. LCD_DCX_HIGH;
  99. for(i=0;i<8;i++){
  100. j=0x80>>i;
  101. LCD_SCK_LOW;
  102. if(Data & j)LCD_SDA_HIGH;
  103. else LCD_SDA_LOW;
  104. LCD_SCK_HIGH;
  105. }
  106. LCD_CS_HIGH;
  107. }
  108. #elif(LCD_BUS_TYPE==LCD_BUS_SPI)
  109. void LCD_SPIx_ReadWriteByte(unsigned char byte)
  110. {
  111. SPI_I2S_SendData(LCD_SPI_HANDLE, byte);
  112. while (SPI_I2S_GetFlagStatus(LCD_SPI_HANDLE, SPI_I2S_FLAG_BSY) == SET);
  113. }
  114. void LcdSendCommand(unsigned char Cmd)
  115. {
  116. LCD_DCX_LOW;
  117. LCD_SPI_NSS_LOW;
  118. LCD_SPIx_ReadWriteByte(Cmd);
  119. LCD_SPI_NSS_HIGH;
  120. }
  121. void LcdSendData(unsigned char Data)
  122. {
  123. LCD_DCX_HIGH;
  124. LCD_SPI_NSS_LOW;
  125. LCD_SPIx_ReadWriteByte(Data);
  126. LCD_SPI_NSS_HIGH;
  127. }
  128. #endif
  129. /***************************************************************************
  130. *LcdSetColor 设置画笔颜色和背景颜色
  131. ****************************************************************************/
  132. void LcdSetColor(u16 BrushColor,u16 BackColor)
  133. {
  134. susLcdForeColor=BrushColor; //画笔颜色
  135. susLcdBackColor=BackColor; //背景颜色
  136. }
  137. void LcdInit(void)
  138. {
  139. LcdPortInit();
  140. LCD_BL_LOW;//用户体验
  141. //reset
  142. LCD_RESET_HIGH;
  143. DelayMs(1);
  144. LCD_RESET_LOW;
  145. DelayMs(1);
  146. LCD_RESET_HIGH;
  147. DelayMs(120);
  148. LcdSendCommand(0xfe);
  149. LcdSendCommand(0xef);
  150. LcdSendCommand(0x36);
  151. LcdSendData(0x68);
  152. LcdSendCommand(0x3a);
  153. LcdSendData(0x05);//
  154. LcdSendCommand(0xb6);
  155. LcdSendData(0x11);
  156. LcdSendCommand(0xac);
  157. LcdSendData(0x0b);
  158. LcdSendCommand(0xb4);
  159. LcdSendData(0x21);
  160. LcdSendCommand(0xb1);
  161. LcdSendData(0xc0);
  162. LcdSendCommand(0xb2);
  163. LcdSendData(0x08);
  164. LcdSendCommand(0xb3);
  165. LcdSendData(0x23);
  166. LcdSendCommand(0xeb);
  167. LcdSendData(0x11);
  168. LcdSendData(0x66);
  169. LcdSendCommand(0x2a);
  170. LcdSendData(0x00);
  171. LcdSendData(0x00);
  172. LcdSendData(0x00);
  173. LcdSendData(0x7f);
  174. LcdSendCommand(0x2b);
  175. LcdSendData(0x00);
  176. LcdSendData(0x10);
  177. LcdSendData(0x00);
  178. LcdSendData(0xa0);
  179. LcdSendCommand(0xf5);
  180. LcdSendData(0x09);
  181. LcdSendData(0x84);
  182. LcdSendCommand(0xe6);
  183. LcdSendData(0x28);
  184. LcdSendData(0x00);
  185. LcdSendCommand(0xe7);
  186. LcdSendData(0x70);
  187. LcdSendData(0x00);
  188. //////////////////////////////////////////
  189. LcdSendCommand(0xF0);
  190. LcdSendData(0X4);
  191. LcdSendData(0X32);
  192. LcdSendData(0X25);
  193. LcdSendData(0X41);
  194. LcdSendData(0XBA);
  195. LcdSendData(0XE);
  196. LcdSendData(0XF);
  197. LcdSendData(0X0);
  198. LcdSendData(0X1);
  199. LcdSendData(0XB);
  200. LcdSendData(0XE);
  201. LcdSendData(0X19);
  202. LcdSendData(0X18);
  203. LcdSendData(0XF);
  204. LcdSendCommand(0xF1);
  205. LcdSendData(0X7);
  206. LcdSendData(0X3C);
  207. LcdSendData(0X2D);
  208. LcdSendData(0X50);
  209. LcdSendData(0XAC);
  210. LcdSendData(0X25);
  211. LcdSendData(0X25);
  212. LcdSendData(0X0);
  213. LcdSendData(0X1);
  214. LcdSendData(0XC);
  215. LcdSendData(0XE);
  216. LcdSendData(0X15);
  217. LcdSendData(0X14);
  218. LcdSendData(0XF);
  219. LcdSendCommand(0xfe);
  220. LcdSendCommand(0xff);
  221. LcdSendCommand(0x21);
  222. LcdSendCommand(0x11);
  223. DelayMs(120);
  224. LcdSendCommand(0x29);
  225. //设置前景色和背景色
  226. LcdSetColor(0x0000,0xffff);
  227. //清屏
  228. LcdClrAll();
  229. LCD_BL_HIGH;
  230. }
  231. void LCDBackLight(char Ct)
  232. {
  233. LastKeyLight=KeyLightFlag;
  234. if(Ct==0){
  235. KeyLightFlag=Ct;
  236. LCD_BL_LOW;
  237. LcdSendCommand(0x28);
  238. if(GPS_TimeTable[newPara.gpsTimeIndex]>=30||GPS_TimeTable[newPara.gpsTimeIndex]==0)QueryTime=60;
  239. else QueryTime=GPS_TimeTable[newPara.gpsTimeIndex]*2;
  240. }else{
  241. QueryTime=10;
  242. KeyLightFlag=Ct;
  243. LCD_BL_HIGH;
  244. LcdSendCommand(0x29);
  245. }
  246. }
  247. /**********************************************************************************************
  248. 设置一个写地址区域
  249. ************************************************************************************************/
  250. void LcdBlockWrite(u16 x1,u16 y1,u16 x2,u16 y2)
  251. {
  252. u16 Xstart,Xend,Ystart,Yend;
  253. #if(LCD_TYPE==0)
  254. //旧屏
  255. Xstart=x1+1;
  256. Xend=x2+1;
  257. Ystart=y1+2;
  258. Yend=y2+2;
  259. #else
  260. //新屏
  261. Xstart=x1;
  262. Xend=x2;
  263. Ystart=y1;
  264. Yend=y2;
  265. #endif
  266. LcdSendCommand(0x2a);
  267. LcdSendData(Xstart>>8);
  268. LcdSendData(Xstart&0xff);
  269. LcdSendData(Xend>>8);
  270. LcdSendData(Xend&0xff);
  271. LcdSendCommand(0x2b);
  272. LcdSendData(Ystart>>8);
  273. LcdSendData(Ystart&0xff);
  274. LcdSendData(Yend>>8);
  275. LcdSendData(Yend&0xff);
  276. LcdSendCommand(0x2c);
  277. }
  278. void LcdClrAll(void)
  279. {
  280. int i,j;
  281. unsigned char colorH,colorL;
  282. colorH=susLcdBackColor>>8;
  283. colorL=susLcdBackColor&0xff;
  284. LcdBlockWrite(0,0,127,127);
  285. for(i=0;i<LCD_HEIGHT;i++)
  286. {
  287. for(j=0;j<LCD_WIDTH;j++)
  288. {
  289. LcdSendData(colorH);
  290. LcdSendData(colorL);
  291. }
  292. }
  293. }
  294. void LcdClrAll2(void)
  295. {
  296. int i,j;
  297. unsigned char colorH,colorL;
  298. colorH=susLcdForeColor>>8;
  299. colorL=susLcdForeColor&0xff;
  300. LcdBlockWrite(0,0,127,127);
  301. for(i=0;i<LCD_HEIGHT;i++)
  302. {
  303. for(j=0;j<LCD_WIDTH;j++)
  304. {
  305. LcdSendData(colorH);
  306. LcdSendData(colorL);
  307. }
  308. }
  309. }