Simple code runs in main, but not in ticker. Please help.

18 Apr 2011

Hi all..

Here's an explanation, but you can probably jump straight to the code. (The code below is for a differential opAmp circuit. I can control a reference voltage, and by reading the output voltage deduce what the analogue input voltage is. I'm reading to 0.04v, and so I have a 10x multiplier on the opAmp, so I can accurately read the input. I'll explain if anyone is interested. But it works PERFECTLY)

This code runs perfectly in a while loop, on its own in a main. It is subsecond.

If I attach it to a ticker, and run it every 21 secs. Mbed hangs. I presume with a memory problem.

I'm new to C, being a Java programmer. This would all be fine in the languages I usually work in. What am I doing that's enough of a bad practise to cause mbed to hang ?

//main declarations.
AnalogOut reference(p18);
AnalogIn reading(p20);
float ref=0.0;
float aRead = 0.0;

.....

in the method....

ref=2.6/3.3;

//1800 steps to zero!
//So we can get a stable reading in 1800 steps.
//start at the top and work down till we find the reading.

//Adjust the reference, when the output is stable at 0.3v. We can work out input.
//as we know the hardware and the reference voltage.

        int i=0;

        while (i<1800) {

            if (aRead>0.30 && ref>0) { 
                ref=ref-0.0005;
            }

            if (aRead<0.30 && ref<(2.6/3.3)) {
                ref=ref+0.0005;
            }

            if (ref>2.6/3.3) {
                ref=2.6/3.3;
            }

            if (ref<0) {
                ref=0;
            }

            i++;

            reference=ref;          //sets the vReference
            aRead = reading.read(); //reads the new output
            aRead = aRead*3.3;      //turns it into a real voltage.

        } //output is stable.

        float theValue=((2.6-(ref*3.3)))/0.040;
        value = theValue - 0.7; //there's an offset.
        if (theValue<0) {
            theValue = 0.0;
        }

        if (debug==1) {
            printf("VALUE: %.1f\n\r",theValue);
        }

18 Apr 2011

Publish the whole program that shows the problem.

18 Apr 2011

Have a read of this article which may shed some light on why things that seem ok when in the main loop but "mysteriously" malfunction within callbacks.

18 Apr 2011

Brilliant article.

Thankyou.

I removed any printf's in the method attached to the ticker. But it still failed.

So I made my code more efficient, by reducing that loop to 1600 and putting break in, and now it works.

The funny thing is, my main is not really time critical. I wouldn't mind my main stopping what it is doing, while the processor goes off and runs this ticker. This is not time critical either.

From the article I don't understand why it doesn't just branch and do the interrupt, and then come back and carry on with the main ?

18 Apr 2011

The piece of code you posted doesn't look wrong. But since you didn't post the whole program we can only keep guessing what could be the reason.

18 Apr 2011

As Igor says, without the bigger picture we'll not be able to take it further. The fact that you "got it working" is a clue as to why. But we'ed need to see more to figure out the true underlying reason.

18 Apr 2011

Hi,..

the thing is, the hardware is horrendous.. I must admit I didn't build/design all of it myself. I think the program looks ok on paper, but doesn't run.

In Java I'm used to compilers being fussy where you declare variables, in loops is a big no no for instance.. as it re assigns storage every time round the loop. Which makes for slow running programs. (Worse than that, it eats memory, as you keep all the old pointers till the garbage collector gets them)

I suppose I was just after someone to put an experienced eye over that bit of code, incase I'm doing anything that's a bit of a daft thing to do in C.

I *MUST* do a C course, the only one I started got a bit caught up in malloc etc etc.. things we don't really use in the mbed world afaik.

cheers,

Dave.