LCD.c 14 KB

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