arduino如何实现多线程? admin 2023-09-19 11:48:02 篇首语:本文由小编为大家整理,主要介绍了arduino如何实现多线程?相关的知识,希望对你有一定的参考价值。 Arduino只有一个CPU,要在一个CPU上实现多线程的话,最终都是通过软件实现的。 Arduino是一款便捷灵活、方便上手的开源电子原型平台,包含硬件(各种型号的Arduino板)和软件(Arduino IDE)。 它构建于开放原始码simple I/O介面版,并且具有使用类似Java、C语言的Processing/Wiring开发环境。 看实际情况吧, 如果只是差一个半个输入 I/O, 可以选用 Nano 板代替, 比 UNO 多出两个 analog input。 如果真的不够用, 想要调用的库也不少了, 也要考虑程式的空间是否足够。UNO 只有 32KB flash, Mega2560 可是 256KB。 而 SDRAM mega2560 亦是 UNO 的 4 倍。 对比较大的程式, mega 是必然的。 这些记忆体的限制, 单靠扩展I/O是不行的。 如果空间及预算许可的话, mgea2560 硬体上也可以给更大的弹性, 将来再增加设备也比较容易, 而且有多个 serial I/O 及 interrupt, 可实现的东西更多。 但 mgea2560 长一半, 对细小系统来说会比较大了。 所以, 如果可以的话, 个人觉得 mega2560 比 UNO+扩展I/O 更好。 参考技术A 首先,Arduino只有一个CPU,要在一个CPU上实现多线程的话,最终都是通过软件实现的。从软件的角度把CPU的时间分成很小的时间片,看起来像是多个任务同时在运午。你可以考虑的是对你的循环做优化来实现你讲的“多线程”。还有问题的话建议您贴一下代码会清楚一些吧。本回答被提问者采纳 c_cpp 如何“多线程”Arduino(Protothreading教程) - 来自https://create.arduino.cc/projecthub/reanimationxp/how-to /*Arduino Protothreading Example v1.0by Drew Alden (@ReanimationXP) 1/12/2016*///COMPONENTS/*This code was made using the Sunfounder Arduino starter kit"s blue LCD.It can be found at Amazon.com in a variety of kits.*///THIRD-PARTY LIBRARIES //these must be manually added to your Arduino IDE installation //see sites for details. //gives us the ability to do a foreach loop: //http://playground.arduino.cc/Code/Utility #include //allows us to set actions to perform on separate timed intervals //http://playground.arduino.cc/Code/TimedAction //http://wiring.uniandes.edu.co/source/trunk/wiring/firmware/libraries/TimedAction #include //NATIVE LIBRARIES #include /* LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello World!" to the LCD and shows the time. The circuit: * LCD RS pin to digital pin 12. * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * LCD VSS pin to ground * LCD VCC pin to 5V * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe modified 22 Nov 2010 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/LiquidCrystal *///GLOBALSint backlightPin = 9; // used for backlight fadingint timerCounter = 0; // incrementing counter. will crash eventually.int stringNo = 0; //which text string to show// "16 CHARACTER MAX"char* stringArray[]={"Check it out... ", "I have 3 threads", "going at once...", "Bitches! :D "}; //INITIALIZATION//This should probably be done inside setup(), but whatever.// initialize the LCD library with the numbers of the interface pinsLiquidCrystal lcd(12, 11, 5, 4, 3, 2);//FUNCTIONS//create a couple timers that will fire repeatedly every x msTimedAction numberThread = TimedAction(700,incrementNumber);TimedAction textThread = TimedAction(3000,changeText);//this is our first task, print an incrementing number to the LCDvoid incrementNumber(){ // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // add one to the counter, then display it. timerCounter = timerCounter + 1; lcd.print(timerCounter);}//our second task, fires every few seconds and rotates text stringsvoid changeText(){ // Print a message to the LCD. lcd.setCursor(0, 0); lcd.print(stringArray[stringNo]); //next string if (stringNo >= sizeof(stringArray)/sizeof(char *)){ //nasty hack to get number of Array elements stringNo = 0; changeText(); //not sure if this is technically right, but it works? } else{ stringNo = stringNo + 1; }}// where"s our third task? well, it"s the loop itself :) the task// which repeats most often should be used as the loop. other// tasks are able to "interrupt" the fastest repeating task.void setup() { //define the LCD"s number of columns and rows: lcd.begin(16, 2); //fire changeText once to paint the initial string [0] changeText();}void loop() { //check on our threads. based on how long the system has been //running, do they need to fire and do work? if so, do it! numberThread.check(); textThread.check(); //third task, fade in backlight from min to max brightness //in increments of 5 points: digitalWrite(13, HIGH); for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 10) { //wait a second, why am i checking on the threads here? because //this is a for loop. you must check on your threads during ANY //loops that occur, including the main one! numberThread.check(); textThread.check(); //sets the value (range from 0 to 255): analogWrite(backlightPin, fadeValue); // wait for 20 milliseconds to see the dimming effect // keep delays on the main loop SHORT. these WILL prevent // other threads from firing on time. delay(20); } //fade out from max to min in increments of 5 points: digitalWrite(13, LOW); for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 10) { //check on our threads again numberThread.check(); textThread.check(); //sets the value (range from 0 to 255): analogWrite(backlightPin, fadeValue); //wait for 20 milliseconds to see the dimming effect delay(20); } /* For some scrolling message fun in the future... lcd.setCursor(15,0); // set the cursor to column 15, line 0 for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++) { lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left. lcd.print(array1[positionCounter1]); // Print a message to the LCD. delay(tim); //wait for 250 microseconds } lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner. lcd.setCursor(15,1); // set the cursor to column 15, line 1 for (int positionCounter = 0; positionCounter < 26; positionCounter++) { lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left. lcd.print(array2[positionCounter]); // Print a message to the LCD. delay(tim); //wait for 250 microseconds } lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner. */ } 以上是关于arduino如何实现多线程?的主要内容,如果未能解决你的问题,请参考以下文章 OBS 虚拟摄像头的使用 SQL语句怎么加列 您可能还会对下面的文章感兴趣: 相关文章 浏览器打不开网址提示“ERR_CONNECTION_TIMED_OUT”错误代码的解决方法 如何安装ocx控件 VMware的虚拟机为啥ip地址老是自动变化 vbyone和EDP区别 linux/debian到底怎么重启和关机 苹果平板键盘被弄到上方去了,如何调回正常? 机器学习常用距离度量 如何查看kindle型号