Integer Variable is not SET (when simulator is used)Atmel Studio 7 (Version: 7.0.1645 -)

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all.

I am Alfredo Rábago from Argentina, I am new in this forum.

 

I am trying to use Atmel Studio 7 (my first project on it), I have used Eclipse several times but now I am trying to use Atmel Studio because I bought an Atmel-Ice.

 

I was looking for if this problem had been reported, but I didn't find anything.

 

 

My project is using an Atmega328p, but the issue that I saw so far (maybe my Atmel Studio is not set correctly) is the follow:

 

this is the skeleton of the program......

 

 

#include <avr/io.h>

#include <avr/interrupt.h>

#include <util/delay.h>

#include "TWI_driver.h"

#include <stdio.h>

#include <string.h>

#include <stdbool.h>

 

#define F_CPU 8000000UL

 

---

---

---

volatile int IntTimerActive=0; //------ this VAR is set to 1 when TIMER0 triggered an ISR

---

---

---

void timer_configuracion_0_ctc(void)

{

---

// Timer0 is set, ctc funtion i used.....

---

}

 

ISR (TIMER0_COMPA_vect)

{

     IntTimerActive=1;    //TIMER0 generated an INT, Flag is set to 1, in Main the MUX configuration can start.

}

 

 

void Mux_configuration(void)

{

---

---

Line N° 184 ---                                //  here IntTimerActive=1, it was set to 1 in ISR Timer0

Line N° 185      IntTimerActive=0;    //  Flag is set to zero, so Timer0 must turn on again, the issue is like as this line is not executed, but I am using the                                                                        simulator and I can                                            see the program running step by step

Line N° 186 ---                               //  here IntTimerActive=1, for some reason, VAR is not equal to 0

---

---

---

---

---

 

}

int main (void)

{

  ---

  ---

  ---

  timer_configuracion_0_ctc;

  ---

  ---

  ---

 while (1)

  {

      if ((IntTimerActive)&(Number_bits_to_be_sentd>0))

          Mux_configuration(); // if there is data to be send and Timer0 set the flag to 1, MUXs must be configured  

      if (........)

         other_routine

  

  }  

 

In few words, when the program is into Mux_configuration ruotine on line N° 184 IntTimerActive=1, in line N° 185 IntTimerActive is set to zero, but in line N° 186 IntTimerActive has 1 instead 0.

 

Do you know why that behavior is happening?

 

I am running the program using the Debug function (F5, F10, F11), the Program is not running over the Atmega328P, it is just a simulation (Atmel Studio feature)

Please check the files attached i tryed to show states before and after the line 185 is executed.

 

I loaded the elf file in ISIS-Proteus, but in this test I added an while (1){} after line N° 101, and when I check the Variable on proteus the VAR has 1 instead 0, it means this issue is not just a simulation issue.....

 

Any comment or advice are welcome.

 

Thanks

Alfredo

 

Attachment(s): 

This topic has a solution.
Last Edited: Tue. Mar 6, 2018 - 05:08 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how sure are you that in the mean time the ISR did not trigger?

Have not used the simulator in a long time, but you could try to set a breakpoint and then see if the simulator respects it. If so put a breakpoint inside the ISR and see if by any chance it fires right after you made the flag low.

Also note that it might be that to set a volatile variable to 0 it will take IIRC at least 3 instructions. (get var to register, update register and store register to var... although the first instruction might be skipped as it has no function, but that depends on the compiler)

 

you could also disable the timer interrupt and see what happens to the variable then.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When simulating right click the code and "Goto Disassembly" (which actually shows you a mixed view of C and Asm. Now when you single step it is one opcode at a time not one whole C statement at a time. Follow it across the code of lines 184..186 and see what's actually going on "underneath". You will probably see an LDI on R24 and then STS to the "IntTimerActive" location in RAM.

 

Post a picture - also the LSS file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What was in R1 as you stepped over the two STS?

 

Also why are you using a 2 byte variable for a 1 bit flag?

 

On the surface it looks like you have "run timers in background" and it executed the ISR code between stepping foreground code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CLiff, it might be just oneinstruction.

IIRC teh compiler always sets 1 register to 0 thought it was R1.

as the variable needs to be made 0, it might even be that the only instruction is an sts of R1 to the ram location.

although it then most likely is 2 instructions were the whole int value is to be made 0 (low byte + high byte)

 

but my first guess is also that the interrupt changes the variable back right after the main code set it to 0.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes that was my point entirely. R1 is supposed to contain 0x00 but some naughty programmers abuse that (though if it were the case both bytes would be identical).
.
Me, I'd just put a breakpoint in the ISR and see what transpires.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi meslomp.
 

how sure are you that in the mean time the ISR did not trigger?

       I am not surre, but I will test adding a breakpoint into ISR.

 

you could also disable the timer interrupt and see what happens to the variable then.

      ok I will test it too.

 

Thanks for your comments, and I will update the thread.

 

Alfredo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi clawson.

the var has 2 bytes, as you said it used just 1 bit, i will change it for a struct...

 

 

Timer0 is triggering ISR every few instructions, so Timer0 is not set correctly, I will check it now.

 

Timer0 should be working with a 100Khz (I use this clock to move some data from Micro to somes serial/parallel register in order to configure some Mux's)

the Atmega Clock is 8 Mhz....

 

Thanks a lot for your comments, I will update the thread when I have the result os changes

 

br

Alfredo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Gentlemen.

 

Now is working fine (the VAR is set and it remains with the set value).

 

So the issue was: Timer0 was firing INT every 19 clock pulses,  just few C instructions spent it and INT was firing again, so VAR was set in ISR (normally).

 

Thanks a lot for your help, today I learned something else....thanks again.

br

Alfredo.