Read ADS1231 data by timer interrupt

Hi guys

I am working on a project need to pass the ADS1231 read data through BLE. But I haven't done so far. I just try to read the ADS1231 value first. Referring to some source online, I have my data read functions as below, and they work fine without BLE environment.

ADS1231 datasheets.pdf

/*
*** jousis's gitlab open source code for reference
*** <Scale FeatherWing>
*** https://gitlab.com/jousis/scale-featherwing/-/tree/master/Firmware/nRF52832/ScaleTest
*/


#include <stdint.h>
#include "ADS1231.h"

#define SCL_PIN 15
#define DOUT_PIN 16
#define SPEED_PIN 29
#define PWDN_PIN 7

void ADS1231_init(void){
		// set scl_pin output
		nrf_gpio_cfg_output(SCL_PIN);
		// set dout_pin input
		nrf_gpio_cfg_input(DOUT_PIN, NRF_GPIO_PIN_PULLUP);
		// set pwdn_pin
		nrf_gpio_cfg_output(PWDN_PIN);
		// reset ADS1231
		ADS1231_powerOn();
		setSpeed(0);										// should controled by 
}


void ADS1231_powerOn(void){					
		nrf_gpio_pin_clear(PWDN_PIN);		// make pwdn_pin low, to reset the ADC
		nrf_delay_us ( 52 ); 						// Datasheet p15. At least 26us ( Security Factor: 2*26us = 52us )
		nrf_gpio_pin_set(PWDN_PIN);
		nrf_gpio_pin_clear(SCL_PIN);		// initial state
		// should be safeguarding
}


int32_t ADS1231_readADC(void){
		int32_t adcvalue = 0, digit = 0;
		int i=0;
		for(i=0; i<24; i++){						// 24 pulses
				nrf_gpio_pin_set(SCL_PIN);
				nrf_delay_us(1);
				digit = nrf_gpio_pin_read(DOUT_PIN);
				adcvalue = (adcvalue << 1) + digit;
				nrf_gpio_pin_clear(SCL_PIN);
				nrf_delay_us(1);
		}
		nrf_gpio_pin_set(SCL_PIN);			// keep dout high // 25th pulse
		nrf_delay_us(1);
		nrf_gpio_pin_clear(SCL_PIN);
		// yield() to confirm dout is high
		//adcvalue = (adcvalue << 8) / 256;
		return adcvalue;
}


void setSpeed(uint8_t sps){
		if(sps>0){
				nrf_gpio_pin_set(SPEED_PIN);
		}
		else{
				nrf_gpio_pin_clear(SPEED_PIN);
		}
}

uint32_t * ADS1231_readADCbyte(void){
		static int32_t value[24] = {0};
		int i=0;
		
		for(i=0; i<24; i++){						// 24 pulses
				nrf_gpio_pin_set(SCL_PIN);
				nrf_delay_us(1);
				value[i] = nrf_gpio_pin_read(DOUT_PIN)+48;
				nrf_gpio_pin_clear(SCL_PIN);
				nrf_delay_us(1);
		}
		nrf_gpio_pin_set(SCL_PIN);			// keep dout high // 25th pulse
		nrf_delay_us(1);
		nrf_gpio_pin_clear(SCL_PIN);
		// yield() to confirm dout is high
		//adcvalue = (adcvalue << 8) / 256;
		return value;
}

main.c

int main(void)
{
	
		uint32_t err_code;
	
		APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    //NRF_LOG_DEFAULT_BACKENDS_INIT();
		
		bsp_board_init(BSP_INIT_LEDS);
	
		uart_init();
		ADS1231_init();
		int32_t adcValue;
		uint32_t* adcByte;
	
    printf("\r\nUART example started.\r\n");

	
    while (1)
    {
			#ifdef adcByte
				adcByte = ADS1231_readADCbyte();
				
				for(int i=0; i<24; i++){
						while(app_uart_put(adcByte[i]));
						//printf("%d", adcByte[i]);
				}
				printf("\r\n");
				//printf("ADC value: %x", adcValue);
				//NRF_LOG_FLUSH();
			#else
				adcValue = ADS1231_readADC();
				printf("ADC value: %d\r\n", adcValue-load_cell_offset);
			#endif
				nrf_delay_ms(100);
    }
}

Then I try to use timer interrupt to call the ADS1231_readADC()

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
// ADS
#include "ADS1231.h"
// UART
#include "nrf.h"
#include "bsp.h"
#include "nrf_uart.h"
#include "nrf_uarte.h"
#include "app_uart.h"
// ISR
#include "app_timer.h"

#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED

#define load_cell_offset 0

APP_TIMER_DEF(ads1231_timer_id);

int8_t rx[100] = {0};

void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
} 

static void uart_init(void){
		uint32_t err_code;
	
		const app_uart_comm_params_t comm_params =
    {
        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        UART_HWFC,
        false,
        NRF_UART_BAUDRATE_115200
    };
			
		
    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);
}


static void ads1231_timer_handler(void * p_context)
{
		ret_code_t err_code;
		//static uint32_t * ADC_data;
		int32_t ADCValue;
		
		//ADC_data = ADS1231_readADCbyte();
		ADCValue = ADS1231_readADC();
		printf("ADC value: %d",ADCValue);
		
}


static void timers_init(void)
{
    // Initialize timer module.
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    // Create timers.
	
    err_code = app_timer_create(&ads1231_timer_id, APP_TIMER_MODE_REPEATED, ads1231_timer_handler);
    APP_ERROR_CHECK(err_code);
}



int main(void)
{
	
		uint32_t err_code;
	
		APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    //NRF_LOG_DEFAULT_BACKENDS_INIT();
		
		bsp_board_init(BSP_INIT_LEDS);
	
		uart_init();
		timers_init();
		ADS1231_init();
	
    err_code = app_timer_start(ads1231_timer_id, APP_TIMER_TICKS(100), NULL);
    APP_ERROR_CHECK(err_code);
	
		int32_t adcValue;
		uint32_t* adcByte;
	
    printf("\r\nUART example started.\r\n");

	
    while (1)
    {
				
    }
}

The program can be compiled and run, the interrupt also worked. But I can't get the correct value from ADS1231. The clock cycles I made by ADS1231_readADC() didn't work, so all the data I get were in high mode. That means, normally ADS1231 has 24 bits data, and now they were all in 1 mode.

I guess the problem may caused by the timer confliction, but I am not sure about it. Please give me some guide. This is my first nRF52832 project, I have really no idea how to deal with this problem. Thank you very much.

Parents Reply Children
Related