#define SAMPLES_IN_BUFFER 5
volatile uint8_t state = 1;
/**
* @brief UART events handler.
*/
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
//
}
//saadc的初始化
void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
//初始化saadc
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);
//加入通道配置
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
}
/**
* @brief Function for main application entry.
*/
int main(void)
{ nrf_saadc_value_t saadc_val;
float val; //保存SAADC采样数据计算的实际电压值
uint32_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
ret_code_t ret_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(ret_code);
NRF_LOG_INFO("rSAADC HAL simple example.");
//调用模块初始化
saadc_init();
while(1)
{
//启动一次ADC采样。
nrf_drv_saadc_sample_convert(0,&saadc_val);
//串口输出ADC采样值。
val = saadc_val * 3.6 /1024;
NRF_LOG_INFO(" %d mV",val*1000);
if(val*1000>=500)
{
nrf_gpio_pin_clear(17);
}
else nrf_gpio_pin_set(17);
NRF_LOG_FLUSH();
//延时300ms,方便观察SAADC采样数据
nrf_delay_ms(300);
nrf_pwr_mgmt_run();
}
}