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.
10 years, 10 months ago.
Can Extended
I am a NOOB to CAN and would like to read j1939 Can bus data. This I think is 29bit format at 250kbs. How do you set the Can API up to read CanExtended?
Thanks.
1 Answer
10 years, 10 months ago.
Hi Narrab ,
The first place to start, if you haven't already read it in detail, is Handbook - CAN.
The read() method can fill in a CANMessage, The CANMessage has a CANFormat element, which can be "CANStandard" (11-bit) or "CANExtended" (29-bit).
So, you can filter by the message format when you extract it. If you want the CAN controller to only pass 29-bit messages to your application, you may be able to configure that at the register level - but there is not a ready-made high level API for that.
Here how you can read and write: .
- include "mbed.h"
- include "C12832.h"
C12832 lcd(p5, p7, p6, p8, p11);
Ticker ticker; DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); AnalogIn Pot1(p19); AnalogIn Pot2(p20); DigitalIn fire(p14);
DigitalIn up(p15); DigitalIn rigth(p16); DigitalIn left(p13); DigitalIn down(p12);
CAN can1(p9, p10); CAN can2(p30, p29);
Serial pc(USBTX, USBRX); tx, rx
Timer t; Timeout flipper;
char mensaje[8]; char counter = 0;
void send() {
uint32_t id=0x18FEF320; CANMessage messageOut;
led1 = 1; for (int i=0; i<8; i++) messageOut.data[i]=rand()%255;
create the message
messageOut.format = CANExtended; standard or extended ID (can be skipped for standard) messageOut.id = id; messageOut.len = 8;
if(can2.write(CANMessage(messageOut))) { printf("Id: %x",id); printf(" Sent: "); for(int i = 0; i<8; i++) { if(mensaje[i]<0x10) printf("0"); printf("%x",mensaje[i]); printf(" "); } } else{ printf("Failed to send the message\n\r"); } printf(": >%f\n\r", t.read()); led1 = 0; }
void flip() { led4 = 1; }
int main() { can2.reset(); reset the bus controller pc.baud(115200); t.start(); flipper.attach(&flip, 5.0); setup flipper to call flip after 5 seconds lcd.cls(); lcd.invert(0); 1 lo invierte can1.frequency(250000); can2.frequency(250000); ticker.attach(&send, 1); Calls the function send() every 1 second CANMessage msg;
while(1) {
lcd.set_contrast((Pot2.read_u16()>>10)); set contrast
if(can2.read(msg)) { led4=0; led2 = 1; flipper.detach(); setup flipper to call flip after 2 seconds printf("ID: %x LEN: %x ", msg.id, msg.len); lcd.printf("%0.4x ", msg.id);
for (int i = 0; i < (int)msg.len; i++) { printf("%0.2x ", msg.data[i]); lcd.printf("%0.2x.", msg.data[i]); } lcd.printf("\n\r"); printf("\n\r");
flipper.attach(&flip, 5.0); setup flipper to call flip after 2 seconds } wait(0.2); led2 = 0; indicador de recepción
} }
posted by 30 Mar 2016