我正在尝试使用PIC16F877A单片机和两个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;
}
}
}当泵打开时,我需要等到第二个传感器达到阈值( 30C ),然后我需要“检测”温度从30C开始下降的时间。我上传的这段代码只在一个While(temp > Threshold_Value)循环中起作用。但是当我在它下面插入下一个(temp< Threshold_Value)时,单片机跳跃在未定义的区域并卡住了。这个任务听起来很简单,但是我尝试了很多不同的方法来解决这个问题。也许问题的原因之一可能是多个while循环?
发布于 2020-07-18 03:47:21
不要在while条件后使用分号。
替换
while (condition);
{
... looped code ...
}使用
while (condition)
{
.... looped code ...
}这是我喜欢将左大括号放在条件末尾的原因之一(就像您在if语句中所做的那样):它有助于看到令人讨厌的意外分号。
https://stackoverflow.com/questions/62960138
复制相似问题