写一个基于STM32驱动LCD2004的程序
时间: 2023-10-16 19:08:20 浏览: 67
好的,下面是一个简单的 STM32 驱动 LCD2004 的程序,仅供参考:
```c
#include "stm32f10x.h"
#include "delay.h"
#define RS 0x01 // 数据/命令选择引脚
#define RW 0x02 // 读/写选择引脚
#define EN 0x04 // 使能引脚
void LCD_WriteCmd(uint8_t cmd);
void LCD_WriteData(uint8_t data);
void LCD_Init(void);
void LCD_Clear(void);
void LCD_SetCursor(uint8_t row, uint8_t col);
void LCD_WriteString(char* str);
int main(void)
{
// 初始化IO口和定时器
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
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_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
// 初始化LCD
LCD_Init();
LCD_Clear();
LCD_SetCursor(0, 0);
LCD_WriteString("Hello, World!");
while (1) {
// 循环执行其他操作
}
}
void LCD_WriteCmd(uint8_t cmd)
{
GPIO_ResetBits(GPIOB, RS);
GPIO_ResetBits(GPIOB, RW);
GPIO_SetBits(GPIOB, EN);
GPIO_Write(GPIOB, (GPIO_ReadOutputData(GPIOB) & 0xFF00) | cmd);
delay_us(50);
GPIO_ResetBits(GPIOB, EN);
delay_us(50);
}
void LCD_WriteData(uint8_t data)
{
GPIO_SetBits(GPIOB, RS);
GPIO_ResetBits(GPIOB, RW);
GPIO_SetBits(GPIOB, EN);
GPIO_Write(GPIOB, (GPIO_ReadOutputData(GPIOB) & 0xFF00) | data);
delay_us(50);
GPIO_ResetBits(GPIOB, EN);
delay_us(50);
}
void LCD_Init(void)
{
LCD_WriteCmd(0x38); // 8位数据总线,2行显示,5x8点阵字符
delay_ms(2);
LCD_WriteCmd(0x06); // 光标右移,字符不移动
delay_ms(2);
LCD_WriteCmd(0x0C); // 开启显示,关闭光标
delay_ms(2);
LCD_WriteCmd(0x01); // 清屏
delay_ms(2);
}
void LCD_Clear(void)
{
LCD_WriteCmd(0x01); // 清屏
delay_ms(2);
}
void LCD_SetCursor(uint8_t row, uint8_t col)
{
uint8_t addr;
if (row == 0) {
addr = col;
} else {
addr = col + 0x40;
}
LCD_WriteCmd(0x80 | addr);
delay_us(50);
}
void LCD_WriteString(char* str)
{
while (*str) {
LCD_WriteData(*str++);
}
}
```
这个程序使用了定时器和延时函数来控制LCD显示的时间,使用了GPIO口来控制LCD2004的各个引脚。在主函数中,先进行初始化,然后清屏并设置光标位置,最后输出字符串。需要注意的是,这个程序仅供参考,具体的使用方法和实现细节可能需要根据不同的硬件和需求进行修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)