2012
年第四届全国电子专业人才设计与技能大赛
“自动售水机”设计任务书
1. 系统框图
接下来我们将任务分块:
1. 按键控制单元设定按键 S7 为出水控制按键,当 S7 按下后,售水机持续出水(继电器接通,指示灯 L10 点亮)。设定按键 S6 为停水控制按键,当 S6 按下后,停止出水(继电器断开,指示灯 L10 熄灭)。
# include "reg52.h"
# include "iic.h"
# include "onewire.h"
# include "ds1302.h"
# include "intrins.h"
//S7°´Ï£¬L10µãÁÁ¡£S6°´Ï£¬L10ϨÃð
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
//ledµÆ
sbit L1=P0^0;
sbit L2=P0^1;
sbit L3=P0^2;
sbit L4=P0^3;
sbit L5=P0^4;
sbit L6=P0^5;
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0X1f) | 0x80;
break;
case 5:
P2 = (P2 & 0X1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0X1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0X1f) | 0xe0;
break;
case 0:
P2 = (P2 & 0X1f) | 0x00;
break;
}
}
void Delay(unsigned int t)
{
while(t--);
}
void Scankey()
{
SelectHC573(5);
if(S7 == 0)
{
Delay(500);
if(S7 == 0)
{
P0 = 0xff;
}
}
if(S6 == 0)
{
Delay(500);
if(S6 == 0)
{
P0 = 0x00;
}
}
SelectHC573(0);
}
void main()
{
while(1)
{
Scankey();
}
}
2.
数码管显示单元
任务:通过 4 位数码管 DS1 显示费率 ,单位为元 / 升,保留 2 位有效数字;通过 4 位数码管 DS2 显示当前出水总量 (出水时,单位为升)和总价(停止时,单位为元):按下出水按键 S7 后,清除数码管 DS2 显示数据,数码管 DS2 实时显示出水量 ( 保留两位有效数字 ) ,在出水状态下,再次按下 S7 ,不会影响出水状态,直到按下停止按键 S6 为止;按下停止出水按键 S6 后,数码管 DS2 显示总价(保留两位有效数字)。
这个也就是数码管显示,我们也是可以分来进行
当按下S7时:
当按下S6时:
系统说明:
1. 假定水价为 0.5 元 / 升,出水速度为 100 毫升 / 秒;2. 一次出水总量达到 99.99L 时,继电器自动断开,数码管显示 DS2 显示价格。
我们先来实现出水量,通过定时器来计数出水量,每100ml/s
# include "reg52.h"
# include "Scankey.h"
# include "iic.h"
# include "onewire.h"
# include "ds1302.h"
# include "intrins.h"
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
/* ±äÁ¿ÉùÃ÷Çø */
unsigned char code SMG_duanma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
unsigned int count = 0;
unsigned int water_value = 0; //³öË®Á¿
unsigned int water_money = 0;
unsigned int water_count = 0;
/* ³öË®Á¿¼ÆËã */
void Init_Timer0() //¶¨Ê±50ms
{
TMOD = 0x01;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void SeviceTimer0() interrupt 1
{
//50ms £¬100ms¾Í¿ÉÒÔÔÚ×îºó
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
count++;
water_value = count/2;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
/*************ÊýÂë¹Ü***********/
void DisplaySMG(unsigned char dat,unsigned int pos) //posλÖã¬datÄÚÈÝ
{
SelectHC573(7);
P0 = 0xff;
SelectHC573(6);
P0 = (0x01 << pos);
SelectHC573(7);
P0 = dat;
SelectHC573(0);
}
/******ÏÂÃæÉè¼ÆÁ½¸öÊýÂë¹ÜµÄÖµ£¬ÎÒÃÇ°ÑÁ½±ß·Ö¿ª¼ÆË㣬ÎÒÃÇÏÈ¿´DS2 */
void ShowSMG_DS2_Water()
{
if(water_value<10)
{
DisplaySMG(SMG_duanma[water_value],7);
DelaySMG(300);
}
else if(water_value>=10 && water_value <100)
{
DisplaySMG(SMG_duanma[water_value%10],7);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/10],6);
DelaySMG(300);
}
else if(water_value>=100 && water_value <1000)
{
DisplaySMG(SMG_duanma[water_value%10],7);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/10%10],6);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/100],5);
DelaySMG(300);
}
else if(water_value >= 1000)
{
DisplaySMG(SMG_duanma[water_value%10],7);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/10%10],6);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/100%10],5);
DelaySMG(300);
DisplaySMG(SMG_duanma[water_value/1000],4);
DelaySMG(300);
}
}
void main()
{
Init_Timer0();
Scankey();
while(1)
{
ShowSMG_DS2_Water();
}
}