【问题标题】:How to create a thread in mbed?如何在mbed中创建一个线程?
【发布时间】:2022-01-21 06:40:17
【问题描述】:

使用操纵杆控制鼠标光标。使用 LCD 从温度传感器读取温度。通过运行到 2 个单线程。

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "USBMouse.h"
#include "LM75B.h"
#include "C12832.h"

Thread thread;
Thread thread1;
//Thread thread2;
//Thread thread3;
//Thread thread4;

USBMouse mouse;
// x and y axis of the joystick
AnalogIn   ainx(A0);
AnalogIn   ainy(A1);
BusIn*input = new BusIn(p15,p12,p13,p16);
int16_t x;
int16_t y;

C12832 lcd(p5, p7, p6, p8, p11);
 
LM75B sensor(p28,p27);
Serial pc(USBTX,USBRX);

void MouseCursor() {
   while (1){int state = input->read();  // get current state from input
    x = 0;
    y = 0;
    
    switch(state & input->mask()) { //mask bits to test
    case 0x0:
        // do nothing - stick neutral position
        break;
    case 0x1:
        // stick down (relative to LCD screen)
        y = -1;
        break;
    case 0x2:
        // stick up
        y = 1;
        break;
    case 0x4:
        // stick left
        x = -1;
        break;
    case 0x8:
        // stick right
        x = 1;
    }
        // moves mouse to specified x, y coordinates on screen
        mouse.move(x, y);
        wait_us(500);
    } 
}
void TemperatureSensor(){
    //Try to open the LM75B
    if (sensor.open()) {
        printf("Device detected!\n");
 
        while (1) {
            lcd.cls();
            lcd.locate(0,3);
            lcd.printf("Temp = %.3f\n", (float)sensor);
            wait(1.0);
        }
 
    } else {
        error("Device not detected!\n");
    }
}

int main() {
   thread.start(MouseCursor);
   thread1.start(TemperatureSensor);
}

错误信息是: 错误:“main.cpp”中的未知类型名称“Serial”,行:27,列:1

这些是相关链接: https://os.mbed.com/docs/mbed-os/v6.15/apis/usbmouse.html , https://os.mbed.com/docs/mbed-os/v6.15/program-setup/concepts.html , https://os.mbed.com/cookbook/mbed-application-board#11-lm75b-temperature-sensor.

【问题讨论】:

  • 如果错误是“未知类型名称”,为什么问题是“如何创建线程”?两者无关。鉴于未引用变量 Serial pc,您可以简单地删除它 - 它没有任何作用。这不是唯一未引用的全局变量。除此之外,您在全球范围内声明的变量之一都不需要是全局的。这是不明智的做法:embedded.com/a-pox-on-globals

标签: c++ c multithreading embedded mbed


【解决方案1】:

删除该行:

Serial pc(USBTX,USBRX);

变量pc没有在任何地方使用,对Serial的引用是多余的。

【讨论】:

    【解决方案2】:

    MBED Online Compiler导出工程时,将mbed生成的mbed_config.h文件添加到目标MCU的device.h文件中,如下所示。 :

    在目标MCU的device.h文件中添加如下声明:

    #include "mbed_config.h"
    

    MBED Online Compiler 输出中相关文件的目录结构如下:

    output_folder
      |
      |------ "mbed_config.h"
      |
      |------ mbed/
                |
                |------ {TARGET-PlATFORM}/
                          |
                          |------ {TOOLCHAIN}/
                                    |
                                    |------ "device.h"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2023-03-15
      相关资源
      最近更新 更多