LCD.c 14 KB

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