编写stm32f103c6t6用LCD1602显示数字的代码
时间: 2024-01-04 07:04:37 浏览: 23
以下是基于STM32CubeIDE的示例代码,可以在STM32F103C6T6微控制器上使用LCD1602显示数字:
```c
#include "main.h"
#include "lcd1602.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
// 初始化LCD1602
lcd1602_init();
uint8_t num = 0;
char num_str[4];
while (1)
{
// 将数字转换为字符串
sprintf(num_str, "%d", num);
// 在第一行第一列显示数字
lcd1602_set_cursor(0, 0);
lcd1602_send_string(num_str);
HAL_Delay(1000); // 延时1秒
num++;
}
}
```
需要在项目中添加lcd1602.c和lcd1602.h文件,并在main.c中包含lcd1602.h头文件。在lcd1602.c文件中,需要根据具体的硬件连接情况修改GPIO引脚和时钟等相关设置。
相关问题
写出stm32f103c6t6用LCD1602显示数字的具体程序代码
要写出STM32F103C6T6用LCD1602显示数字的程序代码,需要先定义一些需要用到的函数,例如:用来初始化LCD1602的函数,用来写入数据的函数,用来设置显示模式的函数等等,然后再根据需求编写相应的程序代码。具体代码如下:// 初始化LCD1602
void LCD1602_Init(void)
{
// 其他初始化步骤
}// 写入数据
void LCD1602_WriteData(uint8_t data)
{
// 其他写入数据步骤
}// 设置显示模式
void LCD1602_SetMode(uint8_t mode)
{
// 其他设置模式步骤
}// 显示数字
void LCD1602_DisplayNumber(uint16_t number)
{
// 其他显示数字步骤
}// 主函数
int main(void)
{
LCD1602_Init(); // 初始化LCD1602
LCD1602_SetMode(0x0C); // 设置显示模式
LCD1602_DisplayNumber(12345); // 显示数字
while(1); // 死循环
}
详细写出stm32f103c6t6的LCD1602初始化程序代码
### 回答1:
LCD1602初始化程序代码://初始化LCD1602
void LCD1602_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|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 = GPIO_Pin_7;
GPIO_Init(GPIOA, &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_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//初始化
LCD1602_Write_Cmd(0x38);
LCD1602_Write_Cmd(0x0c);
LCD1602_Write_Cmd(0x06);
LCD1602_Write_Cmd(0x01);
}
### 回答2:
下面是一个基于STM32F103C6T6的LCD1602初始化程序代码:
#include "stm32f10x.h"
// 定义LCD相关的GPIO引脚
#define LCD_Port GPIOB
#define LCD_RS_Pin GPIO_Pin_12
#define LCD_EN_Pin GPIO_Pin_13
#define LCD_D4_Pin GPIO_Pin_14
#define LCD_D5_Pin GPIO_Pin_15
void LCD_Init(void)
{
// 使能GPIOB外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 配置LCD引脚为推挽输出模式
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = LCD_RS_Pin | LCD_EN_Pin | LCD_D4_Pin | LCD_D5_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_Port, &GPIO_InitStruct);
// 默认设置RS、EN、D4、D5为低电平
GPIO_ResetBits(LCD_Port, LCD_RS_Pin);
GPIO_ResetBits(LCD_Port, LCD_EN_Pin);
GPIO_ResetBits(LCD_Port, LCD_D4_Pin);
GPIO_ResetBits(LCD_Port, LCD_D5_Pin);
// 初始化LCD显示器
LCD_WriteCommand(0x28); // 4位数据线,2行显示,5x8点阵字符
LCD_WriteCommand(0x0E); // Display ON,Cursor ON,Cursor Blinking
LCD_WriteCommand(0x06); // Entry Mode Set: 光标右移,显示不移动
LCD_WriteCommand(0x01); // Clear Display
LCD_WriteCommand(0x80); // Set DDRAM地址到第一行第一列
}
void LCD_WriteCommand(uint8_t command)
{
// RS引脚设置为低电平,表示写入的是命令
GPIO_ResetBits(LCD_Port, LCD_RS_Pin);
// 发送高4位
GPIO_WriteBits(LCD_Port, LCD_D4_Pin, (command >> 4) & 0x0F);
// 使能LCD
GPIO_SetBits(LCD_Port, LCD_EN_Pin);
Delay_us(1);
GPIO_ResetBits(LCD_Port, LCD_EN_Pin);
// 发送低4位
GPIO_WriteBits(LCD_Port, LCD_D4_Pin, command & 0x0F);
// 使能LCD
GPIO_SetBits(LCD_Port, LCD_EN_Pin);
Delay_us(1);
GPIO_ResetBits(LCD_Port, LCD_EN_Pin);
}
void Delay_us(uint32_t us)
{
us *= 7;
while(us--);
}
### 回答3:
以下是一个简单的STM32F103C6T6的LCD1602初始化程序代码:
#include "stm32f10x.h"
#define LCD_RS_PIN GPIO_Pin_0 // 连接到LCD的RS引脚
#define LCD_RW_PIN GPIO_Pin_1 // 连接到LCD的RW引脚
#define LCD_EN_PIN GPIO_Pin_2 // 连接到LCD的EN引脚
#define LCD_D4_PIN GPIO_Pin_12 // 连接到LCD的D4引脚
#define LCD_D5_PIN GPIO_Pin_13 // 连接到LCD的D5引脚
#define LCD_D6_PIN GPIO_Pin_14 // 连接到LCD的D6引脚
#define LCD_D7_PIN GPIO_Pin_15 // 连接到LCD的D7引脚
#define LCD_PORT GPIOC // 连接到LCD的引脚所在的GPIO端口
void LCD_Init(void)
{
// 使能GPIOC的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
// 配置LCD引脚为输出模式
GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN | LCD_RW_PIN | LCD_EN_PIN | LCD_D4_PIN | LCD_D5_PIN | LCD_D6_PIN | LCD_D7_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_PORT, &GPIO_InitStructure);
// 初始化LCD
// 设置LCD为8位数据总线
LCD_WriteCommand(0x38);
// 设置显示行数和字体大小
LCD_WriteCommand(0x0C);
// 清屏
LCD_WriteCommand(0x01);
// 设置写入显示数据的地址
LCD_WriteCommand(0x80);
}
void LCD_WriteCommand(uint8_t command)
{
// 设置RS为低电平,表示写入命令
GPIO_ResetBits(LCD_PORT, LCD_RS_PIN);
// 设置RW为低电平,表示写入命令
GPIO_ResetBits(LCD_PORT, LCD_RW_PIN);
// 将命令通过D4-D7引脚写入LCD模块
GPIO_WriteBits(LCD_PORT, LCD_D4_PIN | LCD_D5_PIN | LCD_D6_PIN | LCD_D7_PIN, (command & 0xF0) >> 4);
// 发送使能脉冲使命令生效
GPIO_SetBits(LCD_PORT, LCD_EN_PIN);
delay_us(1);
GPIO_ResetBits(LCD_PORT, LCD_EN_PIN);
delay_us(1);
GPIO_WriteBits(LCD_PORT, LCD_D4_PIN | LCD_D5_PIN | LCD_D6_PIN | LCD_D7_PIN, command & 0x0F);
GPIO_SetBits(LCD_PORT, LCD_EN_PIN);
delay_us(1);
GPIO_ResetBits(LCD_PORT, LCD_EN_PIN);
delay_us(1);
}
void delay_us(uint32_t us)
{
us *= 8;
while (us--)
{
__NOP();
}
}
上述代码假设使用PC0-PC2、PC12-PC15引脚连接到LCD1602,并且已设置正确的时钟和引脚功能。LCD_Init()函数用于初始化LCD,LCD_WriteCommand()函数用于向LCD发送命令,delay_us()函数用于产生延时。
相关推荐
![](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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)