当前位置:文档之家› 基于单片机的水位检测

基于单片机的水位检测

#include
#include//头文件
#define uchar unsigned char //宏定义,为方便编程
#define uint unsigned int
#define ADDATA P0 //将P1 口定义为输出数据位
#define SELECT P3 //宏定义,将P0 口定义为LED
#define DIGI P2 //宏定义,将P1口定义为数码管
#define LED1 P1
uchar digivalue[]={0x28,0x7e,0x0a2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0xff,0xf7}; //显示的数字数组,依次为0,1,..,9
uchar select[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf}; //选择数码管数组,依次选择1,2,..,8
uchar ledselect[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf};
uchar temp[]={0,0,0,0,0,0};
uchar value[]={0,0,0,0};
sbit CS=P3^6; //定义P3.6为ADC0804片选位
sbit _RD=P3^7; //定义P3.7为数据读取位
void delay() //片选及读写数据位保持延迟
{
uchar i=200;
while(i--);
}
char code SST516[3] _at_ 0x003b; //仿真器保留
Read() //读取0804的数据
{
CS=0; //拉低片选位
_RD=0;
value[0]=ADDATA; //延迟
_RD=1; //拉高读数据位
CS=1;
}
jisuan() //计算0804的输入的水位数值
{
uchar a=0;
a=value[0];
value[1]=a*60/255;
value[2]=value[1]/10 ;
value[3]=ledselect[value[2]];
}

zhengfu() //判断正负
{
uchar a=0;uchar b;
a=value[1];
if(a<30)
{
b=30-a;
temp[0]=11;
temp[1]=b/10;
temp[2]=b%10;
temp[3]=11;
temp[4]=(b-1)/10+1;
temp[5]=0;
}
else
{
b=a-30;
temp[0]=10;
temp[1]=b/10;
temp[2]=b%10;
temp[3]=10;
temp[4]=b/10;
temp[5]=0;
}
}

display() //显示
{
uchar i=0;
delay();
for(i=0;i<6;i++) //8个数码管轮流显示
{
SELECT=select[i]; //选择第i个数码管
DIGI=digivalue[temp[i]]; //显示i
delay();
}
}
t0_100ms() interrupt 1
{
uchar c;
TH0=0x3c;
TL0=0xb0;
c++;
if(c==3)
{
Read(); //读取0804的数据
jisuan(); //计算0804的输入的水位数值


zhengfu(); //判断正负
c=0;
}
}
void main() //主函数
{
//定义中间变量
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
IE=0x82;
TR0=1;
while(1)
{
display();
LED1=value[3];
}
}



相关主题
文本预览
相关文档 最新文档