Hello,
I am using an STM32 to communicate with the ADS1241E/1K. I am able to use the RREG and WREG commands to read and write to registers properly, as this image shows. The image show that I wrote and read back the correct values to the MUX register. However, when reading from DOR, as you can see, the value is 0xFFFFE7. I am always getting this or 0xFFFFFF, regardless of what input I set for the channel I am reading (currently working with channel 1).
I am noticing that I did not set IOCON, DIR, or DOR, but it says the reset value is 0x00, and this is a new chip. I will try looking at these registers tomorrow, but I am not sure if this is what is causing the issue.
Also, I notice now that I co...
Hello,
I am using an STM32 to communicate with the ADS1241E/1K. I am able to use the RREG and WREG commands to read and write to registers properly, as this image shows. The image show that I wrote and read back the correct values to the MUX register. However, when reading from DOR, as you can see, the value is 0xFFFFE7. I am always getting this or 0xFFFFFF, regardless of what input I set for the channel I am reading (currently working with channel 1).
I am noticing that I did not set IOCON, DIR, or DOR, but it says the reset value is 0x00, and this is a new chip. I will try looking at these registers tomorrow, but I am not sure if this is what is causing the issue.
Also, I notice now that I could have used the RDATA command instead of RREG. I will try this tomorrow too.
However, from what I can tell, the code below should still be outputting proper results. I was wondering what I should change in the code below. I know it is not the prettiest, but I am on a time-crunch and want to prioritize getting the ADC functional for now.
adc.c:
#pragma once #include "stm32g4xx_hal.h" #include "spi.h" #include "adc.h" #define SETUP 0 #define MUX 1 #define DOR2 0xD #define DOR1 0xE #define DOR0 0xF #define RESOLUTION 12 #define VREF 3.3 #define RREG 0b0001 #define WREG 0b0101 adc_data_voltage_t adc_voltage; HAL_StatusTypeDef adc_write_registers(uint8_t adc_num, uint8_t reg, uint8_t *val, uint8_t num){ uint8_t n = num-1; uint8_t r = (uint8_t)(WREG << 4) | reg; uint8_t gpio_pin; GPIO_TypeDef *gpio_port; switch(adc_num){ case 0: gpio_port = GPIOA; gpio_pin = GPIO_PIN_4; break; case 1: gpio_port = GPIOE; gpio_pin = GPIO_PIN_4; break; } uint8_t buffer[256]; uint8_t dummy[256]; memcpy(buffer, &r, 1); memcpy(buffer+1, &n, 1); memcpy(buffer+2, val, num); send_bytes_SPI(buffer, num+2, gpio_port, gpio_pin); } HAL_StatusTypeDef adc_read_registers(uint8_t adc_num, uint8_t reg, uint8_t *receive, uint8_t num){ uint8_t n = num-1; uint8_t r = (uint8_t)(RREG << 4) | reg; uint8_t gpio_pin; GPIO_TypeDef *gpio_port; switch(adc_num){ case 0: gpio_port = GPIOA; gpio_pin = GPIO_PIN_4; break; case 1: gpio_port = GPIOE; gpio_pin = GPIO_PIN_4; break; } uint8_t buffer[256]; memcpy(buffer, &r, 1); memcpy(buffer+1, &n, 1); memset(buffer+2, 0xFF, n); send_receive_bytes_SPI(buffer, receive, num+2, gpio_port, gpio_pin); } HAL_StatusTypeDef adc_init(uint8_t adc_num){ SPI_Init(); uint8_t temp = 0; adc_write_registers(adc_num, SETUP, &temp, 1); //Gain = 1 } HAL_StatusTypeDef adc_read_data(uint8_t adc_num, uint8_t channel){ uint8_t val = channel << 4; uint8_t temp = 0; adc_write_registers(adc_num, MUX, &val, 1); adc_read_registers(adc_num, MUX, &temp,1); uint8_t receive[3]; adc_read_registers(adc_num, DOR2, receive, 3); uint32_t result = ((uint32_t)receive[0] << 16 | (uint32_t)receive[1] << 8 | (uint32_t)receive[0]); if(adc_num == 0){ result = VREF*(result)/((2 << RESOLUTION)-1); switch(channel){ case 0: adc_voltage.V0 = result; break; case 1: adc_voltage.V1 = result; break; case 2: adc_voltage.V2 = result; break; case 3: adc_voltage.V3 = result; break; default: break; } } else if(adc_num == 1){ switch(channel){ case 0: break; case 1: break; case 2: break; case 3: break; default: break; } } } main.c:
int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); adc_init(1); while (1) { adc_read_data(1, 1); HAL_Delay(1000); } } Let me know if you need to see more of the code, but SPI is working and I do not think the peripheral needs changing, especially considering the ADC is responding properly to everything besides the actual digital voltage values. I might be missing something when configuring the chip.
Thanks for your time and help.