Esempio n. 1
0
byte read_dht11_dat() {
	byte j = 0;
	byte result = 0;
	for (j = 0; j < 8; j++) {
		while (!DHT_GetVal());//while (!(PINC & _BV(DHT11_PIN))); // wait for 50us
		WAIT1_Waitus(30);//delayMicroseconds(30);
		if (DHT_GetVal())//if (PINC & _BV(DHT11_PIN))
		{
			result |= (1 << (7 - j));
			while (DHT_GetVal());//while ((PINC & _BV(DHT11_PIN))); // wait '1' finish
		}
	}
	return result;
}
Esempio n. 2
0
void DHT11() {
	
	byte dht11_in;
	byte i;
	
	// output
	DHT_SetDir(1);
	// pull-down i/o pin for 18ms
	DHT_ClrVal();
	WAIT1_Waitms(19);
	DHT_SetVal();
	//pull-up i/o pin for 30ms
	WAIT1_Waitus(30);

	//input
	DHT_SetDir(0);
	
	WAIT1_Waitus(30);

	while(!dht11_in)
	{
		dht11_in = DHT_GetVal();//dht11_in = PINC & _BV(DHT11_PIN);
	}
//	if (dht11_in) {
//		printf("dht11 start condition 1 not met\r\n");
//		return;
//	}
	WAIT1_Waitus(80);//delayMicroseconds(80);
//	dht11_in = DHT_GetVal();//dht11_in = PINC & _BV(DHT11_PIN);
//	if (!dht11_in) {
//		printf("dht11 start condition 2 not met\r\n");
//		return;
//	}
//	WAIT1_Waitus(80);//delayMicroseconds(80);
	// now ready for data reception
	for (i = 0; i < 5; i++)
		dht11_dat[i] = read_dht11_dat();
	DHT_SetDir(1);//DDRC |= _BV(DHT11_PIN);
	DHT_SetVal();//PORTC |= _BV(DHT11_PIN);
	byte dht11_check_sum = dht11_dat[0] + dht11_dat[1] + dht11_dat[2]
			+ dht11_dat[3];
	// check check_sum
	if (dht11_dat[4] != dht11_check_sum) {
		printf("DHT11 checksum error\r\n");
	}
	
	printf("Current humdity = ");
	printf("%d",dht11_dat[0]);
	printf(".");
	printf("%d",dht11_dat[1]);
	printf("%%\r\n");
	printf("temperature = ");
	printf("%d",dht11_dat[2]);
	printf(".");
	printf("%d",dht11_dat[3]);
	printf("C \r\n");
//	WAIT1_Waitms(2000);//delay(2000);
}
Esempio n. 3
0
int Read(uint8_t *temp, uint8_t *humi) {
	int cntr;
	int loopBits;
	uint8_t buffer[5];
	int i;
	int data;

	/* init buffer */
	for (i = 0; i < sizeof(buffer); i++) {
		buffer[i] = 0;
	}

	//set to input and check if the signal gets pulled up 

	DHT_SetInput();
	WAIT1_Waitus(50);
	if (DHT_GetVal() == 0) {
		return NO_PULLUP;
	}

	// send start signal
	DHT_SetOutput();
	DHT_ClrVal();
	WAIT1_Waitms(18); /* keep signal low for at least 18 ms */
	DHT_SetInput();
	WAIT1_Waitus(50);
	/* check for acknowledge signal */
	if (DHT_GetVal() != 0) { /* signal must be pulled low by the sensor */
		return NO_ACK_0;
	}
	/* wait max 90 us for the ack signal from the sensor */
	cntr = 18;
	while (DHT_GetVal() == 0) { /* wait until signal goes up */
		WAIT1_Waitus(5);
		if (--cntr == 0) {
			return NO_ACK_1; /* signal should be up for the ACK here */
		}
	}
	/* wait until it goes down again, end of ack sequence */
	cntr = 18;
	while (DHT_GetVal() != 0) { /* wait until signal goes down */
		WAIT1_Waitus(5);
		if (--cntr == 0) {
			return NO_ACK_0; /* signal should be down to zero again here */
		}
	}
	/* now read the 40 bit data */
	i = 0;
	data = 0;
	loopBits = 40;
	do {
		cntr = 11; /* wait max 55 us */
		while (DHT_GetVal() == 0) {
			WAIT1_Waitus(5);
			if (--cntr == 0) {
				return NO_DATA_0;
			}
		}
		cntr = 5; /* wait max 75 us */
		while (DHT_GetVal() != 0) {
			WAIT1_Waitus(15);
			if (--cntr == 0) {
				return NO_DATA_1;
			}
		}
		data <<= 1; /* next data bit */
		if (cntr < 3) { /* data signal high > 40 us ==> data bit 1 */
			data |= 1;
		}
		if ((loopBits & 0x7) == 1) { /* next byte */
			buffer[i] = data;
			i++;
			data = 0;
		}
	} while (--loopBits != 0);
	/* now we have the 40 bit (5 bytes) data:
	 * byte 1: humidity integer data
	 * byte 2: humidity decimal data (not used for DTH11, always zero)
	 * byte 3: temperature integer data
	 * byte 4: temperature fractional data (not used for DTH11, always zero)
	 * byte 5: checksum, the sum of byte 1 + 2 + 3 + 4
	 */
	/* test CRC */
	if (buffer[0] + buffer[1] + buffer[2] + buffer[3] != buffer[4]) {
		return BAD_CRC;
	}
	/* store data values for caller */
	*humi = ((int) buffer[0]);
	*temp = ((int) buffer[2]);

	return OK;
}