#include<reg51.h>
#include<intrins.h>
#include <absacc.h>
#include <math.h>
#include<lm4229.h>
#define uchar unsigned char
#define uint unsigned int
sbit ADCS =P3^5;
sbit ADDI =P3^7;
sbit ADDO =P3^7;
sbit ADCLK =P3^6;
uint x1,y1,z1=0,w1;
uchar ad_data,k,n,m,e,num,s; //采样值存储
uint temp1;
sbit beep =P3^0;
char press_data; //标度变换存储单元
float press;
unsigned char ad_alarm,temp; //报警值存储单元
unsigned char abc[5]={48,46,48,48,48};
unsigned char price_all[5]={48,48,46,48,48};
unsigned char price_all0[10]={48,48,46,48,48,' ',' ',' ',' ',};
float price_unit[10]={5.5,2.8,3.6,4.5,5.8,6.8,7.9,8.0,9.5,0}; //商品初始单价
uchar price_danjia[9]={48,46,48,48,' ',' ',' ',' ',};
float price;
uint price_temp1,price_temp2;
uint price_temp3=0; //商品总价
uchar Adc0832(unsigned char channel);
void alarm(void);
void data_pro(void);
void delay(uint k);
void keyscan();
void price_jisuan();
void price_jisuan1();
void price1();
/************
主函数
************/
void main(void)
{
delay(500); //系统延时500ms启动
//ad_data=0; //采样值存储单元初始化为0
lcd_init(); //显示初始化
write_lcd(0,8,"欢迎使用电子秤");
while(1)
{
ad_data =Adc0832(0); //采样值存储单元初始化为0
alarm();
data_pro(); //读取重量
keyscan(); //查询商品种类
write_lcd(40,0,"------------------------------");
write_lcd(56,0,"单价:");
write_lcd(56,11,price_danjia);
write_lcd(56,20,"元/千克");
write_lcd(72,0,"重量:");
write_lcd(72,11,abc);
write_lcd(72,20,"千克");
write_lcd(88,0,"价格:"); //计算出价格
write_lcd(88,10,price_all);
write_lcd(88,20,"元");
write_lcd(104,0,"总价:"); //计算出价格
write_lcd(104,10,price_all0);
write_lcd(104,20,"元");
}
}
/************
读ADC0832函数
************/
//采集并返回
uchar Adc0832(unsigned char channel) //AD转换,返回结果
{
uchar i=0;
uchar j;
uint dat=0;
uchar ndat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
_nop_();
_nop_();
ADCS=0;//拉低CS端
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿1
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=channel&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿2
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿3
ADDI=1;//控制命令结束
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO;//收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次时钟脉冲
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i++)
{
j=0;
j=j|ADDO;//收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次时钟脉冲
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;//拉搞CS端
ADCLK=0;//拉低CLK端
ADDO=1;//拉高数据端,回到初始状态
dat<<=8;
dat|=ndat;
return(dat); //return ad k
}
void data_pro(void)
{
unsigned int;
if(0<ad_data<256)
{
int vary=ad_data;
press=(0.019531*vary);
temp1=(int)(press*1000); //放大1000倍,便于后面的计算
abc[0]=temp1/1000+48; //取压力值百位
abc[1]=46;
abc[2]=(temp1%1000)/100+48; //取压力值十位
abc[3]=((temp1%1000)%100)/10+48; //取压力值个位
abc[4]=((temp1%1000)%100)%10+48; //取压力值十分位
}
}
/*****************报警子函数*******************/
void alarm(void)
{ int i;
if(ad_data>=255)
{for(i=0;i<100;i++)
{
beep = ~beep;
delay(3);
}
}
//则启动报警
else
beep=1;
}
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}
//键盘服务程序
void keyscan()
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:num=1,price=price_unit[0], write_lcd(24,0,"名称:杏仁");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0xde:num=2,price=price_unit[1],write_lcd(24,0,"名称:李子");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0xbe:num=3,price=price_unit[2],write_lcd(24,0,"名称:草莓");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0x7e:num=4,price=price_unit[3],write_lcd(24,0,"名称:葡萄");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:num=5,price=price_unit[4],write_lcd(24,0,"名称:西瓜");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0xdd:num=6,price=price_unit[5],write_lcd(24,0,"名称:苹果");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0xbd:num=7,price=price_unit[6],write_lcd(24,0,"名称:雪梨");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0x7d:num=8,price=price_unit[7],write_lcd(24,0,"名称:核桃");price1();price_jisuan(); price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:num=9,price=price_unit[8],write_lcd(24,0,"名称:香蕉");price1();price_jisuan();price_temp3=price_temp3+price_temp2;price_jisuan1();
break;
case 0xdb:num=10,price=price_unit[9],write_lcd(24,0,"名称:商品");price1();price_jisuan();price_temp3=0;price_jisuan1();
break;
case 0xbb:num=11,price=price_unit[1];price1();price_jisuan();//price_jisuan1();
break;
case 0x7b:num=12,price=price_unit[2];price1();price_jisuan();//price_jisuan1();
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:num=13,price=price_unit[3];price1();price_jisuan();price_temp3=price_temp2;price_jisuan1();
break;
case 0xd7:num=14,price=price_unit[4];price1();price_jisuan();price_temp3=price_temp2;price_jisuan1();
break;
case 0xb7:num=15,price=price_unit[5];price1();price_jisuan();price_temp3=price_temp2;price_jisuan1();
break;
case 0x77:num=16,price=price_unit[6];price1();price_jisuan();price_temp3=price_temp2;price_jisuan