Tips for Using MDK5

Scan the QR code to follow Chip Dynamics, and say goodbye to “chip” bottlenecks!

Tips for Using MDK5Tips for Using MDK5Search WeChatTips for Using MDK5Chip DynamicsTips for Using MDK5

Next, we will introduce some practical tips for using the MDK5 software during the code editing and writing process. These tips can effectively improve development efficiency, and we recommend that everyone focus on mastering them and deepen understanding and memory through practical operation.

Text Beautification

Text beautification mainly involves setting the colors and fonts of keywords, comments, numbers, etc. The default settings in MDK show that the colors of keywords and comments are not very appealing, but MDK provides us with the functionality to customize font colors. We can click on the Tips for Using MDK5(Configuration dialog) to pop up the interface as shown in the figure:

Tips for Using MDK5

In this dialog,

  • First, set Encoding to: Chinese GB2312 (Simplified), then set Tab size to: 4. This better supports Simplified Chinese (otherwise, when copied elsewhere, the Chinese may appear as a bunch of question marks), and the TAB spacing is set to 4 units.

  • Then, select the Colors & Fonts tab, where we can set our own code font and color. Since we are using C language, select: C/C++ Editor Files under Window, and you will see the corresponding elements on the right side. As shown in the figure:

Tips for Using MDK5

Then click on each element to change it to your preferred color (note to double-click, and sometimes it may require multiple settings to take effect due to a bug in MDK), and you can also set the font type and size in the Font section. After setting, click OK, and you will see the modified result in the main interface. For example, the display effect of my modified code is shown in the figure:

Tips for Using MDK5

This looks much better than the initial effect. The font size can be adjusted by holding down: ctrl + mouse scroll wheel to zoom in or out, or you can also set the font size in the configuration interface mentioned earlier.

Those of you who are observant may notice that the code above contains a u32, which is still black. This is a user-defined keyword, and why it does not display in blue (assuming we have already set the user-defined keyword color to blue) is because we need to return to the configuration dialog, but this time we need to select the User Keywords tab, and similarly select: C/C++ Editor Files, then enter your defined keywords in the User Keywords dialog below, as shown in the figure:

Tips for Using MDK5

We defined the keywords u8, u16, and u32, so in future code editing, whenever these three keywords appear, they will definitely turn blue. Click OK, and return to the main interface to see that u8 has turned blue.

Tips for Using MDK5Syntax Checking & Code Suggestions

Versions above MDK4.70 have added code suggestions and dynamic syntax checking features, making the MDK editor increasingly user-friendly. Here, we will briefly discuss how to set it up. Again, click on Tips for Using MDK5 to open the configuration dialog, select the Text Completion tab, as shown in the figure:

Tips for Using MDK5

  • Struct/Class Members, used to enable structure/class member suggestion functionality.

  • Function Parameters, used to enable function parameter suggestion functionality.

  • Symbols after xx characters, used to enable code suggestion functionality, which prompts matching content (such as function names, structure names, variable names, etc.) after entering a certain number of characters. The default setting is to start suggesting after 3 characters, as shown in the figure:

Tips for Using MDK5

Dynamic Syntax Checking is used to enable dynamic syntax checking. For example, if there are syntax errors in the code being written, an icon will appear in front of the corresponding line, as shown in the figure:

Tips for Using MDK5

If a warning appears, an icon will appear as well, and hovering the mouse cursor over the icon will prompt the reason for the error/warning, as shown in the figure:

Tips for Using MDK5

These features are very helpful for writing code, speeding up the coding process, and promptly identifying various issues.

However, I would like to remind everyone that the dynamic syntax checking feature may sometimes produce false positives (for example, in sys.c, there are many false positives). You can ignore them as long as the code compiles successfully (0 errors, 0 warnings); such syntax false positives can generally be ignored.

Code Editing Tips

Here are a few commonly used tips that can greatly facilitate our code editing, and I believe they will be helpful for your coding.

The Magic of the TAB Key

The first tip to introduce is the use of the TAB key. This key is used for spacing in many compilers, moving a few spaces with each press. If you frequently write programs, you are certainly familiar with this key. However, the TAB key in MDK is different from that in general compilers, being similar to that in C++. The TAB key in MDK supports block operations, allowing you to shift a block of code to the right by a fixed number of spaces, or shift it to the left using SHIFT + TAB.

Assuming we have the interrupt response function for Serial Port 1 as shown:

Tips for Using MDK5

Such code is certainly not appealing. This is just a short 30 lines of code; if your code has thousands of lines, it would be quite overwhelming. When faced with such code, we can use the magic of the TAB key to quickly modify it to a more standardized code format.

Select a block and press the TAB key, and you will see the entire block of code shift to the right by a certain distance, as shown in the figure:

Tips for Using MDK5

Continue to select multiple times and press the TAB key several times to quickly achieve a standardized code format. The final effect is shown in the figure:

Tips for Using MDK5

After such organization, the entire code becomes much more orderly and looks much more comfortable.

Quickly Locate Function/Variable Definitions

Having introduced the function of the TAB key, we will now discuss how to quickly view where a function or variable is defined.

When debugging or writing code, you may want to see where a certain function is defined and what its contents are, or you may want to see where a certain variable or array is defined. Especially when debugging code or looking at someone else’s code, if the compiler does not have a quick locating function, you can only search slowly by yourself. If the code volume is small, it’s manageable, but if the code volume is large, it can be frustrating and may take a long time to find where the function is defined. The MDK model provides such a quick locating function. Just place the cursor over the function/variable (xxx) you want to view (xxx being the name of the function or variable), then right-click to bring up the menu as shown in the figure:

Tips for Using MDK5

In the figure, we find the option Go to Definition Of ‘STM32_Clock_Init’, and clicking it will quickly jump to the definition of the STM32_Clock_Init function (note that you must check the Browse Information option in the Output tab of Options for Target, then compile, and then locate; otherwise, it cannot be located!). As shown in the figure:

Tips for Using MDK5

For variables, we can also use this operation to quickly locate where the variable is defined, greatly shortening the time spent searching for code. Observant readers may notice that there is a similar option above, which is Go to Reference To ‘STM32_Clock_Init’. This is a quick way to jump to where the function is declared, which is sometimes useful, but not as frequently used as the former.

Many times, after using Go to Definition/Reference to view the definition/declaration of a function/variable, we want to return to the previous code to continue viewing. At this point, we can quickly return to the previous position using the Tips for Using MDK5 button (Back to previous position) on the IDE. This button is very useful!

Quick Commenting and Uncommenting

Next, we will introduce the methods for quickly commenting and uncommenting code. When debugging code, you may want to comment out a section of code to see the execution situation. MDK provides such a quick commenting/uncommenting block code functionality, which is also achieved through the right-click. This operation is quite simple: first select the code area you want to comment, then right-click, and choose Advanced → Comment Selection.

Taking the STM32_Clock_Init function as an example, if I want to comment out the code in the selected area as shown in the figure:

Tips for Using MDK5

After selecting, we just need to right-click, then choose Advanced → Comment Selection to comment out this segment of code. The result after executing this operation is shown in the figure:

Tips for Using MDK5

This quickly comments out a block of code, and at times, we may want to quickly uncomment this commented code. MDK also provides this functionality. Similar to commenting, first select the commented area, then right-click → Advanced, but here we choose Uncomment Selection.

Other Tips

In addition to the commonly used tips introduced earlier, here are a few other small tips that I hope will enhance your coding experience.

Quickly Open Header Files

Place the cursor over the reference header file you want to open, then right-click and select Open Document “XXX” to quickly open this file (XXX is the name of the header file you want to open). As shown in the figure:

Tips for Using MDK5Find and Replace Function

This is similar to the replace function in many document operations like WORD.

The shortcut key for find and replace in MDK is “CTRL + H”. Just press this button to bring up the interface as shown in the figure:

Tips for Using MDK5

This replace function is very useful at times, and its usage is similar to that of other editing tools or compilers, which I believe everyone is familiar with.

Cross-File Search Function

First, double-click the function/variable name you want to find (here we still take the system clock initialization function: STM32_Clock_Init as an example), then click on the IDE to bring up the dialog as shown in the figure:

Tips for Using MDK5

Click Find All, and MDK will help you find all files containing the Stm32_Clock_Init field and list their locations, as shown in the figure:

Tips for Using MDK5

This method is very convenient for finding various functions/variables, and you can limit the search range (for example, only searching .c files and .h files, etc.), making it a very practical tip.

Tips for Using MDK5Tips for Using MDK5

● Enumeration Types: The “human language translator” of C language, turning code from “heavenly books” into “conversational language”

● A Complete Guide to C Language Unions: Memory Reuse, Bit Manipulation, Type Punning, All in One!

● Practical C Language Tips: Detailed Explanation of Structure Alignment

● C Language Structures: The Data Packing Wizard

If you find this article helpful, please click “Like”, “Share”, “Recommend”!

Leave a Comment