Advertisement
CuriousScientist

ADS1256 Reading register code snippet

Feb 2nd, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This is the function which reads the value of a selected register
  2. //The function expects the address of the register and returns with its value
  3. //The code belongs to the following Youtube tutorial: https://youtu.be/KQ0nWjM-MtI
  4. //If you used the code, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist
  5. /*
  6. If you want to buy the parts and support me at the same time, please use the following affiliation links:
  7. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  8. Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
  9. ADS1256 Board (green): https://www.banggood.com/custlink/Dmv3VZHrDC
  10. ADS1256 Board (blue): https://www.banggood.com/custlink/KKv3bkIZNg
  11. ADS1256 Board with built in connectors (black): https://www.banggood.com/custlink/mm3DnqZIQO
  12. */
  13. //Datasheet: http://www.ti.com/lit/ds/sbas288k/sbas288k.pdf
  14.  
  15. unsigned long readRegister(uint8_t registerAddress) //Function for READING a selected register
  16. {
  17.     waitforDRDY(); //Waiting for DRDY to go low ('1')
  18.     //this is a separate function that watches the attachinterrupt() pin
  19.  
  20.     SPI.beginTransaction(SPISettings(1700000, MSBFIRST, SPI_MODE1));
  21.     //SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1.
  22.  
  23.     digitalWrite(CS_pin, LOW); //CS must stay LOW during the entire sequence [Ref: P34, T24]
  24.  
  25.     delayMicroseconds(7); //see t6 in the datasheet
  26.     //t6 = 50 * tau_CLKIN = 50 * 130.2 ns = 6510 ns = 6.51 us; you want to round it UP to the next integer (even if it would be 6.1 us)
  27.  
  28.     SPI.transfer(0x10 | registerAddress); //0x10 = RREG, '|' = bitwise OR operation
  29.  
  30.     SPI.transfer(0x00);
  31.  
  32.     delayMicroseconds(7); //see t6 in the datasheet
  33.  
  34.     registerValueR = SPI.transfer(0xFF); //obtaining the data and 'saving' into the variable
  35.  
  36.     delayMicroseconds(7); //see t6 in the datasheet
  37.  
  38.     digitalWrite(CS_pin, HIGH);
  39.  
  40.     SPI.endTransaction();
  41.  
  42.     return registerValueR;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement