LCD.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /****************************************Copyright (c)**************************************************
  2. * File Name: Lcd.c
  3. * Function Describe: Driver of LCD
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2016-1-8
  7. ********************************************************************************************************/
  8. #define THIS_FILE_ID 15
  9. //-------------------------------------------------------
  10. #include "includes.h"
  11. unsigned char showApn=1;
  12. unsigned char LcdSleepFlag;
  13. unsigned char LastLcdSleepFlag;
  14. extern const unsigned char g_apucLetter16[];
  15. extern const unsigned char g_apucCLetter16[];
  16. extern const unsigned char g_apucLetter24[];
  17. extern const unsigned char g_apucCLetter24[];
  18. extern const unsigned char g_apucFonts16[];
  19. extern const unsigned char g_apucCFonts16[];
  20. extern const unsigned char g_apucFonts24[];
  21. extern const unsigned char g_apucCFonts24[];
  22. void LcdShowStr(u16 x, u16 y,const char *string,u8 mode);
  23. void LcdDrawVLine(u16 y1, u16 y2, u16 x, u8 width);
  24. void LcdDrawHLine(u16 x1, u16 x2, u16 y, u8 width);
  25. void LcdClearRect(u16 xl, u16 yl, u16 xr, u16 yr);
  26. void LcdFillRect(u16 xl, u16 yl, u16 xr, u16 yr);
  27. static u16 susLcdBackColor; //背景颜色
  28. static u16 susLcdForeColor; //画笔颜色
  29. /***************************************************************************
  30. *LcdSetColor 设置画笔颜色和背景颜色
  31. ****************************************************************************/
  32. void LcdSetColor(u16 BrushColor,u16 BackColor)
  33. {
  34. susLcdForeColor=BrushColor; //画笔颜色
  35. susLcdBackColor=BackColor; //背景颜色
  36. }
  37. /*******************************************************************************
  38. * Function Name : Lcd_Configuration
  39. * Description : Configures LCD Control lines
  40. * Input : None
  41. * Output : None
  42. * Return : None
  43. * Attention : None
  44. *******************************************************************************/
  45. static void LcdPortInit(void)
  46. {
  47. SPI_InitTypeDef SPI_InitStructure;
  48. GPIO_InitTypeDef GPIO_InitStructure;
  49. #if 0
  50. /* Enable GPIOD and GPIOE clocks */
  51. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  52. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  53. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  54. LCD_CS_HIGH;
  55. LCD_BL_HIGH;
  56. GPIO_InitStructure.GPIO_Pin = (LCD_CS_PIN|LCD_SCK_PIN|LCD_DCX_PIN|LCD_SDA_PIN);
  57. GPIO_Init(GPIOB, &GPIO_InitStructure);
  58. LCD_CS_HIGH;
  59. LCD_SCK_HIGH;
  60. LCD_SDA_HIGH;
  61. LCD_DCX_HIGH;
  62. LCD_RESET_HIGH;
  63. LCD_BL_HIGH;
  64. GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN);
  65. GPIO_Init(GPIOC, &GPIO_InitStructure);
  66. LCD_RESET_HIGH;
  67. LCD_BL_HIGH;
  68. #else
  69. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
  70. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  71. GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN|LCD_DCX_PIN;
  72. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  74. GPIO_Init(GPIOB, &GPIO_InitStructure);
  75. GPIO_InitStructure.GPIO_Pin = (LCD_SCK_PIN|LCD_SDA_PIN);;
  76. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  77. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  78. GPIO_Init(GPIOB, &GPIO_InitStructure);
  79. GPIO_InitStructure.GPIO_Pin = (LCD_RESET_PIN|LCD_BL_PIN);
  80. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  81. GPIO_Init(GPIOC, &GPIO_InitStructure);
  82. LCD_RESET_HIGH;
  83. LCD_BL_HIGH;
  84. /*!< SPI configuration */
  85. SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
  86. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  87. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  88. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  89. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  90. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  91. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //决定SPI 速度 //2
  92. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  93. SPI_InitStructure.SPI_CRCPolynomial = 7;
  94. SPI_Init(SPI2, &SPI_InitStructure);
  95. //
  96. /*!< Enable the sFLASH_SPI */
  97. SPI_Cmd(SPI2, ENABLE);
  98. #endif
  99. }
  100. u8 LCD_ReadWriteByte(u8 byte)
  101. {
  102. // !< Send byte through the SPI1 peripheral
  103. SPI_I2S_SendData(SPI2, byte);
  104. // < Loop while DR register in not emplty
  105. while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  106. }
  107. void LCD_Write_SR(u8 sr)
  108. {
  109. LCD_ReadWriteByte(sr); //写入一个字节
  110. }
  111. void LcdSendCommand(unsigned char Cmd)
  112. {
  113. unsigned char i,j;
  114. LCD_CS_LOW;
  115. LCD_DCX_LOW;
  116. // for(i=0;i<8;i++){
  117. // j=0x80>>i;
  118. // LCD_SCK_LOW;
  119. // if(Cmd&j)LCD_SDA_HIGH;
  120. // else LCD_SDA_LOW;
  121. // LCD_SCK_HIGH;
  122. // }
  123. LCD_Write_SR(Cmd);
  124. LCD_CS_HIGH;
  125. }
  126. void LcdSendData(unsigned char Data)
  127. {
  128. unsigned char i,j;
  129. LCD_CS_LOW;
  130. LCD_DCX_HIGH;
  131. // for(i=0;i<8;i++){
  132. // j=0x80>>i;
  133. // LCD_SCK_LOW;
  134. // if(Data & j)LCD_SDA_HIGH;
  135. // else LCD_SDA_LOW;
  136. // LCD_SCK_HIGH;
  137. // }
  138. LCD_Write_SR(Data);
  139. LCD_CS_HIGH;
  140. }
  141. void LcdInit(void)
  142. {
  143. LcdPortInit();
  144. LCD_BL_LOW;//用户体验
  145. //reset
  146. LCD_RESET_HIGH;
  147. DelayMs(1);
  148. LCD_RESET_LOW;
  149. DelayMs(1);
  150. LCD_RESET_HIGH;
  151. DelayMs(120);
  152. //-----------------------------------ST7735S Set REG---------------------------------------------//
  153. LcdSendCommand(0x11); //Sleep out
  154. DelayMs(120); //Delay 120ms
  155. //------------------------------------ST7735S Frame Rate-----------------------------------------//
  156. LcdSendCommand(0xB1);
  157. LcdSendData(0x05);
  158. LcdSendData(0x3C);
  159. LcdSendData(0x3C);
  160. LcdSendCommand(0xB2);
  161. LcdSendData(0x05);
  162. LcdSendData(0x3C);
  163. LcdSendData(0x3C);
  164. LcdSendCommand(0xB3);
  165. LcdSendData(0x05);
  166. LcdSendData(0x3C);
  167. LcdSendData(0x3C);
  168. LcdSendData(0x05);
  169. LcdSendData(0x3C);
  170. LcdSendData(0x3C);
  171. //------------------------------------End ST7735S Frame Rate-----------------------------------------//
  172. LcdSendCommand(0xB4); //Dot inversion
  173. LcdSendData(0x03);
  174. //------------------------------------ST7735S Power Sequence-----------------------------------------//
  175. LcdSendCommand(0xC0);
  176. LcdSendData(0x28);
  177. LcdSendData(0x08);
  178. LcdSendData(0x04);
  179. LcdSendCommand(0xC1);
  180. LcdSendData(0XC0);
  181. LcdSendCommand(0xC2);
  182. LcdSendData(0x0D);
  183. LcdSendData(0x00);
  184. LcdSendCommand(0xC3);
  185. LcdSendData(0x8D);
  186. LcdSendData(0x2A);
  187. LcdSendCommand(0xC4);
  188. LcdSendData(0x8D);
  189. LcdSendData(0xEE);
  190. //---------------------------------End ST7735S Power Sequence-------------------------------------//
  191. LcdSendCommand(0xC5); //VCOM
  192. LcdSendData(0x1A);
  193. LcdSendCommand(0x36); //MX, MY, RGB mode
  194. //LcdSendData(0x60);//-----------这个值非常重要,决定了内部显存和显示区域的关系,以及RGB颜色顺序
  195. LcdSendData(0xA0);
  196. //------------------------------------ST7735S Gamma Sequence-----------------------------------------//
  197. LcdSendCommand(0xE0);
  198. LcdSendData(0x04);
  199. LcdSendData(0x22);
  200. LcdSendData(0x07);
  201. LcdSendData(0x0A);
  202. LcdSendData(0x2E);
  203. LcdSendData(0x30);
  204. LcdSendData(0x25);
  205. LcdSendData(0x2A);
  206. LcdSendData(0x28);
  207. LcdSendData(0x26);
  208. LcdSendData(0x2E);
  209. LcdSendData(0x3A);
  210. LcdSendData(0x00);
  211. LcdSendData(0x01);
  212. LcdSendData(0x03);
  213. LcdSendData(0x13);
  214. LcdSendCommand(0xE1);
  215. LcdSendData(0x04);
  216. LcdSendData(0x16);
  217. LcdSendData(0x06);
  218. LcdSendData(0x0D);
  219. LcdSendData(0x2D);
  220. LcdSendData(0x26);
  221. LcdSendData(0x23);
  222. LcdSendData(0x27);
  223. LcdSendData(0x27);
  224. LcdSendData(0x25);
  225. LcdSendData(0x2D);
  226. LcdSendData(0x3B);
  227. LcdSendData(0x00);
  228. LcdSendData(0x01);
  229. LcdSendData(0x04);
  230. LcdSendData(0x13);
  231. //------------------------------------End ST7735S Gamma Sequence-----------------------------------------//
  232. LcdSendCommand(0x3A); //65k mode
  233. LcdSendData(0x05);
  234. LcdSendCommand(0x29); //Display on
  235. //设置前景色和背景色
  236. LcdSetColor(0x0000,0xffff); //ffff 白色
  237. //LcdSetColor(0xffff,0x0000);
  238. //清屏
  239. //LcdClrAll();
  240. // LCD_RESET_LOW; //防止其刷屏
  241. }
  242. /****************************************************************************
  243. * 名 称:void Set_ramaddr(u16 x,u16 y)
  244. * 功 能:指定坐标点
  245. * 入口参数:x轴y轴
  246. * 出口参数:无
  247. * 说 明:2012.4.27 hxm v1.0
  248. * 调用方法:无
  249. ****************************************************************************/
  250. void LcdSetRamAddr(u16 x,u16 y)
  251. {
  252. LcdSendCommand(0x2a);
  253. LcdSendData(x>>8);
  254. LcdSendData(x&0xff);
  255. LcdSendCommand(0x2b);
  256. LcdSendData(y>>8);
  257. LcdSendData(y&0xff);
  258. }
  259. /**********************************************************************************************
  260. 设置一个写地址区域
  261. ************************************************************************************************/
  262. void LcdBlockWrite(u16 x1,u16 y1,u16 x2,u16 y2)
  263. {
  264. u16 Xstart,Xend,Ystart,Yend;//u16
  265. #if(LCD_TYPE==0)
  266. //旧屏
  267. Xstart=x1+1;
  268. Xend=x2+1;
  269. Ystart=y1+2;
  270. Yend=y2+2;
  271. #else
  272. //新屏
  273. Xstart=x1;
  274. Xend=x2;
  275. Ystart=y1;
  276. Yend=y2;
  277. #endif
  278. LcdSendCommand(0x2a);
  279. LcdSendData(Xstart>>8);
  280. LcdSendData(Xstart&0xff);
  281. LcdSendData(Xend>>8);
  282. LcdSendData(Xend&0xff);
  283. LcdSendCommand(0x2b);
  284. LcdSendData(Ystart>>8);
  285. LcdSendData(Ystart&0xff);
  286. LcdSendData(Yend>>8);
  287. LcdSendData(Yend&0xff);
  288. LcdSendCommand(0x2c);
  289. }
  290. /***************************************************************************
  291. LcdClrAll
  292. ****************************************************************************/
  293. void LcdClrAll(void)
  294. {
  295. static int i,j;
  296. unsigned char colorH,colorL;
  297. colorH=susLcdBackColor>>8;
  298. colorL=susLcdBackColor&0xff;
  299. LcdBlockWrite(0,0,159,127);
  300. for(i=0;i<LCD_HEIGHT;i++)
  301. {
  302. for(j=0;j<LCD_WIDTH;j++)
  303. {
  304. LcdSendData(colorH);
  305. LcdSendData(colorL);
  306. }
  307. }
  308. }
  309. /****************************************************************************
  310. LcdClearRect
  311. 指定区域清屏,填充背景色。
  312. ****************************************************************************/
  313. void LcdClearRect(u16 xl, u16 yl, u16 xr, u16 yr)
  314. {
  315. u16 len;
  316. u8 colorH,colorL;
  317. u16 i,j;
  318. if(xl>xr||yl>yr)return;
  319. len=(xr-xl+1)*(yr-yl+1);
  320. colorH = susLcdBackColor >> 8;
  321. colorL = susLcdBackColor & 0x00ff;
  322. LcdBlockWrite(xl,yl,xr,yr);
  323. for(i=0;i<len;i++){
  324. LcdSendData(colorH);
  325. LcdSendData(colorL);
  326. }
  327. }
  328. /****************************************************************************
  329. LcdFillRect
  330. 指定区域填充前景色
  331. ****************************************************************************/
  332. void LcdFillRect(u16 xl, u16 yl, u16 xr, u16 yr)
  333. {
  334. u16 len;
  335. u8 colorH,colorL;
  336. u16 i,j;
  337. if(xl>xr||yl>yr)return;
  338. len=(xr-xl+1)*(yr-yl+1);
  339. colorH = susLcdForeColor >> 8;
  340. colorL = susLcdForeColor & 0x00ff;
  341. LcdBlockWrite(xl,yl,xr,yr);
  342. for(i=0;i<len;i++){
  343. LcdSendData(colorH);
  344. LcdSendData(colorL);
  345. }
  346. }
  347. /****************************************************************************
  348. LcdSetPoint
  349. 指定位置设置一点的颜色,颜色用全局变量画笔颜色g_usColor
  350. 坐标:x,y
  351. 颜色:color
  352. ****************************************************************************/
  353. void LcdSetPoint(u16 x,u16 y)
  354. {
  355. LcdSendCommand(0x2a);
  356. LcdSendData(x>>8);
  357. LcdSendData(x&0x00ff);
  358. LcdSendCommand(0x2b);
  359. LcdSendData(y>>8);
  360. LcdSendData(y&0x00ff);
  361. LcdSendCommand(0X2C);
  362. LcdSendData(susLcdForeColor>>8);
  363. LcdSendData(susLcdForeColor&0x00ff);
  364. }
  365. /****************************************************************************
  366. LcdDrawHLine
  367. 画水平线
  368. ****************************************************************************/
  369. void LcdDrawHLine(u16 x1, u16 x2, u16 y, u8 width)
  370. {
  371. if(width==0)return;
  372. if(x2<x1)return;
  373. width-=1;
  374. LcdFillRect(x1,y,x2,y+width);
  375. }
  376. /****************************************************************************
  377. LcdDrawVLine
  378. 画垂直线
  379. ****************************************************************************/
  380. void LcdDrawVLine(u16 y1, u16 y2, u16 x, u8 width)
  381. {
  382. if(width==0)return;
  383. if(y2<y1)return;
  384. width-=1;
  385. LcdFillRect(x,y1,x+width,y2);
  386. }
  387. /****************************************************************************
  388. LcdDrawRect
  389. 画矩形框,中间不填充
  390. ****************************************************************************/
  391. void LcdDrawRect(u16 x1, u16 y1, u16 x2, u16 y2,u8 width)
  392. {
  393. if(x1>x2||y1>y2)return;
  394. LcdDrawHLine(x1,x2,y1,width);
  395. LcdDrawHLine(x1,x2,y2,width);
  396. LcdDrawVLine(y1,y2,x1,width);
  397. LcdDrawVLine(y1,y2,x2,width);
  398. }
  399. /***************************************************************************
  400. *在LCD上显示一个字符
  401. ****************************************************************************/
  402. void LcdShowChar(u16 x,u16 y,u8 hiByte,u8 loByte,u8 mode)
  403. {
  404. unsigned short i,j,k,m;
  405. unsigned short width,height;
  406. unsigned char ByteWidth,data;
  407. unsigned char colorH,colorL;
  408. unsigned char bgcolorH,bgcolorL;
  409. const unsigned char *l_pucFont,*l_pucLetter;
  410. unsigned char *p;
  411. unsigned char font;//字体
  412. unsigned char cover;//覆盖方式
  413. font=mode&0xf0;
  414. cover=mode&0x0f;
  415. k = 0;
  416. if(!hiByte) //english
  417. {
  418. if(font==0x00) //8 * 16
  419. {
  420. width=8;
  421. height = 16;
  422. ByteWidth = 1;
  423. l_pucFont = g_apucFonts16;
  424. l_pucLetter = g_apucLetter16;
  425. }else{ //16 * 24
  426. width=16;
  427. height = 24;
  428. ByteWidth = 2;
  429. l_pucFont = g_apucFonts24;
  430. l_pucLetter = g_apucLetter24;
  431. }
  432. //
  433. while(*l_pucLetter)
  434. {
  435. if(loByte == *l_pucLetter)break;
  436. l_pucLetter ++;
  437. k ++;
  438. }
  439. }
  440. else //chinese
  441. {
  442. if(font == 0x00)//16 * 16
  443. {
  444. width=16;
  445. height = 16;
  446. ByteWidth = 2;
  447. l_pucFont = g_apucCFonts16;
  448. l_pucLetter = g_apucCLetter16;
  449. }else{//24 * 24
  450. width=24;
  451. height = 24;
  452. ByteWidth = 3;
  453. l_pucFont = g_apucCFonts24;
  454. l_pucLetter = g_apucCLetter24;
  455. }
  456. //
  457. while(*l_pucLetter)
  458. {
  459. if(hiByte == *l_pucLetter && loByte == *(l_pucLetter + 1))break;
  460. l_pucLetter += 2;
  461. k ++;
  462. }
  463. }
  464. //查找到字库的开始位置
  465. l_pucFont += k * ByteWidth * height;
  466. //刷屏显示
  467. colorH=susLcdForeColor>>8;
  468. colorL=susLcdForeColor&0xff;
  469. bgcolorH=susLcdBackColor>>8;
  470. bgcolorL=susLcdBackColor&0xff;
  471. LcdBlockWrite(x,y,x+width-1,y+height-1);
  472. for(j=0;j<height;j++){
  473. for(i=0;i<ByteWidth;i++){
  474. data=*l_pucFont++;
  475. for(k=0;k<8;k++){
  476. if(data & (0x80>>k)){
  477. LcdSendData(colorH);
  478. LcdSendData(colorL);
  479. }else{
  480. //if(cover){
  481. LcdSendData(bgcolorH);
  482. LcdSendData(bgcolorH);
  483. //}
  484. }
  485. }
  486. }
  487. }
  488. }
  489. /******************************************************************************
  490. LcdShowStr
  491. 指定位置显示字符串
  492. 左上角坐标:x,y
  493. 显示内容:string
  494. 模式:mode 高4位代表字体,低4位为模式
  495. ******************************************************************************/
  496. void LcdShowStr(u16 x, u16 y,const char *string,u8 mode)
  497. {
  498. unsigned char width1,width2;
  499. unsigned char *p;
  500. unsigned short i;
  501. unsigned char font;//字体
  502. font=mode&0xf0;
  503. if(font == 0x00) //16bit font
  504. {
  505. width1 = 2;
  506. width2 = 1;
  507. }
  508. else //24bit font
  509. {
  510. width1 = 3;
  511. width2 = 1;
  512. }
  513. p = (unsigned char *)string;
  514. i = 0;
  515. while(*p)
  516. {
  517. if (*p > 0x9f) //chinese letter
  518. {
  519. LcdShowChar(x+i,y,*p, *(p+1),mode);
  520. i += width1 * 8;
  521. p += 2;
  522. }
  523. else //english letter
  524. {
  525. LcdShowChar(x + i,y,0,*p,mode);
  526. if(font==0){
  527. i += width2 * 8;
  528. }else{
  529. i += width2 * 16;
  530. }
  531. p ++;
  532. }
  533. }
  534. }
  535. //void KeyLedControl(char status)
  536. //{
  537. // if(status)
  538. // KEY_LED(0);
  539. // else
  540. // {
  541. // if(g_ulKeyValue != KEY_PPT &&
  542. // g_ulKeyValue != KEY_PPT_MIC)
  543. // KEY_LED(1);
  544. // }
  545. //}
  546. void LCDTimeOut(int Ct)
  547. {
  548. LastLcdSleepFlag=LcdSleepFlag;
  549. if(Ct){
  550. LCD_BL_LOW;
  551. //LcdSendCommand(0x28);
  552. LcdSleepFlag=1;//
  553. }else{
  554. //LcdSendCommand(0x29);
  555. LCD_BL_HIGH;
  556. LcdSleepFlag=0;
  557. }
  558. }
  559. /*********************************************************************************************************
  560. END FILE
  561. *********************************************************************************************************/