久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1993|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日韩av在线中文字幕 | 日韩免费中文字幕 | 久久三区 | 亚洲一区中文字幕在线观看 | 九九视频网 | 黄色一级免费看 | 精品三区 | 久久高清精品 | 久久国产综合 | 国产精品女人久久久 | www.蜜桃av| 久草在线影 | 国产在线观 | 日韩不卡在线 | 中文字幕一区二区三区日韩精品 | 精品一区二区三区在线视频 | 波多野结衣一区二区 | 午夜无码国产理论在线 | 天天天操| 亚州精品天堂中文字幕 | 91视频在线 | 99久久精品一区二区毛片吞精 | 九色av| 一区二区三区视频在线观看 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 午夜无码国产理论在线 | 欧美日韩亚洲国产综合 | 久久综合av| 中文字幕av在线播放 | 国产高清视频一区 | 一级黄色毛片免费 | 亚洲一二三区精品 | 精品网站999 | 欧美午夜久久 | 大陆一级毛片免费视频观看 | 激情五月婷婷 | 欧美一级在线观看 | 久久久久久久久综合 | 欧美激情va永久在线播放 | 四虎成人精品永久免费av九九 | 久久国产成人 |