问下dht11这样该怎么改才好总感觉不对
时间:10-02
整理:3721RD
点击:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DHT11=P1^0;
- uchar wdz,wdx,sdz,sdx,jy,temp;
- uchar code duanxuan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code weixuan[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f} ;
- uchar xianshi[8];
- int num;
- uchar bai,shi,ge;
- void InitTimer0(void);
- void DHT11init();
- void DHT11receive();
- void display();
- void DHT11read();
- void delay20ms();
- void delay30us();
- void main()
- {
- InitTimer0();
- while(1)
- {
- DHT11read();
- display();
- }
- }
- void delay20ms(void) //误差 0us
- {
- unsigned char a,b;
- for(b=215;b>0;b--)
- for(a=45;a>0;a--);
- _nop_(); //if Keil,require use intrins.h
- _nop_(); //if Keil,require use intrins.h
- }
- void delay30us(void) //误差 0us
- {
- unsigned char a,b;
- for(b=3;b>0;b--)
- for(a=3;a>0;a--);
- }
- void InitTimer0(void)
- {
- TMOD = 0x01;
- TH0 = 0x3C;
- TL0 = 0xB0;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void Timer0Interrupt(void) interrupt 1
- {
- uchar a;
- a++;
- TH0 = 0x3C;
- TL0 = 0xB0;
- if(a>=2)
- {
- a=0;
- DHT11init();
- }
- }
- void DHT11init()
- {
- DHT11=0;
- delay20ms();
- DHT11=1;
- }
- void DHT11receive()
- {
- uchar a;
- for(a=0;a<8;a++)
- {
- while(!DHT11);
- delay30us();
- if(DHT11==1)
- {
- temp=temp+1;
- temp=_cror_(temp,1);
- }
- if(DHT11==0)
- {
- temp=_cror_(temp,1);
- }
- while(DHT11);
- }
- }
- void DHT11read()
- {
- while(DHT11);
- while(!DHT11);
- DHT11receive();
- sdz=temp;
- temp=0;
- DHT11receive();
- sdx=temp;
- temp=0;
- DHT11receive();
- wdz=temp;
- temp=0;
- DHT11receive();
- wdx=temp;
- temp=0;
- DHT11receive();
- jy=temp;
- temp=0;
- }
- void display()
- {
- uchar i;
- num=sdz;
- bai=num/100;
- shi=num%100/10;
- ge=num%10;
- xianshi[1]=duanxuan[bai];
- xianshi[2]=duanxuan[shi];
- xianshi[3]=duanxuan[ge];
- for(i=0;i<4;i++)
- {
- P2=~xianshi<i>;
- P3=weixuan<i>;
- delay20ms();
- }
- }</i></i>
显示函数稍微慢了点没关系我造的其他问题和我说说
谢谢小编分享!