单片机源程序如下:
- #include "stc.h"
- #include "button.h"
- #include "smg.h"
- #include "AT24C02.h"
- sbit led1=P0^0;
- sbit bell=P0^6;
- sbit relay=P0^4;
- unsigned char flag_s7,flag_s6;
- int money=0,amount=0;//价格 容量
- unsigned char count,flag_count;
- unsigned char state; //控制数码管两种显示界面 按键控制
- #define EXE_EN P2 = (P2&0X1F)|0XA0; P2 &= 0X1F
- void init_time0() //1微妙加1
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;//定时50ms
- TL0=(65536-50000)%256;
- ET0=1;
- //TR0=1;
- EA=1;
- }
- void time0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- if(count==20)
- {
- count=0;
- flag_count=1;
- }
- }
- void key()
- {
- unsigned char kk=keyboard();
- unsigned char guan=get_lux(0x41);
- if(guan<64)
- {
- P0=0xff;
- P2&=0x1f;
- P2|=0x80;
- led1=0;//灯l2亮
- P2&=0x1f;
- }
- if(guan>=64)
- {
- P0=0xff;
- P2&=0x1f;
- P2|=0x80;
- led1=1;//灯l1灭
- P2&=0x1f;
- }
- if(kk==7)
- {
- flag_s7=1;
- state=1;
- TR0=1; //启动定时器
- }
- if(kk==6)
- {
- flag_s6=1;
- state=2;
- }
- if(flag_s7)
- {
- flag_s7=0;bell=0;relay=1;EXE_EN;
- }
- if(flag_s6)
- {
- TR0=0; //关闭定时器
- flag_s6=0;bell=0;relay=0;EXE_EN;
- }
- if(state==1)
- {
- display_s(1,0); display(2,5); display(3,0);
- display(4,amount/100); display_s(5,amount%100/10); display(6,amount%10); display(7,0);
- }
- if(state==2)
- {
- display_s(1,0); display(2,5); display(3,0);
- display(4,money/1000); display_s(5,money%1000/100); display(6,money%100/10); display(7,money%10);
- }
- if(flag_count==1)
- {
- flag_count=0;
- money=money+5;
- amount=amount+1;
- }
- if(amount==9990){flag_s6=1;state=2;}
- }
- void main()
- {
- init_time0();
- while(1)
- {
- key();
- }
- }
复制代码
所有资料51hei提供下载:
自动售水机.rar
(540.59 KB, 下载次数: 42)
|