一、系统方案
1、本设计采用51单片机作为主控器。
2、商品选择,购买数量选择,投币,液晶1602显示。
3、电机模拟出货。
三、单片机软件设计
1、首先是系统初始化
void lcd_1602_init() //LCD1602初始化函数
{
lcd_en=0;
lcd_1602_write_com(0x38);
lcd_1602_write_com(0x0e);
lcd_1602_write_com(0x06);
lcd_1602_write_com(0x01);
}
2、液晶显示程序
void lcd_1602_write_com(uchar com) //写命令
{
rs=0;
rw=0;
P2=com;
lcd_1602_delay(5);
lcd_en=1;
lcd_1602_delay(5);
lcd_en=0;
}
void lcd_1602_write_data(uchar date) //写数据
{
rs=1;
rw=0;
P2=date;
lcd_1602_delay(5);
lcd_en=1;
lcd_1602_delay(5);
lcd_en=0;
}
3、按键程序
unsigned char key(void) //按键响应函数
{
unsigned char key,key1,key2;
P1=0XF0;
if((P1&0XF0)!=0XF0) //ked down
{
delay1(10); //延时去抖动
if((P1&0XF0)!=0XF0) //仍然有键按下
{
key1=P1&0XF0; //读入列线值
P1=0X0F;
key2=P1&0X0F;
key=key1|key2;
switch(key) //按键映射
{
case 0xee: e=0x00;break;
case 0xde: e=0x01;break;
case 0xbe: e=0x02;break;
case 0x7e: e=0x03; break;
case 0xed: e=0x04;break;
case 0xdd: e=0x05;break;
case 0xbd: e=0x06;break;
case 0x7d: e=0x07; break;
case 0xeb: e=0x08;break;
case 0xdb: e=0x09;break;
case 0xbb: e=0x0a;break;
case 0x7b: e=0x0b; break;
case 0xe7: e=0x0c;break;
case 0xd7: e=0x0d;break;
case 0xb7: e=0x0e;break;
case 0x77: e=0x0f; break;
}
while((P1&0X0F)!=0X0F);
delay1(10);
return 1;
}
}
return 0;
}
4、核心算法程序
/******************************************************/
/main/
void main()
{
unsigned char ok = 0, water_type = -1, water_is_choose = 0;
char water_count = 0;
unsigned int flag = 0, money = 0, need_money;
lcd_1602_init();
again:
ok = 0; water_type = -1; water_count = 0; water_is_choose = 0;
flag = 0; money = 0; need_money = 0;
lcd1602_info_display(WAIT_SALE, 0, 0);
while(!ok) //choose the water and count
{
flag=key();
if(flag)
{
switch(e) //不同的按键进行不同的操作
{
case(0x00):
water_type++;
water_is_choose = 1;
if(water_type == 8)
{
water_type = 0;
}
P0 = water_change(0xff, water_type);
break;
case(0x01):
water_type–;
water_is_choose = 1;
if(water_type == -1 || water_type == -2)
{
water_type = 7;
}
P0 = water_change(0xff, water_type);
break;
case(0x02):
if(water_is_choose == 1)
{
water_count++;
if(water_count > water_total[water_type])
{
water_count = water_total[water_type];
}
}
break;
case(0x03):
if(water_is_choose == 1)
{
water_count–;
if(water_count < 0)
{
water_count = 0;
}
}
break;
case(0x0e): // 返回
goto again;
break;
case(0x0f): // 确认
if(water_is_choose == 1)
{
ok=1;
}
break;
default:
break;
}
lcd_1602_saling(water_type, water_count);
}
}
//choose ok: calculate the money //计算钱的总数
need_money = water_count * water_price[water_type];
lcd_1602_inputing(need_money, money);
ok = 0;
while(!ok) //get money
{
flag=key();
if(flag)
{
switch(e)
{
case(0x04):
money=money+1;
break;
case(0x05):
money=money+5;
break;
case(0x06):
money=money+10;
break;
case(0x07):
money=money+20;
break;
case(0x08):
money=money+50;
break;
case(0x09):
money=money+100;
break;
case(0x0e): //back
lcd1602_info_display(CHANGE, money, CANCLE);
P0 = 0xff;
delay1(10000);
goto again;
break;
default:
break;
}
//show money
lcd_1602_inputing(need_money, money); //投币系统
if(money>0)
{if(money >= need_money)
{
P3=0x7f;
ok=0; //break and show back money
//show back money
lcd1602_info_display(CHANGE, money-need_money, NO_DATA);
// out_water();
delay1(10000);
P0 = 0xff;
P3=0xff;
goto again;
}
}
}
}
}
四、 proteus仿真设计
Proteus软件是一款应用比较广泛的工具,它可以在没有硬件平台的基础上通过自身的软件仿真出硬件平台的运行情况,这样就可以通过软件仿真来验证我们设计的方案有没有问题,如果有问题,可以重新选择器件,连接器件,直到达到我们设定的目的,避免我们搭建实物的时候,如果当初选择的方案有问题,我们器件都已经焊接好了,再去卸载下去,再去焊接新的方案的器件,测试,这样会浪费人力和物力,也给开发者带来一定困惑,Proteus仿真软件就很好的解决这个问题,我们在设计之初,就使用该软件进行模拟仿真,测试,选择满足我们设计的最优方案。最后根据测试没问题的仿真图纸,焊接实物,调试,最终完成本设计的作品。