|
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0 //数据口
sbit LCD_EN = P3^4; //液晶使能控制
sbit LCD_RS = P3^5; //寄存器选择输入
sbit LCD_RW = P3^6; //液晶读/写控制
sbit LCD_PSB = P3^7; //串/并方式控制
sbit wela = P2^6;
sbit dula = P2^7;
sbit s5 = P3^3;
sbit s4 = P3^2;
sbit s3 = P3^1;
sbit s2 = P3^0;
sbit a4 = P2^5;
sbit a3 = P2^4;
sbit a2 = P2^2;
sbit a1 = P2^0;
sbit HX711_DOUT=P1^6;
sbit HX711_SCK=P1^7;
uchar code dis1[] = {"简易电子称 "};
uchar code dis2[] = {"重量:"};
uchar code dis3[] = {"单价:"};
uchar code dis4[] = {"总价:"};
uchar code dis5[] = {"克"};
uchar code dis6[] = {"元"};
uchar code dis7[] = {"累计金额: "};
float q; //重量
float w=1.00000; //单价
float pi=0;
float mmm=0;
unsigned long HX711_Buffer=0;
uint a,s,d,f,g,je=0,o=0,kk=0,qp=0,pq=0;
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
void delay1(uint xms) //延时函数
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char 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_Weight()
{
HX711_Buffer = HX711_Read();
HX711_Buffer = HX711_Buffer/100;
q= HX711_Buffer;
//获取实物的AD采样数值。
q= ((double)q/7.6/*+0.05*/); //计算实物的实际重量
//因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的4.30这个除数。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该数值一般在4.0-5.0之间。因传感器不同而定。
//+0.05是为了四舍五入百分位
}
void delay(int ms) //延时
{
while(ms--)
{
uchar i;
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
void lcd_wcmd(uchar cmd) //写指令
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_wdat(uchar dat) //写数据
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_pos(uchar X,uchar Y) //设置显示位置
{
uchar pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
pos = X+Y ;
lcd_wcmd(pos); //显示地址
}
void lcd_init() //12864初始化
{
LCD_PSB = 1; //并口方式
lcd_wcmd(0x34); //扩充指令操作
delay(5);
lcd_wcmd(0x30); //基本指令操作
delay(5);
lcd_wcmd(0x0C); //显示开,关光标
delay(5);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(5);
}
void xianshi1() //显示主屏幕(汉字)
{
uchar i;
lcd_pos(0,1);
for(i=0;i<12;i++)
{
lcd_wdat(dis1[i]);
}
lcd_pos(1,0);
for(i=0;i<5;i++)
{
lcd_wdat(dis2[i]);
}
lcd_pos(1,7);
for(i=0;i<2;i++)
{
lcd_wdat(dis5[i]);
}
lcd_pos(2,0);
for(i=0;i<5;i++)
{
lcd_wdat(dis3[i]);
}
lcd_pos(2,7);
for(i=0;i<2;i++)
{
lcd_wdat(dis6[i]);
}
lcd_pos(3,0);
for(i=0;i<5;i++)
{
lcd_wdat(dis4[i]);
}
lcd_pos(3,7);
for(i=0;i<2;i++)
{
lcd_wdat(dis6[i]);
}
}
void xianshi2(uchar pos,uchar z,uchar x,uchar c,uchar v) //显示
{
lcd_pos(pos,3);
if(z!=0)
{
lcd_wdat(0x30+z);
lcd_wdat(0x30+x);
}
else
{
if(x!=0)
lcd_wdat(0x30+x);
}
lcd_wdat(0x30+c);
lcd_wdat('.');
lcd_wdat(0x30+v);
}
void chuli(float q)
{
uint k,l;
q=q+0.005;
l=(uint)q;
q=q*10;
k=(uint)q;
a=l/100;
s=l%100/10;
d=l%10;
f=k%10;
}
void ping1() //主屏1 (全部)
{
float e,t;
uint r;
if(o==0) pi=0;
q=q-pi;
if(q<0) q=0;
q=q*25;
chuli(q);
xianshi2(1,a,s,d,f);
if(a1==0)
{delay1(5);while(a1==0);w=w+1,chuli(w);xianshi2(2,a,s,d,f);}
if(a2==0)
{delay1(5);while(a2==0);w=w+0.1;chuli(w);xianshi2(2,a,s,d,f);}
if(a3==0)
{delay1(5);while(a3==0);w=w-0.1;chuli(w);xianshi2(2,a,s,d,f);}
if(a4==0)
{delay1(5);while(a4==0);w=w-1;chuli(w);xianshi2(2,a,s,d,f);}
chuli(w);
xianshi2(2,a,s,d,f);
{ //显示总价
lcd_pos(3,3);
e=q*w;
r=(int)(e/1000);
t=e-r*1000;
a=r/10;
s=r%10;
if(e>=10000)
{
lcd_wdat(0x30+a);
lcd_wdat(0x30+s);
}
if(e>=1000)
{
lcd_wdat(0x30+s);
}
chuli(t);
if(a!=0||r!=0)
lcd_wdat(0x30+a);
if(e>10)
lcd_wdat(0x30+s);
lcd_wdat(0x30+d);
lcd_wdat('.');
lcd_wdat(0x30+f);
if(s2==0) //记录总价并累加
{
delay1(5);
while(s2==0);
mmm=mmm+e ;
kk++;
}
if(s4==0) //清楚累加数据
{
delay1(5);
while(s4==0);
mmm=0;
kk=0;
}
lcd_pos(0,7);
lcd_wdat(0x30+kk);
}
}
void ping2() //屏2(累计金额)
{
uchar i,r;
float t;
lcd_pos(0,1);
for(i=0;i<12;i++)
{
lcd_wdat(dis1[i]);
}
lcd_pos(1,0);
for(i=0;i<12;i++)
{
lcd_wdat(dis7[i]);
}
lcd_pos(3,7);
for(i=0;i<2;i++)
{
lcd_wdat(dis6[i]);
}
r=(int)(mmm/1000);
t=mmm-r*1000;
a=r/10;
s=r%10;
lcd_pos(2,3);
if(a!=0)
{
lcd_wdat(0x30+a);
lcd_wdat(0x30+s);
}
else
{
if(s!=0)
lcd_wdat(0x30+s);
}
chuli(t);
if(a!=0||r!=0)
lcd_wdat(0x30+a);
lcd_wdat(0x30+s);
lcd_wdat(0x30+d);
lcd_wdat('.');
lcd_wdat(0x30+f);
lcd_wdat(0x30+g);
lcd_pos(0,7); //显示累计次数
lcd_wdat(0x30+kk);
}
void qingping()
{
if(qp==0)
{
if(q>10) qp++;
}
if(qp==1)
{
if(q<10) qp++;
}
if(qp==2)
{
lcd_wcmd(0x01); //清除LCD的显示内容
delay(5);
qp++;
}
if(qp==3) qp=0;
}
void ss3()
{
uint qqq;
if(s3==0) //显示累加或关闭累加
{
delay1(5);
while(s3==0);
{
qqq=1;
lcd_wcmd(0x01); //清除LCD的显示内容
delay(5);
while(qqq)
{
ping2();
if(s3==0)
{
delay1(5);
while(s3==0);
qqq=0;
}
}
lcd_wcmd(0x01);
delay(5);
}
}
}
void ss5()
{
if(s5==0) //去皮
{
delay1(5);
while(s5==0);
o++;
if(o==2) o=0;
lcd_wcmd(0x01);//清除LCD的显示内容
delay(5);
if(o==1) pi=q;
}
}
main()
{
delay(10); //延时
wela=0;
dula=0;
lcd_init(); //初始化LCD
while(1)
{
xianshi1();
Get_Weight(); //称重
ss5(); //去皮
ping1();
qingping();
ss3(); //累计
}
}
|
评分
-
查看全部评分
|