标题:
那位大佬帮忙看看,这1602读数为啥不行啊
[打印本页]
作者:
我—无知小白
时间:
2021-1-24 17:15
标题:
那位大佬帮忙看看,这1602读数为啥不行啊
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^6;
sbit RW=P2^5;
sbit EN=P2^7;
uchar t, i;
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
//判断液晶忙不忙,忙则等待 、
void read_busy()
{
uchar busy;
P0=0xff; //给与高电位
RS=0; //1 数据 0命令
RW=1; //1 读 0写
do
{
EN=1; //使能
busy=P0;
EN=0;
}while(busy &0x80);
}
//1602命令
void write_cmd(uint cmd)
{
read_busy();
RS=0;
RW=0;
P0=cmd;
EN=1;
EN=0;
}
//1602 数据
void write_dat(uint dat)
{
read_busy();
RS=1;
RW=0;
P0=dat;
EN=1;
EN=0;
}
void timer_init()
{
TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X68;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}
void main()
{
timer_init();
write_cmd(0x38);//设置16*2显示
write_cmd(0x0f); //开显示
write_cmd(0x06); //指针地址右加一 0x04左
// write_cmd(0x01); //清屏
write_cmd(0x80|0x06 ); //显示地址
write_cmd(0x01); //清屏
while(1);
{
if(t<1000)
{
write_cmd(0x80 | 0x06);
write_dat(t/10+'0');
//显示十位数字
write_cmd(0x80 | 0x07);
//显示个位数字
write_dat(t%10+'0');
}
}
}
void Timer0() interrupt 1
{
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X68;
i++;
if(i==1000)
{
i=0;
t++;
}
}
作者:
wulin
时间:
2021-1-24 21:05
楼主注意变量的数据类型选择要恰当,while(1);多了分号。给你略改了一点,显示1位小数。
无标题.jpg
(16.17 KB, 下载次数: 47)
下载附件
2021-1-24 21:01 上传
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^6;
sbit RW=P2^5;
sbit EN=P2^7;
uint i;
uchar t;
/*
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}*/
//判断液晶忙不忙,忙则等待 、
void read_busy()
{
uchar busy;
P0=0xff; //给与高电位
RS=0; //1 数据 0命令
RW=1; //1 读 0写
do
{
EN=1; //使能
busy=P0;
EN=0;
}while(busy &0x80);
}
//1602命令
void write_cmd(uint cmd)
{
read_busy();
RS=0;
RW=0;
P0=cmd;
EN=1;
EN=0;
}
//1602 数据
void write_dat(uint dat)
{
read_busy();
RS=1;
RW=0;
P0=dat;
EN=1;
EN=0;
}
void timer_init()
{
TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X68;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}
void main()
{
timer_init();
write_cmd(0x38);//设置16*2显示
write_cmd(0x01); //清屏
write_cmd(0x0c); //开显示不显示光标
write_cmd(0x06); //指针地址右加一 0x04左
// write_cmd(0x01); //清屏
// write_cmd(0x80|0x06 ); //显示地址
while(1)//;
{
if(t>999)
t=0;
write_cmd(0x80+6);
write_dat(t/100%10+'0');//显示十位数字
write_dat(t/10%10+'0');//显示个位数字
write_dat('.'); //显示小数点
write_dat(t%10+'0');//显示小数
}
}
void Timer0() interrupt 1
{
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X68;
i++;
if(i==100)
{
i=0;
t++;
}
}
复制代码
作者:
人人学会单片机
时间:
2021-1-24 21:18
单片机+LCD1602显示℃
http://www.51hei.com/bbs/dpj-200685-1.html
作者:
jchrui
时间:
2021-1-25 16:40
变量的数据类型选择要实型,还要注意语法。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1