完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我看网上的都不全,这次我给大家发个全的
我查了好多资料,感觉没有什么问题 毕竟写个SPI时序就能打点 可他就是不显示我打的点 一直白屏 跪求大佬帮我看看 感谢至极 一、这是tft.c文件 #include “tft.h” #include “sys.h” #include “delay.h” #include “stm32f10x.h” #include “spi.h” //向SPI写一个8位数据 //u8 SPI2_WriteData(u8 qq) //{ // unsigned char i=0; // LCD_CS_SET; delay_us(20); // for(i=8;i>0;i–) // { // // if(qq&0x80) // LCD_SDA=1; //数据线拉高,输出数据 // else LCD_SDA=0; // LCD_SCL=0; // LCD_SCL=1; // qq<<=1; // } //} void Lcd_WriteIndex(u8 Index) //写命令 { u8 i=8; LCD_CS=0; LCD_RS=0; LCD_SCL=0; while(i--) { if(Index&0x80){LCD_SDA=1;} else{LCD_SDA=0;} LCD_SCL=1; LCD_SCL=0; Index<<=1; } LCD_CS=1; } void Lcd_WriteData(u8 aa) //写数据 { // LCD_CS=0; //CLR是低电平 // LCD_RS=1; //SET是高电平 // SPI2_WriteData(aa); // LCD_CS=1; u8 i=8; LCD_CS=0; LCD_RS=1; LCD_SCL=0; while(i--) { if(aa&0x80){LCD_SDA=1;} else{LCD_SDA=0;} LCD_SCL=1; LCD_SCL=0; aa<<=1; } LCD_CS=1; } //向液晶屏写一个16位数据 void Lcd_WriteData_16Bit(u16 dd) { LCD_CS=0; LCD_RS=1; SPI2_WriteData(dd>>8); //写入高8位数据 SPI2_WriteData(dd); //写入低8位数据 LCD_CS=1; } //复位 void Lcd_Reset(void) { LCD_RST=0; delay_ms(100); LCD_RST=1; delay_ms(100); } /************************************************* 设置lcd显示区域,在此区域写点数据自动换行 ******************************/ void Lcd_SetRegion(u16 x_start,u16 y_start,u16 x_end,u16 y_end) { Lcd_WriteIndex(0x2a); Lcd_WriteData(0x00); Lcd_WriteData(x_start+2); Lcd_WriteData(0x00); Lcd_WriteData(x_end+2); Lcd_WriteIndex(0x2b); Lcd_WriteData(0x00); Lcd_WriteData(y_start+1); Lcd_WriteData(0x00); Lcd_WriteData(y_end+1); Lcd_WriteIndex(0x2c); } //------------------------------------------------------------------------------------------------------------------- // @brief 液晶初始化 // @return void // @since v1.0 // Sample usage: //------------------------------------------------------------------------------------------------------------------- void lcd_init(void) { Lcd_Reset(); //Reset before LCD Init. delay_ms(10); Lcd_WriteIndex(0x11); delay_ms(120); Lcd_WriteIndex(0xB1); Lcd_WriteData(0x01); Lcd_WriteData(0x2C); Lcd_WriteData(0x2D); Lcd_WriteIndex(0xB2); Lcd_WriteData(0x01); Lcd_WriteData(0x2C); Lcd_WriteData(0x2D); Lcd_WriteIndex(0xB3); Lcd_WriteData(0x01); Lcd_WriteData(0x2C); Lcd_WriteData(0x2D); Lcd_WriteData(0x01); Lcd_WriteData(0x2C); Lcd_WriteData(0x2D); Lcd_WriteIndex(0xB4); Lcd_WriteData(0x07); Lcd_WriteIndex(0xC0); Lcd_WriteData(0xA2); Lcd_WriteData(0x02); Lcd_WriteData(0x84); Lcd_WriteIndex(0xC1); Lcd_WriteData(0xC5); Lcd_WriteIndex(0xC2); Lcd_WriteData(0x0A); Lcd_WriteData(0x00); Lcd_WriteIndex(0xC3); Lcd_WriteData(0x8A); Lcd_WriteData(0x2A); Lcd_WriteIndex(0xC4); Lcd_WriteData(0x8A); Lcd_WriteData(0xEE); Lcd_WriteIndex(0xC5); Lcd_WriteData(0x0E); Lcd_WriteIndex(0x36); // switch(TFT_DISPLAY_DIR)//y x v // { // case 0: Lcd_WriteData(1<<7 | 1<<6 | 0<<5); break; //竖屏模式 // case 1: Lcd_WriteData(0<<7 | 0<<6 | 0<<5); break; //竖屏模式 旋转180 // case 2: Lcd_WriteData(1<<7 | 0<<6 | 1<<5); break; //横屏模式 // case 3: Lcd_WriteData(0<<7 | 1<<6 | 1<<5); break; //横屏模式 旋转180 // } Lcd_WriteIndex(0xe0); Lcd_WriteData(0x0f); Lcd_WriteData(0x1a); Lcd_WriteData(0x0f); Lcd_WriteData(0x18); Lcd_WriteData(0x2f); Lcd_WriteData(0x28); Lcd_WriteData(0x20); Lcd_WriteData(0x22); Lcd_WriteData(0x1f); Lcd_WriteData(0x1b); Lcd_WriteData(0x23); Lcd_WriteData(0x37); Lcd_WriteData(0x00); Lcd_WriteData(0x07); Lcd_WriteData(0x02); Lcd_WriteData(0x10); Lcd_WriteIndex(0xe1); Lcd_WriteData(0x0f); Lcd_WriteData(0x1b); Lcd_WriteData(0x0f); Lcd_WriteData(0x17); Lcd_WriteData(0x33); Lcd_WriteData(0x2c); Lcd_WriteData(0x29); Lcd_WriteData(0x2e); Lcd_WriteData(0x30); Lcd_WriteData(0x30); Lcd_WriteData(0x39); Lcd_WriteData(0x3f); Lcd_WriteData(0x00); Lcd_WriteData(0x07); Lcd_WriteData(0x03); Lcd_WriteData(0x10); Lcd_WriteIndex(0x2a); Lcd_WriteData(0x00); Lcd_WriteData(0x00+2); Lcd_WriteData(0x00); Lcd_WriteData(0x80+2); Lcd_WriteIndex(0x2b); Lcd_WriteData(0x00); Lcd_WriteData(0x00+3); Lcd_WriteData(0x00); Lcd_WriteData(0x80+3); Lcd_WriteIndex(0xF0); Lcd_WriteData(0x01); Lcd_WriteIndex(0xF6); Lcd_WriteData(0x00); Lcd_WriteIndex(0x3A); Lcd_WriteData(0x05); Lcd_WriteIndex(0x29); dsp_single_colour(BLACK); } //设置整个屏幕的颜色 void dsp_single_colour(int color) { u8 i,j; Lcd_SetRegion(0,0,128-1,128-1); for (i=0;i<127;i++) for (j=0;j<127;j++) Lcd_WriteData_16Bit(color); } //画点 void lcd_drawpoint(u16 x,u16 y,u16 color) { Lcd_SetRegion(x,y,x,y); Lcd_WriteData_16Bit(color); } void Lcd_WriteReg(u8 Cmd,u8 Data) { Lcd_WriteIndex(Cmd); Lcd_WriteData(Data); } 二、tft.h文件 #ifndef _TFT_H #define _TFT_H #include “stm32f10x.h” #include “spi.h” //-------常用颜色---------- #define RED 0XF800 //红色 #define GREEN 0X07E0 //绿色 #define BLUE 0X001F //蓝色 #define BRED 0XF81F #define GRED 0XFFE0 //灰色 #define GBLUE 0X07FF // #define BLACK 0X0000 //黑色 #define WHITE 0XFFFF //白色 #define YELLOW 0xFFE0 //黄色 //#define LCD_CTRLA GPIOA //定义TFT数据端口 //#define LCD_CTRLB GPIOB //定义TFT数据端口 //PA4 PA5 PA7 //#define LCD_SCL GPIO_Pin_13 //PB13—>>TFT --SCL/SCK //#define LCD_SDA GPIO_Pin_15 //PB15 MOSI—>>TFT --SDA/DIN //#define LCD_CS GPIO_Pin_4 //MCU_PB11—>>TFT --CS/CE PB10 PB1 PB0 //#define LCD_LED GPIO_Pin_10 //MCU_PB9—>>TFT --BL //#define LCD_RS GPIO_Pin_1 //PB11—>>TFT --RS/DC DC数据命令位 //#define LCD_RST GPIO_Pin_0 //PB10—>>TFT --RST //#define LCD_CS_SET(x) LCD_CTRL->ODR=(LCD_CTRL->ODR&~LCD_CS)|(x ? LCD_CS:0) #define LCD_SCL PBout(13) #define LCD_SDA PBout(15) #define LCD_CS PAout(4) //片选 #define LCD_RS PBout(1) //时钟/数据 #define LCD_RST PBout(0) 液晶控制口置1操作语句宏定义 //#define LCD_SCL_SET LCD_CTRLB->BSRR=LCD_SCL //#define LCD_SDA_SET LCD_CTRLB->BSRR=LCD_SDA //#define LCD_CS_SET LCD_CTRLA->BSRR=LCD_CS // //#define LCD_LED_SET LCD_CTRLB->BSRR=LCD_LED //#define LCD_RS_SET LCD_CTRLB->BSRR=LCD_RS //#define LCD_RST_SET LCD_CTRLB->BSRR=LCD_RST 液晶控制口置0操作语句宏定义 //#define LCD_SCL_CLR LCD_CTRLA->BRR=LCD_SCL //#define LCD_SDA_CLR LCD_CTRLA->BRR=LCD_SDA //#define LCD_CS_CLR LCD_CTRLA->BRR=LCD_CS // //#define LCD_LED_CLR LCD_CTRLB->BRR=LCD_LED //#define LCD_RST_CLR LCD_CTRLB->BRR=LCD_RST //#define LCD_RS_CLR LCD_CTRLB->BRR=LCD_RS //#define LCD_DATAOUT(x) LCD_DATA->ODR=x; //数据输出 //#define LCD_DATAIN LCD_DATA->IDR; //数据输入 u8 SPI2_WriteData(u8 qq); void Lcd_WriteIndex(u8 Index); void Lcd_WriteData(u8 aa); void Lcd_WriteData_16Bit(u16 dd); void Lcd_Reset(void); void Lcd_SetRegion(u16 x_start,u16 y_start,u16 x_end,u16 y_end); void lcd_init(void); void lcd_drawpoint(u16 x,u16 y,u16 color); void dsp_single_colour(int color); void Lcd_WriteReg(u8 Cmd,u8 Data); #endif 三、spi.c文件 #include “spi.h” #include “tft.h” #include “delay.h” //以下是SPI模块的初始化代码,配置成主机模式,访问SD Card/W25Q64/NRF24L01 //SPI口初始化 //这里针是对SPI2的初始化 //初始化IO口B12、B14、B15 void SPI2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE );//PB时钟使能 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_15|GPIO_Pin_10 | GPIO_Pin_0 | GPIO_Pin_1;//GPIO_Pin_13 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB // GPIO_SetBits(GPIOB,GPIO_Pin_13 |GPIO_Pin_15|GPIO_Pin_10 | GPIO_Pin_0 | GPIO_Pin_1); //PB13/14/15上拉//GPIO_Pin_13| GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_13 |GPIO_Pin_15;//GPIO_Pin_13 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //PB13/14/15复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOB GPIO_SetBits(GPIOA,GPIO_Pin_4); //PB13/14/15上拉//GPIO_Pin_13| GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_15|GPIO_Pin_10 | GPIO_Pin_0 | GPIO_Pin_1;//GPIO_Pin_13 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIOB GPIO_SetBits(GPIOD,GPIO_Pin_13 |GPIO_Pin_15|GPIO_Pin_10 | GPIO_Pin_0 | GPIO_Pin_1); //PB13/14/15上拉//GPIO_Pin_13| RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步时钟的空闲状态为低电平 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步时钟的第1个跳变沿(上升或下降)数据被采样 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器 SPI_Cmd(SPI2, ENABLE); //使能SPI外设 SPI2_WriteData(0xFF);//启动传输 } SPIx 读写一个字节 TxData:要写入的字节 返回值:读取到的字节 //u8 SPI2_WriteData(u8 TxData) //{ // u8 retry=0; // // while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位 // { // retry++; // if(retry>200)return 0; // } // // SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个数据 // // retry=0; // // while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位 // { // // retry++; // // if(retry>200)return 0; // } // // return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据 //} uint8_t SPI2_WriteData(u8 byte) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI2,byte); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(SPI2); } 向SPI写一个8位数据 //u8 SPI2_WriteData(u8 Data) //{ // unsigned char i=0; // LCD_CS_SET; delay_us(20); // for(i=8;i>0;i–) // { // // if(Data&0x80) // // LCD_SDA=1; //数据线拉高,输出数据 // else LCD_SDA=0; // // delay_us(20); // LCD_SCL=0; delay_us(20); // // LCD_SCL=1; // Data<<=1; // } //} 四、spi.h文件 #ifndef __SPI_H #define __SPI_H #include “sys.h” void SPI2_Init(void); //初始化SPI口 //u8 SPI2_WriteData(u8 Data); uint8_t SPI2_WriteData(u8 byte); #endif 五、主函数 #include “stm32f10x.h” #include “spi.h” #include “tft.h” #include “delay.h” #include “usart.h” #include “key.h” int main(void) { delay_init(); SPI2_Init(); Lcd_Reset(); uart_init(9600); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); KEY_Init(); lcd_init(); lcd_drawpoint(10,10,BLACK); lcd_drawpoint(12,10,BLACK); while(1) { lcd_drawpoint(13,10,BLACK); lcd_drawpoint(14,10,BLACK); lcd_drawpoint(15,10,BLACK); Lcd_WriteData(0xfe); if(!KEY0) { // dsp_single_colour(GREEN); Lcd_WriteData(0xfe); Lcd_WriteData(1); Lcd_WriteData(1); Lcd_WriteData(1); Lcd_WriteData(1); Lcd_WriteData(1); Lcd_WriteData(1); printf(“222”); delay_ms(50); } } } |
|
|
|
只有小组成员才能发言,加入小组>>
3294 浏览 9 评论
2970 浏览 16 评论
3473 浏览 1 评论
9023 浏览 16 评论
4061 浏览 18 评论
1140浏览 3评论
589浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
579浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2313浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1876浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-4 00:51 , Processed in 1.424871 second(s), Total 78, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号