Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the function which gets a single conversion result from a chosen input.
- //The code belongs to the following Youtube tutorial: https://youtu.be/GBWJdyjRIdM
- //If you used the code, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist
- /*
- If you want to buy the parts and support me at the same time, please use the following affiliation links:
- Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
- Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
- ADS1256 Board (green): https://www.banggood.com/custlink/Dmv3VZHrDC
- ADS1256 Board (blue): https://www.banggood.com/custlink/KKv3bkIZNg
- ADS1256 Board with built in connectors (black): https://www.banggood.com/custlink/mm3DnqZIQO
- */
- //Datasheet: http://www.ti.com/lit/ds/sbas288k/sbas288k.pdf
- void cycleDifferential()
- {
- //outside while() loop, we have to switch to the first differential channel ([AIN0+AIN1])
- writeRegister(1, 1); //B00000001 = 1; [AIN0+AIN1]
- //this can be done also somewhere by another function
- while(Serial.read() != 's')
- {
- for(muxcycle = 1; muxcycle < 5; muxcycle++)
- {
- //STEP 1, then STEP4
- waitforDRDY();
- //We select the multiplexer based on the current status of the for() loop
- switch (muxcycle)
- {
- case 1: //Channel 2
- writeRegister(0x01, B00100011); //AIN2+AIN3
- break;
- case 2: //Channel 3
- writeRegister(0x01, B01000101); //AIN4+AIN5
- break;
- case 3: //Channel 4
- writeRegister(0x01, B01100111); //AIN6+AIN7
- break;
- case 4: //Channel 1
- writeRegister(0x01, B00000001); //AIN0+AIN1
- break;
- }
- //STEP 2
- SPI.beginTransaction(SPISettings(1700000, MSBFIRST, SPI_MODE1)); //Start SPI
- digitalWrite(CS_pin, LOW); //REF: P34: "CS must stay low during the entire command sequence"
- SPI.transfer(B11111100); //Restarting conversion using SYNC
- delayMicroseconds(4); //t11 delay 24*tau = 3.125 us //delay should be larger, so we delay by 4 us
- SPI.transfer(B11111111); //WAKEUP
- //STEP 3
- SPI.transfer(B00000001); //Issue RDATA (0000 0001) command
- delayMicroseconds(7); //Wait t6 time (~6.51 us) REF: P34, FIG:30.
- registerData = 0; //registerData should be zero before reading a new data
- registerData |= SPI.transfer(0x0F); //MSB comes in, first 8 bit is updated
- registerData <<= 8; //MSB gets shifted LEFT by 8 bits
- registerData |= SPI.transfer(0x0F); //MSB | Mid-byte
- registerData <<= 8; //MSB | Mid-byte gets shifted LEFT by 8 bits
- registerData |= SPI.transfer(0x0F); //(MSB | Mid-byte) | LSB - final result
- Serial.print(registerData); //Print it
- Serial.print('\t');// print a tab, so we will have a nicely formatted ouput
- digitalWrite(CS_pin, HIGH); //We finished the command sequence, so we switch it back to HIGH
- SPI.endTransaction(); //END SPI
- }
- Serial.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement