Advertisement
CuriousScientist

ADS1254 Reading continuously while Cycling MUX code snippet

Feb 9th, 2020
162
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/GBWJdyjRIdM
  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. void cycleDifferential()
  14. {
  15.     //outside while() loop, we have to switch to the first differential channel ([AIN0+AIN1])
  16.     writeRegister(1, 1); //B00000001 = 1;  [AIN0+AIN1]
  17.     //this can be done also somewhere by another function
  18. while(Serial.read() != 's')
  19. {  
  20.     for(muxcycle = 1; muxcycle < 5; muxcycle++)
  21.     {
  22.     //STEP 1, then STEP4
  23.     waitforDRDY();
  24.     //We select the multiplexer based on the current status of the for() loop
  25.     switch (muxcycle)
  26.         {
  27.         case 1: //Channel 2
  28.             writeRegister(0x01, B00100011); //AIN2+AIN3
  29.             break;
  30.  
  31.         case 2: //Channel 3
  32.             writeRegister(0x01, B01000101); //AIN4+AIN5
  33.             break;
  34.  
  35.         case 3: //Channel 4
  36.             writeRegister(0x01, B01100111); //AIN6+AIN7
  37.             break;         
  38.  
  39.         case 4: //Channel 1
  40.             writeRegister(0x01, B00000001); //AIN0+AIN1
  41.             break;
  42.         }
  43.     //STEP 2
  44.     SPI.beginTransaction(SPISettings(1700000, MSBFIRST, SPI_MODE1)); //Start SPI
  45.     digitalWrite(CS_pin, LOW); //REF: P34: "CS must stay low during the entire command sequence"
  46.     SPI.transfer(B11111100); //Restarting conversion using SYNC
  47.     delayMicroseconds(4); //t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
  48.     SPI.transfer(B11111111); //WAKEUP
  49.     //STEP 3
  50.     SPI.transfer(B00000001); //Issue RDATA (0000 0001) command
  51.     delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
  52.     registerData = 0; //registerData should be zero before reading a new data
  53.     registerData |= SPI.transfer(0x0F); //MSB comes in, first 8 bit is updated
  54.     registerData <<= 8;                 //MSB gets shifted LEFT by 8 bits
  55.     registerData |= SPI.transfer(0x0F); //MSB | Mid-byte
  56.     registerData <<= 8;                 //MSB | Mid-byte gets shifted LEFT by 8 bits
  57.     registerData |= SPI.transfer(0x0F); //(MSB | Mid-byte) | LSB - final result
  58.     Serial.print(registerData); //Print it
  59.     Serial.print('\t');// print a tab, so we will have a nicely formatted ouput
  60.     digitalWrite(CS_pin, HIGH); //We finished the command sequence, so we switch it back to HIGH
  61.     SPI.endTransaction(); //END SPI    
  62.     }
  63.     Serial.println();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement