根據網上的例程,自己總結的TFT-LCD驅動,我用的是STM32FX103芯片
單片機源程序如下:
- #include "gui.h"
- #ifdef USE_FONT_UPDATE
- #include "ff.h"
- #include "diskio.h"
- #endif
- #ifdef USE_FLASH_CHAR
- #include "flash.h"
- #else
- #include "asciicode.h"
- #endif
- #ifdef USE_FONT_UPDATE
- #include "malloc.h"
- #endif
- /****************************************************************************
- * Function Name : GUI_Dot
- * Description : 在彩屏上面畫一點
- * Input : x:點的X坐標
- * * y:點的Y坐標
- * * color:點的顏色
- * Output : None
- * Return : None
- ****************************************************************************/
- void GUI_Dot(uint16_t x, uint16_t y, uint16_t color)
- {
- TFT_SetWindow(x, y, x, y); //設置點的位置
- TFT_WriteData(color); //畫點
- }
- void GUI_Line(u16 xStart, u16 yStart, u16 xEnd, u16 yEnd, u16 color)
- {
- u16 t;
- int xerr = 0, yerr = 0, delta_x, delta_y, distance;
- int incx, incy;
- u16 row, col;
- delta_x = xEnd - xStart;//計算坐標增量
- delta_y = yEnd - yStart;
- col = xStart;
- row = yStart;
- if (delta_x > 0)
- {
- incx=1;//設置單步方向
- }
- else
- {
- if (delta_x == 0)
- {
- incx = 0;//垂直線
- }
- else
- {
- incx = -1;
- delta_x = -delta_x;
- }
- }
- if (delta_y > 0)
- {
- incy = 1;
- }
- else
- {
- if (delta_y == 0)
- {
- incy = 0;//水平線
- }
- else
- {
- incy = -1;
- delta_y = -delta_y;
- }
- }
- if (delta_x > delta_y)
- {
- distance = delta_x;//選取基本增量坐標軸
- }
- else
- {
- distance = delta_y;
- }
- for (t=0; t<=distance+1; t++)
- { //畫線輸出
- GUI_Dot(col, row, color);
- xerr += delta_x;
- yerr += delta_y;
- if(xerr > distance)
- {
- xerr -= distance;
- col += incx;
- }
- if(yerr > distance)
- {
- yerr -= distance;
- row += incy;
- }
- }
- }
- /****************************************************************************
- * Function Name : GUI_Box
- * Description : 給一個區域涂上顏色
- * Input : sx:起始X坐標, sy:其實Y坐標,
- * * ex:終止X坐標, ey:終止Y坐標,
- * * color:方框的顏色
- * Output : None
- * Return : None
- ****************************************************************************/
- void GUI_Box(uint16_t xState, uint16_t yState, uint16_t xEnd, uint16_t yEnd, uint16_t color)
- {
- uint16_t temp;
- if((xState > xEnd) || (yState > yEnd))
- {
- return;
- }
- TFT_SetWindow(xState, yState, xEnd, yEnd);
- xState = xEnd - xState + 1;
- yState = yEnd - yState + 1;
- while(xState--)
- {
- temp = yState;
- while (temp--)
- {
- TFT_WriteData(color);
- }
- }
- }
- /****************************************************************************
- * Function Name : GUI_DrowSign
- * Description : 畫一個十字的標記
- * Input : x:標記的X坐標;
- * * y:標記的Y坐標
- * * color:標記的顏色
- * Output : None
- * Return : None
- ****************************************************************************/
- void GUI_DrowSign(uint16_t x, uint16_t y, uint16_t color)
- {
- uint8_t i;
- /* 畫點 */
- TFT_SetWindow(x-1, y-1, x+1, y+1);
- for(i=0; i<9; i++)
- {
- TFT_WriteData(color);
- }
- /* 畫豎 */
- TFT_SetWindow(x-4, y, x+4, y);
- for(i=0; i<9; i++)
- {
- TFT_WriteData(color);
- }
- /* 畫橫 */
- TFT_SetWindow(x, y-4, x, y+4);
- for(i=0; i<9; i++)
- {
- TFT_WriteData(color);
- }
- }
- #ifndef USE_FLASH_CHAR
- /****************************************************************************
- * Function Name : GUI_Show12ASCII
- * Description : 寫12號ASCII碼
- * Input : x:起始X坐標
- * * y:起始Y坐標
- * * p:字符串首地址
- * * wordColor:字體顏色
- * * backColor:背景顏色
- * Output : None
- * Return : None
- ****************************************************************************/
- void GUI_Show12ASCII(uint16_t x, uint16_t y, uint8_t *p,
- uint16_t wordColor, uint16_t backColor)
- {
- uint8_t i, wordByte, wordNum;
- uint16_t color;
- while(*p != '\0') //檢測是否是最后一個字
- {
- /* 在字庫中的ASCII碼是從空格開始的也就是32開始的,所以減去32 */
- wordNum = *p - 32;
-
- TFT_SetWindow(x, y, x+7, y+15); //字寬*高為:8*16
- for (wordByte=0; wordByte<16; wordByte++) //每個字模一共有16個字節
- {
- color = ASCII8x16[wordNum][wordByte];
- for (i=0; i<8; i++)
- {
- if ((color&0x80) == 0x80)
- {
- TFT_WriteData(wordColor);
- }
- else
- {
- TFT_WriteData(backColor);
- }
- color <<= 1;
- }
- }
- p++; //指針指向下一個字
-
- /* 屏幕坐標處理 */
- x += 8;
- if(x > 233) //TFT_XMAX -8
- {
- x = 0;
- y += 16;
- }
- }
- }
- #else
- /****************************************************************************
- * Function Name : GUI_Show12Char
- * Description : 通過FLASH字庫顯示12號字母和漢字(使用GBK)
- * Input : x:起始X坐標
- * * y:起始Y坐標
- * * ch:字符串首地址
- * * wordColor:字體顏色
- * * backColor:背景顏色
- * Output : None
- * Return : None
- ****************************************************************************/
- void GUI_Show12Char(uint16_t x, uint16_t y, uint8_t *ch,
- uint16_t wordColor, uint16_t backColor)
- {
- uint8_t i, j, color, buf[32];
- uint16_t asc;
- uint32_t wordAddr = 0;
- while(*ch != '\0')
- {
- /*顯示字母 */
- if(*ch < 0x80) //ASCII碼從0~127
- {
- /* 在字庫中的ASCII碼是從空格開始的也就是32開始的,所以減去32 */
- wordAddr = *ch - 32;
- wordAddr *= 16;
- wordAddr += GUI_FLASH_ASCII_ADDR;
-
- /* 讀取FLASH中該字的字模 */
- FLASH_ReadData(buf, wordAddr, 16);
-
- /* 顯示該文字 */
- TFT_SetWindow(x, y, x+7, y+15); //字寬*高為:8*16
- for (j=0; j<16; j++) //每個字模一共有16個字節
- {
- color = buf[j];
- for (i=0; i<8; i++)
- {
- if ((color&0x80) == 0x80)
- {
- TFT_WriteData(wordColor);
- }
- else
- {
- TFT_WriteData(backColor);
- }
- color <<= 1;
- }
- }
-
- ch++; //指針指向下一個字
-
- /* 屏幕坐標處理 */
- x += 8;
- if(x > 233) //TFT_XMAX -8
- {
- x = 0;
- y += 16;
- }
- }
- /* 顯示漢字 */
- else
- {
- /* 將漢字編碼轉換成在FLASH中的地址 */
- asc = *ch - 0x81; //高字節是表示分區,分區是從0x81到0xFE,所以轉換成地址-0x80
- wordAddr = asc * 190; //每個分區一共有190個字
-
- asc = *(ch + 1); //低字節代表每個字在每個分區的位置,它是從0x40到0xFF
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
程序可能還有一些問題未解決,求大佬幫助調試:
gui.rar
(5.5 KB, 下載次數: 93)
2018-1-18 16:47 上傳
點擊文件名下載附件
TFTLCD 下載積分: 黑幣 -5
|