Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 33 of 33

Thread: AD7124 understanding registers how to program them in to the Teensy3.6

  1. #26
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,453
    I am not sure of your question?

    If you are asking about something like: #define REG 5
    It does not define any variables or the like: It is a pre-processor macro Again more details in lots of places like: http://www.cplusplus.com/doc/tutorial/preprocessor/

    But simply put, when you compile, one of the first phases of the compile it runs through the pre-processor, and in this simple case it literally just replaces every instance where it finds REG with the character 5

    So to the compiler: AD7124_Write(REG, REG_VAL)
    is the same thing as doing: AD7124_Write(5, 0)

    And yes start off by Keeping it simple...

  2. #27
    Senior Member
    Join Date
    Sep 2016
    Posts
    128
    So I am writing to the register 0x05 hence 000101 ok page 85-94. this register has 8 bits 0-7.
    i write to each of the bits a 1, hence data =0xff. what does 0x3f do? its 63 it doesnt matter if I send 0xff or 0x3f.
    i get back the following from Serial Monitor: register 0x05 shows 10100 Hex 14 DEC 20
    this means that the ID number is 16, and the Silicon Revision number =4;

    Code:
    #include <SPI.h>
    
    //SPI defines
    #define TFT_DC  9
    #define TFT_CS 10
    #define TFT_RST -1
    #define AD7124_CS 8
    #define AD7124_SPEED 5000000 //max speed
    
    #define REG 0x05 
    #define DATA 0xff  
    
    
    SPISettings settingsAD7124(5000000, MSBFIRST, SPI_MODE3); //Page 38-94 SPI_MODE3 CS LOW
    
    
    
    void setup() 
    {
     pinMode(AD7124_CS, OUTPUT);
      pinMode(TFT_CS, OUTPUT);
    
       SPI.begin();
    
         //Initialize serial and wait for port to open:
      Serial.begin (38400);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }
    
    }
    void loop() 
    {
    AD7124_Write(REG,DATA);
    unsigned char val=AD7124_Read(REG);
    Serial.print(" register 0x05 shows ");
    Serial.print(val,BIN);
    Serial.print(" Hex ");
    Serial.print(val,HEX);
    Serial.print(" DEC ");
    Serial.println(val,DEC);
    }
    
    void AD7124_Write(unsigned char reg, unsigned char data)
    {
    digitalWrite(AD7124_CS,LOW);
      SPI.beginTransaction(settingsAD7124);
    
      SPI.transfer(reg & data); // write to register 0x05 hence 000101 bin, and write to the register 0xFF
     // SPI.transfer(data);       //i get back a value for all bits 0-7
      SPI.endTransaction();
      digitalWrite(AD7124_CS,HIGH);
    
    
    
    }
    
    unsigned char AD7124_Read(unsigned char reg)
    {
      unsigned char data;
      
      digitalWrite(AD7124_CS,LOW);
      SPI.beginTransaction(settingsAD7124);
    
      SPI.transfer((reg&0xff)|0x40); // select register to read from
      data = SPI.transfer(0);
      SPI.endTransaction();
      digitalWrite(AD7124_CS,HIGH);
      delay(1000); //   optional delay for between transaction spacing
      return data;
    }
    }
    after this it becomes useful to write an array for the registers and data that needs to be send.

    reading a RTD:

    Reset the ADC. for 0x01 & 0x0000
    2.Select the power mode 0x01 &
    0x01 & ED0 ( bit 15-13 are 0, Bit12 is 0, Bit 11=1=Continious Read,Bit 10=1 status, Bit 9=1 Set diagnostics, Bit 8=0 Internal Ref(not Sure if needed to set), Bit7=1,Bit6=1,Bit5-2=0000; bit 1 =1; bit 0=0; hence 0000111011000011=EC3;
    3.Set the CHANNEL_0 register analog input to AIN1/AIN2. 0x09 & 0x8001 From the AD7124 Library (bit 15=1,14:12=000, Setup 0 AIN1 is AIN2., 11:10=0, 9:5=00000, 4:0=00001 is=0x8001.

    Assign Setup 0 to this channel. this is bit 14:12.
    Configure Setup 0 to have a gain of 16 and select the reference source REFIN2(). 0x31 & 0x50000 ? why i Dont understand this from the PDF it sets the Gain for the Config0 thats a gain of 327680 if I understand it correctly. (not much is written on the Gain part in the pdf)
    Select the filter type and set the output data rate.
    #define AD7124_FILT_REG_FILTER(x) ((uint32_t)((x) & 0x7) << 21) this I dont understand at all ( conversion to uint32_t ((x)&0x07 of errorregister ) << bit shift 21 to the left
    4.Program the excitation currents to 500 μA and output the currents on the AIN0 and AIN3 pins. 5.Wait until RDY goes low. (((x) & 0x7) << 11) on address 0x03 & (23:16=0, bit 15=PDSW=0; 14=0; 13:11=100=500uA, 10:8=500uA?(not sure, but it is written that both excitations currents are required), 7:4=0000 Channel 0; 3:0=0011 for AIN3; gives 2403 in hex value
    Read the conversion value. eeehm ? how do I do this? is this set at the 0x00 & 10000000 at channel 0, and 0x00 10000011 at channel 3?
    6. Repeat Step 4.
    Last edited by Bastiaan; 08-19-2020 at 08:47 AM.

  3. #28
    Senior Member
    Join Date
    Sep 2016
    Posts
    128
    Somehow re running the program doesnt work.
    i get on the output 0x05 in hex FF binary 11111111, 255.
    I re changed the DATA to 0x3f this does not work.

    I tried resetting it with 0x14.... hmmm I dont know what is going on.

  4. #29
    Senior Member
    Join Date
    Sep 2016
    Posts
    128

    unraveling the AD7124 and RTD function and paste it to an array.

    would it be wise to look at the CN0391.cpp 196 - 220 and AD7124.cpp?

    and go here step by step because everything is listed. the RTD configuration is listed, and I would like to hardcode this into an array and run the array through the SPIWrite and SPIRead function.

    can I write Something like this?
    Code:
    const unsigned char reg[]={     0x01,   0x01,  0x09    , 0x31     };  (its not finished because I dont understand some things going on in the AD7124.cpp
    const unsigned char data[]={0x0000, 0xEC3,  0x8001, 0x50000};
    
    for(int i=0;i<ArraySize;i++)
    {
    AD7124_Write(REG,DATA);
    unsigned char val=AD7124_Read(REG);
    Serial.print(" registers values show ");
    Serial.print(val,BIN);
    Serial.print(" Hex ");
    Serial.print(val,HEX);
    Serial.print(" DEC ");
    Serial.println(val,DEC);
    }
    I am not getting the right values

  5. #30
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,453
    Sorry I don't have have time this morning to do much....

    Quick look and your write function has issues:
    Code:
    void AD7124_Write(unsigned char reg, unsigned char data)
    {
      digitalWrite(AD7124_CS,LOW);
      SPI.beginTransaction(settingsAD7124);
    
      SPI.transfer(reg & data); // write to register 0x05 hence 000101 bin, and write to the register 0xFF
       // SPI.transfer(data);       //i get back a value for all bits 0-7
       SPI.endTransaction();
      digitalWrite(AD7124_CS,HIGH);
    }
    You need to have two bytes to SPI.
    Code:
    void AD7124_Write(unsigned char reg, unsigned char data)
    {
      digitalWrite(AD7124_CS,LOW);
      SPI.beginTransaction(settingsAD7124);
    
      SPI.transfer(reg);         // Output Register plus bits 0x40 not set so write operation. Don't care about return value
      SPI.transfer(data);       //Output new data for that register - don't care about return value
      SPI.endTransaction();
      digitalWrite(AD7124_CS,HIGH);
    }

  6. #31
    Senior Member
    Join Date
    Sep 2016
    Posts
    128
    ok thanks

    i was probably confused by the SPI_write();
    i wont ask why and accept it that I should write:

    SPI.transfer(reg);
    SPI.transfer(data);

    has to do with the SPI.transfer function it self

  7. #32
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,453
    It might help you, if you took a quick look through the Arduino documentation on SPI: https://www.arduino.cc/en/reference/SPI

  8. #33
    Senior Member
    Join Date
    Sep 2016
    Posts
    128
    The barometric pressure example looks similar to what I want to do. At least this will be clearer see what Is happening

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •