Advanced Usage of Breakpoints in Keil Debugging

Setting breakpoints is a very effective way to debug programs online. Combined with step-by-step debugging, it can quickly locate problems. However, sometimes manually setting breakpoints is not very convenient.
For example, if you want to stop at the Nth iteration of a loop, you would have to keep clicking step run until the loop reaches the Nth iteration.
Another example is when a variable is changed during program execution, and it’s difficult to locate where it was modified; manually setting breakpoints doesn’t help in this case. This is where some advanced breakpoint techniques come in handy.

1. Setting Breakpoint Hit Count

Taking the following program as an example, we will stop the program on the tenth iteration of the loop. First, manually set a breakpoint in the loop:

Advanced Usage of Breakpoints in Keil Debugging

Click on the Debug menu and select BreakPoints:

Advanced Usage of Breakpoints in Keil Debugging

The following menu will pop up:

Advanced Usage of Breakpoints in Keil Debugging

The topmost breakpoint is the one set manually. Double-clicking on that breakpoint will show its information in the Expression field. Change the Count value to 10 and click Define, then close the window. Here is an explanation:
Expression is the condition for the breakpoint. You can see that the manually set breakpoint ends with \123, indicating line 123 in the main.c file. This supports basic operators such as >, <, ==, !=, etc.
Count indicates how many times the program runs before hitting the breakpoint. The manually set breakpoints have a Count of 1.
Command indicates the command to execute when the breakpoint is reached, which is empty by default.
With the above settings, run the program. You will see that the program does not stop immediately at the breakpoint but stops at the 10th time:

Advanced Usage of Breakpoints in Keil Debugging

2. Variable Matching Breakpoints

Add the variable to the Watch window, right-click and select Set Access BreakPoint at xxx. The same menu will pop up:

Advanced Usage of Breakpoints in Keil Debugging

Advanced Usage of Breakpoints in Keil Debugging

Select the Access method as Read or Write, set the Count value, and click Define. Here, we choose Write, with a Count value of 4, indicating that the program will stop when the variable is written for the fourth time.

3. Conditional Variable Breakpoints

Similar to the previous step, in the Watch window, right-click the variable and select Set Access BreakPoint at xxx. Check the Access method as Read or Write, delete the original content under Expression, and enter the expression “AD== 10”. Click Define. This way, when AD==10, the program will stop.

Advanced Usage of Breakpoints in Keil Debugging

4. Print Information

Select a manually set breakpoint, then fill in printf() under Command. When the program reaches the breakpoint, it will not stop but will print the set information in the Command window.

Advanced Usage of Breakpoints in Keil Debugging

Advanced Usage of Breakpoints in Keil Debugging

5. Conditional Print Information

You can also combine 3 and 4, as follows:

Advanced Usage of Breakpoints in Keil Debugging

When AD==10, the program does not interrupt and prints information in the Command window.
There are many other advanced usages for breakpoints, with different expressions and commands. For specifics, refer to the Keil user help documentation.

Advanced Usage of Breakpoints in Keil Debugging

END
Source: Embedded Technology Development
Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
I found another open-source serial port tool, it’s amazing!
Graduated with a 450,000 offer, working on chips is really nice!
A skill checklist and upgrade roadmap to become an embedded expert
→ Follow for updates ←

Leave a Comment