编写stm32f103c6t6用LCD1602显示数字的代码
时间: 2024-01-04 10:04:04 浏览: 117
好的,以下是使用stm32f103c6t6控制LCD1602显示数字的代码,供参考:
```c
#include "stm32f10x.h"
#include "delay.h"
void LCD_Init(void);
void LCD_Write_Command(uint8_t command);
void LCD_Write_Data(uint8_t data);
void LCD_Write_String(char* string);
void LCD_Clear(void);
void LCD_Set_Cursor(uint8_t row, uint8_t column);
int main(void)
{
SystemInit();
SysTick_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Enable GPIOA clock
GPIO_InitTypeDef GPIO_InitStructure;
// Configure PA0-PA5 as push-pull outputs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
LCD_Init(); // Initialize LCD
while(1)
{
LCD_Set_Cursor(0, 0); // Set cursor to first row, first column
LCD_Write_String("Counter:"); // Write string to LCD
LCD_Set_Cursor(1, 0); // Set cursor to second row, first column
LCD_Write_Data('0'); // Write data to LCD
Delay_ms(1000); // Delay for 1 second
}
}
void LCD_Init(void)
{
LCD_Write_Command(0x38); // Initialize 8-bit mode
LCD_Write_Command(0x0C); // Turn on display, cursor, and blink
LCD_Write_Command(0x01); // Clear display
LCD_Write_Command(0x06); // Set direction of entry
}
void LCD_Write_Command(uint8_t command)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0); // Set RS pin to 0
GPIO_Write(GPIOA, (command << 2)); // Write command to data pins
GPIO_SetBits(GPIOA, GPIO_Pin_1); // Set EN pin to 1
Delay_us(1); // Delay for 1us
GPIO_ResetBits(GPIOA, GPIO_Pin_1); // Set EN pin to 0
Delay_ms(1); // Delay for 1ms
}
void LCD_Write_Data(uint8_t data)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0); // Set RS pin to 1
GPIO_Write(GPIOA, (data << 2)); // Write data to data pins
GPIO_SetBits(GPIOA, GPIO_Pin_1); // Set EN pin to 1
Delay_us(1); // Delay for 1us
GPIO_ResetBits(GPIOA, GPIO_Pin_1); // Set EN pin to 0
Delay_ms(1); // Delay for 1ms
}
void LCD_Write_String(char* string)
{
while(*string) // Loop through each character in string
{
LCD_Write_Data(*string++); // Write character to LCD
}
}
void LCD_Clear(void)
{
LCD_Write_Command(0x01); // Clear display
}
void LCD_Set_Cursor(uint8_t row, uint8_t column)
{
uint8_t address = (row * 0x40) + column; // Calculate address of cursor
LCD_Write_Command(0x80 | address); // Set cursor to address
}
```
在这个例子中,我们使用PA0-PA5(即D0-D5)控制LCD1602,PA0作为RS,PA1作为EN。我们使用delay.h中的延迟函数来控制LCD的操作时间。在main函数中,我们设置LCD光标,写入字符串“Counter:”,然后设置光标到第二行第一列,写入数字0。最后我们使用1秒的延迟,然后重复这个过程。
当然,这只是一个例子,你可以根据自己的需求修改代码。同时,为了让代码更加易懂,我建议你在使用之前先阅读LCD1602的数据手册。
相关推荐
![-](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)