找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1350|回复: 5
打印 上一主题 下一主题
收起左侧

请问arduino能实现多线程吗?

[复制链接]
跳转到指定楼层
楼主
ID:703212 发表于 2020-3-6 00:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题,arduino能实现多线程吗?我想几个程序同时运行
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:560467 发表于 2020-3-6 16:14 | 只看该作者
可以做类多线程
回复

使用道具 举报

板凳
ID:687694 发表于 2020-3-6 17:36 | 只看该作者
操作系统了解一下。。。51都有RTX51
回复

使用道具 举报

地板
ID:155507 发表于 2020-3-6 22:40 | 只看该作者
我给你来个程序试试


  1. // SeveralThingsAtTheSameTimeRev1.ino

  2. // An expansion of the BlinkWithoutDelay concept to illustrate how a script
  3. //  can appear to do several things at the same time

  4. // this sketch does the following
  5. //    it blinks the onboard LED (as in the blinkWithoutDelay sketch)
  6. //    it blinks two external LEDs (LedA and LedB) that are connected to pins 12 and 11.
  7. //    it turns another Led (buttonLed connected to pin 10) on or off whenever a button
  8. //       connected to pin 7 is pressed
  9. //    it sweeps a servo (connected to pin 5) back and forth at different speeds

  10. //  One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
  11. //   resistor of 470 ohms or more and the other end of the resistor to the Arduino GND.
  12. //   If the LED doesn't light its probably connected the wrong way round.

  13. //  On my Uno and Mega the "button" is just a piece of wire inserted into pin 7.
  14. //   Touching the end of the wire with a moist finger is sufficient to cause the switching action
  15. //   Of course a proper press-on-release-off button switch could also be used!

  16. //  The Arduino is not capable of supplying enough 5v power to operate a servo
  17. //    The servo should have it's own power supply and the power supply Ground should
  18. //      be connected to the Arduino Ground.

  19. // The sketch is written to illustrate a few different programming features.
  20. //    The use of many functions with short pieces of code.
  21. //       Short pieces of code are much easier to follow and debug
  22. //    The use of variables to record the state of something (e.g. onBoardLedState) as a means to
  23. //       enable the different functions to determine what to do.
  24. //    The use of millis() to manage the timing of activities
  25. //    The definition of all numbers used by the program at the top of the sketch where
  26. //       they can easily be found if they need to be changed

  27. //=======

  28. // -----LIBRARIES

  29. #include <Servo.h>

  30. // ----CONSTANTS (won't change)

  31. const int onBoardLedPin =  13;      // the pin numbers for the LEDs
  32. const int led_A_Pin = 12;
  33. const int led_B_Pin = 11;
  34. const int buttonLed_Pin = 10;

  35. const int buttonPin = 7; // the pin number for the button

  36. const int servoPin = 5; // the pin number for the servo signal

  37. const int onBoardLedInterval = 500; // number of millisecs between blinks
  38. const int led_A_Interval = 2500;
  39. const int led_B_Interval = 4500;

  40. const int blinkDuration = 500; // number of millisecs that Led's are on - all three leds use this

  41. const int buttonInterval = 300; // number of millisecs between button readings

  42. const int servoMinDegrees = 20; // the limits to servo movement
  43. const int servoMaxDegrees = 150;


  44. //------- VARIABLES (will change)

  45. byte onBoardLedState = LOW;             // used to record whether the LEDs are on or off
  46. byte led_A_State = LOW;           //   LOW = off
  47. byte led_B_State = LOW;
  48. byte buttonLed_State = LOW;

  49. Servo myservo;  // create servo object to control a servo

  50. int servoPosition = 90;     // the current angle of the servo - starting at 90.
  51. int servoSlowInterval = 80; // millisecs between servo moves
  52. int servoFastInterval = 10;
  53. int servoInterval = servoSlowInterval; // initial millisecs between servo moves
  54. int servoDegrees = 2;       // amount servo moves at each step
  55.                            //    will be changed to negative value for movement in the other direction

  56. unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
  57. unsigned long previousOnBoardLedMillis = 0;   // will store last time the LED was updated
  58. unsigned long previousLed_A_Millis = 0;
  59. unsigned long previousLed_B_Millis = 0;

  60. unsigned long previousButtonMillis = 0; // time when button press last checked

  61. unsigned long previousServoMillis = 0; // the time when the servo was last moved

  62. //========

  63. void setup() {

  64. Serial.begin(9600);
  65. Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running

  66.      // set the Led pins as output:
  67. pinMode(onBoardLedPin, OUTPUT);
  68. pinMode(led_A_Pin, OUTPUT);
  69. pinMode(led_B_Pin, OUTPUT);
  70. pinMode(buttonLed_Pin, OUTPUT);

  71.      // set the button pin as input with a pullup resistor to ensure it defaults to HIGH
  72. pinMode(buttonPin, INPUT_PULLUP);

  73. myservo.write(servoPosition); // sets the initial position
  74. myservo.attach(servoPin);

  75. }

  76. //=======

  77. void loop() {

  78.      // Notice that none of the action happens in loop() apart from reading millis()
  79.      //   it just calls the functions that have the action code

  80. currentMillis = millis();   // capture the latest value of millis()
  81.                              //   this is equivalent to noting the time from a clock
  82.                              //   use the same time for all LED flashes to keep them synchronized

  83. readButton();               // call the functions that do the work
  84. updateOnBoardLedState();
  85. updateLed_A_State();
  86. updateLed_B_State();
  87. switchLeds();
  88. servoSweep();

  89. }

  90. //========

  91. void updateOnBoardLedState() {

  92. if (onBoardLedState == LOW) {
  93.          // if the Led is off, we must wait for the interval to expire before turning it on
  94.    if (currentMillis - previousOnBoardLedMillis >= onBoardLedInterval) {
  95.          // time is up, so change the state to HIGH
  96.       onBoardLedState = HIGH;
  97.          // and save the time when we made the change
  98.       previousOnBoardLedMillis += onBoardLedInterval;
  99.          // NOTE: The previous line could alternatively be
  100.          //              previousOnBoardLedMillis = currentMillis
  101.          //        which is the style used in the BlinkWithoutDelay example sketch
  102.          //        Adding on the interval is a better way to ensure that succesive periods are identical

  103.    }
  104. }
  105. else {  // i.e. if onBoardLedState is HIGH

  106.          // if the Led is on, we must wait for the duration to expire before turning it off
  107.    if (currentMillis - previousOnBoardLedMillis >= blinkDuration) {
  108.          // time is up, so change the state to LOW
  109.       onBoardLedState = LOW;
  110.          // and save the time when we made the change
  111.       previousOnBoardLedMillis += blinkDuration;
  112.    }
  113. }
  114. }

  115. //=======

  116. void updateLed_A_State() {

  117. if (led_A_State == LOW) {
  118.    if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
  119.       led_A_State = HIGH;
  120.       previousLed_A_Millis += led_A_Interval;
  121.    }
  122. }
  123. else {
  124.    if (currentMillis - previousLed_A_Millis >= blinkDuration) {
  125.       led_A_State = LOW;
  126.       previousLed_A_Millis += blinkDuration;
  127.    }
  128. }   
  129. }

  130. //=======

  131. void updateLed_B_State() {

  132. if (led_B_State == LOW) {
  133.    if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
  134.       led_B_State = HIGH;
  135.       previousLed_B_Millis += led_B_Interval;
  136.    }
  137. }
  138. else {
  139.    if (currentMillis - previousLed_B_Millis >= blinkDuration) {
  140.       led_B_State = LOW;
  141.       previousLed_B_Millis += blinkDuration;
  142.    }
  143. }   
  144. }

  145. //========

  146. void switchLeds() {
  147.      // this is the code that actually switches the LEDs on and off

  148. digitalWrite(onBoardLedPin, onBoardLedState);
  149. digitalWrite(led_A_Pin, led_A_State);
  150. digitalWrite(led_B_Pin, led_B_State);
  151. digitalWrite(buttonLed_Pin, buttonLed_State);
  152. }

  153. //=======

  154. void readButton() {

  155.      // this only reads the button state after the button interval has elapsed
  156.      //  this avoids multiple flashes if the button bounces
  157.      // every time the button is pressed it changes buttonLed_State causing the Led to go on or off
  158.      // Notice that there is no need to synchronize this use of millis() with the flashing Leds

  159. if (millis() - previousButtonMillis >= buttonInterval) {

  160.    if (digitalRead(buttonPin) == LOW) {
  161.      buttonLed_State = ! buttonLed_State; // this changes it to LOW if it was HIGH
  162.                                           //   and to HIGH if it was LOW
  163.      previousButtonMillis += buttonInterval;
  164.    }
  165. }

  166. }

  167. //========

  168. void servoSweep() {

  169.      // this is similar to the servo sweep example except that it uses millis() rather than delay()

  170.      // nothing happens unless the interval has expired
  171.      // the value of currentMillis was set in loop()

  172. if (currentMillis - previousServoMillis >= servoInterval) {
  173.        // its time for another move
  174.    previousServoMillis += servoInterval;
  175.    
  176.    servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

  177.    if (servoPosition <= servoMinDegrees) {
  178.          // when the servo gets to its minimum position change the interval to change the speed
  179.       if (servoInterval == servoSlowInterval) {
  180.         servoInterval = servoFastInterval;
  181.       }
  182.       else {
  183.        servoInterval = servoSlowInterval;
  184.       }
  185.    }
  186.    if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {
  187.          // if the servo is at either extreme change the sign of the degrees to make it move the other way
  188.      servoDegrees = - servoDegrees; // reverse direction
  189.          // and update the position to ensure it is within range
  190.      servoPosition = servoPosition + servoDegrees;
  191.    }
  192.        // make the servo move to the next position
  193.    myservo.write(servoPosition);
  194.        // and record the time when the move happened
  195. }
  196. }

  197. //=====END

复制代码
回复

使用道具 举报

5#
ID:420836 发表于 2020-3-7 10:00 | 只看该作者
即使是51也可以进行多线程处理,为什么功能更强大的Arduino不能呢?
回复

使用道具 举报

6#
ID:401564 发表于 2020-3-7 13:53 | 只看该作者
多线程什么意思呢?几个程序一起运行?
如果你是说多个功能一起执行,那是可以的,比如:在发送IIC数据的同时可以输出PWM,还可以进行ADC
但这都是CPU的外设在进行的,CPU的代码一样的在一条接着一条指令的去运行的,都会按一个已经设定好的先后顺序或者是默认的顺序来一个一个指令的去完成的
只不过是执行的时间很短,感觉上就是同时在进行的一样
台湾的应广单片机就有双核心的,那就是真正的同时在进行的了,但这功能并不实用,所以,除了低端的消费品,也没有几个是用这种单片机的
Arduino的内核大多是AVR单片机
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表