uchar get_key()
{
uchar temp=0xff;
if(Key_Down()==1)
{
temp=key_scan();
}
return (temp) ;
}
void get_pizhong() //获取皮重,秤盘重量
{
uint j;
ulong hx711_dat;
for(j=0;j<5;j++)
delay_1602(5);
hx711_dat=ReadCount(); //HX711AD转换数据处理
temp=(uint)(hx711_dat/100);
}
void get_weight() //获取被测物体重量
{
ulong hx711_data;
uint get,aa;
hx711_data=ReadCount(); //HX711AD转换数据处理
get=(uint)(hx711_data/100);
if(get<(temp+4))
{
while(1)
{
weight="0;
TR0=0; //不允许运行
chaozhong_flag=0; //无重物,超重标志为0;
weight_shuzhi(weight);//显示重量
hx711_data=ReadCount(); //HX711AD输出数据处理
get=(uint)(hx711_data/100);
if(get">(temp+4))break;
}
}
else
aa=get-temp;
weight=(uint)((float)aa/4.4+0.05);//重量转换函数,传感器型号不同此函数要适当修改
weight=weight-qi_weight; //去皮
if(weight>=0) //重量为正
{
if(weight>5000) //报警极限范围
{
buzz=0;
TR0=1;chaozhong_flag=1;} //重量大于1000g,超重报警
else
{buzz=1;chaozhong_flag=0;TR0=0;}
weizhi_zifu(0x00,'+');
}
else {weizhi_zifu(0x00,'-');} //重量为负时显示负号,此语句可不要
}
void press()
{
nn: P3=0xf0; //保存个位
while(P3==0xf0)
{
get_weight();//得到重量
if(weight>0)
{weight_shuzhi(weight);}//显示重量
else
{weight_shuzhi(-weight);}
}
dat=get_key();
if(dat>=0&&dat<10)
{
pricetable[0]=dat;
price_shuzhi(4,pricetable[0]+0x30);//显示单价
}
if(dat==16) //去皮
{qi_weight=weight+qi_weight;goto nn;}
if(dat==15) //置零
{
zero();
goto nn;
}
//**************************************************
mm: P3=0xf0; //保存十位
while(P3==0xf0)
{
get_weight();//得到重量
if(weight>0)
{weight_shuzhi(weight);}//显示重量
else
{weight_shuzhi(-weight);}
}
dat=get_key();
if(dat>=0&&dat<10)
{
pricetable[1]=pricetable[0];
price_shuzhi(2,pricetable[1]+0x30);
pricetable[0]=dat;
price_shuzhi(4,pricetable[0]+0x30);//显示单价
}
if(dat==16) //去皮
{qi_weight=weight+qi_weight;goto mm;}
if(dat==12) //清除
{price_shuzhi(4,' ');goto nn;}
if(dat==15) //置零
{
zero();
goto nn;
}
//**************************************************
zz: P3=0xf0; //保存百位
while(P3==0xf0)
{
get_weight();//得到重量
if(weight>0)
{weight_shuzhi(weight);}//显示重量
else
{weight_shuzhi(-weight);}
}
dat=get_key();
if(dat>=0&&dat<10)
{
pricetable[2]=pricetable[1];
price_shuzhi(1,pricetable[2]+0x30);
pricetable[1]=pricetable[0];
price_shuzhi(2,pricetable[1]+0x30) ;
pricetable[0]=dat;
price_shuzhi(4,pricetable[0]+0x30);//显示
}
if(dat==12) //清除
{
pricetable[1]=pricetable[2] ;
price_shuzhi(4,pricetable[1]+0x30);
price_shuzhi(2,' ');goto mm;}
if(dat==16) //去皮
{qi_weight=weight+qi_weight;goto zz;}
if(dat==15) //置零
{
zero();
goto nn;
}
//**************************************************
xx:
do{
P3=0xf0; //清除百位
while(P3==0xf0)
{
get_weight();//得到重量
if(weight>0)
{weight_shuzhi(weight);}//显示重量
else
{weight_shuzhi(-weight);}
}
dat=get_key();
if(dat==16) //去皮
{qi_weight=weight+qi_weight;goto xx;}
if(dat==12) //清除
{
pricetable[0]=pricetable[1];
price_shuzhi(4,pricetable[0]+0x30) ;
pricetable[1]=pricetable[2];
price_shuzhi(2,pricetable[1]+0x30);
price_shuzhi(1,' ');goto zz;}
if(dat==15) //置零
{
zero();
goto nn;
}
}while(dat!=14);
}
void main()
{
init(); //初始化定时器
init_1602();//初始化lcd
system(); //显示初始化
dat=get_key(); //获得键
M1=1;M2=1;//电机初始化
if(dat==13) //启动键
{
clcye: M1=1;M2=0; //电机1工作
price_shuzhi(6,'M'); //显示
price_shuzhi(7,'1');
while(1) //循环
{
if(dat==11)break; //停止键
get_pizhong(); //获得皮重
press();//获得键盘输入值 函数调用
jianzhi=(uint)((pricetable[2]*10)+(pricetable[1])+pricetable[0]/10);//转换
if((weight>=(float)jianzhi-0.2)&&(weight<=(float)jianzhi+0.2)) //结果比较
{
TR0=1; //开启计数
M1=0;M2=1; //电机2工作 2min
price_shuzhi(6,'M');
price_shuzhi(7,'2');
if (count==2400) //延时间 2min
{
count=0;
TR0=0;
goto clcye;
}
}
}
}
}
void t0() interrupt 1 //定时器t0中断
{
TH0=0x3c;
TL0=0xb0; //定时50ms
count++;
}
void t1() interrupt 3 //定时器t1中断
{
TH1=0x3c;
TL1=0xb0; //定时50ms
if((weight>5000)&&(chaozhong_flag==1))
{
buzz=1;
}
else
{chaozhong_flag=0;}
}
为什么结果比较的时候总是实现不了? |