请老师看看下面这个程序那里出错了……不会了!
#include <mega16.h>#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
// Declare your global variables here
#define DHT11_DATA PORTD.3
#define DHT11_DATA_SETDHT11_DATA=1
#define DHT11_DATA_CLI DHT11_DATA=0
#define DHT11_DATA_OUT DDRD.3=1
#define DHT11_DATA_IN DDRD.3=0
#define DHT11_DATA_READ PIND.3
char test;
unsigned int dht11_data;
unsigned int dht11_readdata;
bit dht11_flag=0;
unsigned char humidity;//湿度8位数据值
unsigned char humidity1;
unsigned char temp;//温度8位数据值
unsigned chardht11_start(void)//dht11开始标志
{
unsigned char count=0;
DHT11_DATA_OUT;
DHT11_DATA_CLI;//将数据线拉低18ms以上
delay_ms(25);//延迟18ms以上即可
DHT11_DATA_IN;
DHT11_DATA_SET;//释放数据线
while(DHT11_DATA_READ)
{
delay_us(5);
count++;
if(count>10)
return 0;//延迟等待50us,超过则失败
}
while(!DHT11_DATA_READ);//等待低电平结束
return 1;//返回1 ,表示dht11响应
}
void dht11_getdata(void)//读数据函数
{
unsigned char i=0,j=0,count=0;
while(DHT11_DATA_READ);//等待数据线高电平结束
for(j=0;j<2;j++) //舍去校验位
{
for(i=0;i<=15;i++)//读16位数据
{
while(!DHT11_DATA_READ);//1bit开始低电平信号结束
delay_us(30);
if(DHT11_DATA_READ)
{
dht11_readdata<<=1;
dht11_readdata|=0x0001;
}
else
dht11_readdata<<=1;//判断位值
while(DHT11_DATA_READ);//等待高电平结束
}
dht11_data=dht11_readdata; //将读出的16位数据一位位的放入数组保存
dht11_readdata=0;
}
dht11_flag=1; //40位数完成标识
}
void dht11_switchcode(void)
{
humidity1=dht11_data>>8;
if((humidity1>0)&&(humidity1<100)) //判断,如果湿度值大于100直接否定不取
humidity=humidity1;
temp=dht11_data>>8;
//shis=humidity/10;
//shig=humidity%10;
}
void main(void)
{
DHT11_DATA_OUT;
DHT11_DATA_SET;
lcd_init(16);
while(1)
{
while(!dht11_start());//等待温湿度传感器响应
dht11_getdata(); //读数值
if(dht11_flag==1) //如果数读完
{
dht11_flag=0;
dht11_switchcode();
test=humidity;
test=temp;
}
lcd_clear();
lcd_putsf("****liu song****");
lcd_gotoxy(0,1);
lcd_puts(test);
// Place your code here
};
}
页:
[1]