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!

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX