最近经理通知要做项目了,让我选型一个LCD开始试着做下。这是我用仿真实现的一个smt32的硬件SPI1来驱动的ST7735R,难度挺小的,因为大部分的代码LCD的厂商已经给我们提供了,我们主要修改成硬件SPI来驱动就好了。 此次仿真上面有2个问题,不知道是代码的问题还是仿真图上的问题。第一个问题是仿真运行时有时会出通信数据传输问题,导致指令越界报警,这个可能是SPI不稳定导致的;第二个问题是在改用SPI2或者SPI3来驱动没有任何反应,猜测是代码时钟没开对或者仿真图上面还要加上具体晶振。 代码工程和仿真工程都已压缩打包,可放心下载。。。
先贴上仿真实现图:
再贴上部分代码:
/**ST7735驱动**/ #include "ST7735.h" #include "usart.h" u16 BACK_COLOR, POINT_COLOR; //背景色,画笔色
void WriteCommand_7735(u8 CmdData) //写指令 { SPI_LCD_CS_LOW; //片选 SPI_LCD_COMMAND_W;//写指令 SPI_I2S_SendData(LCD_SPIx,CmdData); while (SPI_I2S_GetFlagStatus(LCD_SPIx, SPI_I2S_FLAG_TXE) == RESET){} SPI_LCD_CS_HIGH; }
void WriteDate_7735(u8 Data) //写8位数据 { SPI_LCD_CS_LOW; //片选 SPI_LCD_DATA_W;//写数据 SPI_I2S_SendData(LCD_SPIx,Data); while(SPI_I2S_GetFlagStatus(LCD_SPIx, SPI_I2S_FLAG_TXE) == RESET){} SPI_LCD_CS_HIGH; }
void WriteDate16(int data) //写16位数据 { WriteDate_7735(data>>8); WriteDate_7735(data); }
void LCD_Init(void) { LCD_RES_HIGH; delay_ms(5); LCD_RES_LOW; delay_ms(5); LCD_RES_HIGH; SPI_LCD_CS_HIGH; delay_ms(5); SPI_LCD_CS_LOW; //打开片选使能
WriteCommand_7735(0x11); //Sleep out delay_ms(120); //Delay 120ms //------------------------------------ST7735S Frame Rate-----------------------------------------// WriteCommand_7735(0xB1); WriteDate_7735(0x05);
WriteCommand_7735(0x11);//Sleep exit delay_ms (120); //ST7735R Frame Rate WriteCommand_7735(0xB1); WriteDate_7735(0x01);
WriteDate_7735(0x2C); WriteDate_7735(0x2D); WriteCommand_7735(0xB2); WriteDate_7735(0x01); WriteDate_7735(0x2C); WriteDate_7735(0x2D); WriteCommand_7735(0xB3); WriteDate_7735(0x01); WriteDate_7735(0x2C); WriteDate_7735(0x2D); WriteDate_7735(0x01); WriteDate_7735(0x2C); WriteDate_7735(0x2D);
WriteCommand_7735(0xB4); //Column inversion WriteDate_7735(0x07); //ST7735R Power Sequence WriteCommand_7735(0xC0); WriteDate_7735(0xA2); WriteDate_7735(0x02); WriteDate_7735(0x84); WriteCommand_7735(0xC1); WriteDate_7735(0xC5); WriteCommand_7735(0xC2); WriteDate_7735(0x0A); WriteDate_7735(0x00); WriteCommand_7735(0xC3); WriteDate_7735(0x8A); WriteDate_7735(0x2A); WriteCommand_7735(0xC4); WriteDate_7735(0x8A); WriteDate_7735(0xEE); WriteCommand_7735(0xC5); //VCOM WriteDate_7735(0x0E); WriteCommand_7735(0x36); //MX, MY, RGB mode WriteDate_7735(0xC8);
//ST7735R Gamma Sequence WriteCommand_7735(0xe0); WriteDate_7735(0x0f); WriteDate_7735(0x1a); WriteDate_7735(0x0f); WriteDate_7735(0x18); WriteDate_7735(0x2f); WriteDate_7735(0x28); WriteDate_7735(0x20); WriteDate_7735(0x22); WriteDate_7735(0x1f); WriteDate_7735(0x1b); WriteDate_7735(0x23); WriteDate_7735(0x37); WriteDate_7735(0x00);
WriteDate_7735(0x07); WriteDate_7735(0x02); WriteDate_7735(0x10); WriteCommand_7735(0xe1); WriteDate_7735(0x0f); WriteDate_7735(0x1b); WriteDate_7735(0x0f); WriteDate_7735(0x17); WriteDate_7735(0x33); WriteDate_7735(0x2c); WriteDate_7735(0x29); WriteDate_7735(0x2e); WriteDate_7735(0x30); WriteDate_7735(0x30); WriteDate_7735(0x39); WriteDate_7735(0x3f); WriteDate_7735(0x00); WriteDate_7735(0x07); WriteDate_7735(0x03); WriteDate_7735(0x10);
WriteCommand_7735(0x2a); WriteDate_7735(0x00);WriteDate_7735(0x00); WriteDate_7735(0x00);WriteDate_7735(0x7f); WriteCommand_7735(0x2b); WriteDate_7735(0x00);WriteDate_7735(0x00); WriteDate_7735(0x00);WriteDate_7735(0x7f);
/*下面这2个指令对ST7735R好像不起作用,仿真会报警告,应该是没有这2条指令*/ //WriteCommand_7735(0xF0); //Enable test command //WriteDate_7735(0x01); //WriteCommand_7735(0xF6); //Disable ram power save mode //WriteDate_7735(0x00); WriteCommand_7735(0x3A); //65k mode WriteDate_7735(0x05); WriteCommand_7735(0x29); //Display on WriteCommand_7735(0x2C); }
代码就贴这么多吧,具体的下载来看吧,网上基于这款LCD的项目挺少的。特别注意点是设置SPI的CS脚,费了我一天时间在那测试,一直没找到错误点,当时都快哭了,终于还是做出来了,希望以后的同学仿真少走弯路吧。CS脚(片选)一定要设定成复用推挽输出,并且不要设置到SPI的NSS脚上,即PA4,接上反正是运行不起来。。。
|