5 years, 5 months ago.

how to use the timeout ?

Hello, I would like to make a program that reads a value from a file every x seconds. For this, I used the timeout but my problem is that all values ​​are read in a very short time. I do not get my interval x seconds between each measurement. How should I do? thank you very much Here is my program:

  1. include "mbed.h"
  2. include "TextLCD.h"
  3. include "SDFileSystem.h"

Ticker flipper; DigitalOut led1(LED1); DigitalOut led2(LED2); Serial pc(USBTX, USBRX); tx, rx SPI spi(p11, p12, p13); Pins liaison SPI : mosi, miso, sclk TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); Pins écran LCD : rs, e, d4-d7 DigitalOut cs1(p21), cs2(p22), cs3(p23), cs4(p25); Pins des CS des 4 DAC SDFileSystem sd(p5,p6,p7,p8,"sd"); the pinout on the mbed coll components workshop board

void flip(void);

int dac, val, signe, gain; double intervalle=2;

int main() {

FILE * fp; fp=fopen("/sd/louise/DAC.txt","r"); if (fp==NULL){ lcd.printf("not file oui\n\n"); }

led2 = 0; spi.format(16,1); Setup the spi for 16 bit data, high steady state clock, spi.frequency(1000000); second edge capture, with a 1MHz clock rate cs1=1;

flipper.attach(&flip, intervalle); setup flipper to call flip after x seconds led2 = 1; }

void flip() {

FILE * fp; fscanf(fp, "%d %d %d %d ", &dac, &val, &signe, &gain); if (dac==1) { cs1 = 0; Select the device by seting chip select low } else if (dac==2) { cs2 = 0; } else if (dac==3) { cs3 = 0; } else { /*if (dac==4)*/ cs4 = 0; } spi.write(val); Send 0x8f, the command to read the WHOAMI register lcd.printf("val=%d \n\n", val); cs1 = 1; Deselect the device cs2 = 1; cs3 = 1; cs4 = 1; } }

Question relating to:

Comment on this question

1 Answer

5 years, 5 months ago.

1) As has be pointed out in answers to your other questions today please use <<code>> and <</code>> on their own lines before and after your code so that your posts are more readable. Use the preview option to check it's working before posting.

2) You have a variable scope problem. Currently you have different file pointers in each function so they are going to point to different files (or in this case the one in main points to a file and the one in flip is undefined). Remove the line FILE *fp; from the flip and main functions and put it before main so that it's a global variable. The variables dac, val, signe and gain are only used in flip() and so you can move those from being global to being local to that function. Similarly intervalle can be made local to main. Not required but good practice.