You are currently viewing How to Interface SPI-CAN with LPC2148 ARM7 Advanced Development Board

How to Interface SPI-CAN with LPC2148 ARM7 Advanced Development Board

Spread the love

The ARM7 LPC2148 Advanced Development Board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through serial port.

NXP’s ARM7 (LPC2148), ARM Advanced Development Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 32-bit Microcontrollers.

SPI (Serial Peripheral Interface)

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers.

CAN (Controller Area Network)

CAN is a multi-master broadcast serial bus standard for connecting electronic control units (ECUs). Each node is able to send and receive messages, but not simultaneously. CAN bus is a vehicle bus standard designed to allow microcontrollers and devices to communicate with each other within a vehicle without a host computer. The CAN communication protocol is a CSMA/CD protocol.

The MCP2510

The MCP2510 is a Full CAN protocol controller implementing CAN specification V2.0 A/B. It is capable of transmitting & receiving standard and extended messages. It is also capable of both acceptance filtering and message management. It includes three transmit buffers and two receive buffers that reduce the amount of microcontroller management required. The MCU communication is implemented via SPI with data rates up to 5 Mb/s.

Interfacing SPI-CAN

Fig. 1 shows how to interface the SPI-CAN to microcontroller. With an SPI connection there is always one master device which controls the peripheral devices. Typically there are three lines common to all the devices,

☞Master In Slave Out (MISO) – The Slave line for sending data to the master,

☞Master Out Slave In (MOSI) – The Master line for sending data to the peripherals,

☞Serial Clock (SCK) – The clock pulses which synchronize data transmission generated by the master&

☞Slave Select pin – the pin on each device that the master can use to enable and disable specific devices. When a device’s Slave Select pin is low, it communicates with the master. When it’s high, it ignores the master.

This allows you to have multiple SPI devices sharing the same MISO, MOSI, and CLK lines.

The CAN protocol engine handles all functions for receiving and transmitting messages on the bus. Messages are transmitted by first loading the appropriate message buffer and control registers. The MCP2510 has three transmit and two receive buffers, two acceptance masks (one for each receive buffer), and a total of six acceptance filters. The CRC Register generates the Cyclic Redundancy Check code which is transmitted after either the Control Field (for messages with 0 data bytes) or the Data Field, and is used to check the CRC field of incoming messages.

Interfacing SPI-CAN with LPC2148

In SPI, the clock signal is controlled by the master device LPC2148 Advanced Development Board. All data is clocked in and out using this pin. These lines need to be connected to the relevant pins on the LPC2148 Advanced Development Board. In Mcp2510, Transmission is initiated by using control register bits, via the SPI interface, or by using the transmit enable pins. Interrupt pins are provided to allow greater system flexibility. There are also three pins available to initiate immediate transmission of a message that has been loaded into one of the three transmit registers.

Pin Assignment with LPC2148

Circuit Diagram to Interface SPI-CAN with LPC2148

Source Code

The Interfacing SPI-CAN with LPC2148 program is very simple and straight forward, which controls the LPC2148 Advanced Development Board from another LPC2148 Advanced Development Board using CAN via SPI. Some delay is occurring when a single data is sent through SPI. C programs are written in Keil software.

C Program to interface SPI-CAN with LPC2148

******************************************************************************

Title : Program for SPI-Master

*******************************************************************************

#include "CAN_SPIsw.h" 
#include "CAN_SPIconst.h" 
#include "SPIsw.h" 
#include "Utility.h" unsigned char aa,aa1,aa2,len,zr,cont,oldstate,data[8]; 
unsigned long id; 
int main (void) 
{ 
PINSEL0 = 0; 
PINSEL1 = 0; 
PINSEL2 &= 0x0000000C; 
//--- PORT1[23..16] as Initialization pgr indicator IOCLR1 |= ((unsigned long)0xFF << 16); 
IODIR1 |= ((unsigned long)0xFF << 16); 
//--- PORT1[31..24] as Data Value indicator IOCLR1 |= ((unsigned long)0xFF << 24); 
IODIR1 |= ((unsigned long)0xFF << 24); 
//--- initialize SPI (mode 0,0) IODIR0 |= ((unsigned long)1 << 11); 
// P0.11 output IODIR0 |= ((unsigned long)1 << 10); 
// P0.10 output IOSET0 |= ((unsigned long)1 << 11); 
// RESET line IOCLR0 |= ((unsigned long)1 << 10); 
// CS line SPI_init(&IOPIN0, 10/*CS*/, 5/*MISO*/, 6/*MOSI*/, 4/*SCK*/, 0/*CPOL*/, 0/*CPHA*/); 
//--- configure CAN_SPI module aa = 0; aa1 = 0; aa2 = 0; 
aa = CAN_CONFIG_SAMPLE_THRICE & 
CAN_CONFIG_PHSEG2_PRG_ON & CAN_CONFIG_STD_MSG & 
CAN_CONFIG_DBL_BUFFER_ON & CAN_CONFIG_VALID_XTD_MSG; 
CAN_TX_PRIORITY_0 & 
CAN_TX_XTD_FRAME & 
CAN_TX_NO_RTR_FRAME; IOSET0 |= (1 << 10); 
// set CS line for CAN module high (inactive) id = -1; 
// id temporarily used for SetMask operation CANSPIInitialize(1,1,3,3,1,aa); 
// initialize external CAN module IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 3; IOSET1 |= (0x03 << 16); 
CANSPISetOperationMode(CAN_MODE_CONFIG,0xFF); 
IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 4; 
IOSET1 |= (0x04 << 16); 
CANSPISetMask(CAN_MASK_B1,id,CAN_CONFIG_XTD_MSG); 
// set all mask1 bits to ones CANSPISetMask(CAN_MASK_B2,id,CAN_CONFIG_XTD_MSG); 
// set all mask2 bits to ones CANSPISetFilter(CAN_FILTER_B2_F4,3,CAN_CONFIG_XTD_MSG); 
// set id of filter B1_F1 to 3 CANSPISetOperationMode(CAN_MODE_NORMAL,0xFF); 
// set NORMAL mode IOCLR1 |= (0xFF << 16); // PORT1[23..16] = 5; 
IOSET1 |= (0x05 << 16); data[0] = 7; 
id = 12111; IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 255; 
IOSET1 |= (0xFF << 16); 
CANSPIWrite (id,data,1,aa1); 
/ 1st node communication while (1) 
{ 
zr = CANSPIRead(&id , data , &len, &aa2); 
if ((id == 3) && zr) 
// respond only to data from node No.3 { IOCLR1 |= (0xFF << 16);
 IOSET1 |= ((unsigned long)data[0] << 16) &
 0x00FF0000; data[0]++ ; id = 12111; delay_Nx10cyc(5999999);
 // Delay 1s CANSPIWrite(id, data, 1,aa1); 
} 
} 
} 

Title : Program for SPI-Slave

*********************************************************************************

#include <LPC214X.H> 
#include "CAN_SPIsw.h" 
#include "CAN_SPIconst.h" 
#include "SPIsw.h" 
#include "Utility.h" unsigned char aa,aa1,aa2,len,zr,cont,oldstate,data[8]; 
unsigned long id; int main (void) 
{
PINSEL0 = 0; PINSEL1 = 0; 
PINSEL2 &= 0x0000000C; 
//--- PORT1[23..16] as Initialization progress indicator IOCLR1 |= ((unsigned long)0xFF << 16); 
IODIR1 |= ((unsigned long)0xFF << 16);
//--- prepare port PORT1[31..24] as Data Value indicator IOCLR1 |= ((unsigned long)0xFF << 24); 
IODIR1 |= ((unsigned long)0xFF << 24);
 //--- initialize SPI (mode 0,0) IODIR0 |= ((unsigned long)1 << 11); 
// P0.11 output IODIR0 |= ((unsigned long)1 << 10); 
// P0.10 output IOSET0 |= ((unsigned long)1 << 11); 
// RESET line IOCLR0 |= ((unsigned long)1 << 10); 
// CS line SPI_init(&IOPIN0, 10/*CS*/, 5/*MISO*/, 6/*MOSI*/, 4/*SCK*/, 0/*CPOL*/, 0/*CPHA*/); 
//--- configure CAN_SPI module aa = 0; 
aa1 = 0; aa2 = 0; 
aa = CAN_CONFIG_SAMPLE_THRICE & 
CAN_CONFIG_PHSEG2_PRG_ON &
 CAN_CONFIG_STD_MSG & CAN_CONFIG_DBL_BUFFER_ON & 
CAN_CONFIG_VALID_XTD_MSG; aa1 = CAN_TX_PRIORITY_0 & 
// form value to be used CAN_TX_XTD_FRAME & 
// with CANSPIWrite CAN_TX_NO_RTR_FRAME; IOSET0 |= (1 << 10);
 // set CS line id = -1; 
// id temporarily used for SetMask operation CANSPIInitialize(1,1,3,3,1,aa);
 // external CAN module IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 3; 
IOSET1 |= (0x03 << 16);
 CANSPISetOperationMode(CAN_MODE_CONFIG,0xFF);
 IOCLR1 |= (0xFF << 16);
 // PORT1[23..16] = 4;
 IOSET1 |= (0x04 << 16); 
CANSPISetMask(CAN_MASK_B1,id,CAN_CONFIG_XTD_MSG); 
CANSPISetMask(CAN_MASK_B2,id,CAN_CONFIG_XTD_MSG);
 // set all mask2 bits to ones CANSPISetFilter(CAN_FILTER_B2_F4,12111,CAN_CONFIG_XTD_MSG);
 // set id of filter B1_F1 to 12111 CANSPISetOperationMode(CAN_MODE_NORMAL,0xFF); 
// set NORMAL mode IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 5; IOSET1 |= (0x05 << 16); 
data[0] = 9; data[1] = 0x81; 
id = 3; IOCLR1 |= (0xFF << 16); 
// PORT1[23..16] = 255; 
IOSET1 |= (0xFF << 16); 
while (1) 
{
 zr = CANSPIRead(&id , data , &len, &aa2); 
if ((id == 12111) && zr) 
// respond only to data from node No.12111 
{ 
IOCLR1 |= (0xFF << 16);
 // PORT1[23..16] IOSET1 |= ((unsigned long)data[0] << 16) & 
0x00FF0000; data[0]++ ;
 id = 3;
 // id contains CAN network ID of this node CANSPIWrite(id, data,8,aa1);
 }
 }
 }

To compile the above C code you must need the KEIL software. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In KEIL, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without LPC2148 Advanced Development Board.

The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148 through UART0.

Testing the SPI-CAN with LPC2148

Give +3.3V power supply to LPC2148 Advanced Development Board; the SPI-CAN device is connected with LPC2148 Advanced Development Board. Here we are using Master and Slave LPC2148 Advanced Development Board for CAN transmit & receive.

When the Master Board receives any signals from Slave then it send data to Slave. If the Slave Board receives data from Master Board then it can enables some LED’s for indication. Then the Slave sends some acknowledgeable data into Master LPC2148 Advanced Development Board. Now the Master Board enables some LED for indication.

If you are not reading any output from LED, then you just check the jumper connections & check the LED is working. Check the CAN device is working or not & check the connections of Master & Slave LPC2148 Advanced Development Board.

Otherwise you just check the code with debugging mode in KEIL. If you want to see more details about debugging just see the videos in below link.

How to Create & Debug a Project in KEIL

General Information

☞For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.

☞Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.

☞Use the exact value crystal shown in schematic.

☞More instructions are available in following articles,

How to Interface LEDs with LPC2148 – ARM7 Advanced development board

User Manual for NXP LPC213X Advanced Development Board

Creating & Debugging a Project in KEIL

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.