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/4-A8aJ5BzIs
- //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 readSingle()
- {
- //Reading a single conversion value
- //First we need to write some registers to select an input:
- //Set up MUX (0x01) and select the input channels [AIN0(+) + COM(-) = 0000 1000]
- writeRegister(0x01, B00001000); //In the video, I accidentally had B00101000 which is the AIN2(+) + AINCOM(-)
- registerData = 0; // every time we call this function, this should be 0 in the beginning!
- //Wait for DRDY to go LOW
- waitforDRDY();
- SPI.beginTransaction(SPISettings(1700000, MSBFIRST, SPI_MODE1));
- digitalWrite(CS_pin, LOW); //REF: P34: "CS must stay low during the entire command sequence"
- //Issue RDATA (0000 0001) command
- SPI.transfer(B00000001);
- //Wait t6 time (~6.51 us) REF: P34, FIG:30.
- delayMicroseconds(7);
- //step out the data: <MSB | mid-byte | LSB>
- //registerData is ZERO
- registerData |= SPI.transfer(0x0F); //MSB comes in, first 8 bit is updated // '|=' bitwise OR operator
- 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
- //After this, DRDY should go HIGH automatically (see datasheet)
- digitalWrite(CS_pin, HIGH); //We finished the command sequence, so we switch it back to HIGH
- SPI.endTransaction();
- //taking care of the negative numbers (0 to -5V)
- if (long minus = registerData >> 23 == 1) //if the 24th bit (sign) is 1, the number is negative
- {
- registerData = registerData - 16777216; //conversion for the negative sign
- //"mirroring" around zero
- }
- double voltage = (5.0 / 8388608)*registerData; //5.0 = Vref; 8388608 = 2^{23} - 1
- //Basically, dividing the positive range with the resolution of it and multiplying with the bits
- Serial.println(voltage, 8); //print it on serial, 8 decimals
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement