8 years, 4 months ago.

Question about fprintf with interrupt

Hi guys!

I've studied "fprintf" function with "interrupt" and I couldn't get exact value when I run below code.

In this code, theoretically, I thought I can get the i = 4000 number on the text file but I got the number by 4002.

And if I put in more complex code in interrupt, the error is more increased.

Could you let me know which part is wrong in this code?

Thanks

Sincerely,

Charlie.

#include "mbed.h"
Serial pc(USBTX, USBRX);
Ticker flipper1;
Timer timer;
LocalFileSystem local("local");
float i = 0;
FILE *fp = fopen("/local/out3.txt","w");
void interrupt()
    {
    i++;
    fprintf(fp,"%f\r\n",i);
    }
int main()
{
//    pc.baud(115200);
    flipper1.attach(&interrupt,0.001); // 
    while (1){
        if(i>4000)
        {
        fclose(fp);
        }
    }   
}

2 Answers

8 years, 4 months ago.

Look at https://developer.mbed.org/handbook/Ticker : "No printf, malloc, or new in ISR" .

Accepted Answer

thanks for answer dude!. Then in this case, which function should I use in order to get interrupt output values instead of fprintf?

posted by Gucheol Jeong 05 Dec 2015

you have to use variables, with the volatile attribute, as written above. The interrupt routine update this variable without doing any processing or as few as possible (for instance a conversion to float ). The main program read, process and print the variable. Try to google at "interrupt volatile" Maybe you can google at "interrupt volatile"

posted by Robert Spilleboudt 05 Dec 2015

Thanks Robert! I tried to find "interrupt volatile" at google but I couldn't get appropriate info in order to solve this problem. I've changed to

#include "mbed.h"
Serial pc(USBTX, USBRX);
Ticker flipper1;
Timer timer;
LocalFileSystem local("local");
volatile float i = 0;
FILE *fp = fopen("/local/out3.txt","w");
void interrupt()
    {
    i++;
    fprintf(fp,"%f\r\n",i);
    }
int main()
{
//    pc.baud(115200);
    flipper1.attach(&interrupt,0.001); // 
    while (1){
        if(i>4000)
        {
        fclose(fp);
        }
    }   
}

but it still doesn't work. Sorry for bothering you and could you tell me little more hint or how to solve this problem?

And the bad thing is when I changed to more complex code, it never stop even though " i " was over 4000

posted by Gucheol Jeong 07 Dec 2015

You still have a printf in the interrupt. You need something like this:

#include "mbed.h"
Serial pc(USBTX, USBRX);
Ticker flipper1;
Timer timer;
LocalFileSystem local("local");
FILE *fp = fopen("/local/out3.txt","w");

volatile float i = 0;
void interrupt()
    {
    i++;
    }
int main()
{
//    pc.baud(115200);
    flipper1.attach(&interrupt,0.001); // 
    float oldi = i;
    while (fp){ // exit when the file is closed.
      if (oldi != i) {
        oldi=i;
        fprintf(fp,"%f\r\n",oldi);
      }
      if(i>4000)  {
        fclose(fp);
        fp = null;  // don't keep pointers to closed files.
        }
    }   
    flipper1.attach(null,1); // remove the ticker.
}
posted by Andy A 07 Dec 2015

wow!! it works!!!! haha thanks you for helping me Andy!! really appreciate it!

posted by Gucheol Jeong 08 Dec 2015
8 years, 4 months ago.

You can use an prinf, fprintf functions into an interrupt. Use an volatile variable as a flag. And do you fprintf into another thread.

Thanks for answer dude!. You mean do I change output variable to volatile variables such as "volatile float i = 0;" ?

posted by Gucheol Jeong 05 Dec 2015

Sorry... You can not ... Must be readed....

posted by Raph Francois 09 Dec 2015