|
电子秤的程序及仿真图
目录
电子技能课程设计报告书......................................................................................... 1
基于AT89C51的数字电子秤的设计...................................................................... 1
1、设计目的......................................................................................................... 1
2、设计的主要内容和要求................................................................................ 1
3、 整体设计方案............................................................................................... 1
3.1设计方案................................................................................................... 1
3.2工作原理................................................................................................... 1
4、 硬件电路的设计........................................................................................... 2
5、软件设计......................................................................................................... 3
5.1主程序设计.............................................................................................. 3
5.2 LM4229液晶显示驱动程序.................................................................. 4
5.3 ADC0832采样程序................................................................................. 5
5.4 4*4键盘程序............................................................................................ 6
6、系统仿真......................................................................................................... 6
7、 使用说明 ..................................................................................................... 9
8、设计总结......................................................................................................... 9
参考文献................................................................................................................ 9
附录A ................................................................................................................ 10
单片机源程序如下:
- #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[6]={48,48,46,48,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[5]={48,46,48,48,48};
- float price;
- uint price_temp1,price_temp2; //商品总价
- uchar Adc0832(unsigned char channel);
- void alarm(void);
- void data_pro(void);
- void delay(uint k);
- void keyscan();
- void disp_init();
- void price_jisuan();
- /************
- 主函数
- ************/
- void main(void)
- {
- delay(500); //系统延时500ms启动
- //ad_data=0; //采样值存储单元初始化为0
- lcd_init(); //显示初始化
- disp_init(); //开始进入欢迎界面
- delay(1000); //延时进入称量画面
- clear_lcd(0,4,40);
- clear_lcd(16,0,100);
- clear_lcd(28,0,40);
- clear_lcd(44,0,100);
- clear_lcd(56,0,40);
- clear_lcd(72,0,100);
- clear_lcd(84,0,40);
- clear_lcd(100,0,100);
- clear_lcd(112,0,40);
- 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,"总 价:");
- price_jisuan(); //计算出价格
- write_lcd(88,10,price_all);
- write_lcd(88,20,"元");
- write_lcd(112,0,"设计学生: 0612201*16 朱珍奇");
- }
- }
- /************
- 读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)
- {
- if(ad_data>=256)
- beep=0; //则启动报警
-
- else
- beep=1;
- }
- void delay(uint k)
- {
- uint i,j;
- for(i=0;i<k;i++)
- for(j=0;j<100;j++);
- }
- //开机欢迎界面
- void disp_init()
- {
- write_lcd(0,8,"欢迎使用电子秤");
- write_lcd(16,0,"------------------------------");
- write_lcd(28,0,"设计学生:朱珍奇");
- write_lcd(44,0,"------------------------------");
- write_lcd(56,0,"班级学号:0612201*16");
- write_lcd(72,0,"------------------------------");
- write_lcd(84,0,"指导老师:龚伦峰");
- write_lcd(100,0,"------------------------------");
- write_lcd(112,0,"设计日期:2009年6月16日");
-
- }
- //键盘服务程序
- 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,"名 称: 杏 仁");
- break;
- case 0xde:num=2,price=price_unit[1],write_lcd(24,0,"名 称: 李 子");
- break;
- case 0xbe:num=3,price=price_unit[2],write_lcd(24,0,"名 称: 草 莓");
- break;
- case 0x7e:num=4,price=price_unit[3],write_lcd(24,0,"名 称: 葡 萄");
- 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,"名 称: 西 瓜");
- break;
- case 0xdd:num=6,price=price_unit[5],write_lcd(24,0,"名 称: 苹 果");
- break;
- case 0xbd:num=7,price=price_unit[6],write_lcd(24,0,"名 称: 雪 梨");
- break;
- case 0x7d:num=8,price=price_unit[7],write_lcd(24,0,"名 称: 核 桃");
- 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,"名 称: 香 蕉");
- break;
- case 0xdb:num=10,price=price_unit[9],write_lcd(24,0,"名 称: 商品代码");
- break;
- case 0xbb:num=11,price=price_unit[1];
- break;
- case 0x7b:num=12,price=price_unit[2];
- 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)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载 带仿真 程序 课程设计报告书:
dfg.rar
(1.1 MB, 下载次数: 132)
|
|