Advertisement
CuriousScientist

Arduino with ADS1256

May 30th, 2019
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //This code belongs to the following tutorial video: https://youtu.be/Qgy_p_7wLZc
  3. /*
  4. If you want to buy the parts and support me at the same time, please use the following affiliation links:
  5. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  6. Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
  7. ADS1256 Board (green): https://www.banggood.com/custlink/Dmv3VZHrDC
  8. ADS1256 Board (blue): https://www.banggood.com/custlink/KKv3bkIZNg
  9. ADS1256 Board with built in connectors (black): https://www.banggood.com/custlink/mm3DnqZIQO
  10. */
  11.  
  12.  
  13. #include <SPI.h>
  14. #include "ads12xx.h"
  15.  
  16. const int  START = 8;  //start pin
  17. const int  CS = 10; //clock pin
  18. const int  DRDY = 2; //data ready pin
  19. //const int RESET_  = 9; //reset pin, not used
  20. float samplingfreq;  //
  21.  
  22.  
  23. ads12xx ADS;  //initialize ADS as object of the ads12xx class
  24.  
  25.  
  26. void setup()
  27. {
  28.     //set up high baud rate for high data rates
  29.     Serial.begin(115200);
  30.    
  31.     while (!Serial) {}
  32.  
  33.   ADS.begin(CS, START, DRDY);  //initialize ADS as object of the ads12xx class
  34.  
  35.   ADS.Reset(); //Reset the AD chip. Every time you see the message on the Serial Terminal, everything is set to default!
  36.  
  37.     delay(10); //wait a moment
  38.  
  39.     //preconfig
  40.      ADS.SetRegisterValue(STATUS, B00110100);  //Autocal is ON
  41.      ADS.SetRegisterValue(MUX, B00001000); //
  42.      ADS.SetRegisterValue(ADCON, B00000000); //
  43.      ADS.SetRegisterValue(DRATE, B11010000); //7500 SPS    
  44.      ADS.SetRegisterValue(IO, B11101111); //
  45.      ADS.SendCMD(SELFCAL); //run self calibration    
  46.     //end of preconfig  
  47.  
  48.  //too high SPS will mess up the channels!
  49. }
  50.  
  51. void loop() {
  52.  
  53.   //"start" the timer  
  54.   double startTime = millis();
  55.  
  56.     if (Serial.available()) {      
  57.     //read a character - which is a command
  58.     char cin = Serial.read();
  59.         //char  check = 'y';
  60.         //uint8_t cmd;
  61.         uint8_t cin1;
  62.    
  63.         switch (cin) {      
  64.      
  65.         //-------------------------------------------------------------------------------------------------
  66.         //----------------READ REGISTER--------------------------------------------------------------------
  67.       case 'r':
  68.             //after sending r, we ask for the next input
  69.             //Serial.println("Which Register to read?");
  70.             while (!Serial.available());
  71.             //Serial.print("Register Value for: ");
  72.             cin1 = Serial.parseInt(); //cin1 is the number of the register
  73.             //Serial.println(cin1);
  74.             //Printing out a message and the value of the register cin1
  75.             //Serial.println("The value of the register is:");
  76.             Serial.println(ADS.GetRegisterValue(cin1));
  77.             break; //break exits the case and the loop will wait for the next command to enter another case
  78.         //-------------------------------------------------------------------------------------------------
  79.         //----------------WRITE REGISTER-------------------------------------------------------------------
  80.         case 'w':
  81.             //after sending w, we ask for the next input
  82.             //Serial.println("Which Register to write?");
  83.             while (!Serial.available());
  84.             cin1 = Serial.parseInt();//cin1 is the number of the register
  85.             //Serial.println("Which Value to write?");
  86.             while (!Serial.available());
  87.             ADS.SetRegisterValue(cin1, Serial.parseInt());
  88.             //cin1 register is written with the following number (whole command could be: w 3 128)
  89.         //Serial.println("The value of the register now is:");
  90.         //Serial.println(ADS.GetRegisterValue(cin1)); //we want to see if the value is set or not. get back the -cin1- value register's value
  91.         break;
  92.          //-------------------------------------------------------------------------------------------------
  93.          //----------------SINGLE READ----------------------------------------------------------------------
  94.         case 'R': //simple conversion result, reading the first channel in differential mode, once.
  95.        
  96.                 ADS.SendCMD(SELFCAL); //Self calibration
  97.                 delay(200); //wait a little            
  98.                 ADS.SetRegisterValue(MUX, B00000001);//AIN0+AIN1 -- CH0
  99.                        
  100.         //printing the result
  101.         Serial.println(ADS.GetConversion());
  102.        
  103.             break;
  104.         //----------------------------------------------------------------------------------------------------
  105.         //----------------SELF CALIBRATION--------------------------------------------------------------------
  106.         case 'c': //simple one-command self calibration and offset calibration
  107.                
  108.                 ADS.SendCMD(SELFCAL); //Self calibration
  109.                 delay(200); //wait a little
  110.                
  111.                                
  112.                 break;
  113.         //-------------------------------------------------------------------------------------------------
  114.        
  115.     //-------------------------------------------------------------------------------------------------
  116.     //----------------SINGLE CHANNEL ------------------------------------------------------------------
  117.     case 's': //s: single - 8 channels (more channels, less accuracy(common ground, more noise))
  118.                 samplingfreq = Serial.parseFloat();
  119.         //command looks like: "s 300".
  120.                 ADS.SendCMD(SELFCAL); //Self calibration
  121.                 delay(200); //wait a little
  122.                
  123.    
  124.        ADS.SetRegisterValue(DRATE, B01100011); //50 SPS// for 1 sample / second on the PC
  125.        
  126.        while (Serial.read() != 'N') {
  127.         //Elapsed time since the sw is started
  128.         double elapsedTime = millis() - startTime;              
  129.         //prints time since program started  
  130.         Serial.print(elapsedTime);
  131.         Serial.print("\t");
  132.         ADS.SetRegisterValue(MUX, B00001000);//AIN0+AINCOM -- CH0
  133.         Serial.print(ADS.GetConversion());
  134.         Serial.print("\t");
  135.         delayMicroseconds(100);
  136.         ADS.SetRegisterValue(MUX, B00011000);//AIN1+AINCOM -- CH1
  137.         Serial.print(ADS.GetConversion());
  138.         Serial.print("\t");
  139.         delayMicroseconds(100);
  140.         ADS.SetRegisterValue(MUX, B00101000);//AIN2+AINCOM -- CH2
  141.         Serial.print(ADS.GetConversion());
  142.         Serial.print("\t");
  143.         delayMicroseconds(100);
  144.         ADS.SetRegisterValue(MUX, B00111000);//AIN3+AINCOM -- CH3
  145.         Serial.print(ADS.GetConversion());
  146.         Serial.print("\t");
  147.         delayMicroseconds(100);
  148.         ADS.SetRegisterValue(MUX, B01001000);//AIN4+AINCOM -- CH4
  149.         Serial.print(ADS.GetConversion());
  150.         Serial.print("\t");
  151.         delayMicroseconds(100);
  152.         ADS.SetRegisterValue(MUX, B01011000);//AIN5+AINCOM -- CH5
  153.         Serial.print(ADS.GetConversion());
  154.         Serial.print("\t");
  155.         delayMicroseconds(100);
  156.         ADS.SetRegisterValue(MUX, B01101000);//AIN6+AINCOM -- CH6
  157.         Serial.print(ADS.GetConversion());
  158.         Serial.print("\t");
  159.         delayMicroseconds(100);
  160.         ADS.SetRegisterValue(MUX, B01111000);//AIN7+AINCOM -- CH7
  161.         Serial.print(ADS.GetConversion());     
  162.         Serial.print("\n");
  163.         //Full output: Time CH1 CH2 CH3 CH4 CH5 CH6 CH7 CH8
  164.         delay(samplingfreq);
  165.         }
  166.         break;
  167.        
  168.         //-------------------------------------------------------------------------------------------------
  169.         //----------------DIFFERENTIAL CHANNEL ------------------------------------------------------------
  170.         case 'd': //d: differential - 4 channels (less channels, better accuracy (less noise))
  171.        
  172.         //another cin for receiving the delay
  173.           samplingfreq = Serial.parseFloat();
  174.      
  175.         //ADS.SetRegisterValue(DRATE, B11010000);  //7500 SPS// for 1 sample / second on the PC
  176.    
  177.        ADS.SetRegisterValue(DRATE, B01100011);
  178.         ADS.SendCMD(SELFCAL); //Self calibration
  179.         delay(200); //wait a little
  180.        
  181.        
  182.        
  183.        //calibration!
  184.        while (Serial.read() != 'N') { //while we don't stop manially by sending N through the terminal....
  185.        
  186.         //Elapsed time since the sw is started
  187.         double elapsedTime = millis() - startTime;              
  188.         //prints time since program started
  189.         Serial.print(elapsedTime);
  190.         Serial.print("\t");
  191.         //delayMicroseconds(50);
  192.         //Switch on the first channel
  193.         ADS.SetRegisterValue(MUX, B00000001);//AIN0+AIN1 -- CH0
  194.         Serial.print(ADS.GetConversion());
  195.         Serial.print("\t");
  196.         //delayMicroseconds(50);
  197.         //Switch on the second channel
  198.         ADS.SetRegisterValue(MUX, B00100011);//AIN2+AIN3 -- CH1
  199.         Serial.print(ADS.GetConversion());
  200.         Serial.print("\t");
  201.         //delayMicroseconds(50);
  202.         //Switch on the third channel
  203.         ADS.SetRegisterValue(MUX, B01000101);//AIN4+AIN5 -- CH2
  204.         Serial.print(ADS.GetConversion());
  205.         Serial.print("\t");
  206.         //delayMicroseconds(50);
  207.         //Switch on the fourth channel
  208.         ADS.SetRegisterValue(MUX, B01100111);//AIN6+AIN7 -- CH3
  209.         Serial.print(ADS.GetConversion());
  210.         Serial.print("\n");
  211.         //Final output: time CH1 CH2 CH3 CH4   
  212.         //delay by the received sampling frequency 
  213.         //there is some time needed for the code above to run, so real sampling frequency is slightly slower (>1%)
  214.         delay(samplingfreq);
  215.        
  216.         }
  217.         break;
  218.         //-------------------------------------------------------------------------------------------------
  219.        
  220.        
  221.         //-------------------------------------------------------------------------------------------------
  222.         default:
  223.        
  224.         //?
  225.         break;
  226.         //-------------------------------------------------------------------------------------------------
  227.             }
  228.        
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement