I am doing a project where I needed to use four ADS1252. I came across this code on the forum.
As I also required using an LCD screen (communication over SPI), I opted to use bit-banging to read the ADC’s
//example code for ADS1252 24-bit ADC with UNO R4
#include "pwm.h"
//constants derived from datasheet (https://www.ti.com/lit/ds/symlink/ads1252.pdf)
#define f_ADCLK 1000000UL //1MHz
#define f_MCLK (f_ADCLK / 6)
#define f_DRATE (f_MCLK / 64)
#define p_MCLK 6 // 1/f_MCLK = 6us <---- need to update if f_ADSCLK changed
#define t_DRDY (14 * p_MCLK) //(t2+t3) * p_MCLK, a little more that the minimum time, ;)
#define t_DOUT (348 * p_MCLK)
#define t_CONVCYCLE (384 * p_MCLK)
#define t_RESET ((5 * t_CONVCYCLE) / 1000) //time in ms, a little more that the minimum time, 4 * t_CONVCYCLE ;)
#define ADC_BITS 24
//pin definitions
#define CLKPIN D6
#define ADC1_SCLKPIN D7
#define ADC1_DOUTPIN D2
#define MUX_A D4
#define MUX_B D5
// Instantiate a PwmOut object for pin D6
PwmOut pwm(CLKPIN);
//global variables
volatile int32_t ADC1_readin = 0;
volatile uint8_t intr_enabled = 1;
char *str[4] = { "ADC4 ", "ADC1 ", "ADC2 ", "ADC3 " };
//function for multiplexer selector
uint8_t mux_Update() {
static uint8_t mux_status = 3;
mux_status = (mux_status + 1) % 4;
switch (mux_status) {
case 0:
digitalWrite(MUX_A, LOW);
digitalWrite(MUX_B, LOW);
break;
case 1:
digitalWrite(MUX_A, HIGH);
digitalWrite(MUX_B, LOW);
break;
case 2:
digitalWrite(MUX_A, HIGH);
digitalWrite(MUX_B, HIGH);
break;
case 3:
digitalWrite(MUX_A, LOW);
digitalWrite(MUX_B, HIGH);
break;
}
return mux_status;
}
//function to read the ADC
void read_adc() {
int32_t ADC_readin = 0;
uint8_t t = p_MCLK / 2;
// wait for DRDY to pass and to reach start-point of DOUT
delayMicroseconds(t_DRDY);
//confirm the pin is still low
if (digitalRead(ADC1_DOUTPIN) == LOW) {
//big banging to read in the adc 24 bits
for (uint8_t i = 0; i < ADC_BITS; ++i) {
delayMicroseconds(t);
digitalWrite(ADC1_SCLKPIN, HIGH);
delayMicroseconds(t);
ADC_readin <<= 1;
ADC_readin |= digitalRead(ADC1_DOUTPIN);
digitalWrite(ADC1_SCLKPIN, LOW);
}
//update global variable
ADC_readin<<=8;
ADC_readin/=256;
ADC1_readin = ADC_readin; //+ 0x800000UL;
intr_enabled = 0;
detachInterrupt(digitalPinToInterrupt(ADC1_DOUTPIN));
}
}
void setup() {
Serial.begin(115200);
// Configure the PWM pin for 1,000,000 Hz frequency (1 us period)
// and an initial duty cycle of 50.0%
// The 'begin' method takes frequency (float) and initial duty cycle (float percentage)
float freq = f_ADCLK * 1.0f;
pwm.begin(freq, 50.0f);
//configure pins
pinMode(ADC1_SCLKPIN, OUTPUT);
pinMode(ADC1_DOUTPIN, INPUT_PULLUP);
pinMode(MUX_A, OUTPUT);
pinMode(MUX_B, OUTPUT);
// to reset ADC, we need SCLK HIGH for min of 4 CONVCYCLES
digitalWrite(ADC1_SCLKPIN, HIGH);
delay(t_RESET);
digitalWrite(ADC1_SCLKPIN, LOW);
mux_Update();
//enable interrupt to start ADC read
attachInterrupt(digitalPinToInterrupt(ADC1_DOUTPIN), read_adc, FALLING);
}
void loop() {
if (intr_enabled == 0) {
Serial.print(str[mux_Update()]);
Serial.println(ADC1_readin);
intr_enabled = 1;
//enable interrupt for ADC read
attachInterrupt(digitalPinToInterrupt(ADC1_DOUTPIN), read_adc, FALLING);
}
}
this code works just fine. I thought I’d post it here for anyone who may need a similar approach.
the only thing that was a bit of a bummer was that I had to include a 4:1 multiplexer in my circuit as it seems the the R4 as per its documentation only supports two external interrupts! ![]()
If anyone knows and would like to share how to increase the number of external interrupts (the uC definitely can I believe!) on an R4 it would be the cherry on top of the icing! ![]()