Table of Contents
Overview
In this tutorial, I am going to show you how to interface the CAN Bus Controller with a TMS320 and how to communicate between two LAUNCHXL-F28379D via CAN BUS.
Components required table
S.N. | COMPONENTS | DESCRIPTION | QUANTITY | link |
1 | LAUNCHXL-F28379DSM | LAUNCHXL-F28379D | 2 | LAUNCHXL-F28379D |
2 | Logic Analyser | Logic Analyser | 1 | Logic Analyser |
3 | connection wires | Jumper Wires | 40 | Jumper Wires |
4 | Oscilloscope | Oscilloscope | 1 | Oscilloscope |
Software Requirements
Block Diagram
Introduction
Controlled Area Network of simple CAN is a bus standard that allows a Microcontroller and its peripheral devices to communicate without the need for a host device or a computer.
Developed by Robert Bosch GmbH, CAN is a protocol that is mainly used in automobiles for communication between a control unit and its components.
CAN Bus, standing for Controller Area Network, is one type of serial communication that is usually used in industrial and automotive environments. For other CAN details please visit the CAN communication project.
CAN Bus is a message-based protocol that can be used for multiple device communication. The figure below represents that when several CAN devices are connected like a network, each device can communicate with other devices in the node. In general, the CAN communication range is in range 50Kbps to 1Mbps, with the distance range being 40 meters (at 1 Mbps) to 1000 meters (at 50 kbps).
In this communication, data is transferred in a certain message format. Each message consists of many segments, but there are two main segments: Identifier and Data. Identifier or CAN ID, or known as Parameter Group Number (PGN) is used to identify the CAN device in the CAN network, usually in 11 (Standard CAN) or 29 bit (Extended CAN) length, based on the CAN protocol. While Data is the message content to be sent, usually 0 to 8 bytes long.
CAN Protocol consists of two wires: CAN_H and CAN_L to send and receive the message. These two wires act as a differential line where the CAN signal is represented with the potential difference between them. If the difference is positive and larger than a certain minimum voltage, then the signal is 1, and if the difference between them is negative, it will be 0.
Usually, in CAN communication, a twisted pair cable is used. And at the ends of the CAN networks, a single 120-ohm resistor is used. It is because the line should be balanced and tied to the same potential.
CAN connections in LAUNCHXL-F28379D
Final Code CAN Transceiver
/*
* CAN_TX.c
*
* Created on: 19-Aug-2022
* Author: Admin
*/
#include "F28x_Project.h"
#include "F2837xD_device.h"
#include "F2837xD_Examples.h"
#include "device.h"
#include "driverlib.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define TX_MSG_OBJ_ID 1
#define MSG_DATA_LENGTH 8
uint16_t txMsgData_1[8]={0x21,0x32,0x23,0x14,0x45,0x26,0x27,0x18};
void main(void)
{
Device_init();
Device_initGPIO();
// Configure GPIO pins for CANTX/CANRX
//
GPIO_setPinConfig(DEVICE_GPIO_CFG_CANRXB);
GPIO_setPinConfig(DEVICE_GPIO_CFG_CANTXB);
// set clock frequency 500khz
CAN_initModule(CANB_BASE);
//
// Set up the CAN bus bit rate to 500kHz for each module
// Refer to the Driver Library User Guide for information on how to set
// tighter timing control. Additionally, consult the device data sheet
// for more information about the CAN module clocking.
//
CAN_setBitRate(CANB_BASE, DEVICE_SYSCLK_FREQ, 500000, 16);
//
// Initialize the receive message object used for receiving CAN messages.
// Message Object Parameters:
// CAN Module: A
// Message Object ID Number: 1
// Message Identifier: 0x18FB47FF
// Message Frame: EXTENDED
// Message Type: Receive
// Message ID Mask: 0x0
// Message Object Flags: None
// Message Data Length: "Don't care" for a Receive mailbox
//
CAN_setupMessageObject(CANB_BASE, TX_MSG_OBJ_ID, 0x18FB47FF,
CAN_MSG_FRAME_EXT, CAN_MSG_OBJ_TYPE_TX, 0,
CAN_MSG_OBJ_NO_FLAGS, MSG_DATA_LENGTH);
CAN_startModule(CANB_BASE);
EINT;
while(1)
{
CAN_sendMessage(CANB_BASE, TX_MSG_OBJ_ID, MSG_DATA_LENGTH, txMsgData_1);
while(((HWREGH(CANB_BASE + CAN_O_ES) & CAN_ES_TXOK)) == CAN_ES_TXOK)
{
}
DEVICE_DELAY_US(1000000);
}
}
Final Code CAN Transceiver
/*
* main.c
*
* Created on: 05-Apr-2022
* Author: Admin
*/
#include "device.h"
#include "driverlib.h"
#define MSG_DATA_LENGTH 8 // "Don't care" for a Receive mailbox
#define RX_MSG_OBJ_ID 1 // Use mailbox 1
//
// Globals
//
uint16_t rxMsgData[8];
volatile uint32_t rxMsgCount = 0;
void main(void)
{
Device_init();
Device_initGPIO();
//
// Configure GPIO pins for CANTX/CANRX
//
GPIO_setPinConfig(DEVICE_GPIO_CFG_CANRXB);
GPIO_setPinConfig(DEVICE_GPIO_CFG_CANTXB);
//
// Configure GPIO pin which is toggled upon message reception
//S
GPIO_setPinConfig(GPIO_31_GPIO31);
GPIO_setDirectionMode(31, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(31, GPIO_PIN_TYPE_STD);
//
// Initialize the CAN controller
//
CAN_initModule(CANB_BASE);
//
// Set up the CAN bus bit rate to 500kHz for each module
// Refer to the Driver Library User Guide for information on how to set
// tighter timing control. Additionally, consult the device data sheet
// for more information about the CAN module clocking.
//
CAN_setBitRate(CANB_BASE, DEVICE_SYSCLK_FREQ, 500000, 16);
//
// Initialize the receive message object used for receiving CAN messages.
// Message Object Parameters:
// CAN Module: A
// Message Object ID Number: 1
// Message Identifier: 0x18FB47FF
// Message Frame: EXTENDED
// Message Type: Receive
// Message ID Mask: 0x0
// Message Object Flags: None
// Message Data Length: "Don't care" for a Receive mailbox
//
CAN_setupMessageObject(CANB_BASE, RX_MSG_OBJ_ID, 0x18FB47FF,
CAN_MSG_FRAME_EXT, CAN_MSG_OBJ_TYPE_RX, 0,
CAN_MSG_OBJ_NO_FLAGS, MSG_DATA_LENGTH);
// Start CAN module B operations
//
CAN_startModule(CANB_BASE);
while(1)
{
//
// Poll RxOk bit in CAN_ES register to check completion of Reception
//
if(((HWREGH(CANB_BASE + CAN_O_ES) & CAN_ES_RXOK)) == CAN_ES_RXOK)
{
//
// Get the received message
//
CAN_readMessage(CANB_BASE, RX_MSG_OBJ_ID, rxMsgData);
GPIO_togglePin(31);
rxMsgCount++;
}
}
}
can i conect a launchpad directly with launchpad in CAN protocol ?