Variable gets incremented without doing this in the code.

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

I just wonder "what the f***".

What is goind on ?

"count" gets incremented still when it is commented out.

 

I added "WTF" to verify that the program has compiled.

 

Now i see the incremending has stopped...

 

Am i dreaming?

 

#define NANOSECONDS_PER_TIC  500
#define MICS_TO_TICS(mics)( mics * (unsigned long) 1000 / NANOSECONDS_PER_TIC)
#define TICS_TO_MICS(tics)( tics * (unsigned long) NANOSECONDS_PER_TIC / 1000)

void setup(){

        Serial.begin(57600);
        delay(300);

        Serial.println(__DATE__);
        Serial.println(__TIME__);

        //all on pin 4 on ProMicro (ICP1)
        DDRB &= ~(1 << PB0);

        //testpin 6
        DDRD |= (1 << PD7);

        //testpin 2
        DDRD |= (1 << PD1);

        //testpin 3
        DDRD |= (1 << PD0);

        TCCR1A = 0;
        TCCR1B = 0;
        TIMSK1 = 0;
        TCCR1A = 0;

        //bit to one, raising edge!
        TCCR1B |= (1 << ICES1);

        //enable interrupt
        TIMSK1 |= (1 << ICIE1);

        //Timer overflow interrupt enable
        TIMSK1 |= (1 << TOIE1);

        TCCR1B |= (1 << CS11);  // full counter to FFFF / 8 prescaler, so 1 tic is 500ns
}

volatile unsigned int lastICP = 0;
volatile unsigned int currICP;
volatile unsigned long pulseTics;

volatile unsigned long lastPulseTics;

volatile unsigned int overFlowCount = 0;

volatile unsigned int data[3][4] = {0};

void loop(){

        cli();
        unsigned long pulseCopy = pulseTics;
        unsigned long lastPulseTicsCopy = lastPulseTics;
        sei();

        if(abs((signed long)pulseCopy - (signed long)lastPulseTics) < MICS_TO_TICS(150) && abs((signed long)pulseCopy - (signed long)lastPulseTics) > MICS_TO_TICS(5)){
                Serial.println(TICS_TO_MICS(pulseCopy));
        }
        else if(abs((signed long)pulseCopy - (signed long)lastPulseTics) > MICS_TO_TICS(150) ){
                //      Serial.println(TICS_TO_MICS(pulseCopy));
                //      Serial.println("ERROR too high!");
                //
        }

        lastPulseTics = pulseCopy;
}

volatile byte count = 0;

/*
 * ca. 3us 10.01.2019
 * starts about 2us after edge change
 * ca. 10us 14.01.2019 0:36 (ohne delay(); welches zur Besseren Sichtbarkeit fürs oszi ist!)
 * ca. 8us 19.01.2019 21:51
 */
ISR(TIMER1_CAPT_vect){
        byte oldSREG = SREG;

        PORTD |= (1 << PD7);

        //get counter value
        unsigned int currICP = ICR1;

        unsigned int ICPTics = currICP - lastICP;

        lastPulseTics = pulseTics;

        // If currICP is greater that lastICP, overflowCount is true for calculation. In the other case not!
        if(currICP > lastICP){
                pulseTics = ICPTics + (unsigned long) (overFlowCount) * 65535;
        }
        else{
                if(overFlowCount < 2)
                        pulseTics = ICPTics;
                else
                        pulseTics = ICPTics + (unsigned long) (overFlowCount - 1) * 65535;
        }

        data[0][count] = lastICP;
        data[1][count] = currICP;
        data[2][count] = overFlowCount;
        data[3][count] = pulseTics;

        if(abs((signed long)pulseTics - (signed long)lastPulseTics) > MICS_TO_TICS(150) ){

                Serial.println("WTF ERROR too high: ");
                for(int i = 0; i < 3; i++){

                        Serial.print(count);
                        Serial.print(" ");
                        Serial.print(data[0][i]);
                        Serial.print(" ");
                        Serial.print(data[1][i]);
                        Serial.print(" ");
                        Serial.print(data[2][i]);
                        Serial.print(" ");
                        Serial.println(data[3][i]);
                }
        }

        //count++;

        if(count >= 3){
                count = 0;
        }

        lastICP = currICP;
        overFlowCount = 0;

        // toggle interrupt: next time with the other edge
        TCCR1B ^= (1 << ICES1);
        delayMicroseconds(32);

        SREG = oldSREG;
        PORTD &= ~(1 << PD7);
}

ISR(TIMER1_OVF_vect){
        byte oldSREG = SREG;
        overFlowCount++;
        SREG = oldSREG;
}

 

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

Last Edited: Thu. Jan 24, 2019 - 10:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps the binary/hex you upload is out of date?  I've had similar issues when:

1. Binary upload happens before linker is done

2. Compiling one profile (e.g. release) but uploading from another folder (e.g. debug)

3. Make or compiler misses a time stamp compare and doesn't realize a file has to be re-compiled before linking (probably unlikely)

 

Not sure whether you may be susceptible to similar issues in your particular tool chain.  One way to eliminate these type of errors is to delete all output files (*.o, binary/hex etc.), recompile and upload.

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

I do not understand your problem. What do you expect to happen. What was observed to happen.

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

ccrause wrote:

Perhaps the binary/hex you upload is out of date?  I've had similar issues when:

1. Binary upload happens before linker is done

2. Compiling one profile (e.g. release) but uploading from another folder (e.g. debug)

3. Make or compiler misses a time stamp compare and doesn't realize a file has to be re-compiled before linking (probably unlikely)

 

Not sure whether you may be susceptible to similar issues in your particular tool chain.  One way to eliminate these type of errors is to delete all output files (*.o, binary/hex etc.), recompile and upload.

ccrause wrote:

Perhaps the binary/hex you upload is out of date?  I've had similar issues when:

1. Binary upload happens before linker is done

2. Compiling one profile (e.g. release) but uploading from another folder (e.g. debug)

3. Make or compiler misses a time stamp compare and doesn't realize a file has to be re-compiled before linking (probably unlikely)

 

Not sure whether you may be susceptible to similar issues in your particular tool chain.  One way to eliminate these type of errors is to delete all output files (*.o, binary/hex etc.), recompile and upload.

I wrote:

I added "WTF" to verify that the program has compiled.

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

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

ajcashin wrote:

I do not understand your problem. What do you expect to happen. What was observed to happen.

Well... if i dont increment it should not increment...

 

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

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

Tobey wrote:

 

...
volatile unsigned int data[3][4] = {0};
...
volatile byte count = 0;
...
  data[0][count] = lastICP;
  data[1][count] = currICP;
  data[2][count] = overFlowCount;
  data[3][count] = pulseTics;
...

 

 

Tobey wrote:

"count" gets incremented still when it is commented out.

 

It doesn't get incremented; it gets overwritten.

 

You have, again, made your array too small.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

How do you KNOW it's incremented? How are you observing the value? Is this some wonkiness in the simulator/debugger perhaps?

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

It's probably worth noting how I worked this out. I did not read all your code looking for errors. My thought process was...

 

1) If it is commented out then it cannot be incremented.

2) But it is changing so it must be being overwritten.

3) The compiler will often allocate memory space in the order variables are declared (in simple code anyways).

4) Where is 'count' declared?

5) What is declared just before it?

6) Is that declaration correct?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

***** for Brian.

if you have a:

int data[3] and only indexes 0, 1 and 2 are valid, and when you write to data[3] you write to memory that is not part of the array, and could be anything.

Sometimes it's your counter, but when you move some code around it may change from overwriting the counter to overwriting something else.

 

Errors like this are easily caught by static code analysis.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

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

He declares it before the CAPT ISR:

volatile byte count = 0;

Note that in GCC the order variables are defined does not necessarily mean that's the order they are allocated in memory. For efficiency on larger architecture where packing together all the char, short, int, long you may find that all the 1 byte variables are collected together. Also by a strange quirk of fate that was introduced a few years ago it seems the linker now places them in reverse alpha order. So the only way to know what's really adjacent is to build then study the .map file. I agree that variables that change "unexpectedly" are often as a result of a buffer overflow or something in the memory immediately before. For example there are 24 bytes here:

volatile unsigned int data[3][4] = {0};

If an out of bounds write was made (bad index) and it just happens that "count" is in RAM just after this then that could well explain it being changed.

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

Problem solved. I just mixed up the index of the array. Jeeez... while trying to find an error i introduced another one....

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

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

This shows the dangers of arrays in C - very easy to write off the end (in fact many of the famous hacks into PCs rely on this very thing). You might want to get into the habit of bounds checking indices with assert() in your code. (or use Ada ;-)

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

What is Ada?

 

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

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

Google Ada.

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

Google should arrive at:

 

https://en.wikipedia.org/wiki/Ada_(programming_language)

 

It's yet another procedural language so very like C etc but where it scores in safety terms is that when you define variables you can specify allowed ranges so you just can't go out of range.

 

It was principally designed for military/safety use so the chances are that F35's and Airbus 380's are programmed in it. It is "safer" than things like C which kind of counts when setting the aileron deflection to 80 degrees (about 50 more than designed) can have some pretty visible consequences (like 400 bodies spread across a field somewhere!)

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

Bloody Google brought me to the bloody german Wikipedia site which is bloody unstructured and i couldnt find ada....

Usually i keep an eye on those arrays since i am aware of the problem. I was just to focus-locked on that variable.

 

Yes i use Google, but there is no search enginge within Google to search through those many useless results...

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

Tobey wrote:
Bloody Google brought me to the bloody german
I get that all the time because my VPN to my (German based) company makes Google and others think I'm in Frankfurt!

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

There appear to be 5.8M articles on the English site.  2.2M on the Deutsch site.

But you can select any main site.    Then read a translation in your native language.

 

You don't need to be Einstein to select the main English site.

 

Incidentally,   I find that AliExpress always tries to send me to Spanish or Italian instead of English.

 

David.

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

What is Ada?

 Isn't that the AVR version of micro-python from Adafruit Electronics?  smileywinksmiley

(More seriously, expecting a web search of "ada" to get any where near a page about the programming language is ... very optimistic.  I get (lots) about the Americans with Disabilities Act, The American Dental Association, the American Diabetes Association...)

 

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

clawson wrote:
https://en.wikipedia.org/wiki/Ada_(programming_language)
Broken link (forum software doesn't auto-handle well URLs with special characters).

 

https://en.wikipedia.org/wiki/Ada_(programming_language)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Kartman wrote:
Google Ada.

Ada => Lovelace => Linda

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Close the loop? Ada => Lovelace => Linda => Deep Throat => Deep Thought => IBM => Computer languages => Honywell => developed Ada