#include <reg52.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
uint16 number[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit key4=P2^4;
sbit key5=P2^5;
sbit key6=P2^6;
sbit key7=P2^7;
sbit keyout1=P2^3;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit L1= P0^0;
sbit L2=P0^1;
sbit buzz=P1^6;
uint8 i[8];//申请寄存
uint8 y,n,x=0,a=50;//y报警变量,模拟数据n,状态转换选择变量x a阀值设置变量
void ref();
void conversion();
void aut_ref();
void aut();
void aut_key(void);
void CTP();
void _manual();
void delay(uint8 z)//延时函数
{
while(z--);
}
void ref() //数码管刷新函数
{
uint8 j=0;
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;ADDR3 = 1;P0 = number[i[0]%10];j++;break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;ADDR3 = 1;P0 = number[i[0]/10];j=0;break;
//case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;ADDR3 = 1;P0 = 0xbf;j++;break;
// case 3:ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;ADDR3 = 1;P0 = 0xbf;j=0;break;
// case 4:ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;ADDR3 = 1;P0 = number[0];delay(1);j++;break;
// case 5:ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;ADDR3 = 1;P0 = number[0];delay(1);j++;break;
// case 6:if(i<i[0]){ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;ADDR3 = 1;P0=0xfe;delay(1);}j=0;break;
default:break;
}
}
void conversion()//状态转换函数
{
while(1)
{
if(key7==0)
{
delay(1000);
if(key7==0)
x=1;//状态1------自动状态
while(!key7);
}
if(x==1)
{
if(key7==0)
{
delay(1000);
if(key7==0)
x=2;//状态2---手动状态
while(!key7);
}
}
aut();
manual();
}
}
void aut_ref()//自动状态设置阀值显示刷新
{
uint8 j=0;
while(1)
{
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;ADDR3 = 1;P0 = number[a%10];j++;break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;ADDR3 = 1;P0 = number[a/10];j++;break;
case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;ADDR3 = 1;P0 = 0xbf;j++;break;
case 3:ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;ADDR3 = 1;P0 = 0xbf;j=0;break;
default:break;
}
}
}
void aut()//自动状态提示L1亮0xfe
{
while(1)
{
if(((x==1)||(x==0))&&(n<i[0]))
{
ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;ADDR3 = 1;
P0=0xfe;
}
aut_key();
}
}
void aut_key(void)//自动状态阀值设置
{
uint8 m=0;
while(1)
{
if(key6==0)
{
delay(1000);
if(key6==0)
{
m=1;
a=i[0];
if(key5==0)
{
delay(1000);
if(key5==0)
a=a+1;
}
if(key4==0)
{
delay(1000);
if(key4==0)
a=a-1;
}
aut_ref();
}
}
i[0]=a;//需要送给eeprom
if(m==1&&key6==0)
{
delay(1000);
if(key6==0)
//a送给eeprom
//返回自动状态
aut();
m=0;
while(!key6);
}
}
}
void CTP()//Call the police手动状态报警
{
uint8 m,n;
while(1)
{
if(n>i[0])
{
buzz=1;
if(key6==0)
{
delay(1000);
if(key6==0)
{
//关蜂鸣器
y=1;
m=1;
}
}
else
m=0;
if(m==1)
buzz=0;
if(y==1)
{
y=0;
if(key6==0)
{
delay(1000);
if(key6==0)
n=1;
}
}
else
n=0;
if(n==1)
buzz=1;//开蜂鸣器
}
}
}
void _manual()//手动状态提示
{ uint8 m,n;
while(1)
{
if(x==2)
{
CTP();
ref();
ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;ADDR3 = 1;
if(key5==0)//S5开灌溉系统L2亮
{
delay(1000);
if(key5==0)
{
m=1;
while(!key5);
}
}
else
m=0;
if(m==1)
P0=0xfd;
if(key4==0)//S4关灌溉系统L2灭
{
delay(1000);
{
if(key4==0)
{
n=1;
while(!key4);
}
}
}
else
n=0;
if(n==1)
P0=0xff;
}
}
}
void mian()
{
uint16 counter;
i[0]=a;
ENLED = 0;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
while(1)
{
if(1 == TF0)
{
TF0 = 0;
TH0 = 0xFC;
TL0 = 0x67;
counter++;
}
if(1000 == counter)
{
counter = 0;
n++;
}
ref();
conversion();
if(n == 55)
{
n = 35;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>