Pages

Thursday, 9 August 2012

DEBUGGING IN KEIL

I have already written a brief post on how to use KEIL uVision in which I deliberately left the debugging part out because I wanted to talk about it at a proper time. I think it's now. If you haven't noticed it yet, KEIL uVision is also a debugger and will help you debug many of your codes and programs. The debugging environment of KEIL is very simple to understand and make use of. Today, I will introduce you to it. Ready?

UPDATE: You can also click this link for an updated version of this post.


DEBUGGING

First thing first, if you are wondering what really debugging is then know that it is a process of removing 'bugs' from your code (as the name suggests of course). It is not as easy as it sounds but it gets better with practise. If you're smart enough, you'll debug your codes in your mind as you write it but one can't be that much alert every time. So debugging basically helps you verify your program step by step, check each value along the way, analyse the logical flow, and ultimately the final output.

WHY DO WE NEED DEBUGGING

Perhaps this is important and that's why it's all over my mind. In programming, we face two types of errors.
  • Syntax errors
  • Logical errors

Syntax Errors occur when you forget placing a semicolon after a  C statement, or use the wrong brackets, or use an invalid name for a variable etc. They are very easily detected even by the compiler and you can correct them instantly. So let's not worry about them.

Logical Errors occur when you're expecting 128 at the output and you see something like 4 and you keep wondering where it came from? Now this is the real deal and it's dangerous. There is no syntax error and no compilation error but still you're getting the wrong answer. This means that the logic you're implementing to solve the problem has some problem in itself. 

This is where you need to check your code step by step or debug it.

DEBUGGING IN KEIL uVISION

After you compile your code in KEIL, you're notified of any syntax error in the program. Just double click the error and the cursor will move to the statement containing the error. Correct it accordingly.

Click to enlarge

If you're done with syntax errors and want to debug your program then go to the debugging menu as shown below

Click to enlarge

The KEIL uVISION is in evaluation mode so there is 2K bytes limit on the code you debug but don't worry about it because we won't be writing long codes at the moment.

Click to enlarge

Once you get in the debugging mode, there are only few things that you need to know about. Here is the list as labelled in the picture below
  • a. Values of all registers present on the microcontroller
  • b. Run the program all at once (till breakpoint)
  • c. Execute and show each statement
  • d. Execute blocks of statement in one go
  • e. Jump to the cursor position in code
  • f. Insert breakpoints (where the running of program can be halted)
Click to enlarge

These are the basic tools to use the debugging environment in almost all debuggers. Furthermore, you can add a certain variable to watch window for keeping an eye on its value.  This specifically comes in handy if you're programming in C language.

Click to enlarge

This is all about debugging in KEIL uVISION. It's not difficult to grasp, is it? I hope you've got the idea by now and already making use of it for your own good. 

No comments:

Post a Comment