日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

arduino 程序的机制

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 arduino 程序的机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??????? 從一個簡單的 arduino 程序說起:

/*BlinkTurns on an LED on for one second, then off for one second, repeatedly.This example code is in the public domain.*/// Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13;// the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output.pinMode(led, OUTPUT); }// the loop routine runs over and over again forever: void loop() {digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(led, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second }

??????? 這個程序的主體,提供了 setup() 和 loop() 二個函數。setup 函數做一些初始化的工作,在系統上電或復位后,此函數只會執行一次。???? loop 函數會在 setup 之后一直循環運行。


??????? 在了解如何使用之后,其背后的機制是怎樣的呢,那么,可以到安裝目錄下打開代碼文件:C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\main.cpp ?代碼如下:


#include <Arduino.h>int main(void) {init();#if defined(USBCON)USBDevice.attach(); #endifsetup();for (;;) {loop();if (serialEventRun) serialEventRun();}return 0; }

?????? 相信您看到了這段代碼,就該知道 setup 和 loop 函數的前世今生了吧。我們所寫的setup 和 loop 函數是這兩個函數的定義。這兩個函數在 main ( )主程序中已經預調用了。?? 在 loop 函數運行之后,我們還會看到 serialEventRun 的函數,此函數的功能是當串口有數據過來的時候,它可以調用Arduino的另一個函數?serialEvent。

?????? 打開 Arduino IDE , 選擇菜單:文件 -> 示例 -> 04.Communication?-> SerialEvent?具體看下面的代碼:


/*Serial Event exampleWhen new serial data arrives, this sketch adds it to a String.When a newline is received, the loop prints the string andclears it.A good test for this is to try it with a GPS receiverthat sends out NMEA 0183 sentences.Created 9 May 2011by Tom IgoeThis example code is in the public domain.http://www.arduino.cc/en/Tutorial/SerialEvent*/String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is completevoid setup() {// initialize serial:Serial.begin(9600);// reserve 200 bytes for the inputString:inputString.reserve(200); }void loop() {// print the string when a newline arrives:if (stringComplete) {Serial.println(inputString);// clear the string:inputString = "";stringComplete = false;} }/*SerialEvent occurs whenever a new data comes in thehardware serial RX. This routine is run between eachtime loop() runs, so using delay inside loop can delayresponse. Multiple bytes of data may be available.*/ void serialEvent() {while (Serial.available()) {// get the new byte:char inChar = (char)Serial.read();// add it to the inputString:inputString += inChar;// if the incoming character is a newline, set a flag// so the main loop can do something about it:if (inChar == '\n') {stringComplete = true;}} }
??????? 此代碼的功能是:系統上電后,接收串口的輸入數據并發送回去,類似 echo。系統的實現是通過在主循環判斷全局變量 stringComplete 的狀態來決定是否發送接收到的數據。而 stringComplete 的狀態是在 serialEvent 這個函數里賦值的。根據 serialEvent 函數注釋看,此函數的調用是在每次 loop 函數運行之后才執行的。再回到我們之前的代碼:


for (;;) {loop();if (serialEventRun) serialEventRun();}
??????? 通過上面的代碼,可以很明確的看出 serialEventRun 函數是在 loop 函數之后執行的。如果我們有多個串口,比如 2560 的板子提供了4個串口,那么 serialEventRun 函數又是如何處理的呢,我們打開代碼文件:C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp 找到 serialEventRun 函數的實現,代碼如下:

void serialEventRun(void) { #ifdef serialEvent_implementedif (Serial.available()) serialEvent(); #endif #ifdef serialEvent1_implementedif (Serial1.available()) serialEvent1(); #endif #ifdef serialEvent2_implementedif (Serial2.available()) serialEvent2(); #endif #ifdef serialEvent3_implementedif (Serial3.available()) serialEvent3(); #endif }
??????? serialEventRun 的函數體中是依次的調用 serialEvent() serialEvent1() serialEvent2() serialEvent3() 函數的。如果您的應用需要通過多個串口讀寫數據,那么在使用 serialEvent 函數的過程中,就要考慮接受的數據長度以及函數的處理時間,否則極有可能會導致其它串口的接收緩沖區滿而影響應用。



總結

以上是生活随笔為你收集整理的arduino 程序的机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。