Using the AVR Studio 5 Simulator and Debugger


AVR SeriesAVR Studio 5 Simulator and Debugger

Dear readers, please note that this is the old website of maxEmbedded. The articles are now no longer supported, updated and maintained. Please visit the new website here and search for this post. Alternatively, you can remove .wordpress from the address bar to reach the new location.

Example: If the website address is http://maxEmbedded.wordpress.com/contact/, then removing .wordpress from it will become http://maxEmbedded.com/contact/.

We apologize for the inconvenience. We just want to give you a better viewing and learning experience! Thanks!

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! 🙂

Advertisements

44 responses to “Using the AVR Studio 5 Simulator and Debugger

  1. Pingback: Using AVR Studio 5 « maxEmbedded·

  2. You talked about “creating a stimuli file”. Can you please explain how a stimuli file can be used in the AVR Studio 5 simulator?

    • To create stimuli file, view this. You can download it and create stimuli files using this. To learn how to use stimuli files during simulation, see if this is helpful.

      PS. Sorry for late reply. I was stuck somewhere.

      • Thanks for that. I was looking for a way to simulate ‘interaction’ while I’m waiting for my actual dev board to arrive.

        Thanks for posting your other tutorials as well. I was reading through the official document files and even though I have experience with programming, the language in the official AVR documents can be quite technical. You explain it in a much more understandable way.

  3. Hi friend,
    how can i debug in case of AT90PWM316 ? i think it doesn’t support, isn’t it ? if there is a way, please reply me. Thanks in advance..:)

    • Well, the AVR Simulator and STK500 don’t support AT90PWM316. However, in-system debugging is possible if you use any one of the following AVR programmers: AVR Dragon, AVR ONE!, JTAGICE3 or JTAGICEmkII.

      PS> Sorry for the late reply, I was stuck somewhere.

  4. Hello ! Can you please explain me where exactly you can choose a Stimulis File in the AVR Studio Version 5 ? You are talking here about the AVR Studio 5 and telling us that its possible to use Stimuli Files. But as far as I know its only possible to use the stimuli files in the old version of AVR Studio (Version 4.18). Maybe I don’t see menu entry or what ever in AVR Studio 5 to choose a Stimuli File, so please help me.

    Thank you very much.

    • Could you please check the previous reply that I had made to CAESAR? I hope that helps.

      PS. This reply wasn’t removed. It was still awaiting moderation.

      • Hello Mayank,
        thank you for your quick reply. I am new to the wordpress system and didnt know this with the moderation, but it makes sense :-). I already checked your reply to CAESARs question, but my friend this only works for AVR Studio 4.xx and not for the AVR Studio 5 or do I understand something wrong ? So my question to you: Are you able to use the stimuli files in the AVR Studio 5 Version (i m talking here about the AVR Studio 5 with the IDE Design/System of Visual Studio 2010…). I am a little bit confused…

        thank you !

        • Hello opcode
          I havent worked with stimuli files yet! Actually I never required to do so.. So, I am sorry that I wouldn’t be able to help you out with it. If you get to know, then please intimate me, so that I can update this post for other visitors like you.

  5. Hello Mayank,
    no problem mate. As far as I know from the AVR-Freaks forums the AVR Studio developers are working on the stimuli implementation. When I get more informations and I don’t forget, I will send you a message :-).

    Take care mate and many success in your projects.
    opcode

  6. Hi Mayank,
    thanks a lot for your AVR tutorial. They are very helpful, simple therefore easy to understand.

    I was checking the above tutorial with AVR studio 5, device ATmega8, optimization level -O2. I have some different results then you. There happens a change in register PORTB and PINB after every 4 pulses (4 events on counter value) …okay …let me make it simpler …
    when i press continue button for 4 times, when value of PORTB is 0x04, then there appears a change in LSB of PORTB (turns to gray solid block)….then the next change happens when the value of PORTB is 0x08….

    could you please tell me where have a gone wrong? what could be the possible mistake ?

    thanks, awaiting for your reply…
    best regards,
    Milind.

  7. The simulator does’t work. It says “No source available. There is no source code available for the current location.” This happens after the code gets compiled. Hex file also gets generated. Five files are created in the output folder .eep, .elf, .hex, .lss and .map

  8. ok i figured it out. there is a UI display problem. even if i get the above problem, the program goes into debug mode but the yellow arrow is not diaplayed

  9. Hi,
    do you know how to trace assembler functions included as part of the code,
    a have added the -c -Wa,–gstabs , with no success,
    thanks

  10. these tutorials are great.i like these tutorials very much.and will be very happy if you make a tutorial for me. 4 digit (0 to 30) voltmeter on avr atmega8. I shall be very thankfull to you
    thanks……..mr.rizwan

    • Hey Rizwan
      I am sorry, but I don’t write tutorials just for anybody! I only discuss the concepts here. If you are able to understand what I have written here, it shouldn’t be difficult for you to write your own code! For your problem, you can use the ADC of the AVR.

      Regards
      Mayank

  11. Pingback: Using Atmel Studio 6 « maxEmbedded·

    • _delay_ms() is very heavily dependent upon frequency. The outcome is the same if you have specified the correct FCPU value.
      For example, if your FCPU is 8MHz, and in the software, you have mentioned that F_CPU = 8MHz, then _delay_ms(1000) will give you a delay of 1sec. But if you provide a wrong F_CPU value, say you mention 16MHz instead of 8MHz, then the delay will not be as desired. This is because the compiler optimizes your code to work with 16MHz, but in hardware, since it is 8MHz, you will get a wrong delay.

      • /*

        Description: Program to demonstrate the use of Seven

        Segment Displays In Multiplexed Mode.
        ________________________________________________________

        */

        #include
        #include
        #include

        #define SEVEN_SEGMENT_PORT PORTD
        #define SEVEN_SEGMENT_DDR DDRD

        volatile uint8_t digits[3];

        void SevenSegment(uint8_t n,uint8_t dp)
        {
        /*
        This function writes a digits given by n to the display
        the decimal point is displayed if dp=1

        Note:

        n must be less than 9
        */
        if(n999) return;

        while(num)
        {
        digits[i]=num%10;
        i++;

        num=num/10;
        }
        for(j=i;j<3;j++) digits[j]=0;
        }

        void main()
        {
        uint16_t i;

        //Port B[2,1,0] as out put
        DDRB|=0b00000111;

        PORTB=0b00000110;

        //Port D
        SEVEN_SEGMENT_DDR=0XFF;

        //Turn off all segments

        SEVEN_SEGMENT_PORT=0XFF;

        //Infinite loop
        //Print a number from 1 to 999
        while(1)
        {
        for(i=0;i<1000;i++)
        {
        Print(i);
        PORTB=~(1<<0); // active display zero
        SevenSegment(digits[0],0);
        _delay_ms(1000) ;
        PORTB=~(1<<1); // active display 1
        SevenSegment(digits[1],0);
        _delay_ms(1000) ;
        PORTB=~(1<<2); // active display 2
        SevenSegment(digits[2],0);
        _delay_ms(1000) ;
        }

        }
        }

        the following code when i simulated it in proteus at frequency 8mega hz and configuire the atmega 32 in proteus and F_CPU in avr studio to 8 mega hz

        the delaying time is too small in simulation

        but when i used 1 mega hz the time became right
        so what is the problem now with the delay ??

        • Hmm.. there may be some trouble in the timing of the simulation then. I am sure that it will work fine in hardware.

  12. hi,
    I am very new to the AVR family ,i am using AT X-mega256a3b. I have started with small blinking program i am using AVR studio 6.0 and AVRISP mkii debugger in isp mode.When the chip get programmed the led goes to high and then it is not going low(ie not blinkin) always in high state.
    Can you pls heelp me to figure out this problem

    #include
    #include

    #ifndef F_CPU
    #define F_CPU 16000000UL
    #endif

    int main(void)
    {
    PORTA.DIRSET = 0x0f;
    while(1)
    {
    PORTA.OUTSET= 0x0f;
    _delay_ms(1);
    PORTA.OUTSET= 0x00;
    _delay_ms(1);

    }
    }

    • If you have implemented it in hardware, then you will not be able to see the LED blinking at such a high rate. A delay of 1ms is too low for the LED to respond as well as for your eyes to see and distinguish. Try increasing the delay to around 200ms and check.

      • HI,
        Thank you for your reply ,the problem has been identified that the registers such as PORTA.DIR and PORTA.OUT has to be used instead of previous.

  13. Hi Mayank

    Above counter++ example working great in AVR Debugging & Simulator I/O. I’am using Atmel Studio 6 and Atmega32a uC. But when I’am using _delay_ms(200); in this code, It’s simulator not working. What’s wrong with my code?

    #ifndef F_CPU
    #define F_CPU 16000000UL
    #endif

    #include <avr/io.h>

    int main(void)
    {
    uint8_t counter;
    DDRB = 0xFF;
    while(1)
    {
    counter++;
    PORTB = counter;
    _delay_ms(200); // I add only this code. This code is working with my Atmega31a uC
    // But not working with AVR Debugging & Simulator in Studio 6.
    }
    }

    Please give me any code example of _delay_ms for AVR Debugging & Simulator.

    Mayank your website is Great, very Useful & Readable.

    Thanks,
    prashant

  14. For some strange reason my simulator doesn’t works right.

    For instance, the changes in bits aren’t shown correctly. The red block doesn’t shows when data is 0x01. The first change is visible only after 4 iterations. That is, when data changes to 0x04. This is a pattern and it remains the same. No matter how many times I refresh the values. Debug the code. Rebuilt it. The result remains the same. Only thing left is to try to close application. Create a fresh project and re-write the code then re-do the whole process.

    I’ll follow-up when I’m done.

    • Problem Fixed.

      Its quite stupid and embarassing though 😛
      I realized from the start that the byte row was slightly small. I counted the bits then to find out that I was looking at 6 bits 😀

      This was because by default the I/O pane was short in width. I widened the pane and viola the remaining hidden bits(bit 0 and bit 1) came into view. So obviously I was taking bit 2 to be bit 0. This was why the change showed up only at 4th iteration.(I mean AFTER the 4th)

      0000
      0001
      0010
      0011
      0100

      Haha…really a n00b joke got played on me 😛

We'd love to hear from you!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s