This tutorial describes the implementation of a MSP430 CAN interface as 3.3 V-logic for the MSP430-series from Texas Instruments. The MSP430G2553 is used as microcontroller, which is positioned on the launchpad EXP430G2 revision 1.5. An expansion in the form of a shield is developed for the EXP430G2, whereupon the MCP2515 from Microchip is used as a CAN controller.
1.1 Hardware of the MSP430 CAN interface
The schematic and the board layout for the EXP430G2 were both with eagle created (eagle-msp430 with mcp2515). In the figure 1 you can see in the left side the schematic and in the right side the board layout from the shield.
1.2 Software of the MCP2515 library
The IAR-IDE was used for the software (iar-mcp2515 example_code). The source code is 1.56 kB large and can be also played with the Kickstart-Version from IAR (maximal 8 kB). The software for the MSP430 CAN interface is divided as the following:
Tab. 1: Division of the MCP2515 library
|main.c||The main program of the project includes an example of transmitting and receiving messages with 250 kBit/s|
|mcp2515.c||All the Functions and Defines for the MCP2515-CAN-controller|
2 Hardware of the MSP430 CAN interface
The means that were used here are summarized in the Table 2.
Tab. 2: Used Hard- and Software
|MSP-EXP430G2 rev. 1.5||Hardware||Launchpad of Texas Instruments|
|IAR – MSP430||Software||Development environments for the MSP430 series|
The launchpad of Texas Instruments MSP-EXP430G2 revision 1.5 (http://www.ti.com/tool/msp-exp430g2) is used. Then the microcontroller MSP430G2553 is placed upon the launchpad. The resulting EXP430G2 shield is plugged upon the launchpad and should be mounted, as shown in figure
The free IAR-kicksartversion is used as software (http://www.ti.com/tool/iar-kickstart).The free license can be used for source code up to 8 kB. Another free alternative is the program: Code Composer Studio of Texas Instruments.
The block diagram of the hardware is shown in figure 3.
The EXP430G2 shield is shown on the right and the launchpad on the left. The shield has a slat strip on the underside in the same grid as the launchpad. Thus, the shield can be easily mounted on the launchpad (see also Figure 2). The supply (3.3 V and GND) takes over the launchpad. In addition, the SPI (MOSI, MISO, and SCK), the CS, and an interrupt-enabled pin are passed to the shields. The SPI pins control the CAN controller, which is then connected to the CAN transceiver. After the CAN transceiver, a termination in the form of a 120 Ω resistor is required before the CAN bus (CANL and CANH) can finally be accessed via the sub-D socket. In addition, the green LED indicates the power supply and the red LED indicates whether information was received via CAN.
The figure below shows the circuit diagram of the EXP430G2 shield.
Please note that all components of the EXP430G2 shield are supplied with 3.3 V from the launchpad. A controller and a transceiver are required for the CAN. The MCP2515, which creates the CAN protocol, is used as a controller. Among them is the creation of the CAN frame, the filtering and checking (CRC checksum) of data, the handling of error frames and the sending of frames. This is controlled via SPI through the lines MCP_CS, MCP_SCK, MCP_MISO and MCP_MOSI. The pin MCP_INT (pin 12) triggers an interrupt on the MCP2515 and is connected to an interrupt pin of the MSP430G2553. The pin 12 is “inactive high”, which means that it will be 3.3 V as long as the controller has not received a message. The MCP2515-CAN controller also requires an external clock, which is supplied by the 16 MHz quartz .
The SN65HVD231D is used as a transceiver. This has the property that the operating voltage is 3.3 V. There are not many transceivers that can handle 3.3 V (an alternative to that can be found in Texas Instruments). The transceiver has the task of providing the CAN information for the CAN bus. The resistor R12 sets the chip in high-speed mode. In this mode, the transceiver attempts to switch the output as quickly as possible without internal limits. Alternatively, the slope control mode can be used if EMC (electromagnetic compatibility) problems are expected during the application. The improved EMC behavior is caused by the fact that the transceiver is switching the edges slower (sharp edges and fast clock rates impair the EMC behavior). In addition, the resistor R13 is required for terminating the line . Finally, the CAN lines can be accessed via the 9-pin sub-D connector (according to CiA® 102).
The pin assignment of the 9-pin Sub-D socket is as follows:
3 Software of the MCP2515 library
Three files can be found for the IAR project. These are:
- “main.c” which includes the main program and
- “mcp2515.h” / “mcp2515.c” which together generate the MCP2515 library and are responsible for the communication and interpretation of the MCP2515.
The main program (main) provides an example for receiving and sending messages via CAN. For this purpose an echo is sent, therefore the information which the microcontroller receives is sent directly back. The following flow diagram is intended to illustrate the main program. The flow diagram follows the numbering of the source code.
The figure shows the main function on the left and the IR function on the right. In order to start the program at the main function (fig. 6-i), the watchdog timer must first be interrupted (point 1). The frequency of the microcontroller is then set (point 2). The pin for the led control and the port 1 (pin P1.4) are then initialized for an interrupt on falling edge. Furthermore, in the fourth point the SPI interface, the MCP2515 – since SPI has been initialized, can be interacted with the MCP2515 – the global CAN variable and the interrupts are activated (important for IR port 1). This is followed by the endless loop, in which the microcontroller tacks the led 2 of the launchpad every second.
When information is received via CAN, the CAN controller sets its IR pin (pin 12) to “low”. The microcontroller recognizes this, because the pin is connected to the IR port 1 and triggers an interrupt. The IR interrupts the main flow of the main function. The IR function (fig. 6-ii) is now processed. At first the function reads out the receive channel of the CAN controller, waits 10 ms and sends the received information back via CAN (echo). Now the IR has to be reset by clearing the corresponding flag. Finally, the program returns to the Main and continues to toggle the LED.
A communication partner for CAN is now required. As a communication partner, is used the software pCAN-View from Peak-Systems (with the corresponding hardware, link peak system) and as baud rate 250 kBit / s.
The library takes care of the control of the MCP2515 CAN controller. This is divided into header and source file. The header contains: register names, prototypes, struct (summary of different data types in a self-generated data type) of the CAN variable and pin designations. All the functions of the library are found in the source file.
The table 3 summarizes all the functions of the “mcp2515.c”
Tab. 3: Functions and their description for the “mcp2515.c”
|MCP2515_SPI_init()*||Initializes the SPI interface of the microcontroller.|
|MCP2515_SPI_transmit()*||Sending and receiving messages via the SPI interfaces.|
|MCP2515_spi_test()||Tests whether the communication between microcontroller and MCP2515 is running correctly. If not, the function returns 1, otherwise 0.|
|MCP2515_reset()||Software reset of the MCP2515. Since the reset pin is permanently high (over a pull up), this is the only way to reset the MCP2515 in this project.|
|MCP2515_CanVariable_init()||Initialize the variable of the can_t (structure for the CAN variable) with initial values.|
|MCP2515_init()||Initializes the MCP2515. Things like, baud rate, filter, send and receive channel, are set.|
|MCP2515_bit_modify()||Only certain bits are set or deleted here. Which bits will be manipulated, is determined from the mask. The bits that are high on the mask are edited.|
|MCP2515_write()||Write via SPI in a single register of the MCP2515|
|MCP2515_write_many_registers()||To write multiple registers of the MCP2515 with one command.|
|MCP2515_write_id()||Describes the ID for the CAN frame which should be sent.|
|MCP2515_read()||Reads a single register.|
|MCP2515_read_many_registers()||Reads multiple registers of the MCP25115.|
|MCP2515_read_id()||Reads the ID for the CAN frame which was received.|
|MCP2515_can_tx0()||CAN transmit channel 0 (from 3). For sending a CAN frame (ID, RTR and data).|
|MCP2515_can_tx1()||CAN transmit channel 1 (from 3). For sending a CAN frame (ID, RTR and data).|
|MCP2515_can_tx2()||CAN transmit channel 2 (from 3). For sending a CAN frame (ID, RTR and data).|
|MCP2515_can_rx0()||CAN receive channel 0 (from 2). For receiving a CAN frame (ID, RTR and data).|
|MCP2515_can_rx1()||CAN receive channel 1 (from 2). For receiving a CAN frame (ID, RTR and data).|
* This function must be adjusted only if another microcontroller is used.
If a user wants to use another microcontroller as the MSP430G2553, the user must make the following changes:
- Edit function MCP2515_SPI_init () (see also table 3)
- Edit the MCP2515_SPI_ transmit () function (see also table 3)
- Edit the Defines MCP2515_CS_LOW and MCP2515_CS_High (found in the “mcp2515.h”)
According to data sheet (12.1 Overview, page 65) the MCP2515 supports the SPI modes 0,0 and 1,1. This means that the data / commands are read in with increasing SCK edge and are read out when the SCK edge drops. The CS line must be held low during sending / receiving.
The bit timing of the SPI interface is shown in figure 7.
It can be seen that when the CS pin shows low, the start of a frame is signaled. First the data is sent with the MSB (Most Significant Bit). Initially, the direction must be sent. Where 02h is the write direction and 03h is the read direction. The address that is to be written / read should be sent / received. Finally, the data can be sent / received (depends on the direction). The frame is terminated by pulling the CS line back to high. Note that the MCP2515 can only send / receive 8-bit.
The following figure shows the source code for initializing the SPI.
P1SEL = BIT5 + BIT6 + BIT7; // Spezial Funktion SCK, MOSI und MISO ...
P1SEL2 = BIT5 + BIT6 + BIT7; // (Falls P1SEL2 unterstützt).-
P2DIR |= BIT5; // !CS-Leitung (beachte auch Defines MCP2515_CS_LOW und MCP2515_CS_High) ...
P2OUT |= BIT5; // .-
UCB0CTL1 |= UCSWRST; // Reset
UCB0CTL0 |= UCCKPL + UCMSB + UCMST + UCMODE_0 + UCSYNC; // 3-pin, 8-bit SPI master
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 |= 0x02; // /2 ...
UCB0BR1 = 0; // .-
//UCA0MCTL = 0; // No modulation
UCB0CTL1 &= ~UCSWRST; // Initialize USCI state machine
__delay_cycles(DELAY_100ms); // Warte 100ms
while (!(IFG2 & UCB0TXIFG)); // Warte bist übermittelt
UCB0TXBUF = 0x00; // Dummy Senden
MCP2515_SPI_ transmit ()
What a user has to change is the desired baud rate. The sample program uses 250 kbit / s. In general, the MCP2515 changes (filter, baud rate, channel, etc.) should be set in the MCP2515_init () function. Additionally, in the source code of the function is specified in which pages of the datasheet is located the respective register.
The following settings are common:
- Set the IC (e.g. to start the configuration mode of the MCP2515)
- Timing (baud rate)
By default, the MCP2515 CAN controller starts with the following settings:
- Baud rate 250 kbit / s
- Receive channel activated for RX0 and RX1
- Send only via TX0
- Disable RXnBF (prevents an interrupt from being triggered)
- Disable RTS pins (prevent an interrupt from being triggered)
- Allow the interrupt of the INT pin
The most difficult setting for a user is the baud rate. However, Microchip makes it easy for us, as they have developed a program (Link Microchip CAN bit timing calculator) which takes the calculations for the timing (baud rate).
The procedure should be as follows:
- Install the program
- Start the program as an administrator
- Set frequency and baud rate (see Fig. 9, step 3)
- Press the “next” button (see fig. 9, step 4)
- A new window will open. Here, you can move immediately forward or, if more than one option is available, remove a Time Quanta (see Fig. 8).
- If multiple time quanta (nothing is removed at the previous point) are present, then optionally a time quanta can be selected. Press button “Generate Report …” (see figure 9).
- Now the standard browser of the PC (see fig. 10) is opened. At the end of the page, read the register values under “Configuration Register Setup (PIC18 / MCP251X)”.
- Finally, update the values in the MCP2515_init () function (see Fig. 11).
The EXP430G2 shields were subjected to various software tests for the high-speed mode of the transceiver and the standard ID of the MCP2515-CAN controller. Various baud rates, channels for sending and receiving CAN messages and microcontrollers were tested. The following table summarizes the results of the tests:
Tab. 4: Software tests for the EXP430G2 shield
|Baud rate||10, 125, 250 und 500 kBit/s||Operates correctly – In order to achieve baud rates faster than 125 kbit / s, a quartz with 16 MHz is required.|
|Send channel||TX0, TX1, TX2||Operates correctly|
|Receive channel||RX0, RX1||Channel RX0 operates correctly.
The channel RX1 appears to have only one value, if channel RX0 is full.
5 Further information
|||Data sheet MCP2515, microchip, 2005|
|||Data sheet SN65HVD231D, Texas instruments, 2015|
Do you have questions about the tutorial?
Then just drop me a message via the form below or send me an email.