📄 lcd1602.h
字号:
/*********************************************
文件名 :LCD1602.h
功能 : 1602字符型液晶显示驱动
开发环境:WinAVR
硬件环境:ATMEGA32/AVR开发板
创建日期:2008-3-27
最后修改:2008-3-27
作 者:bjj9217@163.com
日 志:
**********************************************/
/*********************************************
RT1602C接口
PA7-------DB7(BF)
PA6-------DB6
PA5-------DB5
PA4-------DB4
PA3-------E
PA2-------RW
PA1-------RS
**********************************************/
#ifndef _LCD1602_H_
#define _LCD1602_H_ 1
#include<avr/io.h>
#include<util/delay.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
//#define sbi(PORT,BIT) (PORT|=(1<<(BIT)))
//#define cbi(PORT,BIT) (PORT&=~(1<<(BIT)))
#define chkbit(PORT,BIT) (PORT&(1<<(BIT)))
#define LCD_PORT PORTA //LCD_DATA_PORT
#define LCD_DDR DDRA
#define LCD_PIN PINA
#define RS PA1
#define RW PA2
#define E PA3
#define BF PA7
//定义液晶16*2
#define L 16
#define H 2
/************************
* 检测忙信号
************************/
void LCD1602ReadBusy(void)
{
LCD_DDR&=~(1<<BF); //输入
LCD_PORT|=1<<BF;
LCD_PORT&=~(1<<RS);
LCD_PORT|=1<<RW;
LCD_PORT|=1<<E;
while(chkbit(LCD_PIN,BF)); //查询
LCD_PORT&=~(1<<E);
LCD_PORT&=~(1<<RS);
LCD_PORT&=~(1<<RW);
LCD_PORT|=1<<BF;//输出
_delay_us(1);
}
/************************
* lcd1602_写指令
* data: 要写到LCD寄存器的指令值
************************/
void WriteComand(unsigned char Comand)
{
LCD1602ReadBusy();//查询状态
LCD_PORT&=~(1<<RS);
LCD_PORT&=~(1<<RW);
LCD_DDR|=0xFF; //设置为输出
LCD_PORT|=1<<E;
LCD_PORT&=0x0F; //高四位清零,即数据线清零
LCD_PORT|=Comand&0xF0; //输出高四位
LCD_PORT&=~(1<<E);
LCD_DDR|=0xFF; //设置为输出
LCD_PORT|=1<<E;
LCD_PORT&=0x0F; //高四位清零,即数据线清零
LCD_PORT|=(Comand<<4)&0xF0; //输出低四位
LCD_PORT&=~(1<<E);
}
/************************
* lcd1602_写数据
* data: 要写到LCD寄存器的数据值
************************/
void WriteData(unsigned char data)
{
LCD1602ReadBusy();//查询状态
LCD_PORT|=1<<RS;
LCD_PORT&=~(1<<RW);
LCD_DDR|=0xFF; //设置为输出
LCD_PORT|=1<<E;
LCD_PORT&=0x0F; //高四位清零,即数据线清零
LCD_PORT|=data&0xF0; //输出高四位
LCD_PORT&=~(1<<E);
LCD_DDR|=0xFF; //设置为输出
LCD_PORT|=1<<E;
LCD_PORT&=0x0F; //高四位清零,即数据线清零
LCD_PORT|=(data<<4)&0xF0; //输出低四位
LCD_PORT&=~(1<<E);
}
/************************
* 光标定位
* x,y: 位置
************************/
void Locate_xy(unsigned char x,unsigned char y)
{
unsigned char address = (y == 0) ? (0x80 + x) : (0xc0 + x);
WriteComand(address);
}
/************************
* 显示一个字符
* data: 要显示的字符,为ASCII码
************************/
void DispChar(unsigned char data)
{
WriteData(data);
}
/************************
* 在(x,y)位置显示一个字符
* x,y: 位置
* data: 要显示的字符,为ASCII码
************************/
void XYChar(unsigned char x,unsigned char y,unsigned char data)
{
Locate_xy(x,y);
WriteData(data);
}
/************************
* 显示一个字符串
* String: 要显示的字符串
************************/
void DispString(unsigned char *String)
{
unsigned int i=0;
while(String[i]!='\0')WriteData(String[i++]);
}
/************************
* 在(x,y)位置显示一个字符串
* x,y: 位置
* String: 要显示的字符串
************************/
void XYString(unsigned char x,unsigned char y,unsigned char *String)
{
unsigned int i=0;
Locate_xy(x,y);
//DispString(*String);
while(String[i]!='\0')WriteData(String[i++]);
}
/************************
* 显示一个0-9的数字
* num: 要显示的数字 0~9
************************/
void DispNum(unsigned char num)
{
WriteData(num+0x30);
}
/************************
* 在(x,y)位置显示一个0-9的数字
* x,y: 位置
* num: 要显示的数字 0~9
************************/
void XYNum(unsigned char x,unsigned char y,unsigned char num)
{
Locate_xy(x,y);
WriteData(num+0x30);
}
/************************
* 显示十六进制
* hex: 要显示的数,为十进制数 0~255
************************/
void DispHex(unsigned char hex)
{
unsigned char hex_h;
unsigned char hex_l;
hex_h=hex/16;
hex_l=hex%16;
WriteData('0');
WriteData('x');
if(hex_h>=10) hex_h=hex_h-10+'A';
else hex_h+=0x30;
WriteData(hex_h);
if(hex_l>=10) hex_l=hex_l-10+'A';
else hex_l+=0x30;
WriteData(hex_l);
}
/************************
* 在(x,y)位置显示十六进制
* x,y: 位置
* hex: 要显示的数,为十进制数 0~255
************************/
void XYHex(unsigned char x,unsigned char y,unsigned char hex)
{
Locate_xy(x,y);
DispHex(hex);
}
/************************
* 在(x,y)位置显示5位数字
* x,y: 位置
* data: 5位数字
* dot: 小数点位置
************************/
void DispInit(unsigned char x,unsigned char y,unsigned int data,unsigned char dot)
{
unsigned char bcd0;
unsigned char bcd1;
unsigned char bcd2;
unsigned char bcd3;
unsigned char bcd4;
Locate_xy(x,y);
bcd4=(data/10000);
bcd3=(data%10000)/1000;
bcd2=(data%1000)/100;
bcd1=(data%100)/10;
bcd0=data%10;
if(dot==4)
{
DispNum(bcd4);
WriteData('.');
DispNum(bcd3);
DispNum(bcd2);
DispNum(bcd1);
DispNum(bcd0);
}
else if(dot==3)
{
if(bcd4!=0) DispNum(bcd4); //为0时不显示
DispNum(bcd3);
WriteData('.');
DispNum(bcd2);
DispNum(bcd1);
DispNum(bcd0);
}
else if(dot==2)
{
if(bcd4!=0) DispNum(bcd4); //为0时不显示
if((bcd4!=0)||((bcd4==0)&&(bcd3!=0))) DispNum(bcd3);
DispNum(bcd2);
WriteData('.');
DispNum(bcd1);
DispNum(bcd0);
}
else if(dot==1)
{
if(bcd4!=0) DispNum(bcd4); //为0时不显示
if((bcd4!=0)||((bcd4==0)&&(bcd3!=0))) DispNum(bcd3);
if((bcd4!=0)||((bcd4==0)&&(bcd3!=0))||((bcd4==0)&&(bcd3==0)&&(bcd2!=0))) DispNum(bcd2);
DispNum(bcd1);
WriteData('.');
DispNum(bcd0);
}
else
{
DispNum(bcd4);
DispNum(bcd3);
DispNum(bcd2);
DispNum(bcd1);
DispNum(bcd0);
}
}
/************************
* 液晶初始化
************************/
void LCD_Init(void)
{
WriteComand(0x30);
WriteComand(0x30);
WriteComand(0x30);
WriteComand(0x02);
WriteComand(0x28);/*4位总线....*/
WriteComand(0x01);/*清屏*/
WriteComand(0x02);/*归位*/
WriteComand(0x0c);/*显示开,光标开,闪烁开*/
WriteComand(0x06);/*光标自动右移,显示文字不动*/
WriteComand(0x80);/*设置显示地址*/
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -