stm32f103c8t6驱动TFT-lcd显示屏代码
时间: 2023-08-28 10:04:50 浏览: 365
TFT stm32驱动代码
4星 · 用户满意度95%
要驱动TFT-LCD显示屏,您需要一个TFT-LCD控制器驱动芯片。STM32F103C8T6并没有内置这样的控制器,因此您需要使用外部控制器,例如ILI9341。
以下是使用STM32F103C8T6和ILI9341驱动TFT-LCD显示屏的示例代码:
首先,您需要在STM32F103C8T6上配置SPI接口,并使用以下库函数进行初始化:
```c
SPI_InitTypeDef SPI_InitStructure;
/* Enable SPI1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
```
接下来,您需要编写ILI9341的驱动程序。这里需要注意的是,ILI9341与TFT-LCD显示屏的连接方式可能不同,因此您需要根据您使用的具体显示屏和芯片进行修改。
以下是一个简单的ILI9341驱动程序,用于在TFT-LCD显示屏上显示颜色块:
```c
#include "ili9341.h"
/* ILI9341 initialization */
void ili9341_init(void)
{
/* Reset */
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
delay_ms(100);
GPIO_SetBits(GPIOA, GPIO_Pin_1);
delay_ms(100);
/* Software reset */
ili9341_write_command(ILI9341_SWRESET);
delay_ms(100);
/* Power control A */
ili9341_write_command(ILI9341_POWERA);
ili9341_write_data(0x39);
ili9341_write_data(0x2C);
ili9341_write_data(0x00);
ili9341_write_data(0x34);
ili9341_write_data(0x02);
ili9341_write_command(ILI9341_POWERB);
ili9341_write_data(0x00);
ili9341_write_data(0xC1);
ili9341_write_data(0x30);
/* Driver timing control A */
ili9341_write_command(ILI9341_DTCA);
ili9341_write_data(0x85);
ili9341_write_data(0x00);
ili9341_write_data(0x78);
/* Driver timing control B */
ili9341_write_command(ILI9341_DTCB);
ili9341_write_data(0x00);
ili9341_write_data(0x00);
/* Power on sequence control */
ili9341_write_command(ILI9341_POWER_SEQ);
ili9341_write_data(0x64);
ili9341_write_data(0x03);
ili9341_write_data(0x12);
ili9341_write_data(0x81);
/* Pump ratio control */
ili9341_write_command(ILI9341_PRC);
ili9341_write_data(0x20);
/* Power control 1 */
ili9341_write_command(ILI9341_POWER1);
ili9341_write_data(0x23);
/* Power control 2 */
ili9341_write_command(ILI9341_POWER2);
ili9341_write_data(0x10);
/* VCOM control 1 */
ili9341_write_command(ILI9341_VCOM1);
ili9341_write_data(0x3E);
ili9341_write_data(0x28);
/* VCOM control 2 */
ili9341_write_command(ILI9341_VCOM2);
ili9341_write_data(0x86);
/* Memory access control */
ili9341_write_command(ILI9341_MAC);
ili9341_write_data(0x48);
/* Pixel format */
ili9341_write_command(ILI9341_PIXEL_FORMAT);
ili9341_write_data(0x55);
/* Frame rate control */
ili9341_write_command(ILI9341_FRC);
ili9341_write_data(0x00);
ili9341_write_data(0x18);
/* Display function control */
ili9341_write_command(ILI9341_DFC);
ili9341_write_data(0x08);
ili9341_write_data(0x82);
ili9341_write_data(0x27);
/* 3GAMMA function disable */
ili9341_write_command(ILI9341_3GAMMA_EN);
ili9341_write_data(0x00);
/* Gamma curve selected */
ili9341_write_command(ILI9341_GAMMA);
ili9341_write_data(0x01);
/* Set gamma */
ili9341_write_command(ILI9341_PGAMMA);
ili9341_write_data(0x0F);
ili9341_write_data(0x31);
ili9341_write_data(0x2B);
ili9341_write_data(0x0C);
ili9341_write_data(0x0E);
ili9341_write_data(0x08);
ili9341_write_data(0x4E);
ili9341_write_data(0xF1);
ili9341_write_data(0x37);
ili9341_write_data(0x07);
ili9341_write_data(0x10);
ili9341_write_data(0x03);
ili9341_write_data(0x0E);
ili9341_write_data(0x09);
ili9341_write_data(0x00);
/* Set gamma */
ili9341_write_command(ILI9341_NGAMMA);
ili9341_write_data(0x00);
ili9341_write_data(0x0E);
ili9341_write_data(0x14);
ili9341_write_data(0x03);
ili9341_write_data(0x11);
ili9341_write_data(0x07);
ili9341_write_data(0x31);
ili9341_write_data(0xC1);
ili9341_write_data(0x48);
ili9341_write_data(0x08);
ili9341_write_data(0x0F);
ili9341_write_data(0x0C);
ili9341_write_data(0x31);
ili9341_write_data(0x36);
ili9341_write_data(0x0F);
/* Sleep out */
ili9341_write_command(ILI9341_SLPOUT);
delay_ms(120);
/* Display on */
ili9341_write_command(ILI9341_DISPON);
}
/* ILI9341 write command */
void ili9341_write_command(uint8_t command)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_3);
SPI_I2S_SendData(SPI1, command);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
}
/* ILI9341 write data */
void ili9341_write_data(uint8_t data)
{
GPIO_SetBits(GPIOA, GPIO_Pin_3);
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
}
/* ILI9341 set address */
void ili9341_set_address(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
ili9341_write_command(ILI9341_CASET);
ili9341_write_data(x1 >> 8);
ili9341_write_data(x1 & 0xFF);
ili9341_write_data(x2 >> 8);
ili9341_write_data(x2 & 0xFF);
ili9341_write_command(ILI9341_PASET);
ili9341_write_data(y1 >> 8);
ili9341_write_data(y1 & 0xFF);
ili9341_write_data(y2 >> 8);
ili9341_write_data(y2 & 0xFF);
ili9341_write_command(ILI9341_RAMWR);
}
/* ILI9341 draw pixel */
void ili9341_draw_pixel(uint16_t x, uint16_t y, uint16_t color)
{
ili9341_set_address(x, y, x, y);
ili9341_write_data(color >> 8);
ili9341_write_data(color & 0xFF);
}
/* ILI9341 fill screen */
void ili9341_fill_screen(uint16_t color)
{
uint32_t i;
ili9341_set_address(0, 0, TFT_WIDTH - 1, TFT_HEIGHT - 1);
for (i = 0; i < TFT_WIDTH * TFT_HEIGHT; i++) {
ili9341_write_data(color >> 8);
ili9341_write_data(color & 0xFF);
}
}
```
最后,您可以使用以下代码在TFT-LCD显示屏上显示颜色块:
```c
#define TFT_WIDTH 240
#define TFT_HEIGHT 320
int main(void)
{
/* TFT-LCD initialization */
ili9341_init();
/* Fill screen with red color */
ili9341_fill_screen(0xF800);
/* Fill half screen with green color */
ili9341_set_address(0, 0, TFT_WIDTH - 1, TFT_HEIGHT / 2 - 1);
ili9341_fill_screen(0x07E0);
/* Fill half screen with blue color */
ili9341_set_address(0, TFT_HEIGHT / 2, TFT_WIDTH - 1, TFT_HEIGHT - 1);
ili9341_fill_screen(0x001F);
while (1);
return 0;
}
```
这就是一个简单的使用STM32F103C8T6和ILI9341驱动TFT-LCD显示屏的示例代码,您可以根据您使用的具体显示屏和芯片进行修改。
阅读全文