Best way to learn CAN Communication protocol

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
I want to learn about CAN communication protocol I found the main use of CAN is for automotive - within a car where multiple devices need to exchange information But neither i have a car nor any other device to do practical But still i want to learn about it.

I have learned about protocols like uart, i2c and spi by making a flow chart. Should i make a flow chart? I have another option that I should learn by looking at the sample code

It is better to walk on one way than walking on many paths to reach destination. Hopefully someone will help me to walk on the right track

Thank you very much for any advice. @JohnInTX
 

BobaMosfet

Joined Jul 1, 2009
2,110
Here's your best solution:

Understanding & Using the Controller Area Network Communication Protocol
Author(s): Marco Di Natale; Halbo Zeng; Paolo Giusto; Arkadeb Ghosal
ISBN-13: 978-1461403135
ISBN-10: 1461403138

Learn the way the rest of us did. Good luck.
 

JohnInTX

Joined Jun 26, 2012
4,787
Yeah.. I have to agree with @BobaMosfet on this one. CAN is a relatively complex subject that you can't learn from a forum. You need to read up on the whole thing before asking specific questions. A good way to start would be a book as noted and also just reading application notes and datasheets from vendors that make the stuff. Here are some resources from Microchip that I found helpful way back when. Your interaction with a CAN bus will not be centered on the low level bit transmission - that is handled by a CAN controller. Rather you need to understand the concepts of the CAN messaging, what a message is, how to configure a CAN controller (bit rates etc), how to set up message filters, how to send and receive messages with the CAN controller and how to manage errors.. You also have to understand the physical layer that is implemented with a separate driver IC.

You won't be bit-banging this one.

The PDFs with numbers refer to Microchip Application Nodes - all concise and well written. can2spec.pdf is the 2.0 Spec from Bosch. There are newer versions of most of this available around but it's a starting place. After all of that, read a datasheet for a uC with CAN built in like PIC18F4685 or a datasheet for a Stand Alone CAN controller like the MCP2515 and see how to interface your code to the CAN controller registers. You won't get this in a day or a week.

There also is some discussion of CAN right here on AAC:
https://forum.allaboutcircuits.com/threads/can-bus-specification.166232/


It's a big subject: start by reading, reading, reading. I learned it by reading these and similar stuff. Maybe you can too.
Good luck!
 

Attachments

Last edited:

John P

Joined Oct 14, 2008
2,025
CAN is great, and it appears reasonably simple at first. Data transmitted on two lines, a one is sent when the two lines are at the same voltage, a zero when one line is higher--not difficult to follow. But then you get into the protocol that's used, how data frames are constructed, and you start to be thankful that there are specialized chips out there that do all the interfacing for you. Using CAN is not the same as communicating via a UART! I looked at the data sheet for a PIC processor with a built-in CAN controller, and the number of registers involved with handling communication was really daunting. I wouldn't want to deal with it unless the software is a pre-written module, or the whole thing is dealt with by an external chip. I'd try to avoid any need to understand the entire operation.

And Gajyamadake, I don't want to sound negative here, but you're the guy who was having trouble understanding how to operate a 4x4 keypad, despite several people trying to explain it to you. You'll do best if you don't try to learn the most complicated things before you're ready for them.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
And Gajyamadake, I don't want to sound negative here, but you're the guy who was having trouble understanding how to operate a 4x4 keypad, despite several people trying to explain it to you. You'll do best if you don't try to learn the most complicated things before you're ready for them.
Thank you so much for your explanations.

The title of my thread shows that I just wanted to know how we can learn CAN in a easy and simpler way. I understand that it will take a long time to understand CAN. I just started to learn basic first.

I do not want to leave any of my threads incomplete I am a bit late but still i keep working. I am also working on the Matrix keypad but I did not get much time, so I did not give 100% on it. But i have learned how to interface LCD to 4 bit.

Right now I will only focus on only two threads Keypad and CAN until I finish them
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
It's a big subject: start by reading, reading, reading. I learned it by reading these and similar stuff. Maybe you can too.
Good luck!
I have a question, pic18f4685 has inbuilt CAN. If I have two pic18f4685 and if I want to send data from one pic18f4685 to the other pic18f4685 via CAN, can I do this directly or do I need transceiver IC?

This is just a question that came to my mind that if this can happen, if not why not
 

JohnInTX

Joined Jun 26, 2012
4,787
You still need the transceiver. The PIC outputs are single ended TX out and RX in. The transceiver combines these into the differential CANH and CANL signals for the CAN bus as well as providing necessary feedback of the bus state (on the RX line) when transmitting. Monitoring the bus state is required for message arbitration. The CAN controller won't work properly without it.
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Modern electronics system in the car is very difficult to understand. I'm trying to simplify it a little bit for myself. If I connect just a few devices to CAN bus I don't think anything will go wrong because my primary goal is to understand fundamental first

I assume I am designing automatic system for car that will control two headlights and doors. I have mention in block diagram

I also assume I have PIC18f4685 , How many trans-receiver IC need to control four device over CAN BUS?

1587741552876.png
 

JohnInTX

Joined Jun 26, 2012
4,787
I also assume I have PIC18f4685 , How many trans-receiver IC need to control four device over CAN BUS?
You need one transceiver for each node on the bus. In your drawing, you would need 4. Note however, that in many applications including automobiles like you have shown, one transceiver/node can service more than one function if they are close by. For example, there would be one node with transceiver for a headlight that controls two functions, the high beam and low beam. The connections to the node from these are short and can be done as normal IO functions. The node itself would respond to at least two messages (CAN-IDs) one to service the high beam and one for the low beam.

In more complex nodes, a short-haul multidrop network like LIN can be used to reduce the number of wires back to the node and reduce the number of costly CAN node/transceivers. LIN is a simple protocol that does not have the robust features of CAN but is sufficient for local control of devices near the CAN node. For example, a door might have an electric lock, power window, mirror controls and heat etc. One CAN node would service the door and the simple LIN network would connect those local devices to that CAN node. As before, the CAN node would have the intelligence to accept and route the various messages to those devices in its door.

https://en.wikipedia.org/wiki/Local_Interconnect_Network
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Thank you @JohnInTX

  • MCP2551 CAN Transceiver Chip
  • Microcontroller PIC18f4685 ( CAN Controller)

System has 5 nodes, "controller" + 4 devices connected to same bus.

1587814616546.png

As given in the CAN specification, Microcontroller send arbitration filed over to CAN BUS to every nodes

Question : I can open or close any door same I can switch ON/OFF any light because I know name like left Door, Right Door, Left light and Right light. How does system know it has to open particular DOOR or It has to turn ON/OFF any Light ?
 

JohnInTX

Joined Jun 26, 2012
4,787
You have not drawn a CAN bus. You show one microcontroller with 4 CAN busses. It should look like this drawing:
CAN-LIN Bus.PNG
Note ONE CAN bus that connects remote devices to a MAIN CONTROLLER. I've shown two headlights and two doors with some details.
  • Each device e.g. headlight, is controlled by a CAN node connected to the CAN bus using a transceiver.
  • Each node has whatever intelligence is necessary to control its devices. Simple nodes like the headlights can use a dedicated chip with a little IO. More complex nodes like the doors are usually a microcontroller with CAN peripheral.
  • I've also shown a LIN bus in each door to control the mirror. LIN is a handy way to expand the CAN node, reduce wire count etc. when functions are close together.
  • Note how each device has its own CAN ID. Each node/microcontroller has to be configured to respond to its set of IDs (and only its set of IDs). The result is that each device in the automobile can be controlled by sending a CAN message with its ID and control data.
    • For example, to turn on the heater in the left mirror you would send a message with CAN ID = 0x34 and a '1' in the data field to turn on the heater. Send ID 0x34 and a '0' to turn the heater off.
    • To turn the headlight low beams ON, you send two messages, ID 0x11 and ID 0x21 with the data '1'.
  • Each node knows how to interpret its data to make the function happen. The door locks and door lights are directly connected to the LEFT DOOR microcontroller but the mirror functions are on the LIN bus. The microcontroller responds to mirror messages by routing to the LIN bus.

Question : I can open or close any door same I can switch ON/OFF any light because I know name like left Door, Right Door, Left light and Right light. How does system know it has to open particular DOOR or It has to turn ON/OFF any Light ?
First, you don't know the names, you know the CAN IDs. Second, it is up to the program in the main controller to know when it has to do something. I didn't show it but if you connected a switch to the door microcontroller, it would send its CAN message to the main controller when the door was opened. The main controller would process the message by sending a CAN message to the door light(s) to turn on. It might also send messages to the other lights in the cabin as a result of the door being opened.

That's pretty much how it works.
Good luck!
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
You have not drawn a CAN bus.
CAN Bus specification

1587913330765.png


In CAN protocol, when a MAIN CONTROLLER sends DATA Frame, Each node received DATA Frame . These nodes then decide whether to ignore the DATA Frame, or act upon it, depending on how we've been programmed

e.g. if MAIN CONTROLLER sends Right headlight ON DATA Frame, every node will see that DATA Frame. The Right headlight will act upon the DATA Frame and Right headlight will ON. Left headlight, Left DOOR and Right DOOR will ignore DATA Frame.

The frame format is as follows:

1587914642010.png

Question : MAIN CONTROLLER sends Right headlight ON DATA Frame, What does control field do in this DATA frame ?
 

Papabravo

Joined Feb 24, 2006
21,159
The control frame tells you this is a frame with an 11-bit identifier field (as opposed to a 29-bit identifier field) and the data field is 1 byte long. What did you think it was for?
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
The control frame tells you this is a frame with an 11-bit identifier field (as opposed to a 29-bit identifier field) and the data field is 1 byte long. What did you think it was for?
I found from documents
1587924495515.png

Total are six bits in control filed Which part show DATA LENGTH CODE and What's use of two reserve bits
 

Papabravo

Joined Feb 24, 2006
21,159
Btw Mr. TS, I know what is in a CAN frame and there is no need to shout by using all capital letters. It makes you seem rude.

The Data Length Code is in bits <DL3, DL2, DL1, DL0>. In your picture they are <0001> which means the data field of the CAN frame is....one byte long!
The other two bits are the ID(entifier) Ext(ension) bit, and a reserved bit. If ID Ext is a 1, then the identifier field is extendeded to 29 bits.
I've never seen a use for the reserved bit.
You might check the SAE J-1939 docs for information on the 29 bit identifier field
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Btw Mr. TS, I know what is in a CAN frame and there is no need to shout by using all capital letters. It makes you seem rude.
I am sorry I did not know that this could happen. I had nothing in my mind like this. It had capital latter in paragraph so I wrote in capital latter My signature shows my nature correctly But if you still feel bad then i want to apologize for it

The Data Length Code is in bits <DL3, DL2, DL1, DL0>. In your picture they are <0001> which means the data field of the CAN frame is....one byte long!
How do you know weather data filed is one byte long or two byte long or n byte
What happen if the data length code is in bits <DL3, DL2, DL1, DL0> is <0010> ? What the length of data field ?
 

Papabravo

Joined Feb 24, 2006
21,159
I am sorry I did not know that this could happen. I had nothing in my mind like this. It had capital latter in paragraph so I wrote in capital latter My signature shows my nature correctly But if you still feel bad then i want to apologize for it


How do you know weather data filed is one byte long or two byte long or n byte
What happen if the data length code is in bits <DL3, DL2, DL1, DL0> is <0010> ? What the length of data field ?
Why is this not obvious to you? Because the pattern 0001 in binary is a representation for the number 1, period full stop.

If the 4-bit Data Length field is:
0000 the data field is 0 bytes long, it is empty or null
0001 the data field is 1 byte long
0010 the data field is 2 bytes long
0011 the data field is 3 bytes long
0100 the data field is 4 bytes long
0101 the data field is 5 bytes long
0110 the data field is 6 bytes long
0111 the data field is 7 bytes long
1000 the data field is 8 bytes long

I don't think it is possible, with an off the shelf CAN controller to create a Data Length Field that contains any of the patterns in the set:
[1001, 1010, 1011, 1100, 1101, 1110, 1111]
I don't know what a receiver would do if it saw one. It would probably throw an ERROR Frame (7 consecutive Dominant Bits).
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
I have been reading CRC field in specification but Its hard to understand I don't understand what it does in CAN BUS

CRC filed in bit 010000110000000. What does this sequence represent ?
 

Papabravo

Joined Feb 24, 2006
21,159
I have been reading CRC field in specification but Its hard to understand I don't understand what it does in CAN BUS

CRC filed in bit 010000110000000. What does this sequence represent ?
CRC stands for Cyclic Redundancy Check. These 15 bits are computed by the CAN controller in hardware and appended to the message automatically. The receiver will check these bits to determine if a valid message has been received. You cannot read or write or do anything with these bits. In almost 25 years I have never seen a CRC error. You can't fiddle with the bits on the wire to create one because as soon as you do, the transmitter will throw an active ERROR Frame, because he sent a recessive bit and heard a dominant bit, One more thing you cna; override the transmission of a dominant bit with a recessive bit -- CAN bus doesn't work that way.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
As I previously assumed that I am controlling only two headlights and two doors through can bus. Each data frame is 55 bit's long if I am controlling four devices it means main controller send total 220 bits (55*4 ) to each device

I think only bits for arbitration field will change in each data frame because it node has different identity

1588012484556.png
 
Top