捕獲1.JPG (132.56 KB, 下載次數: 46)
下載附件
2019-9-24 11:12 上傳
捕獲.JPG (172.87 KB, 下載次數: 46)
下載附件
2019-9-24 11:13 上傳
單片機源程序如下:
- #include "TFT.h"
- #include "stdlib.h" //函數調用頭文件
- #include "stdio.h" //為了解除sprintf警告
- //TFT1.8 引腳
- #define LCD_RST PDout(0)
- #define LCD_DC PDout(1)
- #define LCD_SDA PDout(2)
- #define LCD_SCL PDout(3)
- #define LCD_CS PDout(4)
- /*!
- * @brief 不精確延時
- *
- * @param
- *
- * @return
- *
- * @note
- *
- * @example
- *
- * @date 2019/8/6 星期二
- */
- void tft_delayms(int ms)
- {
- volatile uint32_t i = 0;
- while(ms--)
- {
- for (i = 0; i < 1000; ++i)
- {
- __asm("NOP"); /* delay */
- }
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_init()
- 入口參數: 0:橫屏 1:豎屏
- 出口參數:無
- 時間:2018/1/26
- 功能說明:器件初始化
- 其他說明:無
- **********************************************************/
- void TFTSPI_Init(uint8_t type)
- {
- //-----端口初始化----//
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能PD端口時鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4);
-
- LCD_RST=0;
- tft_delayms(50);
- LCD_RST=1;
- tft_delayms(50);
- TFTSPI_Write_Cmd(0x11); //關閉睡眠,振蕩器工作
- tft_delayms(10);
- TFTSPI_Write_Cmd(0x3a); //每次傳送16位數據(VIPF3-0=0101),每個像素16位(IFPF2-0=101)
- TFTSPI_Write_Byte(0x55);
- TFTSPI_Write_Cmd(0x26);
- TFTSPI_Write_Byte(0x04);
- TFTSPI_Write_Cmd(0xf2); //Driver Output Control(1)
- TFTSPI_Write_Byte(0x01);
- TFTSPI_Write_Cmd(0xe0); //Driver Output Control(1)
- TFTSPI_Write_Byte(0x3f);
- TFTSPI_Write_Byte(0x25);
- TFTSPI_Write_Byte(0x1c);
- TFTSPI_Write_Byte(0x1e);
- TFTSPI_Write_Byte(0x20);
- TFTSPI_Write_Byte(0x12);
- TFTSPI_Write_Byte(0x2a);
- TFTSPI_Write_Byte(0x90);
- TFTSPI_Write_Byte(0x24);
- TFTSPI_Write_Byte(0x11);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Cmd(0xe1); //Driver Output Control(1)
- TFTSPI_Write_Byte(0x20);
- TFTSPI_Write_Byte(0x20);
- TFTSPI_Write_Byte(0x20);
- TFTSPI_Write_Byte(0x20);
- TFTSPI_Write_Byte(0x05);
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x15);
- TFTSPI_Write_Byte(0xa7);
- TFTSPI_Write_Byte(0x3d);
- TFTSPI_Write_Byte(0x18);
- TFTSPI_Write_Byte(0x25);
- TFTSPI_Write_Byte(0x2a);
- TFTSPI_Write_Byte(0x2b);
- TFTSPI_Write_Byte(0x2b);
- TFTSPI_Write_Byte(0x3a);
- TFTSPI_Write_Cmd(0xb1); //0xb1 //設置屏幕刷新頻率
- TFTSPI_Write_Byte(0x00); //0x08 //DIVA=8
- TFTSPI_Write_Byte(0x00); //0x08 //VPA =8,約90Hz
- TFTSPI_Write_Cmd(0xb4); //LCD Driveing control
- TFTSPI_Write_Byte(0x07); //NLA=1,NLB=1,NLC=1
- TFTSPI_Write_Cmd(0xc0); //LCD Driveing control Power_Control1
- TFTSPI_Write_Byte(0x0a);
- TFTSPI_Write_Byte(0x02);
- TFTSPI_Write_Cmd(0xc1); //LCD Driveing control
- TFTSPI_Write_Byte(0x02);
- TFTSPI_Write_Cmd(0xc5); //LCD Driveing control
- TFTSPI_Write_Byte(0x4f);
- TFTSPI_Write_Byte(0x5a);
- TFTSPI_Write_Cmd(0xc7); //LCD Driveing control
- TFTSPI_Write_Byte(0x40);
- TFTSPI_Write_Cmd(0x2a); //配置MCU可操作的LCD內部RAM橫坐標起始、結束參數
- TFTSPI_Write_Byte(0x00); //橫坐標起始地址0x0000
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00); //橫坐標結束地址0x007f(127)
- TFTSPI_Write_Byte(0xa8); //7f
- TFTSPI_Write_Cmd(0x2b); //配置MCU可操作的LCD內部RAM縱坐標起始結束參數
- TFTSPI_Write_Byte(0x00); //縱坐標起始地址0x0000
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00); //縱坐標結束地址0x009f(159)
- TFTSPI_Write_Byte(0xb3); //9f
- TFTSPI_Write_Cmd(0x36); //配置MPU和DDRAM對應關系
- if(type)
- TFTSPI_Write_Byte(0xC0); //豎屏顯示 //MX=1,MY=1
- else
- TFTSPI_Write_Byte(0xA0); //橫屏顯示
-
- TFTSPI_Write_Cmd(0xb7); //LCD Driveing control
- TFTSPI_Write_Byte(0x00); //CRL=0
- TFTSPI_Write_Cmd(0x29); //開啟屏幕顯示
- TFTSPI_Write_Cmd(0x2c); //設置為LCD接收數據/命令模式
- }
- void tft18delay_1us(unsigned int Del) //
- {
- int i;
- while(Del--)
- {
- for(i=0;i<3;i++) __asm("NOP");
- }
- }
- /******************DRIVE IC寄存器尋址 8080 SERIES*******************************/
- void TFTSPI_Write_Cmd(uint8_t cmd)
- {
- uint8_t i;
-
- LCD_DC=0;// A0=0 ILI9163_A0=0; //A0=0發送命令
- for(i=0;i<8;i++)
- {
- LCD_SCL=0;//tft18delay_1us(1); // SCK=0 ILI9163_SCK=0;
- if(cmd&0x80){LCD_SDA=1;}// SDI=1
- else{LCD_SDA=0;}// SDI=0
- LCD_SCL=1;//tft18delay_1us(1); // SCK=1 ILI9163_SCK = 1;
- cmd = (cmd<<1);
- }
- }
- /*****************DRIVE IC寄存器寫數據 8080 SERIES******************************/
- void TFTSPI_Write_Byte(uint8_t dat)
- {
- uint8_t i;
-
- LCD_DC=1;// A0=1 ILI9163_A0=1; //A0=1發送數據
- for(i=0;i<8;i++)
- {
- LCD_SCL=0;//tft18delay_1us(1); // SCK=0 ILI9163_SCK=0;
- if(dat&0x80){LCD_SDA=1;}// SDI=1
- else{LCD_SDA=0;}// SDI=0
- LCD_SCL=1;//tft18delay_1us(1); // SCK=1 ILI9163_SCK = 1;
- dat = (dat<<1);
- }
- }
- /****************DRIVE IC GDRAM 寫數據 16BITS 8080 SERIES*****************************/
- void TFTSPI_Write_Word(uint16_t dat)
- {
- uint8_t i;
-
- LCD_DC=1;// A0=1 ILI9163_A0=1;
-
- for(i=0;i<16;i++)
- {
- LCD_SCL=0;//tft18delay_1us(1); // SCK=0 ILI9163_SCK=0;
- if(dat&0x8000){ LCD_SDA=1;}// SDI=1
- else{LCD_SDA=0;}// SDI=0
- LCD_SCL=1;//tft18delay_1us(1); // SCK=1 ILI9163_SCK=1;
- dat<<=1;
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_Set_Pos()
- 入口參數:起始終止坐標
- 出口參數:無
- 時間:2018/1/26
- 功能說明:重新定位輸入信息位置
- 其他說明:無
- **********************************************************/
- void TFTSPI_Set_Pos(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye)
- {
- TFTSPI_Write_Cmd(0x2A); //Colulm addRSTs set
- TFTSPI_Write_Byte(0x00); //行起始坐標高8位,始終為零
- TFTSPI_Write_Byte(xs); //行起始坐標低8位
- TFTSPI_Write_Byte(0x00); //行終止坐標高8位,始終為零
- TFTSPI_Write_Byte(xe); //行終止坐標低8位
- TFTSPI_Write_Cmd(0x2B); //Colulm addRSTs set
- TFTSPI_Write_Byte(0x00); //列起始坐標高8位,始終為零
- TFTSPI_Write_Byte(ys); //列起始坐標低8位
- TFTSPI_Write_Byte(0x00); //列終止坐標高8位,始終為零
- TFTSPI_Write_Byte(ye); //列終止坐標低8位
- TFTSPI_Write_Cmd(0x2C); //GRAM接收MCU數據或命令
- }
- /**********************************************************
- 函數名稱:TFTSPI_Addr_Rst()
- 入口參數:無
- 出口參數:無
- 時間:2018/1/26
- 功能說明:重置地址
- 其他說明:無
- **********************************************************/
- void TFTSPI_Addr_Rst(void)
- {
- TFTSPI_Write_Cmd(0x2a); //配置MCU可操作的LCD內部RAM橫坐標起始、結束參數
- TFTSPI_Write_Byte(0x00); //橫坐標起始地址0x0000
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00); //橫坐標結束地址0x007f(127)
- TFTSPI_Write_Byte(0xa8); //7f
- TFTSPI_Write_Cmd(0x2b); //配置MCU可操作的LCD內部RAM縱坐標起始結束參數
- TFTSPI_Write_Byte(0x00); //縱坐標起始地址0x0000
- TFTSPI_Write_Byte(0x00);
- TFTSPI_Write_Byte(0x00); //縱坐標結束地址0x009f(159)
- TFTSPI_Write_Byte(0xb3); //9f
- TFTSPI_Write_Cmd(0x2C); //GRAM接收MCU數據或命令
- }
- /*從第幾列開始填充某一行
- 例:TFTSPI_Show_line(0,5,WHITE); //填充第五行
- TFTSPI_Show_line(2,5,WHITE); //從第二個字符開始填充第五行
- */
- void TFTSPI_Show_Line(uint8_t xs,uint8_t ys,uint16_t color)
- {
- unsigned int i,j;
-
- //TFTSPI_Addr_Rst();
- //填充行
- TFTSPI_Set_Pos(xs*8,ys*16,TFT18W,(ys+1)*16);
- for(i=0;i<TFT18W;i++) //160
- {
- for(j=0;j<TFT18H;j++)
- {
- TFTSPI_Write_Word(color);
- }
- }
- }
- /*
- 【功能】 填充指定區域
- 【參數】 起始點x坐標,起始點y坐標,終止點x坐標,終止點有坐標,填充色
- 例:TFTSPI_Show_line(0,5,WHITE); //填充第五行
- TFTSPI_Show_line(2,5,WHITE); //從第二個字符開始填充第五行
- */
- void TFTSPI_Fill_Area(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye,uint16_t color)
- {
- unsigned int i,j;
-
- TFTSPI_Set_Pos(xs,ys,xe,ye);
- for(i=0;i<(xe-xs);i++)
- {
- for(j=0;j<(ye-ys);j++)
- {
- TFTSPI_Write_Word(color);
- }
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_CLS()
- 入口參數:color為常用的需要顯示的顏色(已經提前定義),color為
- NULL時,則可將不常用顏色放入dat中加以顯示
- 出口參數:無
- 時間:2012-09-09
- 功能說明:全屏顯示單色畫面
- 其他說明:無
- **********************************************************/
- void TFTSPI_CLS(uint16_t color)
- {
- unsigned int i,j;
-
- TFTSPI_Addr_Rst();
-
- for(i=0;i<TFT18W;i++) //160
- {
- for(j=0;j<TFT18H;j++)
- {
- TFTSPI_Write_Word(color);
- }
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_Draw_part()
- 入口參數:起始、終止橫坐標(0-127),縱坐標(0-159),顯示顏色uint16
- 出口參數:無
- 時間:2012-09-09
- 功能說明:填充矩形區域
- 其他說明:0<=xs<xe<=127
- 0<=ys<ye<=159
- **********************************************************/
- void TFTSPI_Draw_Part(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye,uint16_t color_dat)
- {
- uint8_t i,j;
-
- TFTSPI_Set_Pos(xs,ys,xe,ye);
- for(j=0;j<(ye-ys+1);j++)
- {
- for(i=0;i<(xe-xs+1);i++)
- {
- TFTSPI_Write_Word(color_dat);
- }
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_Draw_rectangle()
- 入口參數:起始、終止橫坐標(0-127),縱坐標(0-159),顯示顏色uint16
- 出口參數:無
- 時間:2012-09-09
- 功能說明:畫矩形邊框
- 其他說明:0<=xs<xe<=127
- 0<=ys<ye<=159
- **********************************************************/
- void TFTSPI_Draw_Rectangle(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye,uint16_t color_dat)
- {
- TFTSPI_Draw_Line(xs,ys,xs,ye,color_dat); //畫矩形左邊
- TFTSPI_Draw_Line(xe,ys,xe,ye,color_dat); //畫矩形右邊
- TFTSPI_Draw_Line(xs,ys,xe,ys,color_dat); //畫矩形上邊
- TFTSPI_Draw_Line(xs,ye,xe,ye,color_dat); //畫矩形下邊
- }
- /**********************************************************
- 函數名稱:TFTSPI_Draw_circle()
- 入口參數:圓心橫坐標(0-127),縱坐標(0-159),半徑(0-128),顯示顏色uint16
- 出口參數:無
- 時間:2012-09-09
- 功能說明:畫圓形邊框(僅支持屏幕內畫圓)
- 其他說明:0<=x<=127
- 0<=y<=159
- **********************************************************/
- void TFTSPI_Draw_Circle(uint8_t x,uint8_t y,uint8_t r,uint16_t color_dat)
- {
- unsigned char dx, dy = r;
-
- if((x>=r) &&((TFT18W-x)>=r) && (y>=r) && ((TFT18H-y)>=r)) //確定所畫圓在屏幕范圍內,沒有超出最外邊,(暫不支持與屏幕邊相交)
- {
- for(dx = 0; dx <= r; dx++)
- {
- while((r * r + 1 - dx * dx) < (dy * dy)) dy--;
- TFTSPI_Draw_Dot(x + dx, y - dy, color_dat);
- TFTSPI_Draw_Dot(x - dx, y - dy, color_dat);
- TFTSPI_Draw_Dot(x - dx, y + dy, color_dat);
- TFTSPI_Draw_Dot(x + dx, y + dy, color_dat);
-
- TFTSPI_Draw_Dot(x + dy, y - dx, color_dat);
- TFTSPI_Draw_Dot(x - dy, y - dx, color_dat);
- TFTSPI_Draw_Dot(x - dy, y + dx, color_dat);
- TFTSPI_Draw_Dot(x + dy, y + dx, color_dat);
- }
- }
- }
- /**********************************************************
- 函數名稱:TFTSPI_Draw_line()
- 入口參數:起始、終止橫坐標(0-127),縱坐標(0-159),顯示顏色uint16
- 出口參數:無
- 時間:2012-09-09
- 功能說明:畫直線
- 其他說明:0<=xs<xe<=127
- 0<=ys<ye<=159
- **********************************************************/
- void TFTSPI_Draw_Line(uint8_t xs,uint8_t ys,uint8_t xe,uint8_t ye,uint16_t color_dat)
- {
- uint8_t i,ds;
- int dx,dy,inc_x, inc_y;
- int xerr = 0, yerr = 0; //初始化變量
-
- if(xs==xe) //如果是畫垂直線則只需對豎直坐標計數
- {
- TFTSPI_Set_Pos(xs,ys,xe,ye);
- for(i=0;i<(ye-ys+1);i++)
- {
- TFTSPI_Write_Word(color_dat);
- }
- }
- else if(ys==ye) //如果是水平線則只需要對水平坐標計數
- {
- TFTSPI_Set_Pos(xs,ys,xe,ye);
- for(i=0;i<(xe-xs+1);i++)
- {
- TFTSPI_Write_Word(color_dat);
- }
- }
- else //如果是斜線,則重新計算,使用畫點函數畫出直線
- {
- dx = xe - xs; //計算坐標增量
- dy = ye - ys;
-
- if(dx > 0) inc_x = 1; //設置單步方向
- else
- {
- inc_x = -1; dx = -dx;
- }
- if(dy > 0) inc_y = 1; //設置單步方向
- else
- {
- inc_y = -1; dy = -dy;
- }
-
- if(dx > dy) ds = dx; //選取基本增量坐標軸
- else ds = dy;
-
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
TFT1.8_SPI代碼移植到STM32.7z
(189.33 KB, 下載次數: 25)
2019-9-24 15:24 上傳
點擊文件名下載附件
龍邱tft代碼自己移植無錯誤警告 下載積分: 黑幣 -5
|