基本概念
基于ILI3193驱动芯片的SPI屏,移植自龙邱的TC264。去除了摄像头相关的无用程序,使代码量更简洁。
目前是用的STC32驱动的。可通过更改宏定义来自行移动到其他单片机。
注意事项
- 修改头文件下#define
- 修改C文件下lq_tft_delayms
程序源码
.H文件
#ifndef SPI_TFT_H
#define SPI_TFT_H
#include "STC32G_GPIO.h"
#define TFT18W 162
#define TFT18H 132
#define u16RED 0xf800
#define u16GREEN 0x07e0
#define u16BLUE 0x001f
#define u16PURPLE 0xf81f
#define u16YELLOW 0xffe0
#define u16CYAN 0x07ff //蓝绿色
#define u16ORANGE 0xfc08
#define u16BLACK 0x0000
#define u16WHITE 0xffff
#define TFTSPI_GPIO_Port GPIO_P2 //GPIO_P6
#define TFTSPI_SCL_PIN GPIO_Pin_5 //GPIO_Pin_4
#define TFTSPI_SDI_PIN GPIO_Pin_3 //GPIO_Pin_3
#define TFTSPI_RST_PIN GPIO_Pin_0 //GPIO_Pin_2 //液晶复位引脚定义
#define TFTSPI_DC_PIN GPIO_Pin_1 //GPIO_Pin_1 //液晶命令位引脚定义
#define TFTSPI_CS_PIN GPIO_Pin_2 //GPIO_Pin_0 //定义SPI_CS引脚
#define TFTSPI_SCK P25 //P25 = x //P64 = x
#define TFTSPI_SDI P23 //P23 = x //P63 = x
#define TFTSPI_RST P20 //P20 = x //P62 = x
#define TFTSPI_DC P21 //P21 = x //P61 = x
#define TFTSPI_CS P22 //P22 = x //P60 = x
#define TFTSPI_CS_H TFTSPI_CS = 1 /*!< CS管脚 */
#define TFTSPI_SCK_H TFTSPI_SCK = 1 /*!< SCL管脚 */
#define TFTSPI_SDI_H TFTSPI_SDI = 1 /*!< SDI管脚 */
#define TFTSPI_DC_H TFTSPI_DC = 1 /*!< DC管脚 */
#define TFTSPI_RST_H TFTSPI_RST = 1 /*!< RST管脚 */
#define TFTSPI_CS_L TFTSPI_CS = 0 /*!< CS管脚 */
#define TFTSPI_SCK_L TFTSPI_SCK = 0 /*!< SCL管脚 */
#define TFTSPI_SDI_L TFTSPI_SDI = 0 /*!< SDI管脚 */
#define TFTSPI_DC_L TFTSPI_DC = 0 /*!< DC管脚 */
#define TFTSPI_RST_L TFTSPI_RST = 0 /*!< RST管脚 */
void TFTSPI_Init(unsigned char type);
void TFTSPI_Write_Cmd(unsigned char cmd);
void TFTSPI_Write_Byte(unsigned char dat);
void TFTSPI_Write_Word(unsigned short dat);
void TFTSPI_Set_Pos(unsigned char xs,unsigned char ys,unsigned char xe,unsigned char ye);
void TFTSPI_Fill_Area(unsigned char xs,unsigned char ys,unsigned char xe,unsigned char ye,unsigned short color);
void TFTSPI_CLS(unsigned short color);
void TFTSPI_P8X8Str(unsigned char x, unsigned char y, char *s_dat,unsigned short word_color,unsigned short back_color);
#endif
.C文件
#include "spi_TFT18.h"
/*字体: 8*6字体
取码规则: 低位在前, 列行扫描, 阴码(1-亮, 0-灭)
查表方法: 要显示的ASCII码-32就可以得到字库码的指针 */
const unsigned char Font_code8[][6] = {
{0x00,0x00,0x00,0x00,0x00,0x00},// (0)
{0x00,0x00,0x00,0x4F,0x00,0x00},//!(1)
{0x00,0x00,0x07,0x00,0x07,0x00},//"(2)
{0x00,0x14,0x7F,0x14,0x7F,0x14},//#(3)
{0x00,0x24,0x2A,0x7F,0x2A,0x12},//$(4)
{0x00,0x23,0x13,0x08,0x64,0x62},//%(5)
{0x00,0x36,0x49,0x55,0x22,0x50},//&(6)
{0x00,0x00,0x05,0x03,0x00,0x00},//'(7)
{0x00,0x00,0x1C,0x22,0x41,0x00},//((8)
{0x00,0x00,0x41,0x22,0x1C,0x00},//)(9)
{0x00,0x14,0x08,0x3E,0x08,0x14},//*(10)
{0x00,0x08,0x08,0x3E,0x08,0x08},//+(11)
{0x00,0x00,0x50,0x30,0x00,0x00},//,(12)
{0x00,0x08,0x08,0x08,0x08,0x08},//-(13)
{0x00,0x00,0x60,0x60,0x00,0x00},//.(14)
{0x00,0x20,0x10,0x08,0x04,0x02},///(15)
{0x00,0x3E,0x51,0x49,0x45,0x3E},//0(16)
{0x00,0x00,0x42,0x7F,0x40,0x00},//1(17)
{0x00,0x42,0x61,0x51,0x49,0x46},//2(18)
{0x00,0x21,0x41,0x45,0x4B,0x31},//3(19)
{0x00,0x18,0x14,0x12,0x7F,0x10},//4(20)
{0x00,0x27,0x45,0x45,0x45,0x39},//5(21)
{0x00,0x3C,0x4A,0x49,0x49,0x30},//6(22)
{0x00,0x01,0x71,0x09,0x05,0x03},//7(23)
{0x00,0x36,0x49,0x49,0x49,0x36},//8(24)
{0x00,0x06,0x49,0x49,0x29,0x1E},//9(25)
{0x00,0x00,0x36,0x36,0x00,0x00},//:(26)
{0x00,0x00,0x56,0x36,0x00,0x00},//;(27)
{0x00,0x08,0x14,0x22,0x41,0x00},//<(28)
{0x00,0x14,0x14,0x14,0x14,0x14},//=(29)
{0x00,0x00,0x41,0x22,0x14,0x08},//>(30)
{0x00,0x02,0x01,0x51,0x09,0x06},//?(31)
{0x00,0x32,0x49,0x79,0x41,0x3E},//@(32)
{0x00,0x7E,0x11,0x11,0x11,0x7E},//A(33)
{0x00,0x7F,0x49,0x49,0x49,0x3E},//B(34)
{0x00,0x3E,0x41,0x41,0x41,0x22},//C(35)
{0x00,0x7F,0x41,0x41,0x22,0x1C},//D(36)
{0x00,0x7F,0x49,0x49,0x49,0x41},//E(37)
{0x00,0x7F,0x09,0x09,0x09,0x01},//F(38)
{0x00,0x3E,0x41,0x49,0x49,0x7A},//G(39)
{0x00,0x7F,0x08,0x08,0x08,0x7F},//H(40)
{0x00,0x00,0x41,0x7F,0x41,0x00},//I(41)
{0x00,0x20,0x40,0x41,0x3F,0x01},//J(42)
{0x00,0x7F,0x08,0x14,0x22,0x41},//K(43)
{0x00,0x7F,0x40,0x40,0x40,0x40},//L(44)
{0x00,0x7F,0x02,0x04,0x02,0x7F},//M(45)
{0x00,0x7F,0x04,0x08,0x10,0x7F},//N(46)
{0x00,0x3E,0x41,0x41,0x41,0x3E},//O(47)
{0x00,0x7F,0x09,0x09,0x09,0x06},//P(48)
{0x00,0x3E,0x41,0x51,0x21,0x5E},//Q(49)
{0x00,0x7F,0x09,0x19,0x29,0x46},//R(50)
{0x00,0x46,0x49,0x49,0x49,0x31},//S(51)
{0x00,0x01,0x01,0x7F,0x01,0x01},//T(52)
{0x00,0x3F,0x40,0x40,0x40,0x3F},//U(53)
{0x00,0x1F,0x20,0x40,0x20,0x1F},//V(54)
{0x00,0x3F,0x40,0x38,0x40,0x3F},//W(55)
{0x00,0x63,0x14,0x08,0x14,0x63},//X(56)
{0x00,0x03,0x04,0x78,0x04,0x03},//Y(57)
{0x00,0x61,0x51,0x49,0x45,0x43},//Z(58)
{0x00,0x00,0x7F,0x41,0x41,0x00},//[(59)
{0x00,0x15,0x16,0x7C,0x16,0x15},//\(60)
{0x00,0x00,0x41,0x41,0x7F,0x00},//](61)
{0x00,0x04,0x02,0x01,0x02,0x04},//^(62)
{0x00,0x40,0x40,0x40,0x40,0x40},//_(63)
{0x00,0x00,0x01,0x02,0x04,0x00},//`(64)
{0x00,0x20,0x54,0x54,0x54,0x78},//a(65)
{0x00,0x7F,0x48,0x44,0x44,0x38},//b(66)
{0x00,0x38,0x44,0x44,0x44,0x20},//c(67)
{0x00,0x38,0x44,0x44,0x48,0x7F},//d(68)
{0x00,0x38,0x54,0x54,0x54,0x18},//e(69)
{0x00,0x08,0x7E,0x09,0x01,0x02},//f(70)
{0x00,0x0C,0x52,0x52,0x52,0x3E},//g(71)
{0x00,0x7F,0x08,0x04,0x04,0x78},//h(72)
{0x00,0x00,0x44,0x7D,0x40,0x00},//i(73)
{0x00,0x20,0x40,0x44,0x3D,0x00},//j(74)
{0x00,0x7F,0x10,0x28,0x44,0x00},//k(75)
{0x00,0x00,0x41,0x7F,0x40,0x00},//l(76)
{0x00,0x7E,0x02,0x0C,0x02,0x7C},//m(77)
{0x00,0x7E,0x04,0x02,0x02,0x7C},//n(78)
{0x00,0x38,0x44,0x44,0x44,0x38},//o(79)
{0x00,0x7C,0x14,0x14,0x14,0x08},//p(80)
{0x00,0x08,0x14,0x14,0x18,0x7C},//q(81)
{0x00,0x7C,0x08,0x04,0x04,0x08},//r(82)
{0x00,0x48,0x54,0x54,0x54,0x20},//s(83)
{0x00,0x04,0x3F,0x44,0x40,0x20},//t(84)
{0x00,0x3C,0x40,0x40,0x20,0x7C},//u(85)
{0x00,0x1C,0x20,0x40,0x20,0x1C},//v(86)
{0x00,0x3C,0x40,0x30,0x40,0x3C},//w(87)
{0x00,0x44,0x28,0x10,0x28,0x44},//x(88)
{0x00,0x0C,0x50,0x50,0x50,0x3C},//y(89)
{0x00,0x44,0x64,0x54,0x4C,0x44},//z(90)
{0x00,0x00,0x08,0x36,0x41,0x00},//{(91)
{0x00,0x00,0x00,0x7F,0x00,0x00},//|(92)
{0x00,0x00,0x41,0x36,0x08,0x00},//}(93)
{0x00,0x08,0x04,0x08,0x10,0x08},//~(94)
{0x00,0x08,0x08,0x2A,0x1C,0x08},//→(127)
{0x00,0x08,0x1C,0x2A,0x08,0x08},//←(128)
{0x00,0x04,0x02,0x7F,0x02,0x04},//↑(129)
{0x00,0x10,0x20,0x7F,0x20,0x10},//↓(130)
{0x00,0x1C,0x2A,0x32,0x2A,0x1C},//笑面(131)
{0x00,0x1C,0x22,0x44,0x22,0x1C}};//爱心(132)
static void lq_tft_delayms(unsigned short ms)
{
volatile unsigned long i = 0;
while(ms--)
{
_nop_();
_nop_();
_nop_();
i = 8748UL;
while (i) i--;
}
}
void tft18delay_1us(unsigned int Del) //
{
unsigned long i;
while (Del--)
{
_nop_();
_nop_();
i = 7UL;
while (i) i--;
};
}
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数据或命令
}
/*!
* @brief TFT18初始化
*
* @param type : 0:横屏 1:竖屏
*
* @return 无
*
* @note 如果修改管脚 需要修改初始化的管脚
*
* @see TFTSPI_Init(1);
*
* @date 2019/6/13 星期四
*/
void TFTSPI_Init(unsigned char type)
{
GPIO_InitTypeDef my_GPIO_Init;
my_GPIO_Init.Pin = TFTSPI_SCL_PIN|TFTSPI_SDI_PIN|TFTSPI_RST_PIN|TFTSPI_DC_PIN|TFTSPI_CS_PIN;
my_GPIO_Init.Mode = GPIO_OUT_PP;
GPIO_Inilize(TFTSPI_GPIO_Port,&my_GPIO_Init);
TFTSPI_SCK =0;
TFTSPI_SDI =0;
TFTSPI_RST =0;
TFTSPI_DC =0;
TFTSPI_CS =0;
TFTSPI_RST_L;
lq_tft_delayms(50);
TFTSPI_RST_H;
lq_tft_delayms(50);
TFTSPI_Write_Cmd(0x11); //关闭睡眠,振荡器工作
lq_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(0x9f); //9f
TFTSPI_Write_Cmd(0x36); //配置MPU和DDRAM对应关系
if(type == 0)
TFTSPI_Write_Byte(0xA0); //横屏显示
else if(type == 1)
TFTSPI_Write_Byte(0xC0); //竖屏 正显示 //MX=1,MY=1
else if(type == 2)
TFTSPI_Write_Byte(0x00); //竖屏 倒屏显示
TFTSPI_Write_Cmd(0xb7); //LCD Driveing control
TFTSPI_Write_Byte(0x00); //CRL=0
TFTSPI_Write_Cmd(0x29); //开启屏幕显示
TFTSPI_Write_Cmd(0x2c); //设置为LCD接收数据/命令模式
}
void TFTSPI_Write_Cmd(unsigned char cmd)
{
unsigned char i;
TFTSPI_DC_L;// A0=0 ILI9163_A0=0; //A0=0发送命令
for(i=0;i<8;i++)
{
TFTSPI_SCK_L;/*tft18delay_1us(1); */// SCK=0 ILI9163_SCK=0;
if(cmd&0x80){TFTSPI_SDI_H;}// SDI=1
else{TFTSPI_SDI_L;}// SDI=0
TFTSPI_SCK_H;/*tft18delay_1us(1); */// SCK=1 ILI9163_SCK = 1;
cmd = (cmd<<1);
}
}
void TFTSPI_Write_Byte(unsigned char dat)
{
unsigned char i;
TFTSPI_DC_H;// A0=1 ILI9163_A0=1; //A0=1发送数据
for(i=0;i<8;i++)
{
TFTSPI_SCK_L;/*tft18delay_1us(1);*/ // SCK=0 ILI9163_SCK=0;
if(dat&0x80){TFTSPI_SDI_H;}// SDI=1
else{TFTSPI_SDI_L;}// SDI=0
TFTSPI_SCK_H;/*tft18delay_1us(1);*/ // SCK=1 ILI9163_SCK = 1;
dat = (dat<<1);
}
}
void TFTSPI_P8X8(unsigned char x, unsigned char y, unsigned char c_dat,unsigned short word_color,unsigned short back_color)
{
unsigned char i,j;
TFTSPI_Set_Pos(x*8,y*8,(x+1)*8-1,(y+1)*8-1); //定位字符显示区域
for(j=0; j<8; j++)
{
TFTSPI_Write_Word(back_color);
for(i=0; i<6; i++)
{
if((Font_code8[c_dat-32][i]) & (0x01<<j))
TFTSPI_Write_Word(word_color);
else
TFTSPI_Write_Word(back_color);
}
TFTSPI_Write_Word(back_color);
}
}
/*!
* @brief 全屏显示单色画面
*
* @param color :填充的颜色
*
* @return 无
*
* @note 起始、终止横坐标(0-127),纵坐标(0-159),显示颜色uint16
*
* @see TFTSPI_CLS(u16YELLOW);
*
* @date 2019/6/13 星期四
*/
void TFTSPI_CLS(unsigned short 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);
}
}
}
void TFTSPI_Write_Word(unsigned short dat)
{
unsigned char i;
TFTSPI_DC_H;// A0=1 ILI9163_A0=1;
for(i=0;i<16;i++)
{
TFTSPI_SCK_L;/*tft18delay_1us(1);*/ // SCK=0 ILI9163_SCK=0;
if(dat&0x8000){ TFTSPI_SDI_H;}// SDI=1
else{TFTSPI_SDI_L;}// SDI=0
TFTSPI_SCK_H;/*tft18delay_1us(1); */// SCK=1 ILI9163_SCK=1;
dat<<=1;
}
}
/*!
* @brief 填充指定区域
*
* @param xs :起始x
* @param ys :起始y
* @param xe :结束x
* @param ys :结束y
* @param color :填充的颜色
*
* @return 无
*
* @note 起始、终止横坐标(0-127),纵坐标(0-159),显示颜色uint16
*
* @see TFTSPI_Fill_Area(10, 20, 30, 40, u16YELLOW);
*
* @date 2019/6/13 星期四
*/
void TFTSPI_Fill_Area(unsigned char xs,unsigned char ys,unsigned char xe,unsigned char ye,unsigned short 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);
}
}
}
/*!
* @brief 液晶字符串输出(8*8字体)
*
* @param x:0 - 15 (行)
* @param y:0 - 19 (列)
* @param word_color: 字体颜色
* @param back_color: 背景颜色
*
* @return 无
*
* @note 无
*
* @see TFTSPI_P8X8Str(1, 1, "123456", u16YELLOW, u16RED);
*
* @date 2019/6/13 星期四
*/
void TFTSPI_P8X8Str(unsigned char x, unsigned char y, char *s_dat,unsigned short word_color,unsigned short back_color)
{
while(*s_dat) TFTSPI_P8X8(x++, y, *s_dat++,word_color,back_color);
}
void TFTSPI_Set_Pos(unsigned char xs,unsigned char ys,unsigned char xe,unsigned char 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数据或命令
}
示例
int main(void)
{
TFTSPI_Init(0);
TFTSPI_CLS(u16BLACK); //黑色屏幕底色
TFTSPI_P8X8Str(0,1,'TEST',u16RED,u16BLACK);
}
标签:0x08,0x00,Write,Byte,驱动,TFT,屏幕,TFTSPI,define
From: https://www.cnblogs.com/songmingze/p/17609786.html