Announcement

Collapse
No announcement yet.

SPI Intrerrupt + SD Card Write

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SPI Intrerrupt + SD Card Write

    Hello,

    i am working on a Project that consists on an Arduino Mega and 4Duino. They are conected to each other using SPI.
    The Arduino Mega is reading some data that have to be transferd to 4Duino per SPI.
    The 4Duino should receive this data and log it to a SD Card.

    I have this in the Setup:

    Code:
    word handle;
      handle = Display.file_Open("test.txt",'w');
      Display.file_Write(6,"setup ",handle);
      Display.file_Close(handle);
    and this in the Loop:

    Code:
    word handle3;
      handle3 = Display.file_Open("test.txt",'a');
      Display.file_Write(5,"loop ",handle3);
      Display.file_Close(handle3);
    Both are working as expected. The file test.txt is created in the Setup and overwritten in the Loop.

    The Problem is that the 4Duino is not saving any data to the SD Card while in an Interrupt.
    I know the Interrupt is working and being triggerd because of the LED going HIGH.
    I also looked on the Serial and the print 10 / print 20 is also working.

    Code:
    ISR (SPI_STC_vect)
    {
      digitalWrite(13, HIGH);
     
      word handle3;
      handle3 = Display.file_Open("test.txt",'a');
      Display.file_Write(8,"trigged ",handle3);
      Display.file_Close(handle3);
    
      byte d = SPDR;
      if(d==0){
      Serial.println(10);
      } else {
      Serial.println(20);
      }
    
    }
    Any ideas why the file_Write wont work inside the Interrupt Routine???
    How could i overcome this Problem?

    Thanks.





  • #2
    Hello,

    It might be that while in an interrupt routine, the interrupt for Serial is turned off which may mean you can't receive Serial. You could use the interrupt routine to set a flag eg,

    Code:
    writeToFile = true;
    Then in your main loop you could check for the flag being set and then write to the file.

    Code:
    if(writeToFile){
    
      word handle3;
      handle3 = Display.file_Open("test.txt",'a');
      Display.file_Write(8,"trigged ",handle3);
      Display.file_Close(handle3);
      writeToFile = false;
    
    }
    It might also be a good idea to detach/disable the interrupt when writing to the file and attach/enable the interrupt when finished.

    I hope this helps

    Paul

    Comment


    • #3
      Hello paulelio,

      this works. Thanks..

      I have now my Arduino Mega sending two datas to 4Duino via SPI.
      4Duino is saving both in a .txt file in the SD Card.

      Now the Problem is that i have Arduino running on 400Hz, but the data in SD Card isnt that fast.
      Do you know at what frequence the 4Duino logs the data on the SD Card?

      One of the data i am logging is an angle. It changes in the .txt file from 0 to 40° in one step...
      By 400Hz i should be able to see every 1° changes.

      Any ideas on how to overcome it?

      Thank you again.

      Comment


      • #4
        Hi,

        I am glad that worked out for you.

        When it comes to writing to your SD card there is a lot of serial commands being sent to the display to open the file, write the data, and then close the file. If the data length is the same each time you write to the SD, then you could make a function that is stored on the SD card and loaded in after you mounted the SD card. This function could then be called which will take 8 bytes or 4 words and effectively reduce your serial traffic.

        If you take a look at page 119 of this datasheet it explains about file_LoadFunction and then file_CallFunction.

        Your routine as it stands sends over 24 bytes via serial and also has to deal with 3 responses. You could possibly do your function in 12 bytes and 1 response. The file_CallFunction is a handy feature to do a task that has several commands which you can get the display to do with minimal serial commands.

        I hope this helps

        Best regards

        Paul


        Comment

        Working...
        X