[QEC教程]
完整教學請參見 範例:採用EtherCAT程式碼在非QEC上進行數位輸入/輸出.
Digital Input/Output
Let the external LEDs be blinked by Digital Output, and the status of the LEDs is received by Digital Input and printed out through Serial Port.
硬體說明
- QEC-M-01
- QEC-R11D0F: EtherCAT 16數位輸出通道從站模組
- 其他: 開關
電路
請參考 QEC Example – Digital Input/Output’s circuit part 用於電路架構。
代碼
請參考 QEC Example – Digital Input/Output’s code part 先。
We will set Digital Output as the first slave (slave0) and Digital Input as the second slave (slave1) through the EthercatDevice_Generic object.
EthercatMaster master; EthercatDevice_Generic slave0; EthercatDevice_Generic slave1;
In the main loop, we’ll first set pin0 in output to High and read pin8 in input, with a delay of 4000 ms, then print in Serial Port; after that, we set pin0 in output to Low and read pin8 in input, with a delay of 1000 ms, then print in Serial Port. The code is as follows.
slave0.pdoWrite8(0, 1); delay(10); Serial.println(slave1.pdoRead8(1), HEX); delay(4000); slave0.pdoWrite8(0, 0); delay(10); Serial.println(slave1.pdoRead8(1), HEX); delay(1000);
Among them, between pdoWrite8 and pdoRead8, we need to add a delay(10) to wait for the Input PDO to be updated.
範例
#include "Ethercat.h" EthercatMaster master; EthercatDevice_Generic slave0; EthercatDevice_Generic slave1; void setup(void) { Serial.begin(115200); /* EtherCAT */ Serial.print("Master begin:"); Serial.println(master.begin()); Serial.print("Slave0 attach:"); Serial.println(slave0.attach(0, master)); Serial.print("Slave1 attach:"); Serial.println(slave1.attach(1, master)); Serial.print("Master start:"); Serial.println(master.start()); } void loop() { slave0.pdoWrite8(0, 1); delay(10); Serial.println(slave1.pdoRead8(1), HEX); delay(4000); slave0.pdoWrite8(0, 0); delay(10); Serial.println(slave1.pdoRead8(1), HEX); delay(1000); }
學習更多
您可以在EtherCAT 應用找到更多QEC的基礎應用介紹。