1602a stm32f103zet6驱动代码
时间: 2023-09-12 21:10:51 浏览: 126
以下是一个简单的1602A液晶显示屏的驱动代码示例,基于STM32F103ZET6:
```c
#include "stm32f10x.h"
#include "delay.h"
#include "lcd1602.h"
#define LCD1602_GPIO_RCC RCC_APB2Periph_GPIOB
#define LCD1602_GPIO GPIOB
#define LCD1602_RS GPIO_Pin_0
#define LCD1602_RW GPIO_Pin_1
#define LCD1602_EN GPIO_Pin_10
#define LCD1602_D4 GPIO_Pin_11
#define LCD1602_D5 GPIO_Pin_12
#define LCD1602_D6 GPIO_Pin_13
#define LCD1602_D7 GPIO_Pin_14
void LCD1602_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LCD1602_GPIO_RCC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = LCD1602_RS | LCD1602_RW | LCD1602_EN |
LCD1602_D4 | LCD1602_D5 | LCD1602_D6 | LCD1602_D7;
GPIO_Init(LCD1602_GPIO, &GPIO_InitStructure);
}
void LCD1602_Delay(uint16_t nCount)
{
while(nCount--);
}
void LCD1602_EnablePulse(void)
{
GPIO_SetBits(LCD1602_GPIO, LCD1602_EN);
LCD1602_Delay(50);
GPIO_ResetBits(LCD1602_GPIO, LCD1602_EN);
LCD1602_Delay(50);
}
void LCD1602_WriteData(uint8_t data)
{
GPIO_SetBits(LCD1602_GPIO, LCD1602_RS);
GPIO_ResetBits(LCD1602_GPIO, LCD1602_RW);
GPIO_Write(LCD1602_GPIO, data << 8);
LCD1602_EnablePulse();
}
void LCD1602_WriteCmd(uint8_t cmd)
{
GPIO_ResetBits(LCD1602_GPIO, LCD1602_RS);
GPIO_ResetBits(LCD1602_GPIO, LCD1602_RW);
GPIO_Write(LCD1602_GPIO, cmd << 8);
LCD1602_EnablePulse();
}
void LCD1602_Init(void)
{
LCD1602_GPIO_Init();
GPIO_ResetBits(LCD1602_GPIO, LCD1602_RS | LCD1602_RW | LCD1602_EN);
LCD1602_Delay(100000);
LCD1602_WriteCmd(0x33);
LCD1602_Delay(5000);
LCD1602_WriteCmd(0x32);
LCD1602_Delay(5000);
LCD1602_WriteCmd(0x28);
LCD1602_Delay(5000);
LCD1602_WriteCmd(0x0C);
LCD1602_Delay(5000);
LCD1602_WriteCmd(0x06);
LCD1602_Delay(5000);
LCD1602_WriteCmd(0x01);
LCD1602_Delay(5000);
}
void LCD1602_WriteString(char *str)
{
while(*str)
{
LCD1602_WriteData(*str++);
}
}
int main(void)
{
LCD1602_Init();
while(1)
{
LCD1602_WriteCmd(0x80 | 0x00); // Set cursor to first line
LCD1602_WriteString("Hello, world!");
LCD1602_Delay(1000000);
LCD1602_WriteCmd(0x80 | 0x40); // Set cursor to second line
LCD1602_WriteString("How are you?");
LCD1602_Delay(1000000);
LCD1602_WriteCmd(0x01); // Clear screen
LCD1602_Delay(1000000);
}
return 0;
}
```
此示例需要使用delay库和LCD1602库,可以根据自己的实际情况进行修改。
阅读全文