示例#1
0
文件: temp.c 项目: rimek/aquarium
void therm_write_bit(uint8_t bit)
{
	//Pull line low for 1uS
	THERM_LOW();
	THERM_OUTPUT_MODE();
	delay_us(1);//1
	//If we want to write 1, release the line (if not will keep low)
	if(bit) THERM_INPUT_MODE();
	//Wait for 60uS and release the line
	delay_us(60);//60
	THERM_INPUT_MODE();
}
示例#2
0
文件: temp.c 项目: rimek/aquarium
uint8_t therm_read_bit(void)
{
	uint8_t bit=0;
	//Pull line low for 1uS
	THERM_LOW();
	THERM_OUTPUT_MODE();
	delay_us(1);//1
	//Release line and wait for 14uS
	THERM_INPUT_MODE();
	delay_us(14);//14
	//Read line value
	if(THERM_PIN&(1<<THERM_DQ)) bit=1;
	//Wait for 45uS to end and return read value
	delay_us(45);//45
	return bit;
}
示例#3
0
文件: temp.c 项目: rimek/aquarium
uint8_t therm_reset()
{
	uint8_t i;
	//Pull line low and wait for 480uS
	THERM_LOW();
	THERM_OUTPUT_MODE();
	delay_us(480);	//480 //this must be smaller when moving delay func to other .c file
	//Release line and wait for 60uS
	THERM_INPUT_MODE();
	delay_us(60); //60
	//Store line value and wait until the completion of 480uS period
	i=(THERM_PIN & (1<<THERM_DQ));
	delay_us(420); //420
	//Return the value read from the presence pulse (0=OK, 1=WRONG)
	return i;
}
示例#4
0
文件: dht22.c 项目: gerbert/ws2
//
// Read the 40 bit data stream from the DHT 22
// Store the results in private member data to be read by public member functions
//
int dht22_read(float *temperature, float *humidity) {
	// uint8_t bitmask = _bitmask;
	//volatile uint8_t *reg asm("r30") = _baseReg;
	uint8_t retryCount;
	uint8_t bitTimes[DHT22_DATA_BIT_COUNT];
	int currentHumidity;
	int currentTemperature;
	uint8_t checkSum, csPart1, csPart2, csPart3, csPart4;
	int i;

	currentHumidity = 0;
	currentTemperature = 0;
	checkSum = 0;
	
	//return currentTime;
	for(i = 0; i < DHT22_DATA_BIT_COUNT; i++) {
		bitTimes[i] = 0;
	}

	// Pin needs to start HIGH, wait until it is HIGH with a timeout
	//cli();
	THERM_INPUT_MODE();
	//sei();

	retryCount = 0;
	do {
		if (retryCount > 125)
			return DHT_BUS_HUNG;
		retryCount++;
		_delay_us(2);
	} while (!THERM_READ());

	// Send the activate pulse
	//cli();
	THERM_LOW();
	THERM_OUTPUT_MODE(); // Output Low
	//sei();
	_delay_us(1100); // 1.1 ms
	//cli();
	THERM_INPUT_MODE();	// Switch back to input so pin can float
	//sei();
	// Find the start of the ACK Pulse
	retryCount = 0;

	do {
		if (retryCount > 25) //(Spec is 20 to 40 us, 25*2 == 50 us)
			return DHT_ERROR_NOT_PRESENT;
		retryCount++;
		_delay_us(2);
	} while (!THERM_READ());

	// Find the end of the ACK Pulse
	retryCount = 0;
	do {
		if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
			return DHT_ERROR_ACK_TOO_LONG;
		retryCount++;
		_delay_us(2);
	} while (THERM_READ());

	// Read the 40 bit data stream
	for (i = 0; i < DHT22_DATA_BIT_COUNT; i++) {
		// Find the start of the sync pulse
		retryCount = 0;
		do {
			if (retryCount > 35) //(Spec is 50 us, 35*2 == 70 us)
				return DHT_ERROR_SYNC_TIMEOUT;
			retryCount++;
			_delay_us(2);
			// Show activity on TX LED
			PORTD &= ~_BV(PD5);
		} while (!THERM_READ());

		// Measure the width of the data pulse
		retryCount = 0;
		do {
			if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
				return DHT_ERROR_DATA_TIMEOUT;
			retryCount++;
			_delay_us(2);
			// Show activity on TX LED
			PORTD &= ~_BV(PD5);
		} while (THERM_READ());
		bitTimes[i] = retryCount;
	}

	// Now bitTimes have the number of retries (us *2)
	// that were needed to find the end of each data bit
	// Spec: 0 is 26 to 28 us
	// Spec: 1 is 70 us
	// bitTimes[x] <= 11 is a 0
	// bitTimes[x] >  11 is a 1
	// Note: the bits are offset by one from the data sheet, not sure why
	for (i = 0; i < 16; i++) {
		if (bitTimes[i + 1] > 11)
			currentHumidity |= (1 << (15 - i));
	}

	for (i = 0; i < 16; i++) {
		if (bitTimes[i + 17] > 11)
			currentTemperature |= (1 << (15 - i));
	}

	for (i = 0; i < 8; i++) {
		if (bitTimes[i + 33] > 11)
			checkSum |= (1 << (7 - i));
	}

	*humidity = ((float)(currentHumidity & 0x7FFF)) / 10.0;
	
	if(currentTemperature & 0x8000) {
		// Below zero, non standard way of encoding negative numbers!
		currentTemperature &= 0x7FFF;
		*temperature = ((float)currentTemperature / 10.0) * -1.0;
	} else {
		*temperature = (float)currentTemperature / 10.0;
	}

	csPart1 = currentHumidity >> 8;
	csPart2 = currentHumidity & 0xFF;
	csPart3 = currentTemperature >> 8;
	csPart4 = currentTemperature & 0xFF;
	
	if (checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) {
		//*humidity = 10;
		return DHT_ERROR_NONE;
	}

	//*humidity = 20;
	return DHT_ERROR_CHECKSUM;
}