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 "STAR_ADS126X.h"
5
6// Change 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::RTD_Board
9
10// These lines MUST be after the #define PINS_ACTIVE_LAYOUT or they will
11// overwrite it with the default value!
12#include "connector_adc_map.h"
13#include "sense_board_pins.h"
14
15using namespace sense_board_pins;
16
18
19float convert_code_to_voltage(int32_t code) {
20 // Assumes the 2.5V internal reference is being used!
21 return ((float)code * 2.5f) / 2147483648.0f;
22}
23
24void setup() {
25 Serial.begin(115200);
26 while (!Serial) {
27 delay(10);
28 }
29
30 delay(1000);
31
32 // Setup SPI
33 SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI, Pins.ADC_CS_1);
34
35 // You can set the freq if you want, or just use the default
36 // SPI.setFrequency(1'000'000);
37
38 // Due to the ADC output having valid data on FALLING CLK edges
39 SPI.setDataMode(SPI_MODE1);
40 pinMode(Pins.ADC_DRDY_1, INPUT);
41
42 // Setup ADC
43 ads126x.begin(Pins.ADC_CS_1);
44
45 // Stop it while we config it, as suggested by datasheet
46 ads126x.stopADC1();
47
48 // Set the input to ADC1 to be the whatever pin you want
49 ads126x.setInputMux(getAdcChannel(2, 1), getAdcChannel(2, 2));
50
51 // Bypas the PGA, so it does not affect measurements
52 ads126x.bypassPGA();
53
54 // Set the filter. You can change this to try different filters
55 ads126x.setFilter(ADS126X_SINC4);
56
57 // Set the datarate. You can change this, but the options depends on the
58 // filter I do not know what happens if you program an invalid data rate for
59 // a given filter
61
62 ads126x.setBiasMagnitude(ADS126X_BIAS_MAG_10M);
63
64 // Start ADC now that configuration is done
65 ads126x.startADC1();
66}
67
68void loop() {
69 // Wait for DRDY pin
70 while (digitalRead(Pins.ADC_DRDY_1) != LOW) {
72 }
73
75
76 // Get the most recent value
77 const auto reading = ads126x.readADC1();
78 Serial.print(F("Current reading: "));
79 Serial.print(convert_code_to_voltage(reading.value), 6);
80 Serial.println(" V");
81
82 // Checks for invalid checksums, which signal corruption
83 if (!reading.checksumValid) {
84 Serial.println("Bad checksum!");
85 }
86}
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_BIAS_MAG_10M
#define ADS126X_RATE_1200
#define ADS126X_SINC4
void delay(int ms)
static ADS126X ads126x
Definition main.cpp:20
void setup()
Definition main.cpp:24
float convert_code_to_voltage(int32_t code)
Definition main.cpp:19
void loop()
Definition main.cpp:68
constexpr int getAdcChannel(uint8_t connector, uint8_t pin)
constexpr const Layout & Pins