Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAN Bus Protocol Implementation

I want to learn and implement CAN BUS protocol. I have implemented UART,SPI,I2C and One Wire Bus protocol using MSP430 Launchpad in software. Now I want to learn about CAN Bus protocol. I have mBed LPC 1768 Cortex M3 Development board. mBed has Can Bus Library but I want to write my own library so that I can learn it in detail, i.e. the way I did for other communication protocols.

I am not able to find suitable resources to start with and the material appears to be scattered on net. Can any one guide how do i write and implement CAN Bus protocol with the development boards available with me.

Thanks

like image 548
Gaurav K Avatar asked Mar 03 '13 18:03

Gaurav K


2 Answers

Developing CAN library is relatively easy as compared to I2C or SPI. This is because CAN Controller of your Cortex will take care of most of complex things.

To transmit the data, You have to write ID and Data in designated registers and set bit to transmit data.

This Application note from NXP can be very useful for you.

I would recommend you to implement following functions:

  1. InitCAN - This should set specified Baud Rate of CAN.
  2. SetFilters - Most CAN Controllers come with Acceptance Filters, So it's good to have that
  3. SendData - Make sure you accept Parameters like ID_Type and RTRs etc.
  4. RecieveData - This can be blocking or Interrupt based.

Before beginning, do read CAN Basics to understand. Application notes AN713 and AN754 from Microchip is a good source. Also Vector's site and Wikipedia Article.

Plus, You can always post your doubts here or on Electronics.StackExchange.com :)

like image 180
Swanand Avatar answered Oct 16 '22 12:10

Swanand


Okay so this post is quite old but people may look at it again so: First of all Can bus is not user friendly protocol like USART or IC2 at all so you have to be very precise about your can bit timing there are tools for that but I suggest you to calculate them by hand. For a microcontroller I would suggest STM32 and be away from PIC series in my opinion. If it's only CAN-BUS without higher level protocols such as SAE J1939, steps are pretty simple and straight forward:

1)Initialize Can

2)Put CAN to configuration mode and remember that you can set baudrate, mask and filters only in configuration mode!

3) Set the baud rate registers.

4) Set the mask and filters. If you need to receive all messages just simply set mask to 0x00. Then filter will be do not care.

5) Set the CAN to the normal or loopback mode. (loopback mode is used for debugging purposes mostly.)

Some remarkable points people try to implement can at the beginning may miss: *** You need at least 2 working CAN nodes for successfull transmission. (of course with matching baud rate). So if you want to send some data via CAN with 1 node it will not be succesfull. Because your transmitter node will not receive ACK.

*** Most likely you will need a CAN tranciever. Do not forget to put a 100 ohm or similar value resistor between Tx and Rx pins of your tranciever.

like image 42
Günkut Ağabeyoğlu Avatar answered Oct 16 '22 13:10

Günkut Ağabeyoğlu