Hi all, I needed also everyone's advice on saving some data!

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

Hi again everyone,

In my project I am reading some switches as inputs,
these switches are the outputs of Hardware Finite State Machine.
I am reading them every time they get changed (This generate an interrupt which cause some main function to read what the current value of that changed switch is.)
if MCU reads logic zero on that state it means it's corresponding
switch was OFF else if Logic High then was ON
In my project i want to read a switch and save whatever value of that switch into a new variable called Val_1 then i would wait 2 mins and get another reading for the same switch this 2nd reading is also saved into Val_2 (This is to check even though it's not 100% guaranteed) that the switch hasn't been changed within previous 2 minutes

then what i would do is to compare the two values as soon as i read the 2nd reading this was just a pseudo-code.

if(PINB&(switch)) // let assume that switch's been defined to some input pin
 {
  A:  Val_1 = (PINB&(switch)) ;
 }

2 mins later...

if(PINB&(switch)) 
 {
   Val_2 = (PINB&(switch)) ;
 }

Then MCU would compare the two if they match then it would conclude that the final value of that switch is whatever is stored into Val_2 then it would load Val_2 into another new variable called D_val. else the switch values don't match, the switch might have been physically changed so the process would be repeated until we find the time when the switch values are the same within some given 2 mins!
(again this doesn't guarantee that switch didn't have been changed within 2 minutes, as it can be switches twice before we read the 2nd value which MCU will still find the statement to be true, however according to my application within 2 minute isn't very probable and even if it was to happen as we are reading from Finite state machine, the system ha got some feedback so when MCU would use these false current data. they would turn back again as input which we can find them true in other 2 mins time period. ( anyway that was not the source of my problem)

if(Val_2==Val_1) //switch's been defined for some pin number;
 {
   D_Val = Val_2 ;
 }
else
{ 
 goto A;
}

The Purpose of D_Val is also to be used by some other functions (but when some Ext ISR fires) to write to some output switches accordingly.

My question was that when the WDT resets (I am having a WDT reseting periodically in about every 2 secs)

I know Ram isn't cleared as a result of a CPU reset but might in some cases
some of these data be lost? when WDT resets? and if so what could i do to prevent the clearing of the heap, so D_val isn't being volatile as some functions will need to use it later? (these functions are being triggered to run by some external event so they won't periodically be executed along with the main routine().

Many Thanks Folks
Regards.

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

You now have 49 posts - surely you understand that the Tutorial Forum is for completed articles that teach others by now? I'll move this to AVR Forum

BTW a couple of points:

If it's just two stage history you are comparing then explore the use of XOR (^ in C) which is very useful for identifying bits that have changed between two bytes. If the history needs to be "deeper" than 2 levels use an array (i.e. Val[N]). As an example of XOR suppose you have a variable (PIN input) that reads:

a = 0b10110100

then after a while you read again and it is:

b = 0b10100101

As a human you can probably see that bit 4 changed from 1 to 0 and bit 0 changed from 0 to 1? See what XOR makes of this:

c = a ^ b
c = 0b00010001

The 1 bits in position 4 and 0 show it was those two bits that changed.

As for preserving variables over a watchdog reset. This will depend on what your C compiler does at start up. I think from reading your previous posts you *may* be using GCC in which case it has a section called .noinit which, as the name suggests, can contain variables that must not be initialised by the C compiler when the code restarts after any kind of reset (such as WDRF). Any other global variables (or statics) in .data or .bss will either be reinitialised or wiped to 0 in accordance with the C standard by the C run time that runs before main().

If you do use GCC then .noinit and how it's used is explained on this page of the user manual:

http://www.nongnu.org/avr-libc/u...

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

This is a poor approach. It would be better to start a timer from 0 anytime any switch was changed. Then if the timer reaches 2 minutes, it is guaranteed that no switch has changed within the two minute period. It also means that you only have to read the actual value of the switches at the end of the two minutes with no need to compare to anything.

Regards,
Steve A.

The Board helps those that help themselves.

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

Thanks you both

i did appreciate your advises.

@Cliff, you are right i am using GCC C complier.
I will have a read on .noinit files

Cheers

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

@ Koshchi

I am now thinking using some PCINT pins with probably Timer overflow in the code and everytime i get a reading to that switch the timer would start counting upto 2 mins) if there happens another reading to the same switch during this 2 mins the then i would reset TCNT to start over and like you advised if 2 mins ever gets elapsed then, there would have been no changes on that switch?

@ Clawson,

Ofcourse i knew about Xor bitwise, and Yes Xor'ing Val_1 with Val_2 could have been of much help.
but also my approach wasn't too bad either, it was probably poor like Koshchi mentioned as i was wasting another byte for an extra variable which wasn't really needed (since i could use a timer and just one variable to do the same job).