1 / 13

Arduino week4

Arduino week4. 內容:串列傳輸 實作: Arduino to PC 、 Arduino to Arduino 簡報:廖崇義. 序列通訊. Asynchronous Communication ( 非同步傳輸 ) 在傳送的資料內 加上同步信號 , 當接收端收到同步信號 , 便知道接下來的信號是資料 Synchronous Communication ( 同步傳輸 ) 傳送與接收雙方 共用一個時鐘脈衝 (Clock). 序列通訊 RS-232 簡介. 在 TxD 和 RxD 上: 邏輯 1(MARK)=-3V ~ -15V      

erling
Télécharger la présentation

Arduino week4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arduino week4 內容:串列傳輸 實作:Arduino to PC 、 Arduino to Arduino 簡報:廖崇義

  2. 序列通訊 • Asynchronous Communication (非同步傳輸)在傳送的資料內加上同步信號, 當接收端收到同步信號 , 便知道接下來的信號是資料 • Synchronous Communication (同步傳輸)傳送與接收雙方共用一個時鐘脈衝(Clock)

  3. 序列通訊RS-232簡介 • 在TxD和RxD上: 邏輯1(MARK)=-3V~-15V       邏輯0(SPACE)=+3~+15V 訊號分析

  4. 序列通訊RS-232簡介 • 設定項目鮑率(Baud Rate)、同位檢查(Parity Check)和停止位(Stop Bit)

  5. DB9硬體簡介 • D-sub9pin(PC COM port)

  6. Arduino Serial Port • The ATmega32U4 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). • 在TxD和RxD上: • 邏輯1(MARK)= 5V       • 邏輯0(SPACE)= 0V

  7. HIN232線路圖

  8. ArduinoSerial語法 • void setup(){  Serial.begin(9600);// opens serial port0, sets data rate to 9600 bps  Serial1.begin(38400);// opens serial port1,sets data rate to 38400 bps  Serial2.begin(19200);// opens serial port2,sets data rate to 19200 bps  Serial.println(“Hello Computer”);//serial port0output  Serial1.println("Hello Serial 1"); //serial port1output  Serial2.println("Hello Serial 2"); //serial port2output}void loop() {} • Leonardo Serial0: 透過USB的虛擬COMPort上傳到電腦 Serial1: 由Arduino的D1、D2輸出

  9. Ex1. • int analogValue = 0; // variable to hold the analog value • void setup() { • Serial.begin(9600);// open the serial port at 9600 bps: • } • void loop() { • analogValue = analogRead(0); // read the analog input on pin 0: • // 以各種格式輸出 • Serial.print(analogValue); //位註明格式將以十進位輸出 • Serial.print(“\t”); //輸出Tab • Serial.print(analogValue, DEC); //以十進位輸出 • Serial.print(“\t”); //輸出Tab • Serial.print(analogValue, HEX); //以十六進位輸出 • Serial.print(“\t”); //輸出Tab • Serial.print(analogValue, OCT); //以八進位輸出 • Serial.print(“\t”); //輸出Tab • Serial.println(analogValue, BIN); // 以二進位輸出並換行 • delay(10); • }

  10. Ex2. • int incomingByte = 0; // 存取電腦鍵入的數值 • void setup() • { • Serial.begin(9600); // opens serial port, sets data rate to 9600 bps • } • void loop() • { • if (Serial.available() > 0)// 判斷緩衝區是否有資料傳入 • { • incomingByte = Serial.read(); // 讀取鍵入資料 • Serial.print(“I received: ”); //輸出字串 • Serial.println(incomingByte, DEC);//輸出鍵入資料 • } • }

  11. Ex3. • 電腦輸入”s ”至Arduino1 • Arduino1通知Arduino2開始做類比取樣 • Arduino2讀取VR值 • Arduino2將值傳給Arduino1 • Arduino1將值由Serial0輸出到電腦 Arduino1 void setup() { Serial.begin(9600); Serial1.begin(9600); } void loop() { if (Serial.available() > 0)//由電腦端讀入 { int incomingByte = Serial.read(); if(incomingByte==115) //判斷是否讀到 ”s” Serial1.println(“s”);//輸出至Arduino2 } if (Serial1.available() > 0) //由Arduino2輸入 { int val = Serial1.read(); Serial.println(val); } Arduino2 void setup() { Serial1.begin(9600); } void loop() { if (Serial1.available() > 0) { int incomingByte = Serial1.read(); if(incomingByte==115) //判斷是否讀到 ”s” { int val = analogRead(3); val=map(val,, 0, 1023, 0,9); Serial1.println(val); } } }

  12. Ex4. • 電腦輸入”s ”至Arduino1 • Arduino1通知Arduino2開始做類比取樣 • Arduino2讀取VR值 • Arduino2將值傳給Arduino1 • Arduino1將值由Serial0輸出到電腦 Arduino1 void setup() { Serial.begin(9600); Serial1.begin(9600); } void loop() { if (Serial.available() > 0)//由電腦端讀入 { char incomingChar = Serial.read(); if(incomingChar==‘s’) //判斷是否讀到 ‘s’ Serial1.println(“s”);//輸出至Arduino2 } if (Serial1.available() > 0) //由Arduino2輸入 { char val = Serial1.read(); Serial.println(val); } Arduino2 void setup() { Serial1.begin(9600); } void loop() { if (Serial1.available() > 0) { char incomingChar = Serial1.read(); if(incomingChar==‘s’) { int val = analogRead(3); Serial1.println(val); } } }

  13. 練習題 • 由電腦鍵入一四位數 • Arduino由SerialPort讀入四個ASCII碼 • 並將之轉換成十進位數輸出到電腦端 • ASCII碼字元0~9為DEC48~57 • 設四位數為”A”,”B”,”C”,”D” • 將ABCD字元轉換成十進位: (A-48)*1000+(B-48)*100+(C-48)*10+(D-48)

More Related