nRF52832 SAADC - modify values

Hi all, I'm developing software for nRF52832.

It includes function of SAADC.

My saadc_callback function is below.

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
	if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
	{
		ret_code_t err_code;
		buffer_number ^= 0x01; //XOR
		saadc_event_callback = true;
		err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
		APP_ERROR_CHECK(err_code);

	}
}

It fills buffer at that time.

However, I'm wondering if I can put modified numbers in the buffer, not original A-D converted one.

For example, the original converted number is 100.

But I want to add 2^10 to 100 and put new number into buffer, not original. (So in buffer, there is 2^10 + 100, not 100).


Is there anyone knows how to to that?

I can't modify them after filling the buffer because I want to change the numbers depends on their states. (as time goes on, states will be changed).

Thanks for reading this article.!

  • Hi,

    The data is stored in the buffer that p_buffer points to. You could modify the buffer but it will be overwritten after a new sample. I would instead recommend copying the buffer in the interrupt handler and save the current state in a variable. Then process the copied buffer in main context based on the saved state. You should avoid doing any time consuming tasks in the interrupt context as it could interfere with time critical operations such as the Softdevice. 

    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            
            /*1. Set temp buffer to p_buffer
              2. Save state
              3. Set flag to indicate that buffer should be processed in main context*/
            
            memcpy(temp_buf, p_buffer, sizeof(p_buffer));
            state_var = state;
            flag = 1;
            
            
            ret_code_t err_code;
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
            APP_ERROR_CHECK(err_code);
            
        }
    }
    
    
    
    
    int main(void)
    flag = 0;
    
    {
        
        while(true)
        {
         if(flag == 1)
         {
            //add number to buffer based on state
            flag = 0;
         }
            
        }
        
    }
        
        
    
    

Related