谁用过HX711啊 帮忙看看程序啊
时间: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;
uint temp;
sbit lcdrs=P3^5;
sbit lcdwr=P3^6;
sbit lcden=P3^4;
sbit hx711_dout=P2^0;
sbit hx711_sck=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
/**********延时程序*************/
void delayms(uint k)
{ uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--); }
/**********LCD1602显示程序*************/
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()
{
dula=0;
wela=0;
lcden=0;
lcdwr=0;
write_com(0x38);//设置16x2 显示5x7点阵,8位数据接口
write_com(0x0c);//设置开显示,不显示光标
write_com(0x06);//写一个字符后地址指针加1
write_com(0x01);//显示清0,数据指针清0
}
void displaywpm(void)
{
uchar code table[]="W:kg";
write_com(0x80);
write_date(table[0]);
write_date(table[1]);
write_com(0x80+0x07);
write_date(table[2]);
write_date(table[3]);
}
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;
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(); //HX711AD转换数据处理
temp=(uint)(hx711_dat/100); }//缩放long数据为int型,方便处理
void get_weight() //获取被测物体重量
{ ulong hx711_data,a;
uint get,aa;
hx711_data=ReadCount(); //HX711AD转换数据处理
get=(uint)(hx711_data/100);
if(get<(temp+4)) //判断是否有正向重量
{ while(1)
{ weight=0;
weight_shuzhi(weight); //显示重量
hx711_data=ReadCount(); //HX711AD输出数据处理
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 main()
{ get_pizhong();
init_1602();
displaywpm();
while(1)
{ get_weight();//得到重量
weight_shuzhi(weight);}//显示重量
}
#include<intrins.h>
#include<stdlib.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
int weight;
uint temp;
sbit lcdrs=P3^5;
sbit lcdwr=P3^6;
sbit lcden=P3^4;
sbit hx711_dout=P2^0;
sbit hx711_sck=P2^1;
sbit dula=P2^6;
sbit wela=P2^7;
/**********延时程序*************/
void delayms(uint k)
{ uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--); }
/**********LCD1602显示程序*************/
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()
{
dula=0;
wela=0;
lcden=0;
lcdwr=0;
write_com(0x38);//设置16x2 显示5x7点阵,8位数据接口
write_com(0x0c);//设置开显示,不显示光标
write_com(0x06);//写一个字符后地址指针加1
write_com(0x01);//显示清0,数据指针清0
}
void displaywpm(void)
{
uchar code table[]="W:kg";
write_com(0x80);
write_date(table[0]);
write_date(table[1]);
write_com(0x80+0x07);
write_date(table[2]);
write_date(table[3]);
}
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;
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(); //HX711AD转换数据处理
temp=(uint)(hx711_dat/100); }//缩放long数据为int型,方便处理
void get_weight() //获取被测物体重量
{ ulong hx711_data,a;
uint get,aa;
hx711_data=ReadCount(); //HX711AD转换数据处理
get=(uint)(hx711_data/100);
if(get<(temp+4)) //判断是否有正向重量
{ while(1)
{ weight=0;
weight_shuzhi(weight); //显示重量
hx711_data=ReadCount(); //HX711AD输出数据处理
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 main()
{ get_pizhong();
init_1602();
displaywpm();
while(1)
{ get_weight();//得到重量
weight_shuzhi(weight);}//显示重量
}