Thursday, December 18, 2014

Implementation of CAN(Controller Area Network) bus using PIC microcontroller

Introduction:

The CAN (Controller Area Network) is an asynchronous serial communication bus developed in the late 80s by the German company Robert Bosch GmBH. The objective was to provide the automotive industry, an inexpensive bus for embedded automotive systems.Today, the efficiency and robustness of this protocol led it to be used in many other industrial applications (aerospace, automation, renewable energy etc), particularly those requiring significant rate up 1Mbits / s a very low error rate.

Many CAN controllers are now available from most manufacturers that also offer versions of their microcontrollers with integrated CAN controllers.
Why CAN bus?
Automotive embedded systems, aerospace, home automation systems are decentralized.You have to communicate between modules (including the control cards, input cards, calculators, etc). Virtually all automotive, aerospace, home automation systems use a bus, CAN is a good choice of such bus.

Layers:

The CAN layer is divided into following layers.
  • Physical layer
  • Transfer layer
  • object layer
  • Application layer
you can read the description of these layers in wikepedia.

Frames:

A CAN network can be configured to work with two different message (Frame) format i.e,the standard or base frame format(described in CAN 2.0A and CAN 2.0 B) and the extended frame format(described in CAN 2.0B).The only difference between the two fornat is that the CAN base frame supports a length of 11 bits for the identifier and the CAN extendedd frame supports the length of 29 bits for identifier made up of 11 bit identifier(base identifier)and an 18bit extension(identifier extension).The distinction between CAN base format and extended format is made by using the IDE bit. All CAN frame starts with Start Of Frame(SOF) bit.
CAN has four frame types.
  • Data frame: a frame containing node data for transmission.
  • Remote frame: a frame requesting the transmission of specific identifier.
  • Error frame: a frame transmitted by any node detecting error.
  • Overload frame: a frame to inject a delay between data and/or remote frame.

2.0A Format:


A Standard CAN (Version 2.0A) Message Frame consists of seven different bit fields:
CAN 2.0A
Fig: CAN 2.0A

  • A Start of Frame (SOF) field. This is a dominant (logic 0) bit that indicates the beginning of a message frame.
  • An Arbitration field, containing an 11 bit message identifier and the Remote Transmission Request (RTR) bit. A dominant (logic 0), RTR bit indicates that the message is a Data Frame. A recessive (logic 1) value indicates that the message is a Remote Transmission Request (otherwise known as Remote Frame.) A Remote Frame is a request by one node for data from some other node on the bus. Remote Frames do not contain a Data Field.
  • A Control Field containing six bits:
  •  two dominant bits (r0 and r1) that are reserved for future use, and
  •  a four bit Data Length Code (DLC). The DLC indicates the number of bytes in the Data Field that follows
  • A Data Field, containing from zero to eight bytes.
  • The CRC field, containing a fifteen bit cyclic redundancy check code and a recessive delimiter bit
  • The ACKnowledge field, consisting of two bits. The first is the Slot bit which is transmitted as a recessive bit, but is subsequently over written by dominant bits transmitted from all other nodes that successfully receive the message. The second bit is a recessive delimiter bit
  • The End of Frame field, consisting of seven recessive bits.

2.0B Format:

The CAN 2.0B format provides a twenty nine (29) bit identifier as opposed to the 11 bit identifier in 2.0A.
CAN 2.0B
Fig: CAN 2.0B

The differences are:
  • In Version 2.0B the Arbitration field contains two identifier bit fields. The first (the base ID) is eleven (11) bits long for compatibility with Version 2.0A. The second field (the ID extension) is eighteen (18) bits long, to give a total length of twenty nine (29) bits.
  • The distinction between the two formats is made using an Identifier Extension (IDE) bit.
  • A Substitute Remote Request (SRR) bit is included in the Arbitration Field. The SRR bit is always transmitted as a recessive bit to ensure that, in the case of arbitration between a Standard Data Frame and an Extended Data Frame, the Standard Data Frame will always have priority if both messages have the same base (11 bit) identifier.
All other fields in a 2.0B Message Frame are identical to those in the Standard format.

CAN Network Structure:

The structure of CAN network is shown below:
Structure of CAN
FiG: CAN structure

Design:

The design to implement CAN protocol is shown below. It uses PIC18F4580 Microcontroller and MCP 2551 CAN controller.
Demonstration of CAn
Fig: Design to implement CAN

In figure, one PIC controller is used as transmitter and another is used as receiver.The communication speed is set to 100Kbps using 20MHZ crystal.The push buttons switch sends CAN frame which is received by receiver PIC controller and the receiver PIC after receiving the frame does the specified work.

Schematic:


Schematic for implementing CAN using PIC Microcontroller
FIG: Schematic for implementing CAN using PIC Microcontroller

Download:

Complete source code can be downloaded by clicking on the download button.


No comments: