Skip to main content
Keil5

Debugging in Keil

I previously wrote a brief tutorial on How to Use Keil and hope that you’re now capable of compiling your code. Moving on, if you haven’t noticed already, Keil uVision is also a debugger and can help you debug codes. I left out the debugging portion deliberately in my previous post as it was themed at getting familiar with Keil uVision. Debugging in Keil is simple to understand and also proves to be useful at crucial times.

 

DEBUGGING

As the name suggests, it’s a process of removing bugs from your code. Debugging is not as straight forward as it sounds but one can pretty much get better at it with practice. In fact if you’re smart enough, you’ll tend to debug code (mentally) in real-time as you write it but that kind of alert level is rare throughout the day. In a nutshell, debugging helps you verify your code in a step-wise fashion, monitor each variable along the path, analyze the logical flow, and ultimately perfect the final output.

 

WHY DEBUG?

It’s all over my mind and I can’t seem to ignore it so just let me write it down. While coding, we normally encounter two major types of errors.
 

Syntax Errors

They occur when you forget placing a semicolon (;) after a C/C++ statement, call a variable without declaring it first, use the wrong type of brackets, or use an invalid name for a variable etc – the list goes on. They are easily detected by the compiler and you can rectify them instantly, without thinking twice. So let’s not worry about them.

 

Logical Errors

Suppose you’re expecting the number 124 at the output and something like 4 comes up and you don’t have a slightest clue as to how that happened. It’s confusing and annoying because there is no syntax error and no compilation error but still you’re getting the wrong answer. That’s a logical error – the logic you’re trying to implement for solving the problem has some problem in itself.

This is where you need to check your code step-by-step or in other words, debug it.

 

Debugging in Keil uVision

When you’re done writing your code, compile it. Keil will notify you of all the syntax errors (if any) in the program. Double click the error message and the cursor will automatically move to the exact statement containing the error. Read the error message and make changes accordingly.

 

Keil1
Click the error statement and the cursor will move to the corresponding statement.

When you’re done fixing all the syntax errors, you may proceed to debugging your code. You can do this by using the Debug Menu as shown.

Keil2
The Debug Menu

 

You’re probably using the evaluation version of Keil uVision which has a code size limit of 2K bytes while debugging. It may sound problematic but it’s actually not. As a beginner, you will hardly cross this threshold with your small testing codes. It’s also worth mentioning here that the 2K size limit applies to the generated HEX file. The size of HEX file is way smaller than the actual code size, so you’re safe there for now. Simply click OK button to confirm that you’re aware of this fact.

 

Keil3
The file size limitation in Evaluation Mode.

 

Once the Debugging Mode is active, there are only few and simple things that you need to know about. Here is the list as labelled in the picture below

 

a – Present values of all registers in the micro-controller
b – Run/Execute the program all at once (till break-point)
c – Execute each statement step by step
d – Execute blocks of statement in one go (i-e execute a function block in one step)
e – Execute till the cursor position in code
f – Insert breakpoints (to halt program execution during debugging)

 

Keil4
Debugging Basics

 

These are the few basic terminologies and functionalities in almost all the debugging environments – not just Keil uVision. In addition to these useful features, you can also add a certain variable to watch window for keeping an eye on its value during the whole process. This specifically comes in handy if you’re programming in C language.

 

Keil5
Adding variable to watch list

Using these simple options, you will be able to properly scan through your code and debug it. I hope you grasped the idea and will be able to do so yourself. Good luck!

Faizan

Muhammad Faizan Khan is an Electrical Engineer and a Computer Geek. He is a technical guy and has more than 3 years of experience in Embedded Systems, Digital Logic Design, Computer Programming and everything else that’s logical. He mostly writes tutorials related to microcontrollers for beginners in his blog, PhaseWire.

3 thoughts on “Debugging in Keil

Leave a Reply

Your email address will not be published. Required fields are marked *