AVR Controller To CAN
AVR Controller To CAN
AUTHOR: CHRIS
KEYWORDS: XXXXXXXXX #040
This document is originally distributed by AVRfreaks.net, and may be distributed, reproduced, and modified
without restrictions. Updates and additional design notes can be found at: www.AVRfreaks.net
Introduction Controller Area Network (CAN) was initially created by German automotive system
supplier Robert Bosch for automotive applications as a method for enabling robust serial
communication. The goal was to make automobiles more reliable, safe, and fuel-
efficient while decreasing wiring harness weight and complexity. Since its inception, the
CAN Protocol has gained widespread popularity in industrial automation and
automotive/truck applications.
Other markets where networked solutions can bring attractive benefits like medical
equipment, test equipment and mobile machines are also starting to utilize the benefits
of CAN.
Description The goal of this design note is to explain how to connect the MCP2510 CAN Controller
via the Serial Peripheral Interface (SPI) to an AVR 8515.
As C-compiler we will use the free AVRGCC.
In detail this note shows how to
• Connect the MCP2510 to the AVR8515 using the SPI.
• Initialize the Serial Peripheral Interface.
• Make a Software Reset.
• Write to the internal registers of MCP2510.
• Read from the internal registers of MCP2510.
• Find out that you have access to the MCP2510 CAN Controller.
www.AVRfreaks.net 1
Design Note #040 – Date: 01/03
CAN Controller CAN Controller make it “easy” to use CAN. The MCP2510 is a stand-alone CAN
Controller with SPI and implements Full CAN V2.0A and V2.0B at 1Mb/s.
MCP2510
CAN Controller The PCA82C250 is the interface between the CAN Protocoll Controller and the physical
bus. How to fit all together is shown in Figure 1.
Interface PCA82C250
Figure 1. CAN Interface
Figure 1 shows how to connect the CAN Controller to the AVR and to the PCA28C250.
The MCP2510 also provides three digital inputs, e.g., to use with switches. This inputs
are pulled high using R1 to R3. To simulate a closed switch you can short circuit the
JP3,JP2, or JP1 pins. The MCP2510 also provide a clock output with programable pres-
caler on pin number three, accessable through JP4.
The SP Interface The MCP2510 is designed to interface directly with the Serial Peripheral Interface port
available on many microcontrollers and supports Mode0,0 and Mode1,1. Five different
operations are available.
Table 1 shows the three important instructions to communicate with the MCP2510.
How to Make a The Reset command is an one-byte command. This command Resets the Internal Reg-
isters to default state. The C-code is shown below.
Software Reset
void ResetMcp (void)
{
unsigned char counter;
cbi(PORTB,PB4);//clear bit Chipselect -> that means pull it low
outp(0xC0,SPDR);//send reset command
loop_until_bit_is_set(SPSR,SPIF);//wait until byte is transmitted
sbi(PORTB,PB4);//set bit Chipselect
for (zaehler=0;counter<128;counter++);//wait a few cycles until oscillator
//is running
}
How to Read a Byte The Read command is three byte long. At first you have to send the read instruction as
described in Table1 (0x03h). Then the address from where you want to read must be
From the MCP2510
sent. After that you have to send a byte just what you like. During this last byte the
MCP2510 will move the addressed byte into the SPI Data Register (SPDR).
The C-code is shown below.
Table 2. Prescaler
Bit1 Bit0 Prescaler
0 0 1
0 1 2
1 0 4
1 1 8 After Reset
Conclusion Its a far way from configuration to reveive and transmit messages but I hope this note
will help. For more information please have a look to the microchip homepage:
www.microchip.com, and Especially to DS21291 data sheet MCP2510
Interesting application notes are AN215, AN713, AN212, AN739