8 years, 8 months ago.

Read adc via Irq

I am trying to read 10 analogs via a timer. The data is stored in an array:

static uint16_t Set0IGA[5][60][10];

To initialize the timer I have DoAdc.attach(&ReadAnalogs, 0.1); DoAdc is defined as Ticker DoAdc;

All it must do is to fill an array of 5 minutes of data. Unfortunately it crashes at random spots.

Any idea how to debug this in mbed.?

The irq routine is:

//  Read analogs by IRQ every 100mS
void  ReadAnalogs()
{
    // toggle Led 1/2 sec
    if ( LedGI++ < 4) {
        LedOnGI = 0;
    }  else if (LedGI < 9 )   {
        LedOnGI=1;
    } else
        LedGI = 0;
    myled = LedOnGI;

    SolSet0IGA[Tm][Ts]Tmm]    =  Adc1.read_u16();      /
// toggle to next arrays spot
    if( TmmI++ >= 9) {
        Tmm = 0;            //     if 1 sec done inc sec count
        if( Ts++  >= 59) {
            Ts= 0;           //      Reset to 0 seconds
            if ( Tm++ >= 4)  {   //  Minutes = 5
                Tm = 0;         //      if done 1 week then reset to 0
            }
        }
    }
}

Please use <<code>> and <</code>> to make format the code correctly.

posted by Andy A 12 Aug 2015

1 Answer

8 years, 8 months ago.

What is the main code doing in the background while the timer is filling the array? Does it read the ADC at any point? If have seen crashes caused by the main loop being in the middle of an ADC read when an interrupt triggers another ADC read.

I'm assuming that Tm,Ts and Tmm are all globals and initalised to 0 in the main code. Any chance that they are getting changed elsewhere?

Accepted Answer

Hi Andy Thanks for the response. Yes Tm,Ts,Tmm are globals. Yes it is reading adc also. Stupid me let me sort that MANY thanks I will revert again

posted by Erastus Coetzee 12 Aug 2015

I removed all the main body reads and used the arrays. Now the software is no longer crashing and all are 100% Many Thanks Erastus

posted by Erastus Coetzee 12 Aug 2015

You're welcome. I only knew about that issue because it took me a few hours to track down the cause of some random crashes I was having. You can always disable interrupts while doing the ADC read in the main loop if you need them there.

posted by Andy A 14 Aug 2015