编写stm32f103c6t6用LCD1602显示数字的代码
时间: 2024-01-04 22:04:15 浏览: 53
下面是一个简单的示例代码,用于在STM32F103C6T6芯片上使用LCD1602显示数字:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "lcd1602.h"
int main(void)
{
/* 初始化LCD1602 */
LCD1602_Init();
/* 显示数字 */
int num = 12345;
char str[16];
sprintf(str, "%d", num);
LCD1602_WriteString(str);
while(1)
{
/* 此处可以添加额外的程序代码 */
}
}
```
其中,`delay.h`和`lcd1602.h`是两个头文件,分别用于延时函数和LCD1602驱动函数的声明。下面是它们的代码:
delay.h:
```c
#ifndef __DELAY_H
#define __DELAY_H
void delay_init(void);
void delay_ms(uint16_t ms);
#endif
```
delay.c:
```c
#include "stm32f10x.h"
#include "delay.h"
static uint32_t fac_us = 0;
void delay_init(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
fac_us = SystemCoreClock / 8000000;
}
void delay_ms(uint16_t ms)
{
uint32_t i;
for(i = 0; i < ms; i++)
{
SysTick->LOAD = SystemCoreClock / 8000 - 1;
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);
SysTick->CTRL = 0;
}
}
```
lcd1602.h:
```c
#ifndef __LCD1602_H
#define __LCD1602_H
void LCD1602_Init(void);
void LCD1602_WriteCommand(uint8_t command);
void LCD1602_WriteData(uint8_t data);
void LCD1602_WriteString(char* str);
#endif
```
lcd1602.c:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "lcd1602.h"
void LCD1602_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOB时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* 配置GPIOB的PB0、PB1、PB5引脚为推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 初始化LCD */
LCD1602_WriteCommand(0x38);
delay_ms(5);
LCD1602_WriteCommand(0x38);
delay_ms(5);
LCD1602_WriteCommand(0x38);
delay_ms(5);
LCD1602_WriteCommand(0x0C);
delay_ms(5);
LCD1602_WriteCommand(0x06);
delay_ms(5);
LCD1602_WriteCommand(0x01);
delay_ms(5);
}
void LCD1602_WriteCommand(uint8_t command)
{
/* RS引脚设置为0,表示写入的是命令 */
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
/* 将命令写入LCD */
GPIO_Write(GPIOB, (GPIO_ReadOutputData(GPIOB) & 0xFF00) | command);
GPIO_SetBits(GPIOB, GPIO_Pin_0);
delay_ms(5);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
}
void LCD1602_WriteData(uint8_t data)
{
/* RS引脚设置为1,表示写入的是数据 */
GPIO_SetBits(GPIOB, GPIO_Pin_5);
/* 将数据写入LCD */
GPIO_Write(GPIOB, (GPIO_ReadOutputData(GPIOB) & 0xFF00) | data);
GPIO_SetBits(GPIOB, GPIO_Pin_0);
delay_ms(5);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
}
void LCD1602_WriteString(char* str)
{
while(*str != '\0')
{
LCD1602_WriteData(*str);
str++;
}
}
```
在这个示例代码中,我们使用了STM32F103C6T6芯片的GPIOB的PB0、PB1、PB5引脚来控制LCD1602的RS、RW、E引脚,PB0引脚用于控制E引脚,PB1引脚用于控制RW引脚,PB5引脚用于控制RS引脚。当需要向LCD1602写入数据时,我们使用`LCD1602_WriteData`函数,当需要向LCD1602写入命令时,我们使用`LCD1602_WriteCommand`函数。`LCD1602_WriteString`函数则用于向LCD1602写入字符串。注意,在使用之前需要初始化LCD1602,使用`LCD1602_Init`函数。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)