Recording 2channel analogue signal NRF52832

Hello, I am trying to record the analog signal by using NRF52832.

The problem is, when I record the signal only with one channel, the signal looks very fine, up to a 2kHz sampling rate.

However, if I just added another channel to the code, I lost much data, even from the 100Hz sampling rate.

Is there any reason for this situation?

I will upload a code that I think is relevant to the recording and sending of a signal.

<code>

static int AIN_INX = 0;

#define AIN_1                   0 // P0.03/AIN 1
#define AIN_3                   2 // P0.04/AIN 2 for Input value
#define PAD_1_MASK              (1UL << AIN_1)
#define PAD_2_MASK              (1UL << AIN_3)
 
#define PAD_ID_0                0
#define PAD_ID_1                1

#define UART_TX_BUF_SIZE 512                              /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1                                /**< UART RX buffer size. */

volatile uint32_t threshold_value_pad1 = 3000;
volatile uint32_t threshold_value_pad2 = 3000;


#define ADC_TIMER_TICKS_TIMEOUT	APP_TIMER_TICKS(1)      // decide sampling rate
#define HRS_BUFFER_LEN	 150

uint16_t InputVal1_buf[HRS_BUFFER_LEN];
uint16_t InputVal2_buf[HRS_BUFFER_LEN];

static void heart_rate_meas_timeout_handler(void * p_context)
{
	ret_code_t      err_code;
    UNUSED_PARAMETER(p_context);
		if (AIN_INX == 0){
				err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, InputVal2_buf);
				array_initialize(InputVal2_buf);
				buf1 = 0;
		}
		else{
				err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, InputVal1_buf);
				array_initialize(InputVal1_buf);
				buf0 = 0;
		}

		if ((err_code != NRF_SUCCESS) &&
				(err_code != NRF_ERROR_INVALID_STATE) &&
				(err_code != NRF_ERROR_RESOURCES) &&
				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
			 )
		{
			APP_ERROR_HANDLER(err_code);
		}
		else;


		tmp = 0;
		btmpChk = true;
}

void csense_handler(nrf_drv_csense_evt_t * p_event_struct)
{   
switch (p_event_struct->analog_channel)
    {
				case AIN_1:
				if(buf_chk == false){
					input_Val = (int32_t)p_event_struct->read_value + VOLTAGE_OFFSET;
					
					if(input_Val <= 0){
						input_Val = 0;
					}
					else if(input_Val >= read_val_max){
						input_Val = read_val_max;
					}
					buf_chk = true;
				}
				
				case AIN_3:
				if(buf_chk == true){
				input_Val = (int32_t)p_event_struct->read_value + VOLTAGE_OFFSET;
				
				if(input_Val <= 0){
					input_Val = 0;
				}
				else if(input_Val >= read_val_max){
					input_Val = read_val_max;
				}
				buf_chk = true;
				}
				default:
				break;
    }
			if (AIN_INX == 0){
				if ((buf0 < HRS_BUFFER_LEN) && (buf0 >= 0)){
					InputVal1_buf[buf0] = (int16_t)input_Val*255/read_val_max ;
					if(InputVal1_buf[buf0] >= 255) InputVal1_buf[buf0] = 255;
					buf0++;
				}
				else{
					InputVal2_buf[buf1] = (int16_t)input_Val*255/read_val_max ;
					if(InputVal2_buf[buf1] >= 255) InputVal2_buf[buf1] = 255;
					buf1++;
					buf0 = 0;
					AIN_INX = 1;
				}
			}
			else if (AIN_INX == 1){
				if ((buf1 < HRS_BUFFER_LEN) && (buf1 >= 0)){
					InputVal2_buf[buf1] = (int16_t)input_Val*255/read_val_max ;
					if(InputVal2_buf[buf1] >= 255) InputVal2_buf[buf1] = 255;
					buf1++;
				}
				else{
					InputVal1_buf[buf0] = (int16_t)input_Val*255/read_val_max ;
					if(InputVal1_buf[buf0] >= 255) InputVal1_buf[buf0] = 255;
					buf0++;
					buf1 = 0;
					AIN_INX = 0;
				}
			}
			else;

}

void csense_initialize(void)
{
    ret_code_t err_code;

    nrf_drv_csense_config_t csense_config = { 0 };

#if USE_COMP == 0
    csense_config.output_pin = OUTPUT_PIN;
#endif
    NRF_LOG_INFO("csense_initialize.\r\n");

    err_code = nrf_drv_csense_init(&csense_config, csense_handler);
    APP_ERROR_CHECK(err_code);

    nrf_drv_csense_channels_enable(PAD_1_MASK | PAD_2_MASK);
}

int main(void)
{
	
    NRF_CLOCK->TASKS_HFCLKSTART = 1; 
    while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
        ;

    bool erase_bonds = false;

    log_init();
    timers_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();
  	application_timers_start();
    services_init();
    sensor_simulator_init();

    NRF_LOG_INFO("Heart Rate Sensor example started.\r\n");

    advertising_start(erase_bonds);
    csense_initialize();

	SEGGER_RTT_WriteString(0,"Hello World from SEGGER!\r\n");

    pwm_init();

    // Enter main loop.
    while (1)
    {

        {
            pwm_update_duty_cycle(50);
        }
			
			
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
						NRF_LOG_FLUSH();
					__WFI();
        }
    }
}

even worse is 

#define ADC_TIMER_TICKS_TIMEOUT 	APP_TIMER_TICKS(10)

This is the line where I determine the sampling rate.

(ex,

APP_TIMER_TICKS(10) = 100Hz sampling rate

APP_TIMER_TICKS(100) = 10Hz sampling rate

)

But the problem is it seems it doesn't work the same for both channels.

I thought I record both signal at 100Hz sampling rate (#define ADC_TIMER_TICKS_TIMEOUT APP_TIMER_TICKS(10))

However, one channel has been recorded at a 200 Hz sampling rate, while another has been recorded at a 100 Hz sampling rate.

It would be sincerely appreciated if someone could help me with this.

Please let me know if you need any further code of mine to fix this situation,

Thank you very much

  • The code that I used as a reference is the 'Heart rate measurement' example!

  • Hello,

    Are these SAADC samplings triggered by the CPU directly, or have you set them to trigger through PPI?
    If you were to use the CPU to trigger the samplings then the samples might be delayed whenever a higher-priority task is currently executing on the CPU, such as in the case of BLE activity.
    In that case, your samples will not actually be taken when the timer timeout occurs, which could lead to the type of samples you are seeing.
    If you instead set up the SAADC sample TASK to trigger by the TIMER Capture/Compare EVENT, then the sampling will be triggered immediately once the Capture/compare event is generated, without CPU interaction.
    The CPU then only needs to wake up to process the SAADC buffer when it is full, but not for each sampling to take place.
    Using the PPI approach is required if you need high accuracy on the timing of the samples, such as when sampling a high frequency signal.

    You could take a look at this example to see how you could set up the SAADC to trigger by a timer through PPI.

    Could you also clarify whether you are working with the nRF Connect SDK or the nRF5 SDK, and which version?
    Lastly, could you show the code of your SAADC initialization as well?

    Best regards,
    Karl

  • I sincerely appreciate your response.

    1. I don't think PPI is triggering it, and the situation you told me about would be the exact one I am faced with, I think. It was fine with one-channel recording but not for multi.

    2. I tried to implant the sample code you gave me, but there is an error message "identifier "m_nus" is undefined" when I compile the code. But I cannot find a line where define "m_nus" from the sample code...
    How should I define the "m_nus" in this case?

    3. I am not sure what initialization code you want, but I hope this can help.

    static void heart_rate_meas_timeout_handler(void * p_context)
    {
    	ret_code_t      err_code;
        UNUSED_PARAMETER(p_context);
    		if (AIN_INX == 0){
    				err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, InputVal2_buf);
    				array_initialize(InputVal2_buf);
    				buf1 = 0;
    		}
    		else{
    				err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, InputVal1_buf);
    				array_initialize(InputVal1_buf);
    				buf0 = 0;
    		}
    
    		if ((err_code != NRF_SUCCESS) &&
    				(err_code != NRF_ERROR_INVALID_STATE) &&
    				(err_code != NRF_ERROR_RESOURCES) &&
    				(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    			 )
    		{
    			APP_ERROR_HANDLER(err_code);
    		}
    		else;
    
    
    		tmp = 0;
    		btmpChk = true;
    }
    
    
    void csense_initialize(void)
    {
        ret_code_t err_code;
    
        nrf_drv_csense_config_t csense_config = { 0 };
    
    #if USE_COMP == 0
        csense_config.output_pin = OUTPUT_PIN;
    #endif
        NRF_LOG_INFO("csense_initialize.\r\n");
    
        err_code = nrf_drv_csense_init(&csense_config, csense_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_csense_channels_enable(PAD_1_MASK); | PAD_2_MASK);
    }


    4. I am working with the Keil V5.28.0

    Once again, really appreciate your help, and please let me know if there is anything that I can tell you more.

  • Hello,

    hyhaerong said:
    I sincerely appreciate your response.

    No problem at all, I am happy to help! :)

    hyhaerong said:
    2. I tried to implant the sample code you gave me, but there is an error message "identifier "m_nus" is undefined" when I compile the code. But I cannot find a line where define "m_nus" from the sample code...
    How should I define the "m_nus" in this case?

    The m_nus in this case is the handle for the Nordic Uart Service (NUS) which is used to transport the measurements over BLE in the provided example.
    If you do not intend to use BLE to transfer your measurements you can omit merging the BLE parts of the example to your application.

    hyhaerong said:
    3. I am not sure what initialization code you want, but I hope this can help.

    I was thinking about the SAADC initialization, but it is not as important now that we know that you were not using PPI initially.

    Is your code working as expected when you remove the BLE parts from the example?

    hyhaerong said:
    4. I am working with the Keil V5.28.0

    Thank you for clarifying! Which version of the nRF5 SDK are you working with?
    If you are starting a new project we highly recommend starting with the nRF Connect SDK.

    Best regards,
    Karl

  • Oh, sorry about the misunderstanding. I am currently working with SDK 13
    I switched m_nus as 0 but still, there are so many errors occurring especially for the header.


    Do you think it would be better to start over from the SDK 17.1 SAADC example?

    The thing that I need is
    1. recording analog channel from the 2 AIN pins with 1kHz sampling rate (no external memory)
    2. right after the buffer has been filled, data should be sent to the Bluetooth communication.

Related