求各位大神解读,基于51单片机的电子称程序lcd亮,但不显示数字
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
#include<stdlib.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
int weight;
double money;
uint temp;
uchar button;
sbit lcdrs=P0^7;
sbit lcdwr=P0^6;
sbit lcden=P0^5;
sbit hx711_dout=P0^0;
sbit hx711_sck=P0^1;
sbit spk=P3^6;
//LCD1602 显示程序
void delayms(uint k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date)
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init_1602()
{
lcden=0;
lcdwr=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void displaywpm(void)
{
uchar num;
uchar code table[]="W:kgP:$Money:$";
write_com(0x80);
write_date(table[0]);
write_date(table[1]);
write_com(0x80+0x07);
write_date(table[2]);
write_date(table[3]);
write_com(0x80+0x0a);
write_date(table[4]);
write_date(table[5]);
write_com(0x80+0x0f);
write_date(table[6]);
write_com(0x80+0x40);
for(num=0;num<6;num++)
write_date(table[num+7]);
write_com(0x80+0x4a);
write_date(table[13]);
}
void weight_shuzhi(uint aaa)
{
uchar idata weighttable[4]={0,0,0,0};
weighttable[3]=aaa%10;
weighttable[2]=aaa/10%10;
weighttable[1]=aaa/100%10;
weighttable[0]=aaa/1000;
write_com(0x80+0x02);
write_date(0x30+weighttable[0]);
write_com(0x80+0x03);
write_date('.');
write_com(0x80+0x04);
write_date(0x30+weighttable[1]);
write_com(0x80+0x05);
write_date(0x30+weighttable[2]);
write_com(0x80+0x06);
write_date(0x30+weighttable[3]);
write_com(0x0c);
}
// hx711 程序
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
ulong ReadCount(void) //增益128
{
ulong count;
uchar i;
hx711_dout=1;
Delay__hx711_us();
hx711_sck=0;
count=0;
while(hx711_dout);
for(i=0;i<24;i++)
{
hx711_sck=1;
count=count<<1;
hx711_sck=0;
if(hx711_dout)
count++;
}
hx711_sck=1;
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
Delay__hx711_us();
hx711_sck=0;
return(count);
}
//皮重,获得重量的程序
void get_pizhong() //获取皮重,秤盘重量
{
uint j;
ulong hx711_dat;
for(j=0;j<5;j++)
delayms(1);
hx711_dat=ReadCount();
temp=(uint)(hx711_dat/100);
}
void get_weight() //获取被测物体重量
{
ulong hx711_data,a;
uint get,aa;
hx711_data=ReadCount();
get=(uint)(hx711_data/100);
if(get<(temp+4))
{
while(1)
{
weight=0;
weight_shuzhi(weight);
hx711_data=ReadCount();
get=(uint)(hx711_data/100);
if(get>(temp+4))break;
}
}
a=ReadCount();
aa=(uint)(a/100)-temp;
weight=(uint)((float)aa/2.15+0.05);
}
//蜂鸣器
void delayus(uint z)
{
while(z--)
{
_nop_();
}
}
void sound()
{
uint j;
for(j=0;j<200;j++)
{
delayus(200);
spk=!spk;
}
spk=0;
for(j=0;j<200;j++)
{
delayms(1);
}
}
void main()
{
get_pizhong();
init_1602();
displaywpm();
while(1)
{
get_weight();
if(weight>0)
{
weight_shuzhi(weight);
}
else
{
weight_shuzhi(0);
}
if(weight>2000)
sound();
}
}
在过重情况下蜂鸣器会响,lcd接上电源就会亮,但是没有显示。求大神解答,十分感谢!
#include<intrins.h>
#include<stdlib.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
int weight;
double money;
uint temp;
uchar button;
sbit lcdrs=P0^7;
sbit lcdwr=P0^6;
sbit lcden=P0^5;
sbit hx711_dout=P0^0;
sbit hx711_sck=P0^1;
sbit spk=P3^6;
//LCD1602 显示程序
void delayms(uint k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date)
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init_1602()
{
lcden=0;
lcdwr=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void displaywpm(void)
{
uchar num;
uchar code table[]="W:kgP:$Money:$";
write_com(0x80);
write_date(table[0]);
write_date(table[1]);
write_com(0x80+0x07);
write_date(table[2]);
write_date(table[3]);
write_com(0x80+0x0a);
write_date(table[4]);
write_date(table[5]);
write_com(0x80+0x0f);
write_date(table[6]);
write_com(0x80+0x40);
for(num=0;num<6;num++)
write_date(table[num+7]);
write_com(0x80+0x4a);
write_date(table[13]);
}
void weight_shuzhi(uint aaa)
{
uchar idata weighttable[4]={0,0,0,0};
weighttable[3]=aaa%10;
weighttable[2]=aaa/10%10;
weighttable[1]=aaa/100%10;
weighttable[0]=aaa/1000;
write_com(0x80+0x02);
write_date(0x30+weighttable[0]);
write_com(0x80+0x03);
write_date('.');
write_com(0x80+0x04);
write_date(0x30+weighttable[1]);
write_com(0x80+0x05);
write_date(0x30+weighttable[2]);
write_com(0x80+0x06);
write_date(0x30+weighttable[3]);
write_com(0x0c);
}
// hx711 程序
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
ulong ReadCount(void) //增益128
{
ulong count;
uchar i;
hx711_dout=1;
Delay__hx711_us();
hx711_sck=0;
count=0;
while(hx711_dout);
for(i=0;i<24;i++)
{
hx711_sck=1;
count=count<<1;
hx711_sck=0;
if(hx711_dout)
count++;
}
hx711_sck=1;
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
Delay__hx711_us();
hx711_sck=0;
return(count);
}
//皮重,获得重量的程序
void get_pizhong() //获取皮重,秤盘重量
{
uint j;
ulong hx711_dat;
for(j=0;j<5;j++)
delayms(1);
hx711_dat=ReadCount();
temp=(uint)(hx711_dat/100);
}
void get_weight() //获取被测物体重量
{
ulong hx711_data,a;
uint get,aa;
hx711_data=ReadCount();
get=(uint)(hx711_data/100);
if(get<(temp+4))
{
while(1)
{
weight=0;
weight_shuzhi(weight);
hx711_data=ReadCount();
get=(uint)(hx711_data/100);
if(get>(temp+4))break;
}
}
a=ReadCount();
aa=(uint)(a/100)-temp;
weight=(uint)((float)aa/2.15+0.05);
}
//蜂鸣器
void delayus(uint z)
{
while(z--)
{
_nop_();
}
}
void sound()
{
uint j;
for(j=0;j<200;j++)
{
delayus(200);
spk=!spk;
}
spk=0;
for(j=0;j<200;j++)
{
delayms(1);
}
}
void main()
{
get_pizhong();
init_1602();
displaywpm();
while(1)
{
get_weight();
if(weight>0)
{
weight_shuzhi(weight);
}
else
{
weight_shuzhi(0);
}
if(weight>2000)
sound();
}
}
在过重情况下蜂鸣器会响,lcd接上电源就会亮,但是没有显示。求大神解答,十分感谢!
仔细看一下,hx711有效位数,还有无噪声位数,24位不能全部用完,一般都是16位,向右移8位.count=count^0x800000后面应该count=count>>8。还有这个是加正负电压,如果都是正向电压,输出ad减小一半。移位后应该在32768左右。
是什么都不显示,还是只有数字不显示,如果什么都不显示,那就是液晶初始化的问题,如果只是数字不显示,那就随意给weight_shuzhi(weight);一个数值,看能不能正确显示,如果不能就是数据格式转换的问题了。
首先一定要确保液晶能显示你想显示的任意内容。