Monday, July 8, 2013

CAN bus

CAN bus (controller area network) is a vehicle bus standard designed to allow microcontrollers and devices to communicate with each other. CAN bus is a message-based protocol, designed specifically for automotive applications but now also used in other areas such as aerospace, industrial automation and medical equipment. The advantages of CAN bus compared to RS232 communication are as follows.

Bit rates

Bit rates up to 1 Mbit/s are possible at network lengths below 40 m.

Number of devices

Number of devices attached to a bus can be more than 100 without using repeater or bridge.

Frames

In CAN bus, message framing, error detection, and handshaking are all taken care by hardware.

Microcontroller

As an example, we present implementation of CAN bus on an 8051 microcontroller. AT89C51CC03 from Atmel has on chip CAN controller. The schematic circuit diagram is shown below.
An example C program for CAN communication using AT89C51CC03 can be found in Atmel's website (atmel.com). The following is the modified version of that C program.

CAN Comm on GitHub


The example for CAN frames sending is shown as follows.
void FraSend2CAN(ui08 d[],ui08 len)//will loop until it is successfully sent
{
 ui08 idata i,j;
 //-------------------------------------------------------------------------
 //For first acc data, 8 bytes
 TB[0]=0;
 TB[1]=0;
 TB[2]=0;
 TB[3]=0x10;//corresponds to id 2 in LabVIEW
    
    for(i=0,j=4;i<8;i++) TB[j++]=d[i];
 while(!CANTxString(12,TB));
 //-------------------------------------------------------------------------
 //For second acc data, 8 bytes
 TB[0]=0;
 TB[1]=0;
 TB[2]=0;
 TB[3]=0x20;//corresponds to id 4 in LabVIEW
    
    for(i=8,j=4;i<16;i++) TB[j++]=d[i];
 while(!CANTxString(12,TB));
 //-------------------------------------------------------------------------
 //For gyro data, 6 bytes
 TB[0]=0;
 TB[1]=0;
 TB[2]=0;
 TB[3]=0x30;//corresponds to id 6 in LabVIEW
    
    for(i=16,j=4;i<len;i++) TB[j++]=d[i];
 while(!CANTxString(10,TB));
}

LabVIEW

To use CAN communication in LabVIEW, you need to install xnet software. The you can create a database for CAN communication from start->All Programs->National Instruments->NI-XNET->Database Editor.



Example CAN Database and LabVIEW VI on GitHub


No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.