Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago.
Using CAN on a nucleo f303k8
Hello,
Does anyone know how to use CAN communication on NUCLEO-F303K8 with a MCP2551? With this example : https://developer.mbed.org/users/hudakz/code/CAN_Hello/ only the initialization works with pins PA_11 and PA_12, for the other pins it crashes at the initialization of the CAN class, I do not detect any signal to a oscilloscope.
Rémi
1 Answer
7 years, 8 months ago.
Hello Rémi,
- According to the board pinout as well as peripheral pin definitions only one CAN channel is available on a NUCLEO-F303K8 board. So it's OK that other pins do not work as CAN interface.
- Could you please explain in more details your configuration:
- Do you connect at least two nodes to the CAN bus?
- What messages are printed to the serial terminals running on the PCs connected over serial line to the mbed boards?
- What do you mean that only initialization works?
Hello Zoltan,
Thanks for your answer. Finally I got it working by changing the wiring and using this code.
CAN on F303K8
#include "mbed.h"
#include "CAN.h"
Ticker ticker;
DigitalOut led(LED1);
char counter = 0;
Serial pc(USBTX, USBRX);
class Sender{
private:
CAN & can;
public:
Sender(CAN & c) : can(c) {
}
void send() {
pc.printf("send()\n");
if(can.write(CANMessage(1337, &counter, 1))) {
counter++;
pc.printf("Message sent: %d\n", counter);
led=!led;
}
}
};
int main() {
pc.baud(9600); // set Serial speed
pc.printf("Hello World!\n");
CAN can(PA_11, PA_12);
can.frequency(500000);
Sender sender(can);
pc.printf("main()\n");
ticker.attach(callback(&sender,&Sender::send), 1);
CANMessage msg;
while(1) {
if(can.read(msg)) {
pc.printf("Message received: %d\n", msg.data[0]);
}
if(can.rderror() || can.tderror()){
pc.printf("rderror:%d\n",can.rderror());
pc.printf("tderror:%d\n",can.tderror());
can.reset();
}
wait(0.2);
}
}
I had a problem with the CAN constructor when it was outside the main function.
posted by 09 Apr 2018