MBed 程序卡住

发布于 2025-01-13 02:18:24 字数 1798 浏览 0 评论 0原文

我在 Mbed 框架的帮助下编写了一些代码,该框架要么接受用户输入,然后显示传感器值,要么在 15 分钟后显示该值。当我尝试执行此代码时,它卡在第 21 行 (display.printf("Inside Loop\n");)。

我无法理解为什么会这样,以及解决这个问题的方法是什么,以便执行开关块。我该如何解决这个问题?仅供参考,虽然不重要,但我使用的微控制器是STM32 bluepill (STM32F103C8T6)。

#include "mbed.h"
#include "Sensor_input.h"
#include "Ticker.h"
#include "Dht11.h"

//#include "USBSerial.h"
Serial display(PA_2, PA_3, 9600);
char* a;
Dht11 DhtSensor(PA_4);
Ticker t;
Sensor_input Soil(PB_7, PB_6, 8);
float *SensorData;
void getSensorData();
int main ( void ){

    uint8_t choice = 0;

    display.printf("Enter 1 or 2:\n1.Greenhouse stats\n2.Return Control to System");
    choice = display.putc(display.getc());
    while(1){
        display.printf("Inside loop\n");
        wait_ms(15000);
        switch(choice)
        {
            
            case 1:
                display.printf("Inside case 1");
                a = Soil.readTemp();
                display.printf("Temperature: %f\n",DhtSensor.getCelsius());
                display.printf("Humidity: %f\n",DhtSensor.getHumidity());
                display.printf("Soil water content: %c\n ",*a);
                break;
            case 2:
                /*<GreenHouse object>*/
                /*Might have to proceed with timer*/
                display.printf("Inside case 2");
                t.attach(&getSensorData,4500);
                display.printf("Temperature: %f\n",a[0]);
                display.printf("Humidity: %f\n",a[1]);
                display.printf("Soil water content: %c\n ",a[2]);
                break;
            default:
                break;
            
        }
    }
}
void getSensorData(){
    static float a[3];
    a[0]=DhtSensor.getCelsius();
    a[1]=DhtSensor.getHumidity();
    a[2]=(int)Soil.readTemp();   

}

I have written some code with the help of Mbed framework, which is either supposed to take user input and then display sensor values or display the value after 15mins. When I try to execute this code, it is getting stuck at line 21 (display.printf("Inside loop\n");).

I am not able to understand why is it so and what is the fix for this problem so that the switch block gets executed. How to I solve this? FYI, although not important, the microcontroller I am using is STM32 bluepill (STM32F103C8T6).

#include "mbed.h"
#include "Sensor_input.h"
#include "Ticker.h"
#include "Dht11.h"

//#include "USBSerial.h"
Serial display(PA_2, PA_3, 9600);
char* a;
Dht11 DhtSensor(PA_4);
Ticker t;
Sensor_input Soil(PB_7, PB_6, 8);
float *SensorData;
void getSensorData();
int main ( void ){

    uint8_t choice = 0;

    display.printf("Enter 1 or 2:\n1.Greenhouse stats\n2.Return Control to System");
    choice = display.putc(display.getc());
    while(1){
        display.printf("Inside loop\n");
        wait_ms(15000);
        switch(choice)
        {
            
            case 1:
                display.printf("Inside case 1");
                a = Soil.readTemp();
                display.printf("Temperature: %f\n",DhtSensor.getCelsius());
                display.printf("Humidity: %f\n",DhtSensor.getHumidity());
                display.printf("Soil water content: %c\n ",*a);
                break;
            case 2:
                /*<GreenHouse object>*/
                /*Might have to proceed with timer*/
                display.printf("Inside case 2");
                t.attach(&getSensorData,4500);
                display.printf("Temperature: %f\n",a[0]);
                display.printf("Humidity: %f\n",a[1]);
                display.printf("Soil water content: %c\n ",a[2]);
                break;
            default:
                break;
            
        }
    }
}
void getSensorData(){
    static float a[3];
    a[0]=DhtSensor.getCelsius();
    a[1]=DhtSensor.getHumidity();
    a[2]=(int)Soil.readTemp();   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-01-20 02:18:24

您的 switch 语句可能正在执行,但始终处于“默认”情况。您可以通过在默认值中放置打印语句来测试这一点。

当您从显示器请求一个字符时,它将以 ASCII 字符的形式返回输入。这意味着,如果您在显示屏上输入“1”,它会给您(如 ASCII 表所示)0x31(十进制 49),而不是 1 的值。因此,您必须将大小写更改为“case '1': " 或 "case 0x31:" 以及第二种情况的等效项。

Your switch statement is probably being executed, but always in the 'default' case. You can test this out by putting a print statement in the default.

When you request a char from the display, it will return the input as an ASCII-character. This means, if you enter '1' on the display, it will give you (as the ASCII table says) 0x31 (decimal 49) and not the value of 1. So you have to change your case to "case '1':" or "case 0x31:" and the equivalent for the second case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文