<li id="8g3ty"><tbody id="8g3ty"><th id="8g3ty"></th></tbody></li>
    <label id="8g3ty"><samp id="8g3ty"></samp></label>
  • <span id="8g3ty"></span>

    1. <center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
    2. <bdo id="8g3ty"><meter id="8g3ty"><bdo id="8g3ty"></bdo></meter></bdo><center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
      <label id="8g3ty"><samp id="8g3ty"></samp></label>

    3. 電子開發網

      電子開發網電子設計 | 電子開發網Rss 2.0 會員中心 會員注冊
      搜索: 您現在的位置: 電子開發網 >> 基礎入門 >> Arduino開發學習 >> 正文

      Arduino入門2——常用函數及用法_arduino常用函數手冊

      作者:佚名    文章來源:網絡整理    點擊數:    更新時間:2025/3/14

      Arduino入門2——串口驅動函數及用法

      上期,我們簡單的認識了一下Arduino,淺淺的入了個門,這一期我們介紹以下Arduino串口常用的函數及用法

      1.IO

      常用串口庫函數如下:

      函數名 用法及解析
      pinMode() 用于IO口初始化
      digitalWrite() 對IO口寫入高低電平
      digitalRead() 讀取IO口的高低電平
      analogReference() 配置AD采集的參考電壓
      analogWrite() 對應IO口實現AD轉換,精度可達10位
      analogRead() 對應IO口實現DA轉換,精度為8位

      下面是對應的一些用法:

      1. pinMode()
         

      2. digitalWrite()
         

      3. digitalRead()
         

      4. analogReference()
         

      5. analogWrite()
         

      6. analogRead()

       

      2.串口

      常用串口庫函數如下:

      函數名 用法及解析
      Serial.begin(9600) 用于串口初始化,參數為波特率
      Serial.print(val,fromat) 串口輸出函數,val為輸出的內容,fromat為輸出格式,可以省略,省略時默認為十進制
      Serial.println(val,fromat) 串口輸出函數,相對于Serial.print的區別是自帶換行
      Serial.write(val) 串口輸出函數,發送單個字節或字符串
      Serial.available() 用于檢測串口是否接收到數據,返回值為讀取數據的長度
      Serial.read() 串口讀取函數,每次讀取一個字節并返回,當串口沒有接收到數據時,返回-1,讀取完數據后,下次調用會讀取下個字節
      Serial.parselnt() 串口解析函數,用來讀取接收到的數據中的整數,在讀取到整數后讀取到字符會停止,讀取后緩沖區內的內容會被清除。比如讀取的內容為123adc456,則需要讀取兩次,第一次讀取到123.第二次讀取adc456,但由于adc為字符,所有只讀取456。
      Serial.parseFloat() 也是串口解析函數,用來讀取小數

      下面是對應的一些用法:

      1. 示例1:
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);   //串口初始化
      }
      void loop() {
        // put your main code here, to run repeatedly:
        Serial.print("Hello");   //發送字符串
        Serial.print('A');       //發送字符
        Serial.println(25);        //默認以10進制發送數字25
        Serial.println(25,DEC);        //十進制發送數字25 -》 25
        Serial.println(25,BIN);        //二進制發送數字25 -》 11001
        Serial.println(25,OCT);        //八進制發送數字25 -》 31
        Serial.println(25,HEX);        //八進制發送數字25 -》 19
        Serial.print(1.2345);    //發送1.23,默認保留2位小數,四舍五入
        delay(50000);
      }
      
      1. 結果1:
         
      2. 示例2:
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);   //串口初始化
      }
      void loop() {
        // put your main code here, to run repeatedly:
        for(int i = 0 ;i<Serial.available();i++)  //判斷是否介紹到數據,
        {
          Serial.write(Serial.read()); //依次打印介紹的數據
          Serial.println();            //換行
          delay(500);                  // 延時,讓顯示更直觀
        }
      }
      
      1. 結果2:
         
      2. 示例3:
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);   //串口初始化
      }
      void loop() {
        // put your main code here, to run repeatedly:
        for(int i = 0 ;i<Serial.available();i++)  //判斷是否介紹到數據,
        {
          Serial.print(Serial.parseInt()); //依次打印接收的整數
          Serial.println();            //換行
          delay(500);                  // 延時,讓顯示更直觀
        }
      
      1. 結果3:
         
      2. 建立開發板與電腦串口連接,像電腦發送姓名、年齡、身高等信息
      //1. 建立開發板與電腦串口連接,像電腦發送姓名、年齡、身高等信息
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);
      }
      void loop() {
        // put your main code here, to run repeatedly:
        Serial.write("naem: 不想寫代碼的我");
        Serial.println();
        Serial.write("age: 18");
        Serial.println();
        Serial.write("height: 180");
        Serial.println();
        delay(1000);
      }
      
      1. 實現通過串口監視器給開發板發送信息,在把接收的信息發送回去
      //實現通過串口監視器給開發板發送信息,在把接收的信息發送回去
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);
      }
      void loop() {
        // put your main code here, to run repeatedly:
        for(int i =0; i<Serial.available();i++)
        {
          Serial.write(Serial.read());
          Serial.println();
          delay(500);
        }
        
      }
      
      1. 實現加法計算器
      //實現加法計算器,
      int Add(int a,int b)
      {
        Serial.print(a);
        Serial.print('+');
        Serial.print(b);
        Serial.print('=');
        Serial.println(a+b);
       return a+b;
      }
      void setup() {
        // put your setup code here, to run once:
        Serial.begin(9600);
      }
      void loop() {
        // put your main code here, to run repeatedly:
        if(Serial.available())
        {
          Add(Serial.parseInt(),Serial.parseInt());
        }
      

      3.中斷

      常中斷函數如下:

      函數名 用法及解析
      attachInterrupt() 中斷開啟函數
      digitalPinToInterrupt() 獲取對應引腳中斷號的函數,返回值為中斷編號
      detachInterrupt() 關閉引腳對應的中斷

      下面是其對應的用法:

      /*
      attachInterrupt()中斷開啟函數,
      第一個參數是開啟的中斷號,不同引腳、不同開發板對應的中斷號不同,這一點需要注意
      第二個參數是對應中斷的服務函數,需要自己定義,必須是無參數無返回值類型
      第三個是中斷的觸發方式,一個四種:LOW,低電平觸發;CHANGE,電平變化觸發;FALLING,下降沿觸發;RISING,上升沿觸發;
      此外,還有一種觸發方式,HIGH,只有 Due, Zero and MKR1000這些板子支持。
      注:不同開發板引腳與中斷號對應如下:
       中斷號:     0       1     2     3     4     5
        UNO       引腳2    3      
        Mega2560  引腳2    3     21    20    19     18
        Leonardo  引腳3    2     0      1
        Due       ------------所有IO口----------------
      digitalPinToInterrupt(pin):獲取對應引腳中斷號的函數,返回值為中斷編號
      detachInterrupt(pin);關閉引腳對應的中斷,一般不常用
      */
      int i = 0;
      void setup() {
        // put your setup code here, to run once:
        pinMode(LED_BUILTIN,OUTPUT);       //設置LED輸出模式
        pinMode(2,OUTPUT);                // 定義引腳2,作為輸入,將引腳2和引腳3通過杜邦線相連,通過對引腳2寫入數據觸發引腳3的中斷
        digitalWrite(2,LOW);              //設置引腳2初始電平為低電平
        digitalWrite(LED_BUILTIN,LOW);    //熄滅LED
        attachInterrupt(digitalPinToInterrupt(3),function,CHANGE);  //開啟引腳3對應的中斷
       
      }
      void loop() {
        // put your main code here, to run repeatedly:
        i++;  
        digitalWrite(2,i%2);  //通過i的值,改變引腳2,進而改變引腳3的電平,使其產生中斷
        delay(500);           //每500ms產生一次中斷
      }
      void function()
      {
        digitalWrite(LED_BUILTIN,i%2);    //閃爍LED。
      }
      
      Tags:Arduino入門,Arduino,常用函數  
      責任編輯:admin
    4. 上一個文章:
    5. 下一個文章: 沒有了
    6. 請文明參與討論,禁止漫罵攻擊,不要惡意評論、違禁詞語。 昵稱:
      1分 2分 3分 4分 5分

      還可以輸入 200 個字
      [ 查看全部 ] 網友評論
      關于我們 - 聯系我們 - 廣告服務 - 友情鏈接 - 網站地圖 - 版權聲明 - 在線幫助 - 文章列表
      返回頂部
      刷新頁面
      下到頁底
      晶體管查詢
      主站蜘蛛池模板: 亚洲Av综合色区无码专区桃色 | 亚洲人成综合在线播放| 国产成人亚洲综合在线| 日韩综合在线视频| 色噜噜狠狠狠狠色综合久一| 青青草原综合久久大伊人精品| 亚洲国产精品综合福利专区 | 色婷婷综合和线在线| 成人伊人青草久久综合网破解版| 一本综合久久国产二区| 亚洲综合一区二区精品导航| 久久天堂av综合色无码专区| 久久综合日韩亚洲精品色| 成人综合激情另类小说| 婷婷色香五月激情综合2020| 青青草原综合久久大伊人 | 狠狠综合亚洲综合亚洲色| 狠狠色丁香婷婷综合尤物| 中文字幕人成无码人妻综合社区| 久久综合亚洲色HEZYO国产| 激情综合婷婷色五月蜜桃| 亚洲国产国产综合一区首页| 亚洲综合伊人久久综合| 亚洲国产成人精品无码久久久久久综合| 亚洲五月丁香综合视频| 99久久综合久中文字幕| 色诱久久久久综合网ywww| 色欲色香天天天综合网站免费| 色综合天天娱乐综合网| 伊人久久亚洲综合影院首页| 国产成人亚洲综合无| 一本久到久久亚洲综合| 国产成人综合亚洲绿色| 六月丁香婷婷综合| 日韩欧国产精品一区综合无码| 狠狠综合久久综合88亚洲| 久久亚洲高清综合| 天天做天天爱天天综合网| 国产成人亚洲综合| 一本色道久久88加勒比—综合| 97色伦图片97综合影院久久|