Arduino week4
Download
Report
Transcript Arduino week4
內容:串列傳輸
實作:Arduino to PC 、 Arduino to Arduino
簡報:廖崇義
Asynchronous Communication (非同步傳輸)在傳
送的資料內加上同步信號 , 當接收端收到同步信
號 , 便知道接下來的信號是資料
Synchronous Communication (同步傳輸)傳送與
接收雙方共用一個時鐘脈衝 (Clock)
• 在TxD和RxD上:
邏輯1(MARK)=-3V~-15V
邏輯0(SPACE)=+3~+15V
訊號分析
設定項目鮑率(Baud Rate)、同位檢查(Parity
Check)和停止位(Stop Bit)
D-sub9pin(PC COM 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
5V
5V
1u
1u
+
+
1u
+
1u
+
1
16
2
15
3
14
4
HIN232
13
5
12
6
11
7
10
8
9
Arduino TX
Arduino RX
1
2
3
4
5
6
7
8
9
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 port0 output
Serial1.println("Hello Serial 1"); //serial port1 output
Serial2.println("Hello Serial 2"); //serial port2 output
}
void loop() {}
Leonardo Serial0: 透過USB的虛擬COM Port上傳到電腦
Serial1: 由Arduino的D1、D2輸出
Ex1.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
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);
}
Ex2.
1.int incomingByte = 0; // 存取電腦鍵入的數值
2.void
setup()
3.{
Serial.begin(9600);
4.
// opens serial port, sets data rate to 9600 bps
5.}
6.void
7.
8.
9.
10.
11.
12.
13.
14.}
loop()
{
if (Serial.available() > 0) // 判斷緩衝區是否有資料傳入
{
incomingByte = Serial.read();
// 讀取鍵入資料
Serial.print(“I received: ”);
//輸出字串
Serial.println(incomingByte, DEC); //輸出鍵入資料
}
Ex3.
1.電腦輸入”s ”至Arduino1
2. Arduino1通知Arduino2開始做類比取樣
3.Arduino2讀取VR值
4.Arduino2將值傳給Arduino1
5.Arduino1將值由Serial0輸出到電腦
Arduino1
1. void setup() {
2.
Serial.begin(9600);
3.
Serial1.begin(9600);
4. }
5. void loop()
6. {
7.
if (Serial.available() > 0) //由電腦端讀入
8.
{
9.
int incomingByte = Serial.read();
10.
if(incomingByte==115) //判斷是否讀到 ”s”
11.
Serial1.println(“s”); //輸出至Arduino2
12.
}
13.
if (Serial1.available() > 0) //由Arduino2輸入
14.
{
15.
int val = Serial1.read();
16.
Serial.println(val);
17. }
Arduino2
1. void setup() {
2. Serial1.begin(9600);
3. }
4. void loop()
5. {
6.
if (Serial1.available() > 0)
7.
{
8.
int incomingByte = Serial1.read();
9.
if(incomingByte==115) //判斷是否讀到 ”s”
10.
{
11.
int val = analogRead(3);
12.
val=map(val, , 0, 1023, 0,9);
13.
Serial1.println(val);
14.
}
15.
}
16. }
Ex4.
1.電腦輸入”s ”至Arduino1
2. Arduino1通知Arduino2開始做類比取樣
3.Arduino2讀取VR值
4.Arduino2將值傳給Arduino1
5.Arduino1將值由Serial0輸出到電腦
Arduino1
1. void setup() {
2.
Serial.begin(9600);
3.
Serial1.begin(9600);
4. }
5. void loop()
6. {
7.
if (Serial.available() > 0) //由電腦端讀入
8.
{
9.
char incomingChar = Serial.read();
10.
if(incomingChar==‘s’) //判斷是否讀到 ‘s’
11.
Serial1.println(“s”); //輸出至Arduino2
12.
}
13.
if (Serial1.available() > 0) //由Arduino2輸入
14.
{
15.
char val = Serial1.read();
16.
Serial.println(val);
17. }
Arduino2
1. void setup() {
2. Serial1.begin(9600);
3. }
4. void loop()
5. {
6.
if (Serial1.available() > 0)
7.
{
8.
char incomingChar = Serial1.read();
9.
if(incomingChar==‘s’)
10.
{
11.
int val = analogRead(3);
12.
Serial1.println(val);
13.
}
14.
}
15. }
由電腦鍵入一四位數
Arduino由Serial Port讀入四個ASCII碼
並將之轉換成十進位數輸出到電腦端
ASCII碼字元0~9為DEC48~57
設四位數為”A”,”B”,”C”,”D”
將ABCD字元轉換成十進位:
(A-48)*1000+(B-48)*100+(C-48)*10+(D-48)