用STM32F4编写程序用LCD屏ILI9341输出“张翼鹏”
时间: 2023-12-10 12:42:44 浏览: 162
以下是使用STM32F4和ILI9341 LCD屏幕输出“张翼鹏”的示例代码:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_spi.h"
// 定义SPI接口
#define LCD_SPI_PORT SPI5
#define LCD_SPI_RCC RCC_APB2Periph_SPI5
#define LCD_SPI_CLK 45000000
// 定义SPI引脚
#define LCD_SPI_SCK_PIN GPIO_Pin_7
#define LCD_SPI_SCK_PORT GPIOA
#define LCD_SPI_MISO_PIN GPIO_Pin_11
#define LCD_SPI_MISO_PORT GPIOC
#define LCD_SPI_MOSI_PIN GPIO_Pin_12
#define LCD_SPI_MOSI_PORT GPIOC
// 定义LCD控制引脚
#define LCD_CS_PIN GPIO_Pin_2
#define LCD_CS_PORT GPIOB
#define LCD_DC_PIN GPIO_Pin_1
#define LCD_DC_PORT GPIOB
#define LCD_RST_PIN GPIO_Pin_0
#define LCD_RST_PORT GPIOB
// 定义LCD屏幕分辨率
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
// 初始化SPI接口
void LCD_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// 使能SPI时钟
RCC_APB2PeriphClockCmd(LCD_SPI_RCC, ENABLE);
// 配置SPI引脚
GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(LCD_SPI_SCK_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(LCD_SPI_SCK_PORT, GPIO_PinSource7, GPIO_AF_SPI5);
GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(LCD_SPI_MISO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(LCD_SPI_MISO_PORT, GPIO_PinSource11, GPIO_AF_SPI5);
GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(LCD_SPI_MOSI_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(LCD_SPI_MOSI_PORT, GPIO_PinSource12, GPIO_AF_SPI5);
// 配置SPI
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_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(LCD_SPI_PORT, &SPI_InitStructure);
// 使能SPI
SPI_Cmd(LCD_SPI_PORT, ENABLE);
}
// 初始化LCD控制引脚
void LCD_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN | LCD_DC_PIN | LCD_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
// 初始化LCD屏幕
void LCD_Init(void)
{
LCD_GPIO_Init();
LCD_SPI_Init();
// 复位LCD屏幕
GPIO_ResetBits(LCD_RST_PORT, LCD_RST_PIN);
DelayMs(10);
GPIO_SetBits(LCD_RST_PORT, LCD_RST_PIN);
DelayMs(10);
// 发送初始化命令
LCD_WriteCommand(0x01); // 软件复位
DelayMs(10);
LCD_WriteCommand(0xCF);
LCD_WriteData(0x00);
LCD_WriteData(0xC1);
LCD_WriteData(0x30);
DelayMs(10);
LCD_WriteCommand(0xED);
LCD_WriteData(0x64);
LCD_WriteData(0x03);
LCD_WriteData(0x12);
LCD_WriteData(0x81);
DelayMs(10);
LCD_WriteCommand(0xE8);
LCD_WriteData(0x85);
LCD_WriteData(0x10);
LCD_WriteData(0x7A);
DelayMs(10);
LCD_WriteCommand(0xCB);
LCD_WriteData(0x39);
LCD_WriteData(0x2C);
LCD_WriteData(0x00);
LCD_WriteData(0x34);
LCD_WriteData(0x02);
DelayMs(10);
LCD_WriteCommand(0xF7);
LCD_WriteData(0x20);
DelayMs(10);
LCD_WriteCommand(0xEA);
LCD_WriteData(0x00);
LCD_WriteData(0x00);
DelayMs(10);
LCD_WriteCommand(0xC0); // 电源控制1
LCD_WriteData(0x23);
DelayMs(10);
LCD_WriteCommand(0xC1); // 电源控制2
LCD_WriteData(0x10);
DelayMs(10);
LCD_WriteCommand(0xC5); // VCOM控制1
LCD_WriteData(0x3E);
LCD_WriteData(0x28);
DelayMs(10);
LCD_WriteCommand(0xC7); // VCOM控制2
LCD_WriteData(0x86);
DelayMs(10);
LCD_WriteCommand(0x36); // MADCTL
LCD_WriteData(0x68);
DelayMs(10);
LCD_WriteCommand(0x3A); // COLMOD
LCD_WriteData(0x55);
DelayMs(10);
LCD_WriteCommand(0xB1); // Frame rate control,最大 70 Hz,同时保证低功耗
LCD_WriteData(0x00);
LCD_WriteData(0x18);
DelayMs(10);
LCD_WriteCommand(0xB6); // Display function control,RGB/MCU interface select
LCD_WriteData(0x08);
LCD_WriteData(0x82);
LCD_WriteData(0x27);
DelayMs(10);
LCD_WriteCommand(0xF2); // 3Gamma control,disable
LCD_WriteData(0x00);
DelayMs(10);
LCD_WriteCommand(0x26); // Gamma curve selected
LCD_WriteData(0x01);
DelayMs(10);
LCD_WriteCommand(0xE0); // Set Gamma,positive gamma correction
LCD_WriteData(0x0F);
LCD_WriteData(0x31);
LCD_WriteData(0x2B);
LCD_WriteData(0x0C);
LCD_WriteData(0x0E);
LCD_WriteData(0x08);
LCD_WriteData(0x4E);
LCD_WriteData(0xF1);
LCD_WriteData(0x37);
LCD_WriteData(0x07);
LCD_WriteData(0x10);
LCD_WriteData(0x03);
LCD_WriteData(0x0E);
LCD_WriteData(0x09);
LCD_WriteData(0x00);
DelayMs(10);
LCD_WriteCommand(0XE1); // Set Gamma,negative gamma correction
LCD_WriteData(0x00);
LCD_WriteData(0x0E);
LCD_WriteData(0x14);
LCD_WriteData(0x03);
LCD_WriteData(0x11);
LCD_WriteData(0x07);
LCD_WriteData(0x31);
LCD_WriteData(0xC1);
LCD_WriteData(0x48);
LCD_WriteData(0x08);
LCD_WriteData(0x0F);
LCD_WriteData(0x0C);
LCD_WriteData(0x31);
LCD_WriteData(0x36);
LCD_WriteData(0x0F);
DelayMs(10);
LCD_WriteCommand(0x11); // Sleep out
DelayMs(120);
LCD_WriteCommand(0x29); // Display on
// 清屏
LCD_Clear(0xFFFF);
}
// 写入数据到LCD屏幕
void LCD_WriteData(uint8_t data)
{
GPIO_SetBits(LCD_DC_PORT, LCD_DC_PIN); // DC设置为高电平表示写入数据
GPIO_ResetBits(LCD_CS_PORT, LCD_CS_PIN); // 使能片选
while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); // 等待发送缓冲区为空
SPI_I2S_SendData(LCD_SPI_PORT, data); // 发送数据
while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); // 等待接收缓冲区非空
SPI_I2S_ReceiveData(LCD_SPI_PORT); // 读取接收到的数据,清除RXNE标志位
GPIO_SetBits(LCD_CS_PORT, LCD_CS_PIN); // 禁止片选
}
// 写入命令到LCD屏幕
void LCD_WriteCommand(uint8_t cmd)
{
GPIO_ResetBits(LCD_DC_PORT, LCD_DC_PIN); // DC设置为低电平表示写入命令
GPIO_ResetBits(LCD_CS_PORT, LCD_CS_PIN); // 使能片选
while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); // 等待发送缓冲区为空
SPI_I2S_SendData(LCD_SPI_PORT, cmd); // 发送命令
while (SPI_I2S_GetFlagStatus(LCD_SPI_PORT, SPI_I2S_FLAG_RXNE) == RESET); // 等待接收缓冲区非空
SPI_I2S_ReceiveData(LCD_SPI_PORT); // 读取接收到的数据,清除RXNE标志位
GPIO_SetBits(LCD_CS_PORT, LCD_CS_PIN); // 禁止片选
}
// 清屏
void LCD_Clear(uint16_t color)
{
int i, j;
LCD_SetArea(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
for (i = 0; i < LCD_HEIGHT; i++) {
for (j = 0; j < LCD_WIDTH; j++) {
LCD_WriteData(color >> 8);
LCD_WriteData(color & 0xFF);
}
}
}
// 设置绘制区域
void LCD_SetArea(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
LCD_WriteCommand(0x2A);
LCD_WriteData(x1 >> 8);
LCD_WriteData(x1 & 0xFF);
LCD_WriteData(x2 >> 8);
LCD_WriteData(x2 & 0xFF);
LCD_WriteCommand(0x2B);
LCD_WriteData(y1 >> 8);
LCD_WriteData(y1 & 0xFF);
LCD_WriteData(y2 >> 8);
LCD_WriteData(y2 & 0xFF);
LCD_WriteCommand(0x2C);
}
// 在指定位置绘制一个像素点
void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
LCD_SetArea(x, y, x, y);
LCD_WriteData(color >> 8);
LCD_WriteData(color & 0xFF);
}
// 在指定位置绘制一个字符
void LCD_DrawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bgcolor)
{
int i, j;
uint8_t font[16];
for (i = 0; i < 16; i++) {
font[i] = Font16x16[c*16+i];
}
for (i = 0; i < 16; i++) {
for (j = 0; j < 8; j++) {
if (font[i] & (1 << (7-j))) {
LCD_DrawPixel(x+j, y+i, color);
} else {
LCD_DrawPixel(x+j, y+i, bgcolor);
}
}
}
}
// 在指定位置绘制一个字符串
void LCD_DrawString(uint16_t x, uint16_t y, char *str, uint16_t color, uint16_t bgcolor)
{
while (*str) {
LCD_DrawChar(x, y, *str++, color, bgcolor);
x += 8;
}
}
int main(void)
{
LCD_Init();
LCD_DrawString(50, 100, "张翼鹏", 0x0000, 0xFFFF);
while (1) {}
}
```
在代码中,需要注意以下几点:
1. 需要定义LCD屏幕的SPI接口、引脚和分辨率;
2. 需要初始化SPI接口和LCD控制引脚;
3. 需要发送初始化命令,具体的命令可以参考LCD屏幕的数据手册;
4. 绘制字符和字符串时需要使用字库,本例中使用了一个16x16的字库,可以根据需要自行替换;
5. 绘制字符和字符串时需要注意字体的大小和颜色。
6. 在main函数中,我们先调用LCD_Init()函数初始化LCD屏幕,然后调用LCD_DrawString()函数在屏幕上绘制字符串“张翼鹏”。
需要注意的是,上述代码中的一些函数调用和数据类型可能与你的程序略有不同,需要根据具体情况进行修改。
阅读全文