سؤال

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

هل كانت مفيدة؟

المحلول

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 :)

نصائح أخرى

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.

I used the software canking to talk to a mcp25050 when I learned how to implement can protocol using an hcs12 dragonboard. It helped a lot because canking will initialize everything for you when u go on the bus and all you have to do is learn how to write and recieve. If you want to learn how to initialize the steps are:

  1. Enables can bus by setting bit on CAN Control Register 1

  2. Enable can initialization Control Register 0

  3. wait until can bus is in initialization mode by checking control register 1 bit

  4. Enables can bus by setting bit on CAN Control Register 1 again and set clock source - Ethier bus clock or eclock

  5. set prescaler baudrate and Tq with Bus timing register

  6. set sample time and prop_seg1, prop_seg2, and phase_seg

  7. set acceptance id on Identifier acceptance register 0-3 or 0-7 - to set your can to recieve everything set those to 00 because when doing a compare the can bus does a ones complement compare with the id coming in

  8. set Identifier mask register 0-3 or 0-7, if you want to not care about any of the bits set them all to FF

  9. set identifier acceptance control register to 32 bit extended or 11 bit - i use 32

  10. set Control Register 0 back to normal mode

  11. wait until bus is normal mode by checking Control Register 1

after this you can start changing registers or reading data to do this you must select the empty can buffer, write your id to write or request data, and then input the address, mask, and value in the 3 transmitter registers if writing and then specify the dlc (3 if writing and 8-1 if reading). to transmit the id and data you then have to set the can transmit flag to equal the can Transmit buffer selection. ** depending on what id you use bit shifting can be tedious so if you are having a problem I would suggest debugging and looking at what your Transmit buffer selection registers are holding. I had this error because i did not shift correctly when i was sending messages to the mcp25050

If your MCU supports CAN Bus, you should start from the related datasheet.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top