ESP32_Arduino_CAN/examples/esp32can_send/esp32can_send.ino

45 lines
1.3 KiB
Arduino
Raw Permalink Normal View History

2022-12-23 23:43:53 +01:00
/* ESP32 Arduino CAN Send Basic Demo
* This example will send 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
*
*/
2022-12-14 02:02:31 +01:00
#include <Arduino.h>
#include <ESP32CAN.h>
void setup() {
Serial.begin(115200);
Serial.println("ESP32-Arduino-CAN Send Basic Demo");
2022-12-23 23:43:53 +01:00
/* initialize and start, use pin 5 as CAN_tx and pin 4 as CAN_rx, CAN bus is set to 500kbps */
2022-12-14 02:02:31 +01:00
ESP32Can.CANInit(GPIO_NUM_5, GPIO_NUM_4, ESP32_SPEED_500KBPS);
}
void loop() {
twai_message_t tx_frame;
2022-12-23 23:43:53 +01:00
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 */
2022-12-14 02:02:31 +01:00
2022-12-23 23:43:53 +01:00
/* assemble the 8 bytes of data */
2022-12-14 02:02:31 +01:00
tx_frame.data[0] = 0xDE;
tx_frame.data[1] = 0xAD;
tx_frame.data[2] = 0xBE;
tx_frame.data[3] = 0xEF;
tx_frame.data[4] = 0xBA;
tx_frame.data[5] = 0x5E;
tx_frame.data[6] = 0xBA;
tx_frame.data[7] = 0x11;
2022-12-23 23:43:53 +01:00
ESP32Can.CANWriteFrame(&tx_frame); /* send the CAN message */
2022-12-14 02:02:31 +01:00
Serial.println("CAN Frame Sent");
2022-12-23 23:43:53 +01:00
/* delay before sending another CAN message*/
2022-12-14 02:02:31 +01:00
delay(1000);
}