Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sbit Data at RB0_bit;
- sbit DataDir at TRISB0_bit;
- unsigned short k;
- unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
- char temp[] = "Temperature is 00.0 C";
- char hum[] = "Humidity is 00.0 %";
- void DHT11StartSignal(){
- DataDir = 0;
- Data = 0;
- Delay_ms(25);
- Data = 1;
- Delay_us(30);
- DataDir = 1;
- }
- unsigned short DHT11CheckResponse(){
- k = 150;
- while(!Data){
- Delay_us(2);
- k--;
- if(k<1) return 0; // time out
- }
- k = 150;
- while(Data){
- Delay_us(2);
- k--;
- if(k<1) return 0; // time out
- }
- return 1;
- }
- unsigned short DHT11ReadByte(){
- int i;
- unsigned short num = 0;
- DataDir = 1;
- for (i=0; i<8; i++){
- while(!Data);
- Delay_us(40);
- if(Data) num |= 1<<(7-i);
- while(Data);
- }
- return num;
- }
- void main() {
- UART1_Init(9600);
- TRISC.RC0 = 0;
- TRISC.RC1 = 0;
- TRISC.RC2 = 0;
- while(1){
- DHT11StartSignal();
- if(!DHT11CheckResponse()) continue;
- RH_Byte1 = DHT11ReadByte();
- RH_Byte2 = DHT11ReadByte();
- T_Byte1 = DHT11ReadByte();
- T_Byte2 = DHT11ReadByte();
- DHT11ReadByte(); /* Checksum */
- // Set temp
- temp[15] = T_Byte1/10 + 48;
- temp[16] = T_Byte1%10 + 48;
- temp[18] = T_Byte2/10 + 48;
- UART1_Write_Text(temp);
- UART1_Write(10);
- UART1_Write(13);
- // Set hum
- hum[12] = RH_Byte1/10 + 48;
- hum[13] = RH_Byte1%10 + 48;
- hum[15] = RH_Byte2/10 + 48;
- UART1_Write_Text(hum);
- UART1_Write(10);
- UART1_Write(13);
- UART1_Write(10);
- UART1_Write(13);
- PORTC.RC0 = (RH_Byte1) >= 20;
- PORTC.RC1 = (RH_Byte1) >= 30;
- PORTC.RC2 = (RH_Byte1) >= 40;
- // Wait
- Delay_ms(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement