Rem*_*ark 0 c microcontroller temperature
我正在尝试使用 PIC16F877A MCU 和两个 DS8B20 传感器检测上升和下降的温度。当我尝试检测温度下降时,我遇到了问题。这是我的代码我想做什么:
#include "main.h"
void main() {
//Turn on LCD backlight
output_high(PIN_D7);
// Initialize LCD module
lcd_init();
float Threshold_Value = 30; // Temperature threshold value
while (TRUE) {
Show_User_Info();
delay_ms(10);
Read_Sensors();
// Starting to read user button values
User_Buttons();
delay_ms(20); // Minimum amount of time to read user button values
// Starting to compare user set temperature value and upper sensor temperature read value.
Compare_Upper_Temp();
delay_ms(20);
//================================
// Checking, if the MCU pin connected to pump is high. If yes - do the waiting 'animation'
if (input(PIN_B5)) {
while(temp > Threshold_Value);
{
Bottom_Waiting_Animation();
}
// Experimenting....
// break;
// continue;
}
if (input(PIN_B5)) {
while(temp < Threshold_Value);
{
Bottom_Waiting_Animation();
}
// break;
}
// If the set temp is less than threshold - turn the pump off.
if (temp < Threshold_Value) {
input(PIN_B5) == 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当泵打开时,我需要等到第二个传感器达到阈值 (30C),然后我需要“检测”温度何时开始从 30C 下降。我上传的这段代码仅适用于一个 While(temp > Threshold_Value) 循环。但是当我在它下面插入下一个 while(temp < Threshold_Value) 时,MCU 会跳转到未定义的区域并卡住。这个任务听起来很简单,但我尝试了很多不同的方法来解决这个问题。也许问题原因之一可能是多个 while 循环?
不要在while条件后使用分号。
代替
while (condition);
{
... looped code ...
}
Run Code Online (Sandbox Code Playgroud)
和
while (condition)
{
.... looped code ...
}
Run Code Online (Sandbox Code Playgroud)
这是我喜欢在条件末尾放置大括号的原因之一(就像您在if语句中所做的那样):它有助于看到令人讨厌的意外分号。