Go Down

Topic: [SOLVED] Gameduino2 library and SPI problems (Read 1 time) previous topic - next topic

Borjecege

Sep 08, 2014, 12:57 am Last Edit: Sep 10, 2014, 12:24 pm by Borjecege Reason: 1
Hi everybody!
I´m new in this forum, so if there´s info missing in my question or this is not the right place just tell me, I´ll be glad to add or change whatever is needed!

I´m using Arduino Uno on my Work order degree to build a electrocardiograph. It works as a ADS1298 (analog-digital converter) and Gameduino2  (TFT) controller.  
My program has two main parts, one where anduino reads data from ADS with an Interrupt, and other one where arduino sends this stored data to Gameduino2 to plot them. The protocol used for communication is SPI (arduino Uno has only one SPI bus), and as those 2 devices use different SPI modes I have to make changes "on the fly". It seems to work well with this, the program enters the "plot" function and enters also de "read data" function attached to the interrupt. But that´s all, it does not plot real data I get, it just plots trash.

Does somebody know what happens? or at least give me a clue about what to try? I think I´m having a problem with the Gameduino2 libraries, (I have to call a function named "GD.begin" in my setup), but I can´t fix it anyway!
Thanks a lot!

Borjecege

Here is a better explanation of my problem (simplified code):

#include <SPI.h>
#include <EEPROM.h>
#include <GD2.h>

//Pin configuration
const int CS_GD_pin = 8;
const int CS_ADS_pin = A5;
const int MOSI_pin = 11;
const int MISO_pin = 12;
const int SCK_pin = 13;


void setup() {

    //Pin settings
    pinMode(CS_GD_pin, OUTPUT);
    digitalWrite(CS_GD_pin, HIGH);
    pinMode(CS_ADS_pin, OUTPUT);
    digitalWrite(CS_ADS_pin, HIGH);


    pinMode(MOSI_pin, OUTPUT);
    pinMode(MISO_pin, INPUT);
    pinMode(SCK_pin, OUTPUT);
    pinMode(ADSReset_pin,OUTPUT);

    GD.begin();
}

void loop() {

    plot();

    ADS_ready();

    attachInterrupt(ADSInterrupt, read_subfunction, FALLING);
    delay(100);
    detachInterrupt(ADSInterrupt);
    digitalWrite(CS_ADS_pin, HIGH);
}


void plot() {

    digitalWrite(CS_GD_pin, LOW);
         //Here plotting code which is basically (for the moment) an order to plot the matrix, with GD.Begin(POINTS) and GD.Vertex2ii...
    digitalWrite(CS_GD_pin, HIGH);
}

void ADS_ready() {

    digitalWrite(CS_ADS_pin, LOW);
    //Initialize SPI for ADS
        SPI.begin();
        SPI.setDataMode(SPI_MODE1);
        SPI.setBitOrder(MSBFIRST);
        SPI.setClockDivider(SPI_CLOCK_DIV8);

    //Here some ADS settings
}

void read_subfunction(){

    //Here reading code, when ADS says new data are ready the interrupt is activated and Arduino enters this ISR and stores data into a matrix
}

Some important points:
1.I change SPI_MODE "on the fly" because it´s mandatory, devices' need.
2.As you can see every part of the code related to ADS is wraped around CS_ADS_pin setting, and idem for Gameduino2
3.The ADS part works fine (checked with Matlab)
4.Gameduino part does not work. If I put a counter inside plot() I can see the program enters, but it´s like it only swaps for the first time. For example, if inside plot() I forget about the matrix and build a little program to display a counter on the screen with one second delay (1,2,3...) it only plots 1.

I think there is a problem with the Gameduino2 SlaveSelect pin (8, CS_GD_pin in my program). I tried with GD.__end(), GD.resume() and so, I admit I don´t understand very well Gameduino libraries because I´m not an expert at all, so any help is gladly appreciate!

Borjecege

Just in case you find helpful, I got the solution, reading about SPI in this forum. The key was to change the SPI_MODE BEFORE selecting the slave and not after as I was doing. GD.__end() and GD.resume() are also necesary.

Go Up