Setting breakpoints is a very effective way to debug programs online. Combined with step-by-step debugging, it allows for quick problem identification. However, sometimes manually setting breakpoints can be inconvenient.
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 case is when a variable is changed during program execution, and it is difficult to locate where it happened; manually setting breakpoints may not be effective. This is where some advanced breakpoint techniques come into play.1. Setting Breakpoint Hit CountTaking the following program as an example, we will stop the program at the tenth iteration of the loop.First, manually set a breakpoint in the loop:
Click on theDebug menu, and selectBreakPoints:
This will pop up the following menu:
The topmost breakpoint is the one set manually. Double-click this breakpoint, and you will see that the Expression will display the information of this breakpoint. Change the Count value to 10 and click Define, then close this window.Here’s an explanation:Expression is the condition for the breakpoint. You can see that the manually set breakpoint ends with 3, indicating line 123 in the main.c file. Basic operators such as >, <, ==, !=, etc. are supported here.Count indicates how many times the program runs before hitting the breakpoint; manually set breakpoints usually have a Count of 1.Command is the command executed 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 waits until the10th iteration to stop:
2. Variable Watchpoint BreakpointsAdd the variable to the Watch window, right-click and select Set Access BreakPoint at xxx. The same menu will pop up:
Select the Access method as Read or Write, set the Count value, and click Define. Here, select 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 BreakpointsSimilar 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, the program will stop when AD==10.
4. Print Information
Select a manually set breakpoint, then enter printf() under Command. When the program reaches the breakpoint, it will not stop but will print the set information in the Command window.

5. Conditional Print InformationYou can also combine steps 3 and 4 as shown below:
Then when AD==10, the program will not break, but will print information in the Command window.There are many other advanced uses of breakpoints, with different expressions and commands, etc. For specifics, please refer to the Keil user help documentation.
Author: Mr. ZhangSource: Embedded Technology DevelopmentCopyright belongs to the original author. If there is any infringement, please contact for deletion.