[QEC教程]
完整教學請參見 範例:採用EtherCAT程式碼在非QEC上進行數位輸入/輸出.
數位輸出
使用 EtherCAT 數位輸出模組讓外部 LED 閃爍。
硬體說明
- QEC-M-01
- QEC-R11D0F: EtherCAT 16數位輸出通道從站模組
- 其他: 24V LED
電路
請參考 QEC 範例 – 數位輸出的電路部分 用於電路架構。
代碼
請參考 QEC 範例 – 數位輸出的程式碼部分 先。
請注意,EtherCAT 從站設備是非 QEC 設備,因此在設定從站物件名稱時應將 EthercatDevice_QECR11D0FS 變更為 EthercatDevice_Generic。
* EthercatDevice_Generic 類別是所有品牌 EtherCAT 裝置的通用物件。
EthercatMaster master; EthercatDevice_Generic slave0;
接下來,初始化 EtherCAT 主站和從站,並將從站連接到 EtherCAT 網路。
在主循環中,將使用 pdoWrite8 將 1 位元組資料寫入 PDO 記憶體位址。
slave0.pdoWrite8(0, 1);
其中, pdoWrite8(offset, value) 有兩個參數描述如下。
- offset:ProcessData 記憶體位址。
- value:1 位元組資料。
然後使用以下命令再次關閉它。
slave0.pdoWrite8(0, 0);
注意:開啟和關閉之間需要有足夠的閃爍時間,因此需要使用 delay() 指令在 4000 ms 內亮起,在 1000 ms 內熄滅,並且不執行任何其他操作。
範例
#include "Ethercat.h" EthercatMaster master; EthercatDevice_Generic slave0; void setup(void) { Serial.begin(115200); master.begin(); slave0.attach(0, master); master.start(); } void loop() { slave0.pdoWrite8(0, 1); delay(4000); slave0.pdoWrite8(0, 0); delay(1000); }
學習更多
您可以在EtherCAT 應用找到更多QEC的基礎應用介紹。