|
debug,截图错误码如下:
main.c中
ADC配置如下代码:
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{ float val;
//判断dmabuffer是否填满,填满表示采样完成
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
//设置好缓存,为下次转换预备缓冲
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
// err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
int i;
NRF_LOG_INFO("ADC event number: %d\r\n",(int)m_adc_evt_counter);
for (i = 0; i < SAMPLES_IN_BUFFER; i++)
{
// printf("%d\r\n", p_event->data.done.p_buffer);
//val=(Vp-Vn)*Gain/Reference*2^resolution=Vp*(1/6)/(0.6)*2^12=Vp*3.6*2048
// val = p_event->data.done.p_buffer * 3.6 /1024; //10bit
val = p_event->data.done.p_buffer * 3.6 /2048; //12bit
NRF_LOG_INFO(" %dmV\r\n",val*1000);
printf(" %f mV",val*1000);
}
m_adc_evt_counter++;
}
}
/**@brief SAADC init.
*
* @details SAADC init.
*
*
* @param[in] void
ADC参考电压,一种是内部参考电压0.6V,一种是采用VDD/4,增益Gain可调1/6,1/5,1/4,1/3,1/2,1,2,4
采用内部电压作为参考:输入采样电压为±0.6V,当使用VDD电压作为参考电压时,输入范围为±VDD/4,可调整增益来调整输入范围
输入电压范围:input = (±0.6V 或 ±VDD/4)/Gain
如选择VDD为参考电压,增益为1/4,input=(VDD/4)/(1/4)=VDD
选择内部电压作为参考,单端输入,增益为1/4,input=(0.6)/(1/6)=3.6V
注意:AIN0~Ain7输入范围不能超过VDD,低于VSS
*/
void saadc_init(void)
{
ret_code_t err_code;
//ADC 通道配置:单端输入,AIN2
nrf_saadc_channel_config_t channel_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);//单端输入,通道AIN2,增益,参考电压,采样时间
//ADC 初始化
err_code = nrf_drv_saadc_init(NULL, saadc_callback); //分辨率 过采样 中断优先级
APP_ERROR_CHECK(err_code);
//ADC通道初始化
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
//添加缓冲配置函数:即easyDMA:单通道ADC-DMA用
// err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool,SAMPLES_IN_BUFFER);
// APP_ERROR_CHECK(err_code);
//添加缓冲配置函数:即easyDMA:单通道ADC-DMA用
//添加缓冲配置函数:双缓冲PPI通道采样
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
//添加缓冲配置函数:双缓冲PPI通道采样
}
/*PPI channel config enable*/
void saadc_sampling_event_enable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
APP_ERROR_CHECK(err_code);
}
void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
}
void saadc_sampling_event_init(void)
{
ret_code_t err_code;
err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);
//定时器初始化为默认配置
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width =NRF_TIMER_BIT_WIDTH_16; // NRF_TIMER_BIT_WIDTH_32
//配置定时器,同时注册回调函数
err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);//m_timer即TIMER0
APP_ERROR_CHECK(err_code);
/* setup m_timer for compare event every 400ms,设置每隔20ms发送一次m_timer对比事件 */
//设置定时器滴答时间
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 50);
//设置定时,捕获/比较通道,比较值,清楚比较任务,关掉比较器中断
nrf_drv_timer_extended_compare(&m_timer,
NRF_TIMER_CC_CHANNEL0,
ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
false);
//使能定时器0
nrf_drv_timer_enable(&m_timer);
//设置PPI两端的通道,一个作为任务,一个作为事件
uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer,
NRF_TIMER_CC_CHANNEL0);
uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
//分配一个PPI通道
/* setup ppi channel so that timer compare event is triggering sample task in SAADC */
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
APP_ERROR_CHECK(err_code);
//分频PPI通道地址,一端是比较事件,一端是ADC采样事件
err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
timer_compare_event_addr,
saadc_sample_task_addr);
APP_ERROR_CHECK(err_code);
}
请教一下到底问题出在哪里了?
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|