[英]How to create a thread in mbed?
使用操纵杆控制鼠标光标。 使用 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
These are the related links: 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 。
从MBED Online Compiler导出工程时,将 mbed 生成的mbed_config.h
文件添加到目标 MCU 的device.h
文件中,如下所示。 :
将以下声明添加到目标 MCU 的device.h
文件中:
#include "mbed_config.h"
MBED在线编译器output中相关文件的目录结构如下:
output_folder
|
|------ "mbed_config.h"
|
|------ mbed/
|
|------ {TARGET-PlATFORM}/
|
|------ {TOOLCHAIN}/
|
|------ "device.h"
删除行:
Serial pc(USBTX,USBRX);
变量pc
没有在任何地方使用,对Serial
的引用是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.