|
| 1 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Protocentral ADS1293 Arduino example — 5-lead ECG (OpenView packet format) |
| 4 | +// |
| 5 | +// Author: Ashwin Whitchurch, Protocentral Electronics |
| 6 | +// SPDX-FileCopyrightText: 2020 Protocentral Electronics |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | +// |
| 9 | +// Streams ECG samples in the OpenView packet format. See the OpenView |
| 10 | +// project for the host-side processing tools: |
| 11 | +// https://github.com/Protocentral/protocentral_openview |
| 12 | +// |
| 13 | +// Hardware connections (Arduino UNO / ESP32 VSPI): |
| 14 | +// |
| 15 | +// | Signal | Arduino UNO | ESP32 (VSPI default) | |
| 16 | +// |-------:|:------------:|:--------------------:| |
| 17 | +// | MISO | 12 | 19 | |
| 18 | +// | MOSI | 11 | 23 | |
| 19 | +// | SCLK | 13 | 18 | |
| 20 | +// | CS | 4 | 4 | |
| 21 | +// | VCC | +5V | +5V | |
| 22 | +// | GND | GND | GND | |
| 23 | +// | DRDY | 2 | 2 | |
| 24 | +// |
| 25 | +// For full documentation and examples, see: |
| 26 | +// https://github.com/Protocentral/protocentral-ads1293-arduino |
| 27 | +// |
| 28 | +///////////////////////////////////////////////////////////////////////////////////////// |
| 29 | + |
| 30 | + |
| 31 | +#include "protocentral_ads1293.h" |
| 32 | +#include <SPI.h> |
| 33 | + |
| 34 | +#define CES_CMDIF_PKT_START_1 0x0A |
| 35 | +#define CES_CMDIF_PKT_START_2 0xFA |
| 36 | +#define CES_CMDIF_TYPE_DATA 0x02 |
| 37 | +#define CES_CMDIF_PKT_STOP 0x0B |
| 38 | +#define DATA_LEN 12 |
| 39 | +#define ZERO 0 |
| 40 | + |
| 41 | +#define DRDY_PIN 2 |
| 42 | +#define CS_PIN 4 |
| 43 | + |
| 44 | +// Uncomment to send voltages instead of raw ADC codes |
| 45 | +// #define PRINT_VOLTAGE |
| 46 | + |
| 47 | +// Optional SPI pin overrides |
| 48 | +#if !defined(SCK_PIN) |
| 49 | +#if defined(ARDUINO_ARCH_ESP32) |
| 50 | +#define SCK_PIN 18 |
| 51 | +#define MISO_PIN 19 |
| 52 | +#define MOSI_PIN 23 |
| 53 | +#else |
| 54 | +#define SCK_PIN 13 |
| 55 | +#define MISO_PIN 12 |
| 56 | +#define MOSI_PIN 11 |
| 57 | +#endif |
| 58 | +#endif |
| 59 | + |
| 60 | +ads1293 ADS1293(DRDY_PIN, CS_PIN); |
| 61 | + |
| 62 | +volatile uint8_t DataPacket[DATA_LEN]; |
| 63 | +const uint8_t DataPacketFooter[2] = {ZERO, CES_CMDIF_PKT_STOP}; |
| 64 | +const uint8_t DataPacketHeader[5] = {CES_CMDIF_PKT_START_1, CES_CMDIF_PKT_START_2, DATA_LEN, ZERO, CES_CMDIF_TYPE_DATA}; |
| 65 | + |
| 66 | +void sendDataThroughUart(int32_t ecgCh1, int32_t ecgCh2, int32_t ecgCh3) { |
| 67 | + // pack little-endian 32-bit values |
| 68 | + DataPacket[0] = static_cast<uint8_t>(ecgCh1 & 0xFF); |
| 69 | + DataPacket[1] = static_cast<uint8_t>((ecgCh1 >> 8) & 0xFF); |
| 70 | + DataPacket[2] = static_cast<uint8_t>((ecgCh1 >> 16) & 0xFF); |
| 71 | + DataPacket[3] = static_cast<uint8_t>((ecgCh1 >> 24) & 0xFF); |
| 72 | + |
| 73 | + DataPacket[4] = static_cast<uint8_t>(ecgCh2 & 0xFF); |
| 74 | + DataPacket[5] = static_cast<uint8_t>((ecgCh2 >> 8) & 0xFF); |
| 75 | + DataPacket[6] = static_cast<uint8_t>((ecgCh2 >> 16) & 0xFF); |
| 76 | + DataPacket[7] = static_cast<uint8_t>((ecgCh2 >> 24) & 0xFF); |
| 77 | + |
| 78 | + DataPacket[8] = static_cast<uint8_t>(ecgCh3 & 0xFF); |
| 79 | + DataPacket[9] = static_cast<uint8_t>((ecgCh3 >> 8) & 0xFF); |
| 80 | + DataPacket[10] = static_cast<uint8_t>((ecgCh3 >> 16) & 0xFF); |
| 81 | + DataPacket[11] = static_cast<uint8_t>((ecgCh3 >> 24) & 0xFF); |
| 82 | + |
| 83 | + // send packet header |
| 84 | + for (int i = 0; i < 5; ++i) { |
| 85 | + Serial.write(DataPacketHeader[i]); |
| 86 | + } |
| 87 | + |
| 88 | + // send actual data |
| 89 | + for (int i = 0; i < DATA_LEN; ++i) { |
| 90 | + Serial.write(DataPacket[i]); |
| 91 | + } |
| 92 | + |
| 93 | + // send packet footer |
| 94 | + for (int i = 0; i < 2; ++i) { |
| 95 | + Serial.write(DataPacketFooter[i]); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Helper: convert 24-bit unsigned raw value to signed 32-bit using two's-complement |
| 100 | +static int32_t raw24_to_int24(uint32_t raw24) { |
| 101 | + raw24 &= 0xFFFFFFu; |
| 102 | + if (raw24 & 0x800000u) { |
| 103 | + return static_cast<int32_t>(raw24 | 0xFF000000u); |
| 104 | + } |
| 105 | + return static_cast<int32_t>(raw24); |
| 106 | +} |
| 107 | + |
| 108 | +// Helper: build a 24-bit raw value from three bytes (MSB, mid, LSB) |
| 109 | +static uint32_t raw24_from_bytes(uint8_t msb, uint8_t mid, uint8_t lsb) { |
| 110 | + return (static_cast<uint32_t>(msb) << 16) | (static_cast<uint32_t>(mid) << 8) | static_cast<uint32_t>(lsb); |
| 111 | +} |
| 112 | + |
| 113 | +// Uncomment to enable debug register/sample dumps on startup |
| 114 | +// #define ENABLE_DEBUG |
| 115 | + |
| 116 | +void setup() { |
| 117 | + Serial.begin(57600); |
| 118 | +#if defined(ARDUINO_ARCH_ESP32) |
| 119 | + ADS1293.begin(SCK_PIN, MISO_PIN, MOSI_PIN); |
| 120 | +#else |
| 121 | + ADS1293.begin(); |
| 122 | +#endif |
| 123 | + // Configure device for 5-lead ECG using explicit helpers |
| 124 | + ADS1293.configureChannel1(FlexCh1Mode::Default); |
| 125 | + ADS1293.configureChannel2(FlexCh2Mode::Default); |
| 126 | + ADS1293.configureChannel3(FlexCh3Mode::Default); |
| 127 | + ADS1293.enableCommonModeDetection(CMDetMode::Enabled); |
| 128 | + ADS1293.configureRLD(RLDMode::Default); |
| 129 | + ADS1293.configureRef(RefMode::Default); |
| 130 | + ADS1293.configureAFEShutdown(AFEShutdownMode::AFE_On); |
| 131 | + // Set PGA gain=8 for all channels |
| 132 | + ADS1293.setChannelGain(1, ADS1293::PgaGain::G8); |
| 133 | + ADS1293.setChannelGain(2, ADS1293::PgaGain::G8); |
| 134 | + ADS1293.setChannelGain(3, ADS1293::PgaGain::G8); |
| 135 | + // Ensure example runs at 100 SPS using the DRATE-based API |
| 136 | + ADS1293.setSamplingRate(ADS1293::SamplingRate::SPS_100); |
| 137 | + ADS1293.configureDRDYSource(DRDYSource::Default); |
| 138 | + ADS1293.configureChannelConfig(ChannelConfig::Default5Lead); |
| 139 | + ADS1293.applyGlobalConfig(GlobalConfig::Start); |
| 140 | + delay(10); |
| 141 | + |
| 142 | +#if defined(ENABLE_DEBUG) |
| 143 | + ADS1293.dumpDebug(Serial); |
| 144 | +#endif |
| 145 | +} |
| 146 | + |
| 147 | +void loop() { |
| 148 | + if (digitalRead(DRDY_PIN) == LOW) { |
| 149 | + // Use convenience overload to get all three channels in one call |
| 150 | + auto samples = ADS1293.getECGData(); |
| 151 | + if (samples.ok) { |
| 152 | + // Read raw 24-bit values from the device, sign-extend to int32_t, |
| 153 | + // and send as 32-bit little-endian (LSB first) in the packet payload. |
| 154 | + uint32_t raw1 = 0, raw2 = 0, raw3 = 0; |
| 155 | + if (ADS1293.getRaw24(1, raw1) && ADS1293.getRaw24(2, raw2) && ADS1293.getRaw24(3, raw3)) { |
| 156 | + int32_t s1 = raw24_to_int24(raw1); |
| 157 | + int32_t s2 = raw24_to_int24(raw2); |
| 158 | + int32_t s3 = raw24_to_int24(raw3); |
| 159 | + sendDataThroughUart(s1, s2, s3); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + delay(10); |
| 164 | +} |
| 165 | + |
0 commit comments