微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 LCD1602驱动程序

STM32 LCD1602驱动程序

时间:11-20 来源:互联网 点击:
#ifndef LCD1602_STM32_H

#define LCD1602_STM32_H

#define LCD_RS_1 GPIOE->BSRR=GPIO_Pin_0 //PE.0--(LCD)RS
#define LCD_RS_0 GPIOE->BRR =GPIO_Pin_0
#define LCD_RW_1 GPIOE->BSRR=GPIO_Pin_1//PE.1--(LCD)RW
#define LCD_RW_0 GPIOE->BRR =GPIO_Pin_1
#define LCD_EN_1 GPIOE->BSRR=GPIO_Pin_2 //PE.2--(LCD)E
#define LCD_EN_0 GPIOE->BRR =GPIO_Pin_2
#define DATAOUT GPIOD->ODR //PD[0..7]--(LCD)D0~D7

#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif

extern void delay_nms(unsigned long n);
extern void delay_nus(unsigned long n);

uint8_t LCD_busy(void); //读LCD忙碌状态,直到LCD1602不忙为止
void Wait_Leisure(void);//一直等待到LCD1602空闲
void LCD_WriteInitcmd(uchar cmd);//写入LCD初始化时的命令,不可以检测忙状态
void LCD_Writecmd(uchar cmd);//写指令到LCD1602,指令数据占一个字节
void LCD_Writedata(uchar dat);//写一字节数据到LCD1602
void LCD_pos(uchar pos);//设定显示位置 00h~27h,40h~47h
void LCD_Setpos(uchar row,uchar col);//根据习惯设定显示位置
void LCD_DispChar(char ch);//显示一个字符
void LCD_Setpos_DispChar(uchar row,uchar col,char ch);//在指定位置显示一个字符
void LCD_DispString(char str[]);//使LCD1602显示一个字符串,显示位置需提前设定
void LCD_Setpos_DispString(uchar row,uchar col,char str[]);//使LCD1602从指定位置开始显示一个字符串
void LCD_Dispnum(uint32_t num);//显示一个不超过8位的整数,显示位置需提前设置
void LCD_Setpos_Dispnum(uchar row,uchar col,uint32_t num);//在指定位置显示一个不超过8位的整数
void LCD_DispDecimal(uint32_t num,uchar dot);//显示一个有效位不超过8位的浮点数,显示位置需要提前设定
//在指定位置显示一个有效位不超过8位的浮点数
void LCD_Setpos_DispDecimal(uchar row,uchar col,uint32_t num,uchar dot);
//显示日历,显示日期与时间
void LCD_DispDateTime(uint32_t year,uchar month,uchar day,uchar hour,uchar min,uchar sec);
//显示秒表,显示时,分,秒,10毫秒,精确到10ms
void LCD_DispStopWatch(uchar hour,uchar min,uchar sec,uchar tenms);
//显示频率计,动态自动调整频率显示
void LCD_DispFreq(uint32_t freq);
void LCD_ShiftLeft(void);//屏幕整体左移一格,用于滚动显示
void LCD_ShiftRight(void);//屏幕整体右移一格,用于滚动显示
void LCD_Clear(void);//清屏,清除显示
void LCD_Return(void);//使光标还回原点位置
void LCD_Close(void);//关显示
void LCD_Open(void);//开显示
void LCD_FlickerChar(uchar row,uchar col);//使指定位置字符闪烁,不显示光标
void LCD_CloseFlicker(void);//关闭字符闪烁
void LCD_FlickerScreen(void);//屏幕秒闪烁一次

void LCD_Init(void);

#endif

#include "stm32f10x.h"
#include "LCD1602.h"
#ifndef LCD1602_STM32_C
#define LCD1602_STM32_C

uint8_t LCD_busy(void)
{
LCD_RS_0;
delay_nus(1);
LCD_RW_1;
delay_nus(1);
do
{
LCD_EN_0;
delay_nus(200);
LCD_EN_1;
delay_nus(200);
}
while(GPIOD->IDR & 0x80);
LCD_EN_0;
return (uint8_t)0;
}

void Wait_Leisure(void)
{
LCD_busy();
}

void LCD_WriteInitcmd(uchar cmd)
{
LCD_RS_0;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
DATAOUT = cmd;
LCD_EN_0;
delay_nus(300);
LCD_EN_1;
delay_nus(300);
LCD_EN_0;
delay_nms(2);
}

void LCD_Writecmd(uchar cmd)
{
while(LCD_busy());
LCD_RS_0; //对同一个寄存器的两次写入,中间延时一会
delay_nus(1);
LCD_RW_0;
delay_nus(1);
LCD_EN_0;
delay_nus(300);//产生脉冲
DATAOUT = cmd;
LCD_EN_1;
delay_nus(300);//必要的延时
LCD_EN_0; //下降沿,LCD1602开始工作
}

void LCD_Writedata(uchar dat)
{
while(LCD_busy());//等待LCD1602空闲
LCD_RS_1;
delay_nus(1);
LCD_RW_0;
delay_nus(1);
DATAOUT = dat;
LCD_EN_1;//先拉高
delay_nus(300);//很重要的延时,经调试,延时300us以上才可以
LCD_EN_0; //下降沿,开始写入有效数据
}

void LCD_pos(uchar pos)
{
LCD_Writecmd(pos | 0x80);
}

void LCD_Setpos(uchar row,uchar col)
{
if(row==1) LCD_Writecmd(col | 0x80);
else LCD_Writecmd(col | 0xC0);
}

void LCD_DispChar(char ch)
{
LCD_Writedata(ch);
}

void LCD_Setpos_DispChar(uchar row,uchar col,char ch)
{
LCD_Setpos(row,col);
LCD_Writedata(ch);
}

void LCD_DispString(char str[])
{
uchar i=0;
while(str[i] != \0)
{
LCD_Writedata(str[i]);
++i;
}
}

void LCD_Setpos_DispString(uchar row,uchar col,char str[])
{
LCD_Setpos(row,col);
LCD_DispString(str);
}

void LCD_Dispnum(uint32_t num)
{
uint i=0,j,k=0,wei,q;
char str[9];
if(num>=10000000)wei=8;
else if(num>=1000000)wei=7;
else if(num>=100000)wei=6;
else if(num>=10000)wei=5;
else if(num>=1000) wei=4;
else if(num>=100)wei=3;
else if(num>=10) wei=2;
else wei=1;
for(i=wei;i>0;i--)
{ q=1;
j=1; //i=1时,q=1,得到个位
for(;j str[k++]=num/q +0;
num %= q;
}
str[k] = \0; //添加字符串结束标志
LCD_DispString(str);//显示字符串
}

void LCD_Setpos_Dispnum(uchar row,uchar col,uint32_t num)
{
LCD_Setpos(row,col);
LCD_Dispnum(num);
}

void LCD_DispDecimal(uint32_t num,uchar dot)
{
uint i=0,j,k=0,wei,q;
char str[10];
if(num>=10000000)wei=8;
else if(num>=1000000)wei=7;
else if(num>=100000)wei=6;
else if(num>=10000)wei=5;
else if(num>=1000) wei=4;
else if(num>=100)wei=3;
else if(num>=10) wei=2;
else wei=1;
for(i=wei;i>0;i--)
{ q=1;
j=1; //i=1时,q=1,得到个位
for(;j str[k++]=num/q +0;
num %= q;
}
str[k] = \0; //添加字符串结束标志
for(i=8;i>0;i--)
{ if((str[i]>=0)&&(str[i]<=9)) break;
}
str[i+2]=\0; //添加字符串结束符
for(j=0;j {
str[i+1]=str[i];
}
str[i+1]=.; //插入小数点
LCD_DispString(str);//显示浮点小数
}

void LCD_Setpos_DispDecimal(uchar row,uchar col,uint32_t num,uchar dot)
{
LCD_Setpos(row,col);
LCD_DispDecimal(num,dot);
}

void LCD_DispDateTime(uint32_t year,uchar month,uchar day,uchar hour,uchar min,uchar sec)
{
LCD_Setpos(1,0);
LCD_DispString("Date:");
LCD_Dispnum((uint32_t)year);
LCD_DispChar(-);
LCD_Dispnum((uint32_t)month);
LCD_DispChar(-);
LCD_Dispnum((uint32_t)day);
LCD_Setpos(1,15);
LCD_DispChar(*); //第一行结束符显示
LCD_Setpos(2,0);
LCD_DispString("Time:");
LCD_Dispnum((uint32_t)hour);
LCD_DispChar(:);
LCD_Dispnum((uint32_t)min);
LCD_DispChar(:);
LCD_Dispnum((uint32_t)sec);
LCD_Setpos(2,15);
LCD_DispChar(*); //第二行结束符显示
}

void LCD_DispStopWatch(uchar hour,uchar min,uchar sec,uchar tenms)
{
LCD_Setpos(1,0);
LCD_DispString("Current Time:");
LCD_Setpos(1,15);
LCD_DispChar(*); //第一行结束符显示
LCD_Setpos(2,2);
LCD_Dispnum((uint32_t)hour);
LCD_DispChar(:);
LCD_Dispnum((uint32_t)min);
LCD_DispChar(:);
LCD_Dispnum((uint32_t)sec);
LCD_DispChar(:);
LCD_Dispnum((uint32_t)tenms);
LCD_Setpos(2,15);
LCD_DispChar(*); //第二行结束符显示
}

void LCD_DispFreq(uint32_t freq)
{
LCD_Setpos(1,0);
LCD_DispString("Current Freq:");
LCD_Setpos(2,1); //显示位置
if(freq>=1000000)
{
LCD_DispDecimal(freq,6);
LCD_DispString("MHz");
}
else if(freq>=1000)
{
LCD_DispDecimal(freq,3);
LCD_DispString("KHz");
}
else { LCD_Dispnum(freq);
LCD_DispString("Hz");
}
LCD_Setpos(2,14);
LCD_DispString("OK");
}
//屏幕整体左移一格,用于滚动显示
void LCD_ShiftLeft()
{
LCD_Writecmd(0x18);
}
//屏幕整体右移一格,用于滚动显示
void LCD_ShiftRight(void)
{
LCD_Writecmd(0x1C);
}
//清屏,清除显示
void LCD_Clear(void)
{
LCD_Writecmd(0x01);
}
//使光标还回原点位置
void LCD_Return(void)
{
LCD_Writecmd(0x02);
}
//关显示
void LCD_Close(void)
{
LCD_Writecmd(0x08);
}
//开显示
void LCD_Open(void)
{
LCD_Writecmd(0x0C);
}

void LCD_FlickerChar(uchar row,uchar col)
{
LCD_Writecmd(0x0D);
LCD_Setpos(row,col);
}
//关闭字符闪烁
void LCD_CloseFlicker(void)
{
LCD_Writecmd(0x0C);
}
//屏幕秒闪烁一次
void LCD_FlickerScreen(void)
{
LCD_Writecmd(0x08);//关显示
delay_nms(500);
LCD_Writecmd(0x0C);//开显示
delay_nms(500);
}

void LCD_Init(void)
{
delay_nms(200); //延时20ms
LCD_WriteInitcmd(0x38); //16*2显示,5*7点阵,8位数据
delay_nms(10);
LCD_WriteInitcmd(0x38); //16*2显示,5*7点阵,8位数据
delay_nms(5);
LCD_WriteInitcmd(0x38); //16*2显示,5*7点阵,8位数据
delay_nms(5);
LCD_WriteInitcmd(0x08); //先关显示,后开显示
delay_nms(5);
LCD_WriteInitcmd(0x06); //自动右移光标,0x04为左移光标
delay_nms(5);
LCD_WriteInitcmd(0x01); //清除LCD的显示内容
delay_nms(6);
LCD_WriteInitcmd(0x0c); //显示开,关光标;0x08为关显示
delay_nms(5);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top