标题: 51单片机土壤温湿度检测什么地方错了 [打印本页]
作者: WANGWEIWEI818 时间: 2018-1-28 20:47
标题: 51单片机土壤温湿度检测什么地方错了
#include<reg52.h> //包含单片机寄存器的头文件
#define uchar unsigned char
#define uint unsigned int
sbit P10=P1^0;
sbit ADD=P1^1;
sbit SUB=P1^2;
sbit P13=P1^3;
uint ya,temp,num=0,aa,th=180,tl=90;
//uchar aa,th=200,tl=100;
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
sbit CS=P2^3; //将CS位定义为P3.4引脚
sbit CLK=P2^4; //将CLK位定义为P1.0引脚
sbit DIO=P2^5; //将DIO位定义为P1.1引脚
void delay(uint z);
void display(uint a);
void displayth(uint b);
void displaytl(uint c);
void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void key()
{
if(P10==0)
{
delay(5);
if(P10==0)
{
num++;
if(num==3)
{
num=0;
}
}
while(!P10)
display(temp);
delay(5);
while(!P10)
display(temp);
}
if(num!=0)
{
if(ADD==0)
{
delay(5);
if(ADD==0)
{
if(num==1)
{
th++;
if(th==250)
th=tl+10;
}
if(num==2)
{
if(tl<(th-10))
tl++;
Else
tl=90;
}
}
while(!ADD)
display(th);
}
if(SUB==0)
{
delay(5);
if(SUB==0)
{
if(num==1)
{
th--;
if(th==(tl+10))
th=250;
}
if(num==2)
{
tl--;
if(tl==90)
tl=th-10;
}
}
while(!SUB)
display(tl);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uint a)
{
P2=0xfe;
P0=table[a/100];
delay(5);
P2=0xfd;
P0=table[a%100/10];
delay(5);
P2=0xfb;
P0=table[a%100%10];
delay(5);
}
void displayth(uint b)
{
P2=0xfe;
P0=table[b/100];
delay(5);
P2=0xfd;
P0=table[b%100/10];
delay(5);
P2=0xfb;
P0=table[b%100%10];
delay(5);
}
void displaytl(uint c)
{
P2=0xfe;
P0=table[c/100];
delay(5);
P2=0xfd;
P0=table[c%100/10];
delay(5);
P2=0xfb;
P0=table[c%100%10];
delay(5);
}
unsigned char A_D()
{
unsigned char i,dat;
CS=1; //一个转换周期开始
CLK=0; //为第一个脉冲作准备
CS=0; //CS置0,片选有效
DIO=1; //DIO置1,规定的起始信号
CLK=1; //第一个脉冲
CLK=0; //第一个脉冲的下降沿,此前DIO必须是高电平
DIO=1; //DIO置1, 通道选择信号
CLK=1; //第二个脉冲,第2、3个脉冲下沉之前,DI必须跟别输入两位数据用于选择通道,这里选通道CH0
CLK=0; //第二个脉冲下降沿
DIO=0; //DI置0,选择通道0
CLK=1; //第三个脉冲
CLK=0; //第三个脉冲下降沿
DIO=1; //第三个脉冲下沉之后,输入端DIO失去作用,应置1
CLK=1; //第四个脉冲
for(i=0;i<8;i++) //高位在前
{
CLK=1; //第四个脉冲
CLK=0;
dat<<=1; //将下面储存的低位数据向右移
dat|=(unsigned char)DIO; //将输出数据DIO通过或运算储存在dat最低位
}
CS=1; //片选无效
return dat; //将读书的数据返回
}
/*****************************************************
函数功能:主函数
***************************************************/
main(void)
{
init();
while(1)
{
key();
if((temp<tl)||(temp>th))
P13=0;
else
P13=1;
if(num==0)
{
display(temp);
}
if(num==1)
{
displayth(th);
}
if(num==2)
{
displaytl(tl);
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
ya=A_D();
temp=(ya*100/51);//((ya*5)*100/255);
aa=0;
}
}
作者: 999333 时间: 2018-1-29 00:51
Else tl=90; } } while(!ADD) display(th);把Else改成else
作者: 614025638 时间: 2018-1-29 05:37
Else tl=90; } } while(!ADD) display(th);把Else改成else
作者: 王燕子 时间: 2020-12-27 16:15
有设计图吗
作者: TTQ001 时间: 2020-12-28 09:08
您的代码有什么错误,例如编译器错误?
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |