STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sense.cpp
Go to the documentation of this file.
1#ifdef RUN_SENSE_TEST
2#include <ADS126X.h>
3#include <Arduino.h>
4#include <SPI.h>
5
6#define DRDY_PIN 22
7#define DOUT 21 // MISO
8#define DIN 19 // MOSI
9#define SCLK 20
10#define CS 17
11
12#define DRDY_PIN 22 // brown 15
13// #define AINCOM 3
14// #define AVSS 5
15#define DOUT 21 // purple 14
16#define DIN 19 // yellow 7
17#define SCLK 20 // green 13
18#define CS 17 // blue 6
19int i = 0; // ADS126X pin AIN0, for positive input
20int pos_pin = 0; // ADS126X pin AIN0, for positive input
21// int pos_pin = ADS126X_AIN0;
22
23int neg_pin = 0xA; // ADS1263 pin AINCOM, for negative input
24
25unsigned long totalReadTime = 0;
26unsigned long minReadTime = ULONG_MAX;
27unsigned long maxReadTime = 0;
28int readCount = 0;
29unsigned long readTime = 0;
30unsigned long start_time = 0;
31
32void setup() {
34 Serial.begin(115200);
35 Serial.println("Here:");
36 SPI.begin(SCLK, DOUT, DIN, CS);
37
38 adc.begin(CS); // setup with chip select pin
39 adc.startADC1(); // start conversion on ADC1
40 adc.setRate(ADS126X_RATE_38400);
41
42 pinMode(DRDY_PIN, INPUT);
43
44 Serial.println("Reading Voltages:");
45}
46
47void loop() {
49 // REAL FOR LOOP
50 for (int i = 0; i < 10; i++) {
51 totalReadTime += readTime;
52 readCount++;
54 unsigned long readTime = 0;
55
56 long volt_reader = adc.readADC1(i, neg_pin); // read the voltage
57 float voltage = (float)(volt_reader) * 5 / 8388608; // 2^23 = 8388608
58
59 while (digitalRead(DRDY_PIN) == HIGH) {
60 // wait
61 }
63 // if drdy doesnt work, use this and comment out drdy
64
65 volt_reader = adc.readADC1(i, neg_pin); // read the voltage
66 Serial.print(i);
67 Serial.print(": ");
68 Serial.println(volt_reader); // send voltage through serial
69 voltage = (float)(volt_reader) * 5 / 8388608;
70 Serial.print(i);
71 Serial.print(": ");
72 Serial.print(voltage, 6);
73 Serial.println(" V");
74
75 while (digitalRead(DRDY_PIN) == HIGH) {
76 // wait
77 }
78
80 // if drdy doesnt work, use this and comment out drdy
81
82 readTime = micros() - start_time;
83 Serial.println("\nTiming statistics:");
84 Serial.print("Current read time: ");
85 Serial.print(readTime);
86 Serial.println(" Aµs");
87
88 // Calculate effective sampling rate
89 float samplesPerSecond = 10000000.0 / readTime;
90 Serial.print("Effective sampling rate: ");
91 Serial.print(samplesPerSecond, 2);
92 Serial.println(" samples per second");
93
94 Serial.print("Total samples read: ");
95 Serial.println(readCount * 10);
96 }
97}
98#endif // RUN_SENSE_TEST
static SerialStub Serial
Definition Arduino.h:128
#define INPUT
Definition Arduino.h:21
int digitalRead(int)
Definition Arduino.h:50
#define HIGH
Definition Arduino.h:19
void pinMode(int, int)
Definition Arduino.h:46
unsigned long micros()
Definition Arduino.h:37
void delayMicroseconds(unsigned int)
Definition Arduino.h:58
#define ADS126X_RATE_38400
uint8_t neg_pin
Definition constants.h:6
void setup()
Definition main.cpp:571
void loop()
Definition main.cpp:621
static STAR_MCP3201 adc(MCP3201_CS_PIN, &mcp_spi)