-1

I wrote a class for Aduino, for reading an ads124x, and I am having one major problem, I can't call a function defined by the class. Here is how it is structured:

  • .ino calls functions using . operator
  • .h contains register map and class definition
  • .cpp contains all functions of the class

so far here is what is happening the .ino is successfully calling a function in .cpp, but when that same function calles another in .cpp it fails to call.

.ino:

#include "ADS124X.h"
void setup(){
    ADS124X ADS124X(1,2,3,4);
    Serial.begin(9600);
    ADS124X.setUP(0x20, 0x20);}

.h:

class ADS124X{
    public:
        void reset(void);
        void setUP(unsigned char* mux1, unsigned char* sys0);
    private:
        void SPI_Write(unsigned char* data, unsigned char size);
}

.cpp:

void ADS124X::setUP(unsigned char * mux1, unsigned char * sys0)
{

    Serial.println("hi");    //prints this
    delay(1);
    reset();                 // stops here
    Serial.println("hi");    // doesn't print this
    delay(1);
    stopDataCont();
    delay(210);
    setREG(MUX1, mux1, 1);
    setREG(SYS0, 0x01, 1);
    delay(1);
}
void ADS124X::reset(void)
{
    unsigned char dataToSend[] = { RESET };
    START_HIGH;
    CS_LOW;
    Serial.println(RESET);       // prints this as 0x06 (correct value)
    SPI_Write(dataToSend, 1);    // Seems to stop here
    START_LOW;
    CS_HIGH;
}
void ADS124X::SPI_Write(unsigned char * data, unsigned char size)
{
    Serial.println("SPI_Write");    //prints this
    for (unsigned int i = 0; i < size; i++) {
        Serial.println("SPI_Write");    //prints this
        Serial.println(* data);    //prints this as 126 (if RESET is 3 * data becomes 189...)
        SPI.transfer(*data);
        Serial.println("SPI_Write");
        data++;
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user169808
  • 503
  • 1
  • 6
  • 27
  • 1
    Could you explain me better – eyllanesc Jun 24 '17 at 14:13
  • 1
    Why are you passing two integers to the `setUP` function which takes two pointers to `char` as arguments? – Some programmer dude Jun 24 '17 at 14:13
  • 1
    And where is the `ADS124X` constructor declared and defined? – Some programmer dude Jun 24 '17 at 14:15
  • 1
    Lastly, what is the problem you are having? Do you get build errors? Unexpected results? Crashes? Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jun 24 '17 at 14:16
  • I left out the constructor to post only what is giving me problems. I'm getting no build errors. I'm passing 2 integers which are defined by how I want the ads to function and the datasheet. the two integers are the values for the settings in the register map. the arduino seems to be stopping at spi.tranfer(* data); so I think the problem is with *data – user169808 Jun 24 '17 at 14:38

1 Answers1

1

This is not right...

   Serial.println(RESET);       // println expects a null terminated string  
                                // you are sending a char.

You should define dataToSend as a null-terminated char array.

void ADS124X::reset(void)
{
  char dataToSend[] = { RESET, 0 };
  // ...
  serial.println(dataToSend);  // maybe println "reset" would be better?
  SPI_Write(dataToSend, 1); 
  //...
}

Don't expect to see a nice 0x06 on your serial monitor, since that is not a printable character.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
  • thanks i'll test changing dataToSend. by the println(RESET) the problem I noticed isn't in reset, but in SPI_Write. the print within reset yields the value I want (6), when 6 is passed into SPI_Write it changes to 126, which is a problem – user169808 Jun 24 '17 at 19:08
  • How is RESET declared? if it is as a `#define`, it does not have a type, and could be interpreted as being either an `int16_t` or a plain byte (`int8_t`), depending on the context. – Michaël Roy Jun 24 '17 at 20:22
  • it is defined as (3 << 1) - 0x6- I find it hard to imagine why it becomes 126. if RESET is (3 << 0) it becomes 189... – user169808 Jun 24 '17 at 21:58
  • Have you tried `const unsigned char RESET=(3 << 1);` instead of a `#define` ? – Michaël Roy Jun 24 '17 at 22:01
  • @MichaëlRoy [`println` also accepts a `char`](https://github.com/arduino/Arduino/blob/1.8.3/hardware/arduino/avr/cores/arduino/Print.h#L80) – gre_gor Jun 25 '17 at 20:00
  • thanks, I changed to serial.println(dataToSend), but it gave me an error so I deleted it(im using println for debugging only) Ibecause the problem is further down the line. apparently everything stops at SPI.transfer(*data). there are no errors, but nothing after SPI.transfer(*data) happens. – user169808 Jun 25 '17 at 22:02
  • @gre_gor That's the problem. println accepts too many types. Constants declared with #define do not have a type, they will be printed as whatever suits the compiler at the point println is called. in this case I think it was printing it as a pointer. Macros break the type safety of c++, this is one of the reasons macros are considered evil. – Michaël Roy Jun 26 '17 at 03:32
  • do you know how I could make spi.transfer transfer? I removed all printlns before it cause everything stops there... – user169808 Jun 26 '17 at 15:04
  • I suggest you use the arduino native spi class https://www.arduino.cc/en/Reference/SPI. Make sure you initialize the SPI port first. Make a sketch using the Arduino library, get the module working, this will help you find out and write which helper functions your API should have. Then, you'll have all the info you need to create a class for it. – Michaël Roy Jun 26 '17 at 15:14
  • yes, I've already used arduinos native spi. I just want SPI_Write so I can write any number of bytes via spi, SPI.transfer(*data, size) doesnt seem to work – user169808 Jun 26 '17 at 16:49
  • ok, I found the problem after reading your comment again, I forgot to put SPI_Setup.... lol. thanks for helping – user169808 Jun 26 '17 at 19:23
  • Glad I could help :) – Michaël Roy Jun 26 '17 at 19:58