9 years, 9 months ago.

How could I use a Ticker (Interrupt) only for one time, but in every round?

I want to write the current time to a file on an SD card, every time a telegram was received from my RS485. But since the program is too slow and then many bytes are lost (so I want to set an interrupt)

If I reduce the time he wrote hundreds of times the same time in the file , contribute to slower time, so 1 second and he just stops and freezes.

That's a part of my code:

while(1) {
 if(trans_4 == 1) {
                          Save_on_sd.attach(&save_data, 1); 
                    }
}

2 Answers

9 years, 9 months ago.

I had a similar requirement to turn a Ticker on and off for timed A/D conversion over a variable duration and here are extracts of my code that may help.

"Running" is flag used to indicate that I'm in my acquisition period.

before Main

// ____________________ DURATION ___________

Timeout SamplingDuration;   // set up a timeout for adc reading duration
Ticker Convert;             // set up a ticker to do timed adc covertions.

void Duration_end()
    {
    Convert.detach();
    Running =0;
    }
    

void ADC_read()
    {
    for (int i=0; i<25; i++)        // I do 25 readings of the A/D
        {
        // my code here
         wait_us(10);
        }
        
}

Within Main I have...

// Acquisition stage
    
    Running=1;                  // set flag to indicate acquisition running

    
    //--------------------------------------------------------------------
  
    Convert.attach(&ADC_read, 0.1);             //do adc convert every 0.1 second and attaches function ADC_read()

    //wait_us(100);                              // Allow first conversion 100uS 'head start'

                                            
    SamplingDuration.attach(&Duration_end, Ta);  // adc reads for acquisition time Ta                                          

    //---------------------------------------------------------------------------------

So I have a timed duration and an interupt time acquisition in the above. I did this a long time ago and it works, so I hope it gives you some pointers.

Accepted Answer
9 years, 9 months ago.

I think you are looking for the TimeOut class. http://mbed.org/handbook/Timeout