STM32 LCD1602显示
STM32 LCD1602显示
LCD1602介绍
接口说明
LCD1602 通常使用的16引脚,其介绍如下:
1、VSS电源地
2、VCC电源
3、VEE对比度调整电压
4、RS 命令数据选择
5、R/W 读写选择
6、EN 使能信号
7、DB0-DB7 数据总线
8、A 背光电压正
9、K 背光电压地
LCD.c文件
#include "1602.h"
#include "delay.h"
void GPIO_InitStructReadtempCmd(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8);
}
void lcd_delay( unsigned char ms) /*LCD1602 延时*/
{
unsigned char j;
while(ms--){
for(j=0;j<250;j++)
{;}
}
}
void lcd_busy_wait(void) /*LCD1602 忙等待*/
{
u8 sta;
LCD1602_RS0;
LCD1602_RW1;
lcd_data_port = 0xff;
do
{
LCD1602_EN1;
lcd_delay(5);
sta = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7);
LCD1602_EN0;
}while(sta & 0x80);
}
void lcd_command_write( unsigned char command) /*LCD1602 命令写入*/
{
lcd_busy_wait();
LCD1602_RS0;
LCD1602_RW0;
LCD1602_EN0;
lcd_data_port = command;
LCD1602_EN1;
LCD1602_EN0;
}
void lcd_system_reset(void) /*LCD1602 初始化*/
{
lcd_delay(20);
lcd_command_write(0x38);
lcd_delay(100);
lcd_command_write(0x38);
lcd_delay(50);
lcd_command_write(0x38);
lcd_delay(10);
lcd_command_write(0x0c);
lcd_command_write(0x06);
lcd_command_write(0x01);
lcd_command_write(0x80);
lcd_delay(100);
}
void LCD1602_Write_Data(unsigned char dat) //数据写入
{
lcd_busy_wait();
LCD1602_RS1;
LCD1602_RW0;
DATAOUT(dat);
LCD1602_EN1;
LCD1602_EN0;
}
void lcd_char_write(unsigned int x_pos,unsigned int y_pos,unsigned char *lcd_dat) //字符写入
{
LCD1602_Set_Cursor(x_pos,y_pos);
lcd_busy_wait();
while(*lcd_dat != '\0')
{
LCD1602_Write_Data(*lcd_dat);
lcd_dat++;
}
}
void LCD1602_Set_Cursor(unsigned int x, unsigned int y) //地址
{
u8 addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
lcd_command_write(addr | 0x80);
}
void LCD_ShowNum(unsigned int x, unsigned int y,unsigned int num) //显示数字
{
LCD1602_Set_Cursor(x, y);
LCD_ShowChar(x,y,num+'0');
}
void LCD_ShowChar(unsigned int x, unsigned int y,unsigned char dat) //显示字符串
{
LCD1602_Set_Cursor(x, y);
LCD1602_Write_Data(dat);
}
LCD,h文件
#define LCD1602_RS0 GPIOB->BRR = GPIO_Pin_10//0x00000004 //低电平 PB.10
#define LCD1602_RW0 GPIOB->BRR = GPIO_Pin_9//0x00000010 //低电平 PB.9
#define LCD1602_EN0 GPIOB->BRR = GPIO_Pin_8//0x00000040 //低电平 PB.8
//============================¶Ë¿ÚλÉèÖÃ/Çå³ý¼Ä´æÆ÷=========================================//
#define LCD1602_RS1 GPIOB->BSRR = GPIO_Pin_10//0x00000004 //高电平 PB.10
#define LCD1602_RW1 GPIOB->BSRR = GPIO_Pin_9//0x00000010 //高电平 PB.9
#define LCD1602_EN1 GPIOB->BSRR = GPIO_Pin_8 //0x00000040 //高电平 PB.8
#define lcd_data_port GPIOA->ODR //数据端口 PA0-PA7
#define DATAOUT(x) GPIO_Write(GPIOA, x)
void lcd_char_write(unsigned int x_pos,unsigned int y_pos,unsigned char *lcd_dat);
void lcd_system_reset(void);
void lcd_command_write( unsigned char command);
void lcd_busy_wait(void);
void lcd_delay( unsigned char ms);
void GPIO_InitStructReadtempCmd(void);
void LCD1602_Write_Data(unsigned char dat);
void LCD1602_Set_Cursor(unsigned int x, unsigned int y);
void LCD_ShowChar(unsigned int x, unsigned int y,unsigned char dat);
void LCD_ShowNum(unsigned int x, unsigned int y,unsigned int num);
代码链接:https://pan.baidu.com/s/17FHSz4E1VxJ7VzvoB1vSCw
提取码:78if