stm32f103c8t6驱动1602 LCD代码
时间: 2023-05-26 08:07:33 浏览: 70
以下是 stm32f103c8t6 驱动 1602 LCD 的示例代码:
```
// include statement for the required libraries
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "delay.h" //delay library header file
// define the port and pin numbers for the LCD connections
#define LCD_PORT GPIOB
#define RS GPIO_Pin_0
#define EN GPIO_Pin_1
#define D4 GPIO_Pin_4
#define D5 GPIO_Pin_5
#define D6 GPIO_Pin_6
#define D7 GPIO_Pin_7
// function prototypes
void LCD_init(void);
void LCD_data(unsigned char data);
void LCD_command(unsigned char command);
void LCD_send(unsigned char data, unsigned char mode);
void LCD_print(char *string);
int main(void)
{
// initialize the system clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// initialize the LCD
LCD_init();
// display "Hello World!" on the LCD
LCD_print("Hello World!");
while(1)
{
// loop indefinitely
}
}
void LCD_init(void)
{
// set the pins as outputs
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = RS | EN | D4 | D5 | D6 | D7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_PORT, &GPIO_InitStructure);
// initialize the LCD display
delay_ms(15);
LCD_command(0x03);
delay_ms(5);
LCD_command(0x03);
delay_us(100);
LCD_command(0x03);
LCD_command(0x02);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01);
}
void LCD_data(unsigned char data)
{
// send data to the LCD display
LCD_send(data, 1);
}
void LCD_command(unsigned char command)
{
// send command to the LCD display
LCD_send(command, 0);
}
void LCD_send(unsigned char data, unsigned char mode)
{
// send data/command to the LCD display
GPIO_WriteBit(LCD_PORT, RS, (BitAction)mode);
GPIO_WriteBit(LCD_PORT, D4, (BitAction)((data >> 4) & 1));
GPIO_WriteBit(LCD_PORT, D5, (BitAction)((data >> 5) & 1));
GPIO_WriteBit(LCD_PORT, D6, (BitAction)((data >> 6) & 1));
GPIO_WriteBit(LCD_PORT, D7, (BitAction)((data >> 7) & 1));
GPIO_WriteBit(LCD_PORT, EN, Bit_SET);
delay_us(50);
GPIO_WriteBit(LCD_PORT, EN, Bit_RESET);
GPIO_WriteBit(LCD_PORT, D4, (BitAction)(data & 1));
GPIO_WriteBit(LCD_PORT, D5, (BitAction)((data >> 1) & 1));
GPIO_WriteBit(LCD_PORT, D6, (BitAction)((data >> 2) & 1));
GPIO_WriteBit(LCD_PORT, D7, (BitAction)((data >> 3) & 1));
GPIO_WriteBit(LCD_PORT, EN, Bit_SET);
delay_us(50);
GPIO_WriteBit(LCD_PORT, EN, Bit_RESET);
}
void LCD_print(char *string)
{
// print a string to the LCD display
while (*string)
{
LCD_data(*string++);
}
}
```
该代码使用了 delay 库来实现一些延时操作。需要注意的是,若你的系统使用了其他的延时库,则部分延时函数需要进行适当修改。另外,该代码中使用 BitAction 枚举类型。它是一种宏定义,可直接代替为 1 或 0。若编译时报错,请将宏定义进行相应修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)