【问题标题】:Detecting rising and falling temperature C检测上升和下降温度 C
【发布时间】:2020-07-17 18:49:59
【问题描述】:

我正在尝试使用 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;
        }
    }
}

打开泵后,我需要等到第二个传感器达到阈值 (30C),然后我需要“检测”温度何时开始从 30C 下降。我上传的这段代码仅适用于一个 While(temp > Threshold_Value) 循环。但是当我在它下面插入下一个时(temp

【问题讨论】:

  • 您可能想要创建一个恒温器。为此任务使用 PID 控制器。在这里你有一个非常基本的解释和代码。 playground.arduino.cc/Code/PIDLibrary
  • @P__J__ 您通常不需要恒温器的 PID 控制器。一般来说。有时你会这样做。但大多数情况下,它只是“太热时打开,太冷时关闭”(反之亦然)
  • @user253751 抱歉,这是一种非常幼稚的方法。一切控制通常你需要打开或关闭。但是使用了PID。是有原因的。
  • @P__J__ 没错,使用 PID 是有原因的,但使用“bang-bang”控制器也是有原因的。你知道选择两者的原因吗?您是否知道大多数恒温器都是 bang-bang 控制器?无论如何,这并不能回答问题。
  • 一个简单的调试会话会发现错误的分号。

标签: c microcontroller temperature


【解决方案1】:

不要在while 条件后使用分号。

替换

while (condition);
{
    ... looped code ...
}

while (condition)
{
    .... looped code ...
}

这是我喜欢在条件末尾放置大括号的原因之一(就像您在 if 语句中所做的那样):它有助于查看令人讨厌的意外分号。

【讨论】:

  • while(temp &gt; Threshold_Value); 旋转很多,但动画很少......下一个循环也是如此......
  • 不止一次...谢天谢地 gcc 添加了误导性缩进的警告:)
  • @DavidC.Rankin - 另一个好点!编译时始终使用-Werror 选项(如果使用 gcc)!这可以在它们产卵和繁殖之前捕获很多虫子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多