I have never used CAN BUS before so I need some help. Im trying to communicate 2 boards like STM32F1 Blue Pill with NUCLEO STM32F4. I know I need a transceivers for this to meet can bus standard but while I'm waiting for delivery of those transceivers I started to run some tests with code etc. At this point I just want to check on the oscilloscope if the code is working and maybe I will try to communicate both boards in this way : I already searched the internet for the examples but none of them wanted to work as it should. One of them is just placing CAN pins in the right state but it is not transmitting anything. Another one after upload made my board (Blue Pill) unreachable (USB port enumeration problems). I've tried this one for example: https://raw.githubusercontent.com/nopno ... 32f103.ino.
I was hoping someone here was able to communicate via CAN BUS and is able to share some example. As I mentioned at the beginning I'm starting my journey with CAN-BUS so I need very simple example.
EDIT:
I also tried the meCAN from here: https://github.com/mackelec/meCAN but nucleo freezes on can1.begin. COde like this:
Code: Select all
#include <PString.h>
#include <Streaming.h>
#include <meCAN.h>
meCAN1 can1;
meCAN1 canData;
// PB0 - LED1
// PB7 - LED2
// PB14 - LED3
#define LED_PIN PB14
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.setRx(PD9); // for STM32F413 STLINK
Serial.setTx(PD8); // for STM32F413 STLINK
Serial.begin(115200);
can1.begin(125,PB8,PB9,STD_ID_LEN); //for STM32F413 (Speed, CAN1_Rx, CAN1_Tx, ID_LEN)
can1.attachInterrupt(can1ISR);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
void can1ISR()
{
readCAN1();
}
uint8_t theNumber=33;
void readCAN1()
{
can1.rxMsgLen = can1.receive(can1.id, can1.fltIdx, can1.rxData.bytes);
Serial << "Can len = " << can1.rxMsgLen << endl;
if (can1.rxMsgLen<1) return;
if (can1.id != 100) reportCAN(&can1);
sendCAN1();
}
void sendCAN1()
{
unsigned char out[8] = {0, 0, 0, 0, 0, 0, 0, 0};
out[0] = theNumber;
bool result = can1.transmit(102,out,8);
}
void reportCAN(meCAN1 *can)
{
Serial << "CAN1 Rx <id> " << can->id << " [" << _HEX(can->id) << "] " << endl ;
for (int i=0;i<can->rxMsgLen;i++)
{
Serial << _HEX(can->rxData.bytes[i]) << " " ;
}
Serial << endl;
}