From b643a09188172cd62980eccafa37cbeb65736202 Mon Sep 17 00:00:00 2001 From: Sam Perry <17242713+sdp8483@users.noreply.github.com> Date: Fri, 23 Dec 2022 17:43:53 -0500 Subject: [PATCH] add comments --- .../esp32can_receive/esp32can_receive.ino | 18 +++++++++++++---- examples/esp32can_send/esp32can_send.ino | 20 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/examples/esp32can_receive/esp32can_receive.ino b/examples/esp32can_receive/esp32can_receive.ino index d533ee8..d829f33 100644 --- a/examples/esp32can_receive/esp32can_receive.ino +++ b/examples/esp32can_receive/esp32can_receive.ino @@ -1,3 +1,12 @@ +/* ESP32 Arduino CAN Receive Basic Demo + * This example will receive messages on the CAN bus + * + * An external transceiver is required and should be connected + * to the CAN_tx and CAN_rx gpio pins specified by CANInit. Be sure + * to use a 3.3V compatable transceiver such as the SN65HVD23x + * + */ + #include #include @@ -5,18 +14,19 @@ void setup() { Serial.begin(115200); Serial.println("ESP32-Arduino-CAN Receive Basic Demo"); + /* initialize and start, use pin 5 as CAN_tx and pin 4 as CAN_rx, CAN bus is set to 500kbps */ ESP32Can.CANInit(GPIO_NUM_5, GPIO_NUM_4, ESP32CAN_SPEED_500KBPS); } void loop() { twai_message_t rx_frame; - if (ESP32CAN_OK == ESP32Can.CANReadFrame(&rx_frame)) { /* CAN message received*/ - Serial.print(rx_frame.identifier, HEX); + if (ESP32CAN_OK == ESP32Can.CANReadFrame(&rx_frame)) { /* only print when CAN message is received*/ + Serial.print(rx_frame.identifier, HEX); /* print the CAN ID*/ Serial.print(" "); - Serial.print(rx_frame.data_length_code); + Serial.print(rx_frame.data_length_code); /* print number of bytes in data frame*/ - for (int i=0; i #include @@ -5,16 +14,18 @@ void setup() { Serial.begin(115200); Serial.println("ESP32-Arduino-CAN Send Basic Demo"); + /* initialize and start, use pin 5 as CAN_tx and pin 4 as CAN_rx, CAN bus is set to 500kbps */ ESP32Can.CANInit(GPIO_NUM_5, GPIO_NUM_4, ESP32_SPEED_500KBPS); } void loop() { twai_message_t tx_frame; - tx_frame.extd = 0; - tx_frame.data_length_code = 8; - tx_frame.identifier = 0x123; + tx_frame.extd = 0; /* CAN ID is standard 11bit, for 29bit set to 1*/ + tx_frame.data_length_code = 8; /* send 8 bytes of data */ + tx_frame.identifier = 0x123; /* CAN id is 0x123 */ + /* assemble the 8 bytes of data */ tx_frame.data[0] = 0xDE; tx_frame.data[1] = 0xAD; tx_frame.data[2] = 0xBE; @@ -24,9 +35,10 @@ void loop() { tx_frame.data[6] = 0xBA; tx_frame.data[7] = 0x11; - ESP32Can.CANWriteFrame(&tx_frame); + ESP32Can.CANWriteFrame(&tx_frame); /* send the CAN message */ Serial.println("CAN Frame Sent"); + /* delay before sending another CAN message*/ delay(1000); }