ATTiny44a variable is not setting during debug

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

I have a sensor that goes through and sets variables in a structure and then writes it to memory.  The sensor takes to readings and creates some states based on the values.  The odd part is that the "Double" and "Close" are written and shown in the debugger but it always jumps over single and will not set it.   

 

The code looks like this:

Data_Struct.Double = ADC_Two + Buffer;
Data_Struct.Single = ADC_One + ((ADC_Two-ADC_One)/2);
Data_Struct.Close = ADC_One = ((ADC_Two-ADC_One)/2);
SaveRAMImageToEeprom();

  

Any thoughts?  I tried to change optimizer and debugging setting.  I have tried the mrrelax and looked at the disassembly.  I cann't figure out why this is being skipped.  I even tried to make new variables to break out the arthimetic and the new variables are not skipped for the math but cannot be written to that space.  

 

Thanks.

 

 

 

This topic has a solution.
Last Edited: Mon. Feb 17, 2020 - 02:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make a small, complete project that demos the problem and post it so we can examine the issue, please.

In most cases doing the above, you will find your problem, but if not, someone may be able to help.

BTY: you should use -Og optimization when debugging.

Jim

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

I will do some pseudo-code of the issue.  It is a 1000 line program so just down to the issue. 

typedef struct
{
	U16 ADC_One,
	U16 ADC_Two,
	U16 Region_Double,
	U16 Region_Single,
	U16 Region_Zero
} Data_Struct;

U8 button_counter = 0;

int main(void)
{
	while(1){
		if(button_pressed){
			if(!button_Counter){
				Data_Struct.ADC_One = ADC;				
				button_Counter++;				
			}else{
				Data_Struct.ADC_Two = ADC;
				Data_Struct.Region_Double = Data_Struct.ADC_Two + 100;
				Data_Struct.Region_Single = Data_Struct.ADC_One + ((Data_Struct.ADC_Two - Data_Struct.ADC.One)/2);
				Data_Struct.Region_Zero = Data_Struct.ADC_One - ((Data_Struct.ADC_Two - Data_Struct.ADC.One)/2)
				button_counter = 0;
			} //button_Counter
		} //button_Pressed
	} //while
} //main

There is a button press to two the microcontroller to ADC points and form regions for the device.  The ADC_One and ADC_Two write are in memory no problem.  The Region_Double works but then it skips the single and zero.  Not sure why that happens.  

 

 

 

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

Also if I do even Data_Struct.Region_Single = 0xFFFF, it just skips it.  I can't even straight set it.  

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

Well pseudo-code can't be debugged, so zip up your project and post it so some one with AS7 can try to debug it for you.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

cl10Greg wrote:

typedef struct
{
	U16 ADC_One,
	U16 ADC_Two,
	U16 Region_Double,
	U16 Region_Single,
	U16 Region_Zero
} Data_Struct;

 

You have defined a struct type.

You haven't created a variable of that type.

 

Is this what you did in your actual program?

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

Chuck99 wrote:

Is this what you did in your actual program?

Clearly not! It's not even close to being build able

OPs example is riddled with typos missing semi-colons and errors. The commas in the struct definition give it away.

 

Also. After correction that code extract will work so ignore the debugger stepping on optimised code. Your while(1) loop isn't helping with that.

Instead print the values on LCD or examine over UART. In essence do something that actually uses those Region variables.

 

Last Edited: Fri. Feb 14, 2020 - 08:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cl10Greg wrote:
It is a 1000 line program

That's why Jim suggested that you write a small, complete, buildable project which demonstrates the problem.

 

and note:

 

ki0bk wrote:
In most cases doing the above, you will find your problem

This is, indeed, very often the case - so the exercise is well worth it!

 

For an example, see: https://www.avrfreaks.net/commen...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
typedef struct
{
	U16 ADC_One,
	U16 ADC_Two,
	U16 Region_Double,
	U16 Region_Single,
	U16 Region_Zero
} Data_Struct;

As chuck says you can't just define a type then treat it as a variable and assign to it - there is no instantiation. The quick fix here is to simply drop the "typedef" so it's simply:

struct
{
	U16 ADC_One,
	U16 ADC_Two,
	U16 Region_Double,
	U16 Region_Single,
	U16 Region_Zero
} Data_Struct;

that simply instantiates the thing.

As to the original problem two thoughts occur:

 

1) if the compiler is smart and it could see that there's an assignment to ".Single" but nothing after that ever access it then it could be smart enough to not do the assignment in the first place. So is that the case? If it is then what happens if you make the entire struct instance "volatile"?

 

2) the optimizer does not have to generate code in the order that you write the lines. If it suits it to set ".Single" in another place, either before or after where it is setting the other .struct fields it is quite at liberty to do this. So study the generated Asm and look at the wider context - is the memory location that represents Data_Struct.Single ever accessed?

 

Also how are you actually observing this? Are you using a debugger watch window? Sometimes debuggers fail and may not be showing reality. A much more "raw" view is to find out what &Data_Struct is and then set a Memory Window on that location and then simply watch changes in the raw data and forget what the debugger is showing you as its own interpretation of the same. Do the two raw bytes that represent ".Single" ever change?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So thanks for all the thoughtful response.  My coworker swapped my screen to be vertical for the last few day of design and hid a few issues.  

 

  1. Issue one, making the struct variable as volatile makes the write possible.
  2. Issue two, made sure it was in debugging mode
  3. Issue three, the best one of all.  I did a variable find and replace and it brought a semicolon to the end of the if statement.  Face smash.

 

I understand the frustration and will make a better effort to do full code examples and thanks for your time and effort