1.按鍵控制單元 設定按鍵S7為出水控制按鍵,當S7按下后,售水機持續(xù)出水〈繼電器接通,指示燈L10點亮設定按鍵S6為停水控制按鍵,當S6按下后,停止出水〈繼電器斷開,指示燈L10熄滅X 1.數(shù)碼管顯示單元 通過4位數(shù)碼管DS1顯示費率,單位為元丨升,保留2位有效數(shù)字; 通過4位數(shù)碼管DS2顯示當前出水總量(出水時,單位為升)和總價(停止時,單 位為元按下出水按鍵S7后,清除數(shù)碼管DS2顯示數(shù)據(jù),數(shù)碼管DS2實時顯示出水 量(保留兩位有效數(shù)字〕,在出水狀態(tài)下,再次按下S7,不會影響出水狀態(tài),直到按下停 止按鍵S6為止;按下停止出水按鍵S6后,數(shù)碼管DS2顯示總價3^入0轉(zhuǎn)換單元通過光敏電阻RD1和AD轉(zhuǎn)換芯片PCF8591組成的亮度檢測電路〈亮度值轉(zhuǎn)換為PCF8591光敏電阻通道的電壓〕檢測環(huán)境亮度;當:PCF8591光敏電阻通道輸入電壓小于1.25 V時,L1點亮,大于1.25V時,L1熄滅。 4^系統(tǒng)說明 1.假定水價為0.5元/升,出水速度為100毫升/秒; 1. 一次出水總量達到99.99L時,繼電器自動斷開,數(shù)碼管顯示DS2顯示價格。
單片機源程序如下:- #include "reg52.h"
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
- uchar code smg_du[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code smg_we[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- uchar code smg_du1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x7f}; //有小數(shù)點的段選
- sbit SDA = P2^1;
- sbit SCL = P2^0;
- sbit s7=P3^0;
- sbit s6=P3^1;
- uchar tflag,dspflag,qian,bai,shi,ge,qian1,bai1,shi1,ge1,num,temp,bb;
- uint t,aa,date;
- void delayms(uint x)
- {
- uchar t;
- while(x--)for(t=0;t<120;t++);
- }
- void keyscan()
- {
- if(s7==0)
- {
- delayms(10);
- if(s7==0)
- {
- P0=0x10;P2=0xa0;P2=0;
- TR0=1;num=1;t=0;
- while(!s7);
- }
- }
- if(s6==0)
- {
- delayms(10);
- if(s6==0)
- {
- date=t/2;TR0=0;
- P0=0;P2=0xa0;P2=0;
- num=2;
- TR0=1;
- while(!s6);
- }
- }
- }
- void display(uchar we,uchar du)
- {
- P0 = 0xFF;
- P2 |= 0xE0;
- P2 &= 0x1F;
- P0 = smg_du[du];
- P2 |= 0xE0;
- P2 &= 0x1F;
- P0 =smg_we[we];
- P2 |= 0xC0;
- P2 &= 0x3F;
-
- }
- /////////////////////////////////////////////////////////////////
- void iic_start(void)
- {
- SDA = 1;
- _nop_();
- SCL = 1;
- somenop;
- SDA = 0;
- somenop;
- SCL = 0;
- }
- void iic_stop(void)
- {
- SDA = 0;
- _nop_();
- SCL = 1;
- somenop;
- SDA = 1;
- }
- void ack()
- {
- unsigned char i;
- SCL=1;somenop;while((SDA==1)&&(i<200))i++;SCL=0;
- }
- void iic_sendbyte(unsigned char byt)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(byt&0x80)
- SDA = 1;
- else
- SDA = 0;
- somenop;
- SCL = 1;
- byt <<= 1;
- somenop;
- SCL = 0;
- }
- ack();
- }
- unsigned char iic_recbyte(void)
- {
- unsigned char da;
- unsigned char i;
- for(i=0;i<8;i++)
- {
- SCL = 1;
- somenop;
- da <<= 1;
- if(SDA)
- da |= 0x01;
- SCL = 0;
- somenop;
- }
- return da;
- }
- unsigned char adc_pcf8591(void)
- {
- unsigned char temp;
- iic_start();
- iic_sendbyte(0x91);
- temp = iic_recbyte();
- iic_stop();
-
- return temp;
- }
- void init_pcf8591(void)
- {
- iic_start();
- iic_sendbyte(0x90);
- iic_sendbyte(0x01);
- iic_stop();
- delayms(10);
- }
- ////////////////////////////////////////////////////////////////
- void main()
- {
- P0=0xff;P2=0x80;P2=0;
- P0=0;P2=0xa0;P2=0; //關蜂鳴器
- TMOD = 0x22;
- TH0 = 0x06;
- TL0 = 0x06;
- TH1 = 0x06;
- TL1 = 0x06;
- ET0 = 1;
- EA = 1;
- ET1=1;TR1=1;
- init_pcf8591();
-
- while(1)
- {
- keyscan();
- if(t==10000)
- {
- TR0=0;
- P0=0;P2=0xa0;P2=0;
- num=3; //超出標志
- TR0=1;
- }
- }
- }
- void isr_t0(void) interrupt 1 //數(shù)碼管顯示
- {
- tflag++;
- aa++;
- if(aa==400) //10ms t++
- {
- aa=0;
- t++;
- if(t==10000)
- {
- P0=0;P2=0xa0;P2=0;
- }
- qian=t/1000;bai=t%1000/100;shi=t%100/10;ge=t%10;
- }
- if(num==1) //計時標志
- {
- if(tflag == 4) //1ms 動態(tài)顯示
- {
- tflag = 0;
- dspflag++;
- if(dspflag == 7)
- dspflag = 0;
- if(dspflag==0)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0=smg_du1[bai];P2 |= 0xE0;P2 &= 0x1F;
- P0 =smg_we[5];P2 |= 0xC0;P2 &= 0x3F;
- }
-
- if(dspflag==1)
- display(6,shi);
- if(dspflag==2)
- display(7,ge);
- if(dspflag==3)
- display(4,qian);
- if(dspflag==4)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0=smg_du1[0];P2 |= 0xE0;P2 &= 0x1F;
- P0 =smg_we[1];P2 |= 0xC0;P2 &= 0x3F;
- }
- if(dspflag==5)
- display(2,5);
- if(dspflag==6)
- display(3,0);
- }
- }
- if(num==2) //算費標志
- {
- if(tflag == 4) //1ms
- {
- tflag = 0;dspflag++;
- if(dspflag == 7)
- {
- dspflag = 0;
- qian1=date/1000;bai1=date%1000/100;shi1=date%100/10;ge1=date%10;
- }
- if(dspflag==0)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0 = smg_du1[bai1];P2 |= 0xE0;P2 &= 0x1F;
- P0 =smg_we[5];P2 |= 0xC0;P2 &= 0x3F;
- }
- if(dspflag==1)
- display(6,shi1);
- if(dspflag==2)
- display(7,ge1);
- if(dspflag==3)
- display(4,qian1);
- if(dspflag==4)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0=smg_du1[0];P2 |= 0xE0;P2 &= 0x1F;
- P0 =smg_we[1];P2 |= 0xC0;P2 &= 0x3F;
- }
- if(dspflag==5)
- display(2,5);
- if(dspflag==6)
- display(3,0);
- }
- }
- if(num==3)
- {
- if(tflag == 4) //100ms
- {
- tflag = 0;dspflag++;
- if(dspflag == 7)
- {
- dspflag = 0;
- }
- if(dspflag==0)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0 = smg_du1[9];P2 |= 0xE0; P2 &= 0x1F;
- P0 =smg_we[5];P2 |= 0xC0;P2 &= 0x3F;
- }
- if(dspflag==1)
- display(6,9);
- if(dspflag==2)
- display(7,9);
- if(dspflag==3)
- display(4,4);
- if(dspflag==4)
- {
- P0 = 0xFF;P2 |= 0xE0;P2 &= 0x1F;
- P0=smg_du1[0];P2 |= 0xE0;P2 &= 0x1F;
- P0 =smg_we[1];P2 |= 0xC0;P2 &= 0x3F;
- }
- if(dspflag==5)
- display(2,5);
- if(dspflag==6)
- display(3,0);
- }
- }
- }
- void T1_timer() interrupt 3 //LED PCF8591
- {
- bb++;
- if(bb==8)
- {
- bb=0;
- temp=adc_pcf8591();
- if(temp<64)
- {
- P0=0xfe;P2=0x80;P2=0;
- }
- if(temp>64)
- {
- P0=0xff;P2=0x80;P2=0;
- }
- }
- }
復制代碼
以上圖文的Word格式文檔下載(內(nèi)容和本網(wǎng)頁上的一模一樣,方便保存):
2012_小區(qū)自動售水機_參考程序.doc
(132.5 KB, 下載次數(shù): 15)
2021-10-27 17:40 上傳
點擊文件名下載附件
|