📄 lcd1602.c
字号:
#include"lcd1602.h"
#include"delay.h"
///////////////////////////////////////////////////////////////////////////////
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT=LCD_EN;
delay_nus(10);
LCD_EN_PORT=!LCD_EN;
}
///////////////////////////////////////////////////////////////////////////////
void LCD_write_command(unsigned char command) //写指令
{
delay_nus(16);
LCD_RS_PORT=!LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write();
delay_nus(30);
command=command<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写低四位
LCD_en_write();
delay_nus(30);
}
///////////////////////////////////////////////////////////////////////////////
void LCD_write_data(unsigned char Adata) //写数据
{
delay_nus(16);
LCD_RS_PORT=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=Adata&0xf0; //写高四位
LCD_en_write();
//delay_nus(30);
Adata=Adata<<4; //低四位移到高四位
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=Adata&0xf0; //写低四位
LCD_en_write();
delay_nus(30);
}
///////////////////////////////////////////////////////////////////////////////
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0)
address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_command( address);
delay_nus(30);
}
////////////////////////////////////////////////////////////////////////////////
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
//////////////////////////////////////////////////////////////////////////////
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Adata)
{
LCD_set_xy( X, Y ); //写地址
delay_nus(30);
LCD_write_data(Adata);
}
///////////////////////////////////////////////////////////////////////////////
void LCD_init(void) //液晶初始化
{
LCD_write_command(0x28);
LCD_en_write();
delay_nus(40);
LCD_write_command(0x28); //4位显示
delay_nus(3);
LCD_write_command(0x0c); //显示开
delay_nus(3);
LCD_write_command(0x01); //清屏
delay_nus(3000);
// delay_nus(3000);
}
/*------------------------------------------------
16进制转换为10进制
-------------------------------------------------*/
uchar hexotd(uchar hexdata)
{
return (hexdata&0x01)+ ((hexdata&0x02)>>1)*2+ ((hexdata&0x04)>>2)*4+ ((hexdata&0x08)>>3)*8+ ((hexdata&0x10)>>4)*16+ ((hexdata&0x20)>>5)*32;
}
#if 0
/*------------------------------------------------
时间的显示
-------------------------------------------------*/
void disp_time(uchar x,uchar y,uchar dat)
{
uchar disp_dat;
uchar d2,d3;
disp_dat=hexotd(dat);
if(disp_dat>=10)
{
d2=disp_dat/10;
d3=disp_dat%10;
LCD_write_char((y),x,(d2+48));
LCD_write_char((y+1),x,(d3+48));
}
else
{
LCD_write_char((y),x,'0');
LCD_write_char((y+1),x,(disp_dat+48));
}
}
#endif
/*------------------------------------------------
数据的显示
------------------------------------------------ */
void disp_hex(uchar x,uchar y,uchar hex)
{
uchar dat;
dat=disp_char((hex&0xf0)>>4);
// LCD_write_char(y,x,disp_char((hex&0xf0)>>4));
LCD_write_char(y,x,dat);
dat=disp_char(hex&0x0f);
LCD_write_char((y+1),x,dat);
}
/*------------------------------------------------
16进制显示
-------------------------------------------------*/
uchar disp_char(uchar dat)
{
uchar num;
switch(dat)
{
case 0x00:
num=48;
break;
case 0x01:
num=49;
break;
case 0x02:
num=50;
break;
case 0x03:
num=51;
break;
case 0x04:
num=52;
break;
case 0x05:
num=53;
break;
case 0x06:
num=54;
break;
case 0x07:
num=55;
break;
case 0x08:
num=56;
break;
case 0x09:
num=57;
break;
case 0x0A:
num=65;
break;
case 0x0B:
num=66;
break;
case 0x0C:
num=67;
break;
case 0x0D:
num=68;
break;
case 0x0E:
num=69;
break;
case 0x0F:
num=70;
break;
}
return num;
}
/*------------------------------------------------
数据的显示
------------------------------------------------ */
void disp_dat(uchar x,uchar y,uchar dat)
{
uchar disp_dat;
uchar d1,d2,d3;
disp_dat=hexotd(dat);
if(disp_dat>=100)
{
d1=disp_dat/100;
d2=(disp_dat-d1*100)/10;
d3=disp_dat%10;
LCD_write_char(y,x,(d1+48));
LCD_write_char((y+1),x,(d2+48));
LCD_write_char((y+2),x,(d3+48));
}
else
if(disp_dat>=10)
{
d2=disp_dat/10;
d3=disp_dat%10;
LCD_write_char(y,x,' ');
LCD_write_char((y+1),x,(d2+48));
LCD_write_char((y+2),x,(d3+48));
}
else
{
LCD_write_char(y,x,' ');
LCD_write_char((y+1),x,' ');
LCD_write_char((y+2),x,(disp_dat+48));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -