Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jun 13, 2011 in Atmel AVR, Microcontrollers | 60 comments

Using the AVR Studio 5 Simulator and Debugger

Using the AVR Studio 5 Simulator and Debugger

AVR Series

Hello friends. Since you now aware of creating and building your project using AVR Studio 5, you are in a position to know how to debug and simulate your code using the AVR Simulator. The AVR Studio 5 Simulator has the following features:

  • It supports software emulation of any real AVR device without actually connecting it.
  • It gives access to all the peripherals of the real MCU but no external devices.
  • So if you want to give external signals, you need to do it yourself, either by manually updating the registers or creating a stimuli file.

Now, let’s take the following code example to explain the functionality of the AVR Simulator.

#include <avr/io.h>

int main(void)
{
    uint8_t counter;
    DDRB = 0xFF;
    while(1)
    {
        counter++;    // insert breakpoint here <-----
        PORTB = counter;
    }
}

Debugging the code using Simulator

  • Now, click on the Debug menu and then click on Start Debugging and Break. If initially no debugger is chosen, AVR Studio 5 will ask you to choose a Debug Tool. AVR Simulator is always an option there. Choose it and click OK.
Debug Tool

Debug Tool

  •  After this, debugging starts and halts in the beginning of main(). You can see a yellow arrow mark determining the current executing line.
  • Let’s place a breakpoint in the main and start execution. Highlight the variable counter in counter++, right click it, go to Breakpoint and then click on Insert Breakpoint.
Insert Breakpoint

Insert Breakpoint

  • Now press the play button (F5) or click on Continue from the Debug menu to run to the breakpoint.
  • Now look at the affected registers in the I/O view. If you don’t have the I/O View open, you can select it from the Debug toolbar or from the Debug windows menu.
I/O View

I/O View

  • All the peripheral features are mentioned over here. We can monitor any changes from the software and also manipulate the values to provide input.
  • Now, since the counter changes the value of PORTB, scroll down in the I/O View and click on PORTB.
Choose PORTB in I/O View

Choose PORTB in I/O View

  • Upon clicking PORTB, you can see the three registers assigned for PORTB operations, PINB, DDRB and PORTB. You can also view their current values.
  • A solid block represents ‘1’ whereas a blank block represents ‘0’.
  • Since it the beginning of main(), we defined DDRB = 0xFF, all the blocks are filled. You can also look at its value there.
  • Now, press the play button. The loop iterates once and stops at the breakpoint. You can see that values of PINB and PORTB have changed to 0x01. This is because after one iteration, counter = 1.
Debug in Progress (1)

Debug in Progress (1)

  • The red block indicates that there has been a change in the value of the bit. If it’s a solid red block, a change has been there from 0 to 1. If it’s just a red outline, it’s the other way round.
  • Once again click on play. You will be able to see the following sequences.
Debug in Progress (2)

Debug in Progress (2)

Debug in Progress (3)

Debug in Progress (3)

Debug in Progress (4)

Debug in Progress (4)

  • Now, if you want to change some other registers (apart from the ones changed by the code), simply click on the corresponding register and change its value.
  • Say for example you want to change the value of DDRD. Click on PORTD and then give any value you want. You can also click on the corresponding bits to toggle the values.
Making External Changes

Making External Changes

So now we are done with the basics of AVR Studio 5. There are more advanced features of debugging in AVR Studio 5 which includes In-System Debugging which is a kind of runtime debugging unlike the software emulation that we learnt in this post. However, we are not interested in discussing these concept here as it is possible only with AVR Programmers.

So for now, enjoy! :)

60 Comments

  1. Hi,

    What about Simulating a variable ? How to check that ? using AVR Simulator ?

    Thanks

    • What do you mean by “simulating” a variable?

      • I mean to say how to check the variable value changing in simulator without assigning variable to physical port / resistors ?

        • You can use the “Watch” feature. Google up to learn more! :)

  2. In the code, there isn’t any syntax for PIN register to change then can you please explain why is the value of PIN register changing here?

    • That’s because PORT and PIN essentially refer to the same hardware pin. The one that is chosen depends upon the DDR value.

      • Ohh! Thanks.
        So it means every time I make change to either PIN or PORT both will get affected for any port…am I correct?

  3. Hello, I am having trouble using the watch function to view variable values. I am using your exact example and have entered “counter” into the box, but it gives me an error: “Unable to evaluate expression. Invalid pointer”

    I can’t seem to figure this one out.

    • To add a variable to watch, right click on that variable in the code and click on Watch. In this way the simulator will be able to link the values to your variable and show it.

      • I have tried to simulate and debug using chip atmega8535 but sadly the chip is not supported. Is it really avr 5 dont support the chip or there are some configuration needed to make it work? Thanks

        • I have no idea about this Wayan. Sorry. Please ask your question on avrfreaks.net. It is the official AVR support forum and I’m sure you’ll find an answer there. Thanks!

  4. Hi Max, I ws using AVR Studio4 till now, bt now switched on to Studio5, Pl. tell me how to “re-build” an old existing project in AVR Studio5…? I am not able to “re-build” and modify my existing projects in this Studio5, so each time I have to make a New Project, as the option to re-build my old project does not appear on the “build menu”. pl. give some tips, Thanks regards.

  5. How does the code works?Being specific ,what does counter++ signifies?

  6. I got cleared with my last doubt but how come the value of PINB changing?As PORTB =counter ,only PORTB should change?

  7. hi
    after install AVR studio it shows below error

    AVR Studio was unable to start your debug session.
    Please verify that the debugger is correctly attached to the target and that target power has been switched on. Also verify that the correct device and interface settings have been specified in the project.

    Plz help me

  8. I would like to check if I’m actually running debug mode or not. Is there any compiler directive or pragma?

  9. sir, how to do solve this error lcd.h:no such file or directory so please help

    • It means your compiler couldn’t locate the file. Put them in the same directory and add it to your project.

Leave a Reply

%d bloggers like this: