Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
- //This code belongs to the following tutorial video: https://youtu.be/Qgy_p_7wLZc
- /*
- 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
- */
- #include <SPI.h>
- #include "ads12xx.h"
- const int START = 8; //start pin
- const int CS = 10; //clock pin
- const int DRDY = 2; //data ready pin
- //const int RESET_ = 9; //reset pin, not used
- float samplingfreq; //
- ads12xx ADS; //initialize ADS as object of the ads12xx class
- void setup()
- {
- //set up high baud rate for high data rates
- Serial.begin(115200);
- while (!Serial) {}
- ADS.begin(CS, START, DRDY); //initialize ADS as object of the ads12xx class
- ADS.Reset(); //Reset the AD chip. Every time you see the message on the Serial Terminal, everything is set to default!
- delay(10); //wait a moment
- //preconfig
- ADS.SetRegisterValue(STATUS, B00110100); //Autocal is ON
- ADS.SetRegisterValue(MUX, B00001000); //
- ADS.SetRegisterValue(ADCON, B00000000); //
- ADS.SetRegisterValue(DRATE, B11010000); //7500 SPS
- ADS.SetRegisterValue(IO, B11101111); //
- ADS.SendCMD(SELFCAL); //run self calibration
- //end of preconfig
- //too high SPS will mess up the channels!
- }
- void loop() {
- //"start" the timer
- double startTime = millis();
- if (Serial.available()) {
- //read a character - which is a command
- char cin = Serial.read();
- //char check = 'y';
- //uint8_t cmd;
- uint8_t cin1;
- switch (cin) {
- //-------------------------------------------------------------------------------------------------
- //----------------READ REGISTER--------------------------------------------------------------------
- case 'r':
- //after sending r, we ask for the next input
- //Serial.println("Which Register to read?");
- while (!Serial.available());
- //Serial.print("Register Value for: ");
- cin1 = Serial.parseInt(); //cin1 is the number of the register
- //Serial.println(cin1);
- //Printing out a message and the value of the register cin1
- //Serial.println("The value of the register is:");
- Serial.println(ADS.GetRegisterValue(cin1));
- break; //break exits the case and the loop will wait for the next command to enter another case
- //-------------------------------------------------------------------------------------------------
- //----------------WRITE REGISTER-------------------------------------------------------------------
- case 'w':
- //after sending w, we ask for the next input
- //Serial.println("Which Register to write?");
- while (!Serial.available());
- cin1 = Serial.parseInt();//cin1 is the number of the register
- //Serial.println("Which Value to write?");
- while (!Serial.available());
- ADS.SetRegisterValue(cin1, Serial.parseInt());
- //cin1 register is written with the following number (whole command could be: w 3 128)
- //Serial.println("The value of the register now is:");
- //Serial.println(ADS.GetRegisterValue(cin1)); //we want to see if the value is set or not. get back the -cin1- value register's value
- break;
- //-------------------------------------------------------------------------------------------------
- //----------------SINGLE READ----------------------------------------------------------------------
- case 'R': //simple conversion result, reading the first channel in differential mode, once.
- ADS.SendCMD(SELFCAL); //Self calibration
- delay(200); //wait a little
- ADS.SetRegisterValue(MUX, B00000001);//AIN0+AIN1 -- CH0
- //printing the result
- Serial.println(ADS.GetConversion());
- break;
- //----------------------------------------------------------------------------------------------------
- //----------------SELF CALIBRATION--------------------------------------------------------------------
- case 'c': //simple one-command self calibration and offset calibration
- ADS.SendCMD(SELFCAL); //Self calibration
- delay(200); //wait a little
- break;
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- //----------------SINGLE CHANNEL ------------------------------------------------------------------
- case 's': //s: single - 8 channels (more channels, less accuracy(common ground, more noise))
- samplingfreq = Serial.parseFloat();
- //command looks like: "s 300".
- ADS.SendCMD(SELFCAL); //Self calibration
- delay(200); //wait a little
- ADS.SetRegisterValue(DRATE, B01100011); //50 SPS// for 1 sample / second on the PC
- while (Serial.read() != 'N') {
- //Elapsed time since the sw is started
- double elapsedTime = millis() - startTime;
- //prints time since program started
- Serial.print(elapsedTime);
- Serial.print("\t");
- ADS.SetRegisterValue(MUX, B00001000);//AIN0+AINCOM -- CH0
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B00011000);//AIN1+AINCOM -- CH1
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B00101000);//AIN2+AINCOM -- CH2
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B00111000);//AIN3+AINCOM -- CH3
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B01001000);//AIN4+AINCOM -- CH4
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B01011000);//AIN5+AINCOM -- CH5
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B01101000);//AIN6+AINCOM -- CH6
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- delayMicroseconds(100);
- ADS.SetRegisterValue(MUX, B01111000);//AIN7+AINCOM -- CH7
- Serial.print(ADS.GetConversion());
- Serial.print("\n");
- //Full output: Time CH1 CH2 CH3 CH4 CH5 CH6 CH7 CH8
- delay(samplingfreq);
- }
- break;
- //-------------------------------------------------------------------------------------------------
- //----------------DIFFERENTIAL CHANNEL ------------------------------------------------------------
- case 'd': //d: differential - 4 channels (less channels, better accuracy (less noise))
- //another cin for receiving the delay
- samplingfreq = Serial.parseFloat();
- //ADS.SetRegisterValue(DRATE, B11010000); //7500 SPS// for 1 sample / second on the PC
- ADS.SetRegisterValue(DRATE, B01100011);
- ADS.SendCMD(SELFCAL); //Self calibration
- delay(200); //wait a little
- //calibration!
- while (Serial.read() != 'N') { //while we don't stop manially by sending N through the terminal....
- //Elapsed time since the sw is started
- double elapsedTime = millis() - startTime;
- //prints time since program started
- Serial.print(elapsedTime);
- Serial.print("\t");
- //delayMicroseconds(50);
- //Switch on the first channel
- ADS.SetRegisterValue(MUX, B00000001);//AIN0+AIN1 -- CH0
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- //delayMicroseconds(50);
- //Switch on the second channel
- ADS.SetRegisterValue(MUX, B00100011);//AIN2+AIN3 -- CH1
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- //delayMicroseconds(50);
- //Switch on the third channel
- ADS.SetRegisterValue(MUX, B01000101);//AIN4+AIN5 -- CH2
- Serial.print(ADS.GetConversion());
- Serial.print("\t");
- //delayMicroseconds(50);
- //Switch on the fourth channel
- ADS.SetRegisterValue(MUX, B01100111);//AIN6+AIN7 -- CH3
- Serial.print(ADS.GetConversion());
- Serial.print("\n");
- //Final output: time CH1 CH2 CH3 CH4
- //delay by the received sampling frequency
- //there is some time needed for the code above to run, so real sampling frequency is slightly slower (>1%)
- delay(samplingfreq);
- }
- break;
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- default:
- //?
- break;
- //-------------------------------------------------------------------------------------------------
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement