em đang làm một mạch đo nhiệt độ và độ ẩm dht21 giao tiếp 8051 hiển thị ra lcd16x2 mà không chạy có ai giúp em kiểm tra đoạn code này sai chổ nào với ạ, đọc data từ cảm biến và hiển thị lên lcd không được
em cảm ơn nhiều
chương trình chính
#include <at89x51.h>
#include <regx51.h>
#include <stdio.h>
#include "D:\lo ap trung\code\lib\delay.h"
#include "D:\lo ap trung\code\DHT21\dht21.h"
#define TAT 0
#define SANG 1
#define OUT 1
#define IN 0
#define DHT_DATA P2_5
// khai bao ket noi voi lcd
#define LCD_DATA P0
sbit LCD_RS = P2^6;
sbit LCD_EN = P2^7;
//
sbit MODE=P2^4;
sbit TANG=P2^3;
sbit GIAM=P2^2;
sbit den_60w=P1^0; // tao nhiet do
sbit dong_co=P1^1; // dong co dao trung
sbit quat_hut=P1^2; // quat hut gio ra ngoai
sbit den_dao_trung=P1^3; // DENF BAOS DAO TRUNG
unsigned char temp1,temp2,temp3,huni1,huni2,huni3;
unsigned int doam,nhietdo;
char NHIET_DO,PHAY,do_am=0;doi_hien_thi=0;dem_hien_thi= 0;
char dem_xung;ND_D1=37;ND_D2=8;tinh_ngat=0;dem1=0;dem_p hut=0;dem_gio=0;dem_ngay=0;gio=0;phut=0;giay=0;DAT _PHUT=0;DAT_GIO=1;CHON=0;
//Ham tao tre
// KHAI BAOS LCD
void Lcd_Cmd(unsigned char cmd)
{
LCD_RS = 0; // dia chi con tro
LCD_DATA = cmd;
LCD_EN = 0;
LCD_EN = 1;
if(cmd<=0x02)
{
Delay_ms(2);
}
else
{
Delay_ms(1);
}
}
void Lcd_Chr_Cp(char c) // Current Position doc du lieu ra lcd
{
LCD_RS = 1;
LCD_DATA = c;
LCD_EN = 0;
LCD_EN = 1;
Delay_ms(1);
}
// Xuat ra man hinh LCD 1 ky tu, ngay tai vi tri dong row, cot col
void Lcd_Chr(unsigned char row, unsigned char col, char c)
{
unsigned char cmd;
// Di chuyen co tro den vi tri can thiet
cmd = (row==1?0x80:0xC0) + col - 1;
Lcd_Cmd(cmd);
// Xuat ky tu
Lcd_Chr_Cp(c);
}
void Lcd_Out_Cp(char *str)
{
unsigned char i = 0;
while(str[i]!=0)
{
Lcd_Chr_Cp(str[i]);
i++;
}
}
// xuat ra man hinh lcd 1 chuoi kys tu
void Lcd_Out(unsigned char row, unsigned char col, char *str)
{
unsigned char cmd;
// Di chuyen co tro den vi tri can thiet
cmd = (row==1?0x80:0xC0) + col - 1;
Lcd_Cmd(cmd);
Lcd_Out_Cp(str);
}
void Lcd_Init()
{
Lcd_Cmd(0x30);
Delay_ms(5);
Lcd_Cmd(0x30);
Delay_ms(1);
Lcd_Cmd(0x30);
Lcd_Cmd(0x38); // So dong hien thi la 2, font 5x8
Lcd_Cmd(0x01); // Xoa noi dung hien thi
Lcd_Cmd(0x0C); // Bat hien thi va tat con tro
}
// KET THUC KHAI BAO LCD
void hien_thi_dht21 (void)
{
temp1=nhietdo/100;
temp2=nhietdo%100/10;
temp3=nhietdo%10;
huni1=doam/100;
huni2=doam%100/10;
huni3=doam%10;
Lcd_Out(2,1,"NHIET DO: ");
Lcd_Chr_Cp(temp1 + 0x30);
Lcd_Chr_Cp(temp2 + 0x30);
Lcd_Chr_Cp(',');
Lcd_Chr_Cp(temp3 + 0x30);
Lcd_Out(2,16,"C");
Lcd_Out(1,3,"DO AM: ");
Lcd_Chr_Cp(huni1 + 0x30);
Lcd_Chr_Cp(huni2 + 0x30);
Lcd_Chr_Cp(',');
Lcd_Chr_Cp(huni3 + 0x30);
Lcd_Out(1,15,"% ");
}
// chuong trinh cho time
void time ()
{ if(tinh_ngat==11){P2_7=!P2_7;}
if(tinh_ngat==22)
{
//read_dht21(&nhietdo,&doam);
giay++;dem_hien_thi++;
if(dem_hien_thi==15){doi_hien_thi=1;}
if(dem_hien_thi==30){doi_hien_thi=0;dem_hien_thi=0 ;}
if(giay>=59){dem_phut++;phut++;giay=0;}
if(dem_phut>=59){dem_phut=0;}
if(phut>=59){dem_gio++;gio++;phut=0;}
if(dem_gio>=23){dem_gio=0;}
if(gio>=23){dem_ngay++;gio=0;}
if(tinh_ngat==22){tinh_ngat=0;}
}
}
void time0(void) interrupt 1 using 1 //ngat TIME 0
{ TR0=0;TF0=0;
tinh_ngat++;
TL0=0xb0;
TH0=0x3c;
time();
TR0=1;
}
// chuong trinh chinh
void main()
{
TMOD=0x01;
TL0=0xb0;
TH0=0x3c;
EA=1;
ET0=1;
TR0=1;
dong_co=1;
quat_hut=0;
den_60w=0;
Lcd_Init();
while(1)
{
if(read_dht21(&nhietdo,&doam))
{
hien_thi_dht21(); //Delay_ms(1);
}
///
}
}
chương trình dht21
#include "dht21.h"
void start (void)
{
DHT_DATA = OUT;
DHT_DATA = OUT;
delay_us(60);
DHT_DATA = 0;
Delay_ms(20);
DHT_DATA = OUT;
DHT_DATA = IN;
delay_us(60);
}
unsigned int check_respon(void)
{
if(DHT_DATA) return 0;
else
while(DHT_DATA); //cho cho chan DATA duoc keo xuong 0
delay_us(100);
if(DHT_DATA)
return 1;
else
return 0;
}
unsigned int read_dht21(unsigned int *temp,unsigned int *humi)
{
unsigned char buff[5]={0,0,0,0,0};
unsigned char ii,i,checksum;
EA=0;
start();
if(!check_respon())
return 0;
else
while(DHT_DATA); //doi DATA ve 0
//doc du lieu
for(i=0;i<5;i++)
{
for(ii=0;ii<8;ii++)
{
while(!DHT_DATA); //doi DATA len 1
delay_us(50);
if(DHT_DATA)
{
buff[i]|=(1<<(7-ii));
while(DHT_DATA); //doi DATA xuong 0
}
}
}
checksum=buff[0]+buff[1]+buff[2]+buff[3];
if(checksum!=buff[4]) return 0;
if(buff[2]&0x80)
*temp=buff[3];
else
*temp=buff[2]*256+buff[3];
*humi=buff[0]*256+buff[1];
return 1;
EA=1;
}
em cảm ơn nhiều
chương trình chính
#include <at89x51.h>
#include <regx51.h>
#include <stdio.h>
#include "D:\lo ap trung\code\lib\delay.h"
#include "D:\lo ap trung\code\DHT21\dht21.h"
#define TAT 0
#define SANG 1
#define OUT 1
#define IN 0
#define DHT_DATA P2_5
// khai bao ket noi voi lcd
#define LCD_DATA P0
sbit LCD_RS = P2^6;
sbit LCD_EN = P2^7;
//
sbit MODE=P2^4;
sbit TANG=P2^3;
sbit GIAM=P2^2;
sbit den_60w=P1^0; // tao nhiet do
sbit dong_co=P1^1; // dong co dao trung
sbit quat_hut=P1^2; // quat hut gio ra ngoai
sbit den_dao_trung=P1^3; // DENF BAOS DAO TRUNG
unsigned char temp1,temp2,temp3,huni1,huni2,huni3;
unsigned int doam,nhietdo;
char NHIET_DO,PHAY,do_am=0;doi_hien_thi=0;dem_hien_thi= 0;
char dem_xung;ND_D1=37;ND_D2=8;tinh_ngat=0;dem1=0;dem_p hut=0;dem_gio=0;dem_ngay=0;gio=0;phut=0;giay=0;DAT _PHUT=0;DAT_GIO=1;CHON=0;
//Ham tao tre
// KHAI BAOS LCD
void Lcd_Cmd(unsigned char cmd)
{
LCD_RS = 0; // dia chi con tro
LCD_DATA = cmd;
LCD_EN = 0;
LCD_EN = 1;
if(cmd<=0x02)
{
Delay_ms(2);
}
else
{
Delay_ms(1);
}
}
void Lcd_Chr_Cp(char c) // Current Position doc du lieu ra lcd
{
LCD_RS = 1;
LCD_DATA = c;
LCD_EN = 0;
LCD_EN = 1;
Delay_ms(1);
}
// Xuat ra man hinh LCD 1 ky tu, ngay tai vi tri dong row, cot col
void Lcd_Chr(unsigned char row, unsigned char col, char c)
{
unsigned char cmd;
// Di chuyen co tro den vi tri can thiet
cmd = (row==1?0x80:0xC0) + col - 1;
Lcd_Cmd(cmd);
// Xuat ky tu
Lcd_Chr_Cp(c);
}
void Lcd_Out_Cp(char *str)
{
unsigned char i = 0;
while(str[i]!=0)
{
Lcd_Chr_Cp(str[i]);
i++;
}
}
// xuat ra man hinh lcd 1 chuoi kys tu
void Lcd_Out(unsigned char row, unsigned char col, char *str)
{
unsigned char cmd;
// Di chuyen co tro den vi tri can thiet
cmd = (row==1?0x80:0xC0) + col - 1;
Lcd_Cmd(cmd);
Lcd_Out_Cp(str);
}
void Lcd_Init()
{
Lcd_Cmd(0x30);
Delay_ms(5);
Lcd_Cmd(0x30);
Delay_ms(1);
Lcd_Cmd(0x30);
Lcd_Cmd(0x38); // So dong hien thi la 2, font 5x8
Lcd_Cmd(0x01); // Xoa noi dung hien thi
Lcd_Cmd(0x0C); // Bat hien thi va tat con tro
}
// KET THUC KHAI BAO LCD
void hien_thi_dht21 (void)
{
temp1=nhietdo/100;
temp2=nhietdo%100/10;
temp3=nhietdo%10;
huni1=doam/100;
huni2=doam%100/10;
huni3=doam%10;
Lcd_Out(2,1,"NHIET DO: ");
Lcd_Chr_Cp(temp1 + 0x30);
Lcd_Chr_Cp(temp2 + 0x30);
Lcd_Chr_Cp(',');
Lcd_Chr_Cp(temp3 + 0x30);
Lcd_Out(2,16,"C");
Lcd_Out(1,3,"DO AM: ");
Lcd_Chr_Cp(huni1 + 0x30);
Lcd_Chr_Cp(huni2 + 0x30);
Lcd_Chr_Cp(',');
Lcd_Chr_Cp(huni3 + 0x30);
Lcd_Out(1,15,"% ");
}
// chuong trinh cho time
void time ()
{ if(tinh_ngat==11){P2_7=!P2_7;}
if(tinh_ngat==22)
{
//read_dht21(&nhietdo,&doam);
giay++;dem_hien_thi++;
if(dem_hien_thi==15){doi_hien_thi=1;}
if(dem_hien_thi==30){doi_hien_thi=0;dem_hien_thi=0 ;}
if(giay>=59){dem_phut++;phut++;giay=0;}
if(dem_phut>=59){dem_phut=0;}
if(phut>=59){dem_gio++;gio++;phut=0;}
if(dem_gio>=23){dem_gio=0;}
if(gio>=23){dem_ngay++;gio=0;}
if(tinh_ngat==22){tinh_ngat=0;}
}
}
void time0(void) interrupt 1 using 1 //ngat TIME 0
{ TR0=0;TF0=0;
tinh_ngat++;
TL0=0xb0;
TH0=0x3c;
time();
TR0=1;
}
// chuong trinh chinh
void main()
{
TMOD=0x01;
TL0=0xb0;
TH0=0x3c;
EA=1;
ET0=1;
TR0=1;
dong_co=1;
quat_hut=0;
den_60w=0;
Lcd_Init();
while(1)
{
if(read_dht21(&nhietdo,&doam))
{
hien_thi_dht21(); //Delay_ms(1);
}
///
}
}
chương trình dht21
#include "dht21.h"
void start (void)
{
DHT_DATA = OUT;
DHT_DATA = OUT;
delay_us(60);
DHT_DATA = 0;
Delay_ms(20);
DHT_DATA = OUT;
DHT_DATA = IN;
delay_us(60);
}
unsigned int check_respon(void)
{
if(DHT_DATA) return 0;
else
while(DHT_DATA); //cho cho chan DATA duoc keo xuong 0
delay_us(100);
if(DHT_DATA)
return 1;
else
return 0;
}
unsigned int read_dht21(unsigned int *temp,unsigned int *humi)
{
unsigned char buff[5]={0,0,0,0,0};
unsigned char ii,i,checksum;
EA=0;
start();
if(!check_respon())
return 0;
else
while(DHT_DATA); //doi DATA ve 0
//doc du lieu
for(i=0;i<5;i++)
{
for(ii=0;ii<8;ii++)
{
while(!DHT_DATA); //doi DATA len 1
delay_us(50);
if(DHT_DATA)
{
buff[i]|=(1<<(7-ii));
while(DHT_DATA); //doi DATA xuong 0
}
}
}
checksum=buff[0]+buff[1]+buff[2]+buff[3];
if(checksum!=buff[4]) return 0;
if(buff[2]&0x80)
*temp=buff[3];
else
*temp=buff[2]*256+buff[3];
*humi=buff[0]*256+buff[1];
return 1;
EA=1;
}