Master MDK Debugging: A Step‑by‑Step Guide for STM32 Development
This tutorial walks you through downloading the STM32F103C8T6 template project, configuring Keil MDK for simulation, using the debugger’s control buttons and windows, measuring function execution time, and leveraging watch, memory, and peripheral views to efficiently debug embedded code.
1. Source Code and Prerequisites
The required STM32F103C8T6 template project, MDK5 installer, and firmware package are provided via a free download link (add WeChat "0108" to obtain). Beginners can follow a separate quick‑start guide to set up the STM32 development environment and learn five methods for flashing code.
2. MDK Simulation Debug Configuration
Open Keil MDK5, create or load a project, then configure the following options:
Open Options for Target (the "magic wand").
In the C/C++ tab set the optimization level to Level 0.
In the Debug tab enable Load Application at Startup and Run to main.
Under Settings verify the debugger (e.g., ST‑Link) is selected; the device and serial number will appear when the hardware is connected.
In the Utilities tab check Use Debug Driver and confirm.
These settings ensure the program is automatically downloaded to the MCU before each debug session.
3. Debug Control Buttons
After compiling, click the magnifying‑glass button to start simulation, then use the toolbar buttons (left to right): Reset, Run, Stop, Step Into (F11), Step Over (F10), Step Out (Ctrl+F11), Run to Cursor (Ctrl+F10), Show Next Statement.
Using a blinking‑LED example on an STM32F103C8T6 board, the tutorial demonstrates setting breakpoints, running to them, and observing LED behavior at each step.
4. Measuring Execution Time of Code Segments
To verify a delay function, open the Debug → Settings → Trace tab and set the clock frequency to 72 MHz. Run to a specific line (e.g., line 20) using Run to Cursor, then execute the delay function with Step Over. The Sec field shows the elapsed time, confirming the 500 ms delay.
5. Toolbar Windows Overview
Command Window : displays print output and accepts commands.
Disassembly Window : shows assembly code and the next instruction pointer.
Symbols Window : lists symbol information (not covered in depth).
Registers Window : visualizes core registers and runtime counters.
Call Stack Window : displays function call hierarchy and local variables.
Watch Window : monitors variable addresses and values; can set access breakpoints to halt execution on read/write.
Memory Window : inspects memory addresses and values (e.g., viewing an array of uint8_t).
Serial Window : shows UART output.
Peripheral Window : examines core and peripheral registers such as GPIO.
Examples illustrate adding a global variable int temp; to the Watch window and observing its address and type, as well as viewing an array uint8_t temp[10] in the Memory window.
6. Summary
MDK’s simulation and debugging tools provide a powerful yet beginner‑friendly environment for STM32 development. Proper configuration, understanding of the debugger’s buttons, and effective use of the various windows enable rapid error tracing, accurate timing verification, and deeper insight into program execution.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
