STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <SPI.h>
3
4#include "my_ADS126X.h"
5
6// Chnage the following line to automatically use the correct pins for the board
7// being tested (PT_Board, LC_Board, RTD_Board, or TC_Board)
8#define PINS_ACTIVE_LAYOUT sense_board_pins::PT_Board
9
10// This line MUST be after the #define PINS_ACTIVE_LAYOUT or it will override it
11#include "sense_board_pins.h"
12
14
16
17float convert_code_to_voltage(int32_t code) {
18 // Assumes the 2.5V internal reference is being used!
19 return ((float)code * 2.5f) / 2147483648.0f;
20}
21
22void setup() {
23 Serial.begin(115200);
24
25 // Setup SPI
26 SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI, Pins.ADC_CS_1);
27
28 // You can set the freq if you want, or just use the default
29 // SPI.setFrequency(1'000'000);
30
31 // Due to the ADC output having valid data on FALLING CLK edges
32 SPI.setDataMode(SPI_MODE1);
33 pinMode(Pins.ADC_DRDY_1, INPUT);
34
35 // Setup ADC
36 ads126x.begin(Pins.ADC_CS_1);
37
38 // Stop it while we config it, as suggested by datasheet
39 ads126x.stopADC1();
40
41 // If the two lines below are uncommented, the TDACs
42 // will be connected to AIN6 and AIN7. This is useful for probing,
43 // but makes the TDACs "see" our analog front end which could cause issues
44 // ads126x.setOutputTDACP(1);
45 // ads126x.setOutputTDACN(1);
46
47 // Sets the positive TDAC to 3V, since we have 5V power
48 ads126x.setOutputmagnitudeTDACP(ADS126X_TDAC_DIV_0_6);
49
50 // Sets the negative TDAC to 2.5V, since we have 5V power
51 ads126x.setOutputmagnitudeTDACN(ADS126X_TDAC_DIV_0_5);
52
53 // Set the input to ADC1 to be the TDAC
54 ads126x.setInputMux(ADS126X_TDAC, ADS126X_TDAC);
55
56 // Bypas the PGA, so it does not affect measurements
57 ads126x.bypassPGA();
58
59 // Set the filter. You can change this to try different filters
60 ads126x.setFilter(ADS126X_SINC4);
61
62 // Set the datarate. You can change this, but the options depends on the
63 // filter I do not know what happens if you program an invalid data rate for
64 // a given filter
66
67 // Start ADC now that configuration is done
68 ads126x.startADC1();
69}
70
71void loop() {
72 // Wait for DRDY pin
73 while (digitalRead(Pins.ADC_DRDY_1) != LOW) {
75 }
76
78
79 // Get the most recent value
80 const auto reading = ads126x.readADC1();
81 Serial.print(F("TDAC reading: "));
82 Serial.print(convert_code_to_voltage(reading.value), 6);
83 Serial.println(" V");
84
85 // Checks for invalid checksums, which signal corruption
86 if (!reading.checksumValid) {
87 Serial.println("Bad checksum!");
88 }
89}
static SerialStub Serial
Definition Arduino.h:128
#define INPUT
Definition Arduino.h:21
int digitalRead(int)
Definition Arduino.h:50
void pinMode(int, int)
Definition Arduino.h:46
#define SPI_MODE1
Definition Arduino.h:131
#define LOW
Definition Arduino.h:20
void delayMicroseconds(unsigned int)
Definition Arduino.h:58
#define ADS126X_TDAC_DIV_0_5
Definition STAR_ADS126X.h:7
#define ADS126X_TDAC_DIV_0_6
Definition STAR_ADS126X.h:6
#define ADS126X_TDAC
Definition STAR_ADS126X.h:5
#define ADS126X_RATE_38400
#define ADS126X_SINC4
void setup()
Definition main.cpp:142
static ADS126X ads126x
Definition main.cpp:20
constexpr const Layout & Pins
void loop()
Definition main.cpp:201
float convert_code_to_voltage(int32_t code)
Definition main.cpp:17
constexpr const Layout & Pins