|
- #include "iap15f2k61s2.h"
- #include "iic.h"
- #include "intrins.h"
- //IO口定义
- sbit relay=P0^4;
- sbit buzzer=P0^6;
- //变量定义
- #define SetKeyBoard(x) P4 = (x>>3) | (x>>4);P3 = x
- #define GetKeyBoard() ((P4&0x10)<<3) | ((P4&0x04)<<4) | (P3&0x3F)
- #define keyin P3
- #define chushui 1
- #define tingshui 2
- unsigned long int water_jl=0;//10ml
- unsigned long int water_cost=0;
- unsigned char water_flag=tingshui;//
- unsigned char LED_SMGwei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};
- unsigned char SMGduan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00,0x40,0x80};
- unsigned char table[8];
- unsigned char trg,cont;
- static unsigned char LED_change=255,BUZZER_change=255,RELAY_change=255;
- //定时器0 模式0位ne555计数模式 模式1位普通定时模式
- void Timer0Init(unsigned char mode) //1毫秒@11.0592MHz
- {
- if(mode==1)
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0xCD; //设置定时初值
- TH0 = 0xD4; //设置定时初值
- }
- else if(mode==0)
- {
- TMOD =0x16;
- TL0 = 0xff;
- TH0 = 0xff;
- }
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1;
- EA=1;
- }
- void timer0() interrupt 1
- {
- static unsigned char count;//1ms
- if(water_flag==chushui)
- {
- count++;
- if(count==100){water_jl+=1;count=0;if(water_jl>=9999){water_jl=9999;water_flag=tingshui;}}
- }
- }
- //毫秒延迟
- void Delay_ms(unsigned int time) //@11.0592MHz
- {
- unsigned char i, j;
- unsigned int m;
- for(m=0;m<=time;m++)
- {
- _nop_();
- _nop_();
- _nop_();
- i = 11;
- j = 190;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- //蜂鸣器继电器控制
- void buzzer_relay(unsigned char buzzer_mode,unsigned char relay_mode)
- {
- if(BUZZER_change==buzzer_mode&&RELAY_change==relay_mode)return;
- else if(BUZZER_change!=buzzer_mode||RELAY_change!=relay_mode){
- P2=0xa0;relay=relay_mode;buzzer=buzzer_mode;P2=0x00;}
- }
- //led控制
- void LED(unsigned char i)
- {
- if(LED_change==i)return;
- else if(LED_change!=i){
- P0=~LED_SMGwei[ i];P2=0x80;P2=0x00;LED_change=i;} }//延迟做动态数码管void SMG(){ unsigned char i=0; for(i=0;i<8;i++) { P2=0Xe0;P0=0xff;P2=0x00;//清屏 P2=0xc0;P0=0xff;P2=0x00; P2=0xc0;P0=LED_SMGwei[ i];P2=0x00;P0=0xff; P0=~table[ i];P2=0xe0;P2=0x00; Delay_ms(1); }}//模式1矩阵按键 模式0独立按键void KeyRead(unsigned char mode) { unsigned char ReadData; if(mode==1) { SetKeyBoard(0x0f); ReadData=GetKeyBoard(); SetKeyBoard(0xf0); ReadData=(ReadData | GetKeyBoard())^0xff; trg = ReadData & (ReadData ^ cont); cont = ReadData; } else if(mode==0) { ReadData=keyin^0xff; trg=ReadData&(ReadData^cont); cont=ReadData; }}//显示void display(unsigned int du,unsigned char mode,unsigned char num){ if(mode==1) { table[0]=du>=10000000? du/10000000%10:16; table[1]=du>=1000000? du/1000000%10:16; table[2]=du>=100000? du/100000%10:16; table[3]=du>=10000? du/10000%10:16; table[4]=du>=1000? du/1000%10:16; table[5]=du>=100? du/100%10:16; table[6]=du>=10? du/10%10:16; table[7]=du>=0? du%10:16; } else if(mode==0) table[num]=SMGduan[du];}void display_water(unsigned char mode){ if(mode==chushui) { display(16,0,0); table[1]=SMGduan[0]|0x80; display(5,0,2); display(0,0,3); display(water_jl/1000%10,0,4); table[5]=SMGduan[water_jl/100%10]|0x80; display(water_jl/10%10,0,6); display(water_jl%10,0,7); } else if(mode==tingshui) { display(16,0,0); table[1]=SMGduan[0]|0x80; display(5,0,2); display(0,0,3); display(water_cost/1000%10,0,4); table[5]=SMGduan[water_cost/100%10]|0x80; display(water_cost/10%10,0,6); display(water_cost%10,0,7); }}//主函数void main(){ float rb1=0; buzzer_relay(0,0); LED(8); P2=0Xe0;P0=0xff;P2=0x00;//清屏 P2=0xc0;P0=0xff;P2=0x00; PCF8591_Init(); Timer0Init(1); while(1) { rb1=(PCF8591_AD()/255.f)*5; if(rb1>1.25)LED(8); else if(rb1<1.25)LED(0); display_water(water_flag); SMG(); if(water_flag==chushui)buzzer_relay(0,1); else if(water_flag==tingshui)buzzer_relay(0,0); water_cost=water_jl*0.5; KeyRead(0); if(trg&0x01)//s7 { water_flag=chushui;//出水 water_jl=0; } if(trg&0x02)//s6 { water_flag=tingshui;//价格 } }}
复制代码
|
|