Advertisement
CuriousScientist

ADS1256 Reading data code snippet

Feb 3rd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This is the function which gets a single conversion result from a chosen input.
  2. //The code belongs to the following Youtube tutorial: https://youtu.be/4-A8aJ5BzIs
  3. //If you used the code, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist
  4. /*
  5. If you want to buy the parts and support me at the same time, please use the following affiliation links:
  6. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  7. Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
  8. ADS1256 Board (green): https://www.banggood.com/custlink/Dmv3VZHrDC
  9. ADS1256 Board (blue): https://www.banggood.com/custlink/KKv3bkIZNg
  10. ADS1256 Board with built in connectors (black): https://www.banggood.com/custlink/mm3DnqZIQO
  11. */
  12. //Datasheet: http://www.ti.com/lit/ds/sbas288k/sbas288k.pdf
  13.  
  14. void readSingle()
  15. {
  16.     //Reading a single conversion value
  17.  
  18.     //First we need to write some registers to select an input:
  19.     //Set up MUX (0x01) and select the input channels [AIN0(+) + COM(-) = 0000 1000]
  20.     writeRegister(0x01, B00001000); //In the video, I accidentally had B00101000 which is the AIN2(+) + AINCOM(-)  
  21.    
  22.     registerData = 0; // every time we call this function, this should be 0 in the beginning!
  23.    
  24.     //Wait for DRDY to go LOW
  25.     waitforDRDY(); 
  26.  
  27.     SPI.beginTransaction(SPISettings(1700000, MSBFIRST, SPI_MODE1));
  28.  
  29.     digitalWrite(CS_pin, LOW); //REF: P34: "CS must stay low during the entire command sequence"
  30.        
  31.     //Issue RDATA (0000 0001) command
  32.     SPI.transfer(B00000001);
  33.  
  34.     //Wait t6 time (~6.51 us) REF: P34, FIG:30.
  35.     delayMicroseconds(7);
  36.  
  37.     //step out the data: <MSB | mid-byte | LSB>
  38.  
  39.     //registerData is ZERO
  40.     registerData |= SPI.transfer(0x0F); //MSB comes in, first 8 bit is updated // '|=' bitwise OR operator
  41.     registerData <<= 8;                 //MSB gets shifted LEFT by 8 bits
  42.     registerData |= SPI.transfer(0x0F); //MSB | Mid-byte
  43.     registerData <<= 8;                 //MSB | Mid-byte gets shifted LEFT by 8 bits
  44.     registerData |= SPI.transfer(0x0F); //(MSB | Mid-byte) | LSB - final result
  45.     //After this, DRDY should go HIGH automatically (see datasheet)    
  46.    
  47.     digitalWrite(CS_pin, HIGH); //We finished the command sequence, so we switch it back to HIGH
  48.     SPI.endTransaction();  
  49.    
  50.     //taking care of the negative numbers (0 to -5V)
  51.     if (long minus = registerData >> 23 == 1) //if the 24th bit (sign) is 1, the number is negative
  52.     {
  53.         registerData = registerData - 16777216;  //conversion for the negative sign
  54.         //"mirroring" around zero
  55.     }
  56.  
  57.     double voltage = (5.0 / 8388608)*registerData; //5.0 = Vref; 8388608 = 2^{23} - 1
  58.  
  59.     //Basically, dividing the positive range with the resolution of it and multiplying with the bits
  60.  
  61.     Serial.println(voltage, 8); //print it on serial, 8 decimals   
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement