Datasheet

Copyright © 2013 ARM Ltd. All rights reserved
CAN: Controller Area Network Lab using ST STM32 Cortex-M processors. www.keil.com
15
Keil Example CAN Program: Using the Keil Simulator:
1. Start µVision
®
by clicking on its icon on your Desktop. Consider making a backup copy of the example files.
2. Select Project/Open Project. Open the file C:\Keil\ARM\Boards\Keil\MCBSTM32E\CAN\CAN.uvproj.
3. Select “Simulator” in the Target window.
4. In the file GLCD_16bitIF_STM32.c, change #define DELAY_2N (near line 32) to have a value of 2 instead of 18 in
order to start the messages faster the very first time. This is a delay for the LCD which we are not using here. Any
source file can be opened in µVision if not already visible by clicking on File/Open and selecting it or double-
clicking on the file name in the Project window.
5. Compile the source files by clicking on the Rebuild icon.
. They will compile with no errors or warnings.
6. Click on the Target Options icon.
Select the Debug tab and confirm “Use Simulator” is checked. Click OK.
7. Enter the Debug mode by clicking on the Debug icon.
Select OK if the Evaluation Mode box appears.
8. Position the Toolbox, CAN: Communication and CAN: Controller windows as appropriate.
9. Click on the RUN icon.
Note: you can stop the program with the STOP icon.
10. Note CAN messages with an ID of 0x21 will appear in the CAN: Communications window. You can see both the
transmit and receive frames. The CAN controller is in a special Test Mode that allows it to see its own messages.
11. In the Toolbox window, click on the “Analog Sweep 0…3.3v” button.
12. Changing data values representing output from the A/D convertor will now appear inside the CAN messages.
13. Stop the program.
You can stay in Debug mode unless you want to exit µVision.
The Keil CAN Demonstration Software: How it works…
Note: The following source code is from the MCBSTM32E CAN example you loaded above. This uses one CAN controller
set to loop-back mode. The source files for the STM32F4 Discovery board are slightly different as two CAN controllers are
used. You can view and edit the C source files whether in debug mode or edit mode, but to compile them you must be in edit
mode. This example is entirely written in C. There are three source files we will look at:
CAN.h: This file defines a structure to contain the information used to construct the CAN frame and create two instances of
it. The prototypes for functions used in CAN.c are listed in CAN.h in lines 34 to 40.
CAN.c: This C code initializes the CAN controller, writes and transmits a message, receives a message, configures the
Acceptance Filters and provide the transmit and receive interrupt handlers.
CanDemo.c: The main function is located in this file. CanDemo.c calls the functions in CAN.C.
1) CAN.h TIP: To make this file visible, in the Project window, expand CanDemo.c and double-click on Can.h.
The CAN Structure CAN_Msg: (These are MCBSTM32E line numbers but might change without notice)
Shown is the structure declaration in Can.h. You should now be able to recognize each of these elements. You can enter
either an 11 or 29 bit identifier. Two instances of CAN.msg are invoked and are shown below: CAN_TxMsg and
CAN_RxMsg. CanDemo.c writes to CAN_TxMsg to create the CAN messages to be transmitted.
25 typedef struct {
26 unsigned int id; // 29 bit identifier
27 unsigned char data[8]; // Data field
28 unsigned char len; // Length of data field in bytes
29 unsigned char format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER
30 unsigned char type; // 0 - DATA FRAME, 1 - REMOTE FRAME
31 } CAN_msg;
44 extern CAN_msg CAN_TxMsg; // CAN message for sending
45 extern CAN
_
msg CAN
_
RxMsg;
/
/ CAN message for receiving