基于 8051 的饮料售货机系统 - 使用 LCD1602 实现加减功能
#include<reg51.h> #include<stdio.h>
sbit JDQ=P2^0; sbit BEEP=P2^1; sbit lcd_en=P2^5; sbit rs=P2^7; sbit rw = P2^6;
/define******/ #define WAIT_SALE 0 //定义1602显示状态 #define NO_beverage 1 #define INPUT_MONEY 2 #define SALING 3 #define CHANGE 4 #define CANCLE 5 #define NO_DATA 0 #define CLASS 6 #define ADD 1 // 定义加1按键 #define MINUS 5 // 定义减1按键 #define water_change(y, l) ((y) & (~(0x01 << l))) //宏 控制水种类的切换 #define uint unsigned int #define uchar unsigned char
unsigned char water_total[] = {12, 12, 12, 12, 12, 12, 12, 12}; unsigned char water_class[] = {1,2,3 }; unsigned char water_price[] = {1, 2, 3, 4, 5, 6, 7, 8 };
//uchar code wait_sale[]="for sale"; //定义所显示的内容 uchar code wait_sale[]='Welcome to'; uchar code no_beverage[] = 'no beverage'; uchar code price[] = 'Money:'; uchar code number[] = 'Num:'; uchar code class[] = 'Type:'; uchar code sum[] = 'SUM:'; uchar code input[] = 'INPUT:'; uchar code change[] = 'change:'; uchar code put_water[] = 'put water...'; uchar code cancle[] = 'cancle...';
unsigned char e=0x00;
void main() { unsigned char ok = 0, water_type = -1, water_is_choose = 0, uit_sit = 0; unsigned char add=0, minus=0; char water_count = 0; unsigned int flag = 0, money = 0, need_money;
lcd_1602_init(); // LCD初始化
again: ok = 0; water_type = -1; water_count = 0; water_is_choose = -1; uit_sit = 0; flag = 0; money = 0; need_money = 0;
lcd1602_info_display(WAIT_SALE, 0, 0); // LCD显示
while(!ok) //choose the water and count
{
flag=key();
if(flag)
{
switch(e) //不同的按键进行不同的操作
{
case(0x00):
water_type++; // 类型变量+1
water_is_choose = 1;
if(water_type == 8) // 如果类型变量等于8
{
water_type = 0; // 类型变量变为0
}
P3 = water_change(0xff, water_type); // 显示价格指示
break;
case(0x04):
water_type--;
water_is_choose = 1;
if(water_type == -1 || water_type == -2) {
water_type = 7;
}
P3 = water_change(0xff, water_type);// 显示价格指示
break;
case(0x08):
if(water_is_choose == 1) {
water_count++;
if(water_count > water_total[water_type]) {
water_count = water_total[water_type];
}
}
break;
case(0x0c):
if(water_is_choose == 1) {
water_count--;
if(water_count < 0) {
water_count = 0;
}
}
break;
case(ADD): // 加1
add++;
if(add > 3) { // 超过3则归0
add = 0;
}
lcd1602_info_display(INPUT_MONEY, add, 0);
break;
case(MINUS): // 减1
minus++;
if(minus > 3) { // 超过3则归0
minus = 0;
}
lcd1602_info_display(INPUT_MONEY, minus, 0);
break;
case(0x0b): // 返回
goto again;
break;
case(0x0f): // 确认
if(water_is_choose == 1) {
ok=1;
}
break;
default:
break;
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/nLQI 著作权归作者所有。请勿转载和采集!