gpt4 book ai didi

c - 嵌入DHT11,第一次打印后温度变为0

转载 作者:行者123 更新时间:2023-11-30 14:35:54 25 4
gpt4 key购买 nike

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

我从 other sources 获得了帮助和 documentation创建此温度,温度仅显示一次,然后默认为 0。

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

最后,代码如下:

#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 : %d\n", temperature );
/*printf("%d", bits[0]);
printf("%d", bits[1]);
printf("%d", bits[2]);
printf("%d", bits[3]);
printf("%d\n", 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 : %d\n", 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]时,将使用最后的有效温度。

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

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

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

关于c - 嵌入DHT11,第一次打印后温度变为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58290741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com