LCD.c 14 KB

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