I am using the AD/DA Hat from WaveShare https://www.waveshare.com/wiki/High-Precision_AD/DA_Board I want to realize a continuous readout of the data. It worked very well so far, but I am not so experienced with SPI (or serial communcation in gerneral) My two questions are:
1) Is it possible that any data will be lost, if for example the AD/DA Hat writes faster than the refresh rate of my program. Or will all data be saved in a buffer and I can read them afterwards?
2) If I set the sample rate to very low (e.g. 5 Hz).. I will get data from SPI like [12, 125, 85, 0, 0, 0, 0, 0, 0, 0, 0, 15, 114, 43, 0, 0, …] My readout of the ADC consits of 3 Bytes, so the rest is low. Because SPI has no start-, stop-bits I dont know how to extract the 3 bytes from the contiuous readout… For the given example I could program a logic which extract the bytes afterwards But it is not very safe because the first or last bit of my actual readout could be zero as well.
Thanks for your help my friends
Any answer might only be of limited help to you as long as you have not understood how SPI actually works, so you should take a detailed look at this interface.
Concerning question 1: SPI is a master-slave-system where any interaction has to be initiated by the master. The sensor itself is not able to write to the master, instead it is read by the master (raspberry pi). Therefore the sensor will not “write” more often than your loop “makes it write” by calling the corresponding SPI-read-function. The sensor might (or probably will) have done several measurements between two read-outs, but that’s usually the case. It wouldn’t be better if the sensor was slower than your program. If it turns out that this is actually the case you better make use of the data-ready-pin mentioned in RogerJones’ answer.
Question 2: Instead of using start-/stop-bits a transmission is triggered by selecting the slave (pulling its select pin low). You usually have to write the register address you want to read first, after that you can read a specific amount of bytes. As you know the address and length of the measurement data, you don’t need to parse or “extract” any bytes. You simply select the bytes you desire and will get those back right away.
In addition to the other answer about the SPI protocol I notice that the product page you linked to shows that, as well as the SPI CS pin on GPIO 15 (BCM22), the ADS1256 ADC has a “Data Ready” pin connected to GPIO 11 (BCM17). You could monitor this and only fetch a new sample when the data is available rather than just reading the data back constantly — you might be getting the same data repeated if there’s no new conversion between your read attempts. By using the “Data Ready” pin in this way you’d get the fastest data rate from the board (assuming you can read the data out fast enough) without missing or replicating data points. How you’d do this would depend on the programming language you’ve used but using an interrupt on pin 11 would appear to be a good start.
Looking at the provided code and datasheet it looks like you can also change the ADC sample rate so if you are having problems keeping up you can slow the chip down, the slowest sample frequency is about 50Hz giving your code 20ms to read the data.
How to read ADC results from ADS1256?
Getting to know SPI
As pointed out by @Sim Son, you need to know basic SPI and have some practical hardware/software experience, before you can understand how SPI ADS1256 programming.
Getting to know ADC
Then you need to know basics of ADC, like what is the meaning of single end and differential end channels, gain factors etc,
Getting to know ADS1256
Then you need to read the data sheet, to get a rough idea of the functions of the pinout, eg, AN-~AN7, Reset, DataReady (Note 1), Beside the SPI pins (CLK, MOSI, MISO, CS), and the functions of the 11 registers.
Getting to know the WaveShare ADS1256 Demo Program
Then you can now study the program and get a rough idea of what the program is doing its job by 3 big steps:
- Define Gain Channels, Data Rates, Register Addresses, ADC Commands
- Define ADS1256 Class with methods init, reset, writeCommand, writeReg, ReadData
- Define ADC1256 methods readChipId, config, setSingleEndChannel, setDiffChannel, setScanMode, init, waitReady, readData, getOneChannelValue, getAllChannelValues, …
Note 1 – As pointed out by @Roger Jones, the DataReay pin is important if you wish to get the highest sample rate. The following picture show how to roughly calculate the maximum data rate for a particular SPI frequency.
Now I am connecting Rpi to the ADS1256 Module
ADS1256 Test Rig
Appendix A – WaveShare ADS1256 ADC Module Picture
Appendix B – WaveShare ADS1256 ADC Module Schematic
Appendix C – ADS1256 Characteristics
Appendix D – ADS1256 Overview
Appendix E – ADS1256 Connection Diagram
Appendix F – Rpi3B+ ADS1256 Wiring V0.2
Appendix G – ADS1256 Register Map
Appendix H – Commands
Appendix I – One shot mode ADC
For one short mode of standby-wakeUp-readData, the wake up delay is only 33.3 uS.
Appendix J – Write Register and Read Register
Appendix K – Status Register 0x00
/ to continue, …