C语言 嵌入式 DHT11,首次打印后温度变为 0



所以,我正在尝试在C环境中设置DHT11,以尝试学习更多的嵌入式软件编程。

我已经从其他来源和文档中获得了帮助来创建它,温度只显示一次,然后默认为 0。

我正在使用Arduino作为微控制器(因为它是我唯一的微控制器(,并使用Linux Ubuntu环境将程序加载到微控制器中。

最后,代码如下所示:

#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#include "dht.h"
// Trying to set up the DHT11 so it can be used in this project
void read() {
// The bits are for to get the temperature values in.
// reqCounter is for the request later on.
uint8_t bits[5];
// Setting up the DDRD as OUTPUT and PORTD as HIGH
DDRD |= (1 << DDD5);
PORTD |= (1 << PORTD5);
_delay_ms(100);
/*
* Makes an request to get the data from the DHT11
* Set the the PORTD5 as LOW
*/
PORTD &= ~(1 << PORTD5);
_delay_ms(18);
// Setting it back to high and DDRD as an input
PORTD |= (1 << PORTD5);
_delay_us(1);
DDRD &= ~(1 << DDD5);
/*
* Checking to see if the ACK is happening
*/
_delay_us(39);
if ((PIND & (1 << PIND5))) {
return;
}
_delay_us(80);
if (!(PIND & (1 << PIND5))) {
return;
}
_delay_us(80);
// Holds the temperature 
uint8_t temp;
// Reading the data
for (uint8_t i = 0; i < 5; i++) {
// Create a for loop that looks in to every 8 bits
for (uint8_t j = 0; j < 8; j++) {
// While it isn`t high, loop this while loop
while (!(PIND & (1 << PIND5)))
;
_delay_us(30);
// Looking if the input is still high after 30us
if (PIND & (1 << PIND5)) {
temp |= (1 << (7-i));
}
// Wait until the input is low.
while (PIND & (1 << PIND5))
;
}
// Put the temperature in the bit array
bits[i] = temp;
}
// Resets the pins
DDRD |= (1 << DDD5);
PORTD |= (1 << PORTD5);
_delay_ms(100);
uint8_t temperature ;
//checks the sum and gets the temperature 
if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
temperature = bits[2];
}
printf("temperature : %dn", temperature );
/*printf("%d", bits[0]);
printf("%d", bits[1]);
printf("%d", bits[2]);
printf("%d", bits[3]);
printf("%dn", bits[4]);*/
}

这产生的只是 15 一次,然后注意到更多。我试图吹它以提高温度,但什么也没发生。

这里可能有什么问题?

提前致谢

在这里:

uint8_t temperature ;
//checks the sum and gets the temperature 
if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
temperature = bits[2];
}
printf("temperature : %dn", temperature );

如果bits[0] + bits[1] + bits[2] + bits[3] == bits[4]false,则初始化温度(并且可以有任何值 - 包括零或15,其中一个值是垃圾(。

也许:

static uint8_t temperature = 0 ;

这样当bits[0] + bits[1] + bits[2] + bits[3] != bits[4]时,将使用最后的有效温度。

或者更简单地说,如果temperature不在其他地方使用:

//checks the sum and gets the temperature 
if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) 
{
printf( "temperature : %dn", bits[2] ) ;
}

然而,我想象这里发生的事情是temperature碰巧是 15 的未初始化值,然后你打印它,然后bits[0] + bits[1] + bits[2] + bits[3] == bits[4]变成true,位 [2] 总是零。 在这种情况下,错误在于输入和放置在bits[]中的值,并且此解决方案将导致值始终为零。

所以,谢谢大家的帮助,但我设法找到了一种方法来让它工作。 如果你想知道怎么做,我把它放在 pastebin 中,作为你们所有人看到的一种方式。

但简而言之,我做了一个更改,所以它在 for 循环中看起来更像这样:

// Reading the data and starts with bits 0 up to 4
for (j = 0; j < 5; j++) {
uint8_t result = 0;
// I read every bit in the system to be able to get it in to the bits.
for (i = 0; i < 8; i++) {
while (!(PIND & (1 << PIND5)))
; //wait for an HIGH input
// delay for 30us
_delay_us(30);
//if the input is HIGH then I will put the data in result
if (PIND & (1 << PIND5))
result |= (1 << (7 - i));
while (PIND & (1 << PIND5))
; //wait until input get LOW
}
//Put the result in the bit
bits[j] = result;
}

对于给您带来的不便,我们深表歉意,谢谢大家。

目前尚不清楚为什么您选择在"其他来源"重写(并破坏(代码,而不是简单地按原样使用它:

void read( void )
{
printf("temperature : %dn", (int)DHT11_GetData(DHT11_DATA_TEMPERATURE)) ;
}

最新更新