stm32f103vet6 驱动lcd12864程序(不用hal库)
时间: 2023-07-29 13:13:33 浏览: 118
驱动LCD12864液晶屏需要使用STM32F103的IO口输出数据和控制信号。以下是一个简单的示例程序,可以参考实现。
```c
#include "stm32f10x.h"
#define LCD_CTRL_PORT GPIOB
#define LCD_DATA_PORT GPIOC
#define LCD_RS_PIN GPIO_Pin_1
#define LCD_RW_PIN GPIO_Pin_2
#define LCD_EN_PIN GPIO_Pin_3
#define LCD_DATA_PIN GPIO_Pin_All
void LCD_Init(void);
void LCD_WriteCmd(uint8_t cmd);
void LCD_WriteData(uint8_t data);
int main(void)
{
LCD_Init();
while (1) {
// 写入数据
LCD_WriteData(0x48); // 'H'
LCD_WriteData(0x65); // 'e'
LCD_WriteData(0x6C); // 'l'
LCD_WriteData(0x6C); // 'l'
LCD_WriteData(0x6F); // 'o'
LCD_WriteData(0x20); // ' '
LCD_WriteData(0x57); // 'W'
LCD_WriteData(0x6F); // 'o'
LCD_WriteData(0x72); // 'r'
LCD_WriteData(0x6C); // 'l'
LCD_WriteData(0x64); // 'd'
LCD_WriteData(0x21); // '!'
}
}
void LCD_Init(void)
{
// 初始化IO口
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN | LCD_RW_PIN | LCD_EN_PIN;
GPIO_Init(LCD_CTRL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_DATA_PIN;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
// 初始化LCD
GPIO_SetBits(LCD_CTRL_PORT, LCD_RS_PIN);
GPIO_SetBits(LCD_CTRL_PORT, LCD_RW_PIN);
GPIO_SetBits(LCD_CTRL_PORT, LCD_EN_PIN);
LCD_WriteCmd(0x38); // 8bit数据接口,2行显示,5×7点阵字符
LCD_WriteCmd(0x0C); // 显示开,光标关,闪烁关
LCD_WriteCmd(0x06); // 光标右移,字符不移动
LCD_WriteCmd(0x01); // 显示清屏,光标复位
}
void LCD_WriteCmd(uint8_t cmd)
{
GPIO_ResetBits(LCD_CTRL_PORT, LCD_RS_PIN);
GPIO_ResetBits(LCD_CTRL_PORT, LCD_RW_PIN);
GPIO_Write(LCD_DATA_PORT, cmd);
GPIO_SetBits(LCD_CTRL_PORT, LCD_EN_PIN);
GPIO_ResetBits(LCD_CTRL_PORT, LCD_EN_PIN);
}
void LCD_WriteData(uint8_t data)
{
GPIO_SetBits(LCD_CTRL_PORT, LCD_RS_PIN);
GPIO_ResetBits(LCD_CTRL_PORT, LCD_RW_PIN);
GPIO_Write(LCD_DATA_PORT, data);
GPIO_SetBits(LCD_CTRL_PORT, LCD_EN_PIN);
GPIO_ResetBits(LCD_CTRL_PORT, LCD_EN_PIN);
}
```
该程序中使用了GPIOB作为LCD控制信号的IO口,GPIOC作为LCD数据线的IO口。在LCD_Init()函数中,首先初始化IO口,并发送一些命令以初始化LCD。在LCD_WriteCmd()和LCD_WriteData()函数中,分别发送命令和数据到LCD。注意在发送数据和命令时,需要先拉低RS和RW信号,再拉高EN信号,以产生一个下降沿,从而使LCD读取数据。
阅读全文