用stm32f407zet6写一个驱动1.8寸tft代码
时间: 2023-03-23 18:03:30 浏览: 144
很高兴为您提供帮助!以下是使用STM32F407ZET6编写一个驱动1.8寸TFT的示例代码,希望能对您有所帮助。
请注意:由于驱动不同,此示例代码仅供参考。
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#define LCD_DATA_PORT GPIOC // 数据总线端口
#define LCD_CONTROL_PORT GPIOE // 控制端口
#define LCD_RST GPIO_Pin_0 // 复位引脚
#define LCD_RS GPIO_Pin_1 // 命令/数据选择引脚
#define LCD_WR GPIO_Pin_2 // 写引脚
void delay_us(uint32_t us) {
for (uint32_t i = 0; i < us * 10; i++) {
__NOP();
}
}
void write_data(uint8_t data) {
LCD_DATA_PORT->ODR = data; // 写入数据
LCD_CONTROL_PORT->BRR = LCD_RS; // 选择写入数据
LCD_CONTROL_PORT->BRR = LCD_WR; // 写入数据使能
delay_us(1); // 延时
LCD_CONTROL_PORT->BSRR = LCD_WR; // 关闭写入数据使能
}
void write_command(uint8_t cmd) {
LCD_DATA_PORT->ODR = cmd; // 写入命令
LCD_CONTROL_PORT->BRR = LCD_RS; // 选择写入命令
LCD_CONTROL_PORT->BRR = LCD_WR; // 写入命令使能
delay_us(1); // 延时
LCD_CONTROL_PORT->BSRR = LCD_WR; // 关闭写入命令使能
}
void init_lcd(void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE, ENABLE); // 打开GPIO时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure); // 配置数据总线端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_Init(LCD_CONTROL_PORT, &GPIO_InitStructure); // 配置控制端口
GPIO_SetBits(LCD_CONTROL_PORT, LCD_RST); // 复位
delay_us(10);
GPIO_ResetBits(LCD_CONTROL_PORT, LCD_RST);
delay_us(10);
GPIO_SetBits(LCD_CONTROL_PORT, LCD_RST);
delay_us(10);
write_command(0x11); // Sleep Out
delay_us(120);
write_command(0xCF);
write_data(0x00);
write_data(0xC1);
write_data(0x30);
delay_us(10);
write_command(0xED);
write_data(0x64);
write_data(0x03);