ADC有4种模式
今天学习一下周期转换+阈值比较。
首先确认一下周期的计算以及寄存器
周期时间=PRD_CNT*2^PRESCALE / 时钟总线200M(注意这里不是ADC的时钟50M)
关闭周期转换,目前还没有找到相关的API 所以只能直接去操作寄存器
那么程序的步骤如下:
●挂上时钟
●复用相对应的IO口
●设置模式,转换分辨率,分频时钟,同步系统时钟
●设置通道,采样时间,阈值上限,阈值下限
●设置需要转换周期通道,周期分频,周期次数
●开启中断,设置中断抢占位。
这里的中断之前我也想不明白为啥是1<<2,也是请教了我的李师傅。
首先我这里使用的是通道2,寄存器中的一共是有14个通道 0-13 。2是第三个通道如果将它使能就是0000000000100 ==4,刚好1<<2=4.就是刚好对应上了。
adc16_enable_interrupts(BOARD_APP_ADC16_BASE, 1<<2);
static inline void adc16_enable_interrupts(ADC16_Type *ptr, uint32_t mask)
{
ptr->INT_EN |= mask;
}
●开启采样
●中断函数
void isr_adc16(void)
{
uint8_t status =adc16_get_status_flags(HPM_ADC3);//获取状态
adc16_clear_status_flags(BOARD_APP_ADC16_BASE, status);//清除标志位
if (ADC16_INT_STS_WDOG_GET(status) & APP_ADC16_CH_WDOG_EVENT)//判断是否是同一个
{
adc16_disable_interrupts(BOARD_APP_ADC16_BASE, APP_ADC16_CH_WDOG_EVENT);
trig_complete_flag = ADC16_INT_STS_WDOG_GET(status)&(1<<2);//取出通道值
}
}
●测试
void channel_result_out_of_threshold(void)
{
adc16_channel_threshold_t threshold_config={0};
uint8_t i = 31;
if(trig_complete_flag)
{
while (i--) {
if((trig_complete_flag >>i)&0x01)
{
adc16_get_channel_threshold(HPM_ADC3, 2, &threshold_config);
printf("ADC3,Channel%d,threshold_low:%d,threshold_hight:%d\n",i,threshold_config.thshdl,threshold_config.thshdh);
adc16_enable_interrupts(HPM_ADC3,1>>2);
}
}
}
}