This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to read two analog inputs using SAADC?

I have developed an application that reads data from an analog sensor and transmits data with UART. This app is based off the SDK v14.x uart example, and SAADC example. While my application works fine for reading one analog input, I now need to read data from two different analog inputs and transmit their data in the same bluetooth packet. 

The initialization call I use for the SAADC is

NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);   

* followed by *

nrf_drv_saadc_init(NULL, saadc_callback);

and other relevant code (attached to this question below). With these functions, I can only specify one analog input and one callback function. I want to use two analog pins, and I also want my saadc_callback behavior to be different for each "done" event. I essentially want to read analog_input_1, then immediately afterwards read analog_input_2 (no/minimal delay between readings), and once both inputs are read I want to transmit a packet via BLE. Once I can read from two different pins, how can I create different different callback behaviors (I.E. store data from analog_input_1 until analog_input_2 is read, then send after input_2 is read)? I could potentially use a global boolean/counter and datastructure to store information from analog_input_1 until it's ready, then simply use an if statement within my callback, but there certainly has to be a more elegant solution.

I also use a timer event to read from the SAADC, and will need to make sure I can take both readings using the same timer still.

Code is attached. Relevant functions I need to edit to support multiple analog pins and callback behavior are saadc_sampling_event_init, saadc_sampling_event_enable, saadc_callback, and saadc_init. Any help would be greatly appreciated!

Parents
  • Figured it out earlier this morning! After the first reading, I unitialize SAADC and then instantly reinitialize it with my second analog pin as input, put both readings into a packet, transmit, then restart the whole process!

Reply
  • Figured it out earlier this morning! After the first reading, I unitialize SAADC and then instantly reinitialize it with my second analog pin as input, put both readings into a packet, transmit, then restart the whole process!

Children
No Data