以下是DHT22和nRF24L01无线组合,并且DHT22通过2片CD4067的16选一模拟通道实现32个传感器2个单总线的多点测量,问题就是,上位机读取时,数据可以正常显示,但是过一会儿后数据消失,要等几轮才能再读取到数据。(上位机缓存数据直到有新数据存入替换,但是偶尔数据存入为零),想知道有看法的人能解决一下吗。
- #include "AM2302.h"
- #include "reg52.h"
- #include "delay.h"
- #define uchar unsigned char //无符号字符型 宏定义 变量范围0~255
- #define uint unsigned int //无符号整型 宏定义 变量范围0~65535
- uchar U8FLAG,U8temp;
- uchar COM(uchar a)
- { uchar i,U8comdata=0;
- switch(a){
- case 0:
- {
- for(i=0;i<8;i++)
- {
- U8FLAG=2;
- while((!DHT220)&&U8FLAG++);
- Delay_10us();
- Delay_10us();
- Delay_10us();
- U8temp=0;
- if(DHT220)U8temp=1;
- U8FLAG=2;
- while((DHT220)&&U8FLAG++);
- if(U8FLAG==1)break;
- U8comdata<<=1;
- U8comdata|=U8temp;
- }
- }
- break;
- case 1:
- {
- for(i=0;i<8;i++)
- {
- U8FLAG=2;
- while((!DHT221)&&U8FLAG++);
- Delay_10us();
- Delay_10us();
- Delay_10us();
- U8temp=0;
- if(DHT221)U8temp=1;
- U8FLAG=2;
- while((DHT221)&&U8FLAG++);
- if(U8FLAG==1)break;
- U8comdata<<=1;
- U8comdata|=U8temp;
- }
- }
- break;
- }
- return U8comdata;
- }
- void Read_TRH(int *x,int *y,uchar i)
- {
- uint T_data=0;
- uint RH_data=0;
- static uchar U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
- static uchar U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
- switch(i){
- case 0:
- {
- DHT220=0;
- Delay_ms(5);
- DHT220=1;
- Delay_10us();
- Delay_10us();
- Delay_10us();
- Delay_10us();
- DHT220=1;
- if(!DHT220)
- {
- U8FLAG=2;
- while((!DHT220)&&U8FLAG++);
- U8FLAG=2;
- while(DHT220&&U8FLAG++);
- U8RH_data_H_temp=COM(0);
- U8RH_data_L_temp=COM(0);
- U8T_data_H_temp=COM(0);
- U8T_data_L_temp=COM(0);
- U8checkdata_temp=COM(0);
- DHT220=1;
- U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
- if(U8temp==U8checkdata_temp)
- {
- U8RH_data_H=U8RH_data_H_temp;
- U8RH_data_L=U8RH_data_L_temp;
- U8T_data_H=U8T_data_H_temp;
- U8T_data_L=U8T_data_L_temp;
- U8checkdata=U8checkdata_temp;
- }
- RH_data=U8RH_data_H*255+U8RH_data_L;
- T_data=U8T_data_H*255+U8T_data_L;
- }
- *x=RH_data;
- *y=T_data;
- }break;
- case 1:
- {
- DHT221=0;
- Delay_ms(5);
- DHT221=1;
- Delay_10us();
- Delay_10us();
- Delay_10us();
- Delay_10us();
- DHT221=1;
- if(!DHT221)
- {
- U8FLAG=2;
- while((!DHT221)&&U8FLAG++);
- U8FLAG=2;
- while(DHT221&&U8FLAG++);
- U8RH_data_H_temp=COM(1);
- U8RH_data_L_temp=COM(1);
- U8T_data_H_temp=COM(1);
- U8T_data_L_temp=COM(1);
- U8checkdata_temp=COM(1);
- DHT221=1;
- U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
- if(U8temp==U8checkdata_temp)
- {
- U8RH_data_H=U8RH_data_H_temp;
- U8RH_data_L=U8RH_data_L_temp;
- U8T_data_H=U8T_data_H_temp;
- U8T_data_L=U8T_data_L_temp;
- U8checkdata=U8checkdata_temp;
- }
-
- RH_data=U8RH_data_H*255+U8RH_data_L;
- T_data=U8T_data_H*255+U8T_data_L;
- }
- *x=RH_data;
- *y=T_data;
- }break;
- }
- }
- 以下是主程序的各函数
- void dht22(uchar i)
- {
- Read_TRH(&r,&t,i);//读取数据
- }
- int choiceCH(uchar ch)
- {
- switch(ch)
- {
- case 0:{S0=0;S1=0;S2=0;S3=0;};break; //channel 0
- case 1:{S0=1;S1=0;S2=0;S3=0;};break; //channel 1
- case 2:{S0=0;S1=1;S2=0;S3=0;};break; //channel 2
- case 3:{S0=1;S1=1;S2=0;S3=0;};break; //channel 3
- case 4:{S0=0;S1=0;S2=1;S3=0;};break; //channel 4
- case 5:{S0=1;S1=0;S2=1;S3=0;};break; //channel 5
- case 6:{S0=0;S1=1;S2=1;S3=0;};break; //channel 6
- case 7:{S0=1;S1=1;S2=1;S3=0;};break; //channel 7
- case 8:{S0=0;S1=0;S2=0;S3=1;};break; //channel 8
- case 9:{S0=1;S1=0;S2=0;S3=1;};break; //channel 9
- case 10:{S0=0;S1=1;S2=0;S3=1;};break; //channel 10
- case 11:{S0=1;S1=1;S2=0;S3=1;};break; //channel 11
- case 12:{S0=0;S1=0;S2=1;S3=1;};break; //channel 12
- case 13:{S0=1;S1=0;S2=1;S3=1;};break; //channel 13
- case 14:{S0=0;S1=1;S2=1;S3=1;};break; //channel 14
- case 15:{S0=1;S1=1;S2=1;S3=1;};break; //channel 15
- }
- }
- /******************主程序**********************/
- void main()
- { int i,n,m;
- CE=0; //2.4G无线模块IO口初始化
- SCK=0;
- CSN=1;
- chEN1 = 0;
- chEN2 = 0;
- TX_Mode();
- delay_1ms(600);
- while(1)
- {
- for(m=0;m<2;m++)
- for(n=0;n<4;n++)
- {
- switch(4*m+n)
- {
- case 0: Tx_Buf[0] = 0x01; break;
- case 1: Tx_Buf[0] = 0x02; break;
- case 2: Tx_Buf[0] = 0x03; break;
- case 3: Tx_Buf[0] = 0x04; break;
- case 4: Tx_Buf[0] = 0x05; break;
- case 5: Tx_Buf[0] = 0x06; break;
- case 6: Tx_Buf[0] = 0x07; break;
- case 7: Tx_Buf[0] = 0x08; break;
- }
- for(i=0;i<4;i++)
- {
- choiceCH(4*n+i);
- delay_1ms(10);
- dht22(m);
- Tx_Buf[4*i+1] = t% 256;
- Tx_Buf[4*i+2] = t / 256;
- Tx_Buf[4*i+3] = r% 256;
- Tx_Buf[4*i+4] = r / 256;
- delay_1ms(10);
- }
- led = 0;
- Transmit(Tx_Buf);
- delay_1ms(10);
- sta=SPI_Read(READ_REG + STATUS);
- if(TX_DS) //当前STATUS状态 发送中断应使bit5 = 1
- {
- SPI_RW_Reg(WRITE_REG + STATUS,sta);
- }
- if(MAX_RT) //如果是发送超时
- {
- SPI_RW_Reg(WRITE_REG + STATUS,sta);
- }
- led = 1;
-
- delay_1ms(1500);
- }
- }
- }
复制代码
|