ELEC 3300 Homework 1: Introduction To Keil MDK A. Objective

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

ELEC 3300

HOMEWORK 1: INTRODUCTION TO KEIL MDK

A. OBJECTIVE:

1. To familiarize yourself with the structure of ARM microcontrollers.


2. To familiarize the Keil MDK.
3. To compile programs using Keil MDK.

B. READING:

1. Revise basic C programming and basic assembly programming syntax


2. Study the Getting Started with MDK (available from the course website)
 Acknowledgement: Part of this HW material is from Getting Started with MDK
3. Study the Cortex-M3 programming Manual (available from the course website)
 Chapter 2 – The Cortex-M3 processor.
4. Study the uVision User’s Guide at (http://www.keil.com/support/man/docs/uv4)
5. Install the Keil MDK-ARM Version 5.23. (available from course website)

C. INSTALLAION OF KEIL MDK:

An IDE (Integrated Development Environment) is a software that allows you to design and test
your microcontroller-based application. In this course, we will use Keil MDK (Microcontroller
Development Kit), for details of the software; you can refer to Getting Started with MDK from the
course website.

You need to download the software from the course website. We are using MDK-Lite, it is a
freeware for small project and education, you can download your own copy from the Keil website.
(www.keil.com) or from course website (MDK523.EXE)

CODE size is restricted to 32K byte for this version.

After successful installation, you will have the Keil uVision5 on your Desktop

Figure 1: Keil uVision icon on the Desktop

The installation also adds the Software Packs for ARM CMSIS and MDK Middleware.

MDK Version 5 is capable of using MDK Version 4 projects after installation of the Legacy
Support.
 Legacy support for Cortex-M devices. (Available at course website MDKCM523.EXE,
only installed if you want to open some projects created by MDK Version 4)
 Legacy support for ARM7, ARM9, and Cortex-R processor-based devices. (Available at
www.keil.com/mdk5/legacy)
After the MDK Core installation is complete, the Pack Installer is started automatically, which
allows you to add supplementary Software Packs.

1
Figure 2: Pack Installer

You can uncheck the Show this dialog at startup.

On the right side you will be able to see the Packs that are installed. Make sure you have the
following Packs and they are Up to date

Figure 3: Default Packs Installed

We will be using STM32F1xx in this course, so on the left pane, click STMicroelectronics, and on
the right, the corresponding packs will be listed. On the right side you will be able to see the Packs
that can be installed.

2
Figure 4: STM32 Packs

Click Keil: STM32F1xx_DFP to install the Pack for STM32F1xx series. We will be using
STM32F1xx_DFP v2.2.0

Figure 5: STM32F1xx_DFP Pack Installed

3
D. WORKING WITH A PROJECT

Aim:
1. Understanding the Keil working environment by a Project.
2. Simulate and Debug program by running or step tracing the program.
3. View and trace the run-time values of registers, and memory during Simulation or Debug
process.
4. Show the output by Debug (printf) Viewer

Details:
1. Download the HW1.zip from the course website and unzip it.
2. Open Keil. Go Project  Open Project... Navigate to the project file for this homework,
the project file should be under …/HW1/Project/RVMDK/HW1.uvprojx.

Figure 6: Open HW1 Project

3. After you opened the Project, the Keil should look like this.

Project
Window

Code
Window

Output
Window

Figure 7: HW1 Project

4
4. Right click on the Project Window  HW1 You should be able to see Options for Target
‘HW1’ as shown and click on it.

Figure 8: Options for Target HW1

5. It should be in the Target Tab as shown, make sure it is STM32F103ZE

Figure 9: Options for Target HW1: Target Tab

5
6. Go to Debug Tab, make sure you are using Simulator as shown

Figure 10: Options for Target HW1: Debug Tab

7. Go to C/C++ Tab, make sure Optimization is set to Level 0 (-O0) as shown, then click OK
to close the Option Window

Figure 11: Options for Target HW1: C/C++ Tab

6
8. To Build the Project, Click Project  Rebuild all target files. The status will be shown at
the Output Window

Figure 12: Building the Project

Figure 13: Build Output

9. Modify your main.c such that it will give out 0 Error(s), 0 Warning (s), record the Program
Size: Code___________ . (In above example, it is 1232)

7
10. Now you can start simulating your project by clicking Project  Start/Stop Debug Session.
It will prompt out a window saying
EVALUTION MODE
Running with Code Size Limit: 32K
just click OK.

Figure 14: Start Debug Session

11. You can see now the cursor start at main(). At the Tool bar below the menu, there are
several keys, you can use these keys to run your program.

Key Function
Step
Step Over
Step out
Run to Cursor Line
Run
Figure 15: Debugging Keys

These keys have the corresponding short-cut from the Project. You can check the short-cut by
choosing the Debug in the Menu.

8
You can add windows to check the running of the program. You can click
View 
Command Window – Allows you to enter debug commands
Disassembly Window - Shows the program execution in assembly code
Register Windows – Shows the content of the microcontroller registers
Watch Windows  Watch 1 – You can add specific variable in C program
Memory Windows  Memory 1 – You can add specific address of memory
Serial Windows  Debug (printf) Viewer – Allows you to view printf outputs

Figure 16: View Menu

9
12. You will see the following screen

Disassembly
Window

Source Code Window

Register Window

Call/ Debug (printf) Viewer/


Command Window Watch/ Memory Window

Figure 17: Debugging Environment

The Call / Debug (prinf) Viewer/ Watch / Memory Window are in tab.

Figure 18: Debug Mode with Different View Enabled

13. Now, you can step by step running the program.


Please note that if you click to the source window, the step by step will be in the source
window.
If you click the disassembly window, the step by step will be in the disassembly window.
10
14. In the Watch 1 window, click on Enter expression, to add variables. For example, to see the
value of variable i

Figure 19: Adding a variable to Watch Window

After you type enter, you will be able to see the values of the variable and location of the
variable (for array) in the microcontroller, also the type of the variable.

15. Now you can add other variables. If you have an array added to watch, (like the stdid in the
Figure 21, you can click the tab on the left of stdid to expand the array.

Figure 20: Adding further variables to Watch Window

16. Change variable i and j to display it in hexadecimal format. Right click  choose
Hexadecimal Format.

Figure 21: Changing Display Value Format

11
17. You can now run the program step by step. Remember to click to the C source window and
click step over.

18. Step your program up to the instruction of the for loop as shown in Figure 23.

Figure 22: Step Over the Program

19. Check the variables that listed in the table of Part 2a from the Worksheet, add them to
Watch 1.

20. So, you should know where the variables being stored now. You can check if it is the case
by viewing the Memory 1

Figure 23: Memory View

21. Type the location that you found in above table into the Go to box, make sure you type the
hexadecimal address (e.g. 0x00123456). See if you can find all the variables in the memory.

12
22. Now, execute the program up to the line printf, you can use Run to Cursor Line

Figure 24: Run to Cursor Line

23. You should be able to see the output in the Debug (printf) Viewer.

Figure 25: Debug (printf) Viewer

24. Answer Part 2 b to e of the Worksheet.

25. Execute your C instructions for AND, OR and XOR, answer Part 3 of the Worksheet

26. Examine the Disassembly Window, answer Part 4 of the Worksheet

13
27. Execute your program up to the line dummy = x + y;

Figure 26: C Execution to dummy = x + y instruction

28. Click on the Register Window, expand the register xPSR

Figure 27: xPSR Register Expanded View

14
29. Click the Disassembly Window, make sure that the yellow arrow pointing to the instruction
ADDS, but not yet execute the instruction

Figure 28: Before Executing ADDS instruction

30. Examine the C and V flags in the xPSR register *BEFORE* executing the ADDS
instruction.

31. Click Step Over in the assembly window and execute the ADDS instruction

Figure 29: After Executing ADDS instruction

32. Examine the C and V flags in the APSR register *AFTER* execution of the ADDS
instruction.

33. Answer Part 5 of the Worksheet

15
34. Continue to execute your program up to line dummy = x – y;

Figure 30: C Execution to dummy = x – y instruction

35. Click the Disassembly Window, make sure that the yellow arrow pointing to the instruction
SUBS, but not yet execute the instruction

Figure 31: Before Executing SUBS instruction

36. Examine the C and V flags in the APSR register *BEFORE* execution of the SUBS
instruction.

37. Click Step Over in the assembly window and execute the SUBS instruction

Figure 32: After Executing ADDS instruction

38. Examine the C and V flags in the APSR register *AFTER* execution of the SUBS
instruction.

39. Answer Part 6 of the Worksheet


16
40. You can now stop the debugging process by clicking Project  Start/Stop Debug Session.

41. Now go back on the Project Window Right click on the Project Window  HW1 You
should be able to see Options for Target ‘HW1’ as shown and click on it.

Figure 33: Modifying Project Option

42. Go to C/C++ Tab, make sure Optimization is set to Level 2 (-O2) as shown, then click OK
to close the Option Window

Figure 34: Modifying Optimization Option

43. Re-build your project again by clicking Project  Rebuild all target files, pay attention to
the Build output.

44. Record the Program Size: Code___________ on Part 7 of the Worksheet

45. Is the code size larger or smaller? Can you figure out where is the optimization being done?
(Hint: Check the assembly instructions.)

17
E. EXTENSIVE LEARNING PART (OPTIONAL)

As we are working with the compiler, there is nothing we can control how the compiler translates
our C code to assembly codes.

In the HW1 program, you might think we can use dummy = i + j; and dummy = i – j; instead of
using dummy = x + y; and dummy = x – y;

You can try to change it to use dummy = i + j; and dummy = i – j; and restart from Step 28 to see
the result. (Remember to change back your Optimization to Level 0)

18

You might also like