Xinhai sensor signal conditioning CDS8712 development board ADC test
[Copy link]
I received the information provided by the moderator before New Year's Day, and tested the ADC acquisition by comparing the routine. The plan is to configure it as single-ended acquisition, and connect a sliding resistor as shown above.
The RST of the debug port is not connected, the chip RST is a high-level reset, and the reset output by the debugger is a low-level reset.
After power-on, void fw_system_init(void) is executed first according to the routine, the watchdog is turned off, and some irrelevant ones are also blocked.
void fw_system_init(void)
{
uint8_t count;
hal_gpio_init();
#if LINK_DEBUG_ENABLE
////hal_wdt_init();
#endif
hal_timer_delay_init();
hal_timer_delay_ms(3000);
#if DEBUG_PRINT_EN
hal_uart_init();
fw_debug_init();
#else
hal_iic_init();
fw_reg_init();
#endif
hal_storage_init();
fw_pmu_init();
#if 0
fw_sample_tk_init();
fw_sample_init();
#else
fw_sample_init_all();
#endif
// hal_temperature_init();
// fw_notify_init();
ft_config_init();
// fw_system_temperature = hal_get_temprature(); // 温度初始化
// fw_reg_set_temperature(fw_system_temperature); // 上传温度值
for(count = 0; count < 5; count++) // 激活采样
{
// fw_sample_proc();
}
}
Then execute void hal_afe_init(void). The original routine configures PB4 and PB5, which are not enabled here.
Note the call to hal_afe_sample_param_init() in void hal_afe_init(void); configure channel properties here
After turning on afe2_pga_bypass_set, the collected data will be more stable
void hal_afe_init(void) // AFE模块初始化,包含通道DAC校正
{
static uint8_t valid;
gpio_config_t gpio_config;
nvic_config_t ptr_config;
uint32_t i;
for(i = 0; i < CH_NUM; i++)
{
////gpio_mf_config(GPIO_GROUP_A, GPIO_PIN_NUM0,GPIO_MUX_FUNC_1); //VS0
// 采样通道的IO配置
//// if((g_ainn_channel == AFE2_AINN_SOURCE_FT10) || (g_ainp_channel == AFE2_AINP_SOURCE_FT10 )){
////
//// gpio_mf_config(GPIO_GROUP_B, GPIO_PIN_NUM4, GPIO_MUX_FUNC_3);
//// gpio_mf_config(GPIO_GROUP_B, GPIO_PIN_NUM5, GPIO_MUX_FUNC_5);
//// gpio_config.mode = GPIO_MODE_AN;
//// gpio_config.pin = GPIO_PIN_4;
//// gpio_config.pull = GPIO_PULL_NO_PULL;
//// gpio_init(GPIO_GROUP_B,&gpio_config);
//// }
//// if((g_ainn_channel == AFE2_AINN_SOURCE_FT11) || (g_ainp_channel == AFE2_AINP_SOURCE_FT11 )){
//// gpio_mf_config(GPIO_GROUP_B, GPIO_PIN_NUM5, GPIO_MUX_FUNC_5);
//// gpio_config.mode = GPIO_MODE_AN;
//// gpio_config.pin = GPIO_PIN_5;
//// gpio_config.pull = GPIO_PULL_NO_PULL;
//// gpio_init(GPIO_GROUP_B,&gpio_config);
//// }
}
syscfg_regwrprot_disable();
rcc_hirc_adc_enable_ctrl(ENABLE); // 使能内部 24Mhz 高速振荡器
syscfg_regwrprot_enable();
rcc_apb_periph_clock_enable_ctrl(RCC_APBPeriph_AFE2, ENABLE); // AFE ADC 时钟使能控制
afe2_vs_voltage_set(HAL_AFE_VS); //VS level 28.V
hal_afe_sample_param_init();
afe2_vs_limit_enable_ctrl(ENABLE);
// afe2_vs_output_enable_ctrl(ENABLE); // VS_SEL(由硬件控制VS0输出开启或关闭,只在转换FT通道时开启)
afe2_vs_enable_ctrl(ENABLE); // VS_EN
hal_timer_delay_ms(6);
afe2_vs_limit_enable_ctrl(DISABLE);
hal_timer_delay_ms(1);
do
{
valid = afe_adc_calibration(); // adc calibration
}while(valid == 0);
#if HAL_AFE_INT_ENBALE // 选择是否使能AFE采样完成中断
ptr_config.nvic_channel_priority = 1;
ptr_config.nvic_enable_flag = ENABLE;
ptr_config.nvic_IRQ_channel = IRQn_AFE2;
nvic_init(&ptr_config);
afe2_adc_interrupt_enable_ctrl(ENABLE);
#else
afe_adc_interrupt_disable();
#endif
for(i = 0; i < CH_NUM; i++){ // 采样的通道配置
afe2_pga_bypass_set(g_afe_channel,AFE2_PGA_BYPASS_ENABLE); // default config AA bypass
}
afe2_vs_enable_ctrl(DISABLE);
}
hal_afe_sample_param_init(); Configure channel properties here
afe2_adc_channel_config(g_afe_channel, g_ainp_channel, AFE2_AINN_SOURCE_GND); Configure the channel as single-ended acquisition
The CH_NUM macro definition is changed to 4, corresponding to AIN0~AIN3 on the development board
static void hal_afe_sample_param_init()
{
uint8_t i;
afe2_config_t p_afe_config;
for(i = 0; i < CH_NUM; i++) // 采样的通道配置
{
////afe2_adc_channel_config(g_afe_channel, g_ainp_channel, g_ainn_channel);
afe2_adc_channel_config(g_afe_channel, g_ainp_channel, AFE2_AINN_SOURCE_GND);
// afe2_adc_channel_config(AFE2_AIN_Channel_1,AFE2_AINP_SOURCE_FT0, AFE2_AINN_SOURCE_GND);
}
p_afe_config.data_format = 0;
p_afe_config.sample_time = AFE2_SAMPLE_TIME_8Cycles;
p_afe_config.average_num = AFE2_AVERAGE_NUM_16; //16次有效转换周期
p_afe_config.stop_mode = 0;
p_afe_config.setup_time_int = AFE2_SETUP_TIME_240Cycles; //20us建立时间
p_afe_config.average_mode = AFE2_AVERAGE_MODE_TRIM;
p_afe_config.conv_mode = AFE2_CONV_MODE_SINGLE_SCAN;//AFE2_CONV_MODE_LIMITED_SCAN; //有限周期扫描
p_afe_config.power_mode = AFE2_POWER_MODE_LP;
afe2_init(&p_afe_config);
for(i = 0; i < CH_NUM; i++) // 采样的通道配置
{
afe2_pga_gain_set(g_afe_channel,HAL_AFE_PGA1_GAIN );
}
afe2_pga_enable_ctrl(ENABLE);
}
Finally, the loop calls void hal_afe_sample(void)
View the collected ADC original value in hal_afe_rawdata
void hal_afe_sample(void) // 启动AFE采样
{
char i;
int16_t adc_value;
hal_afe_flag_adc_overflow = 0;
hal_afe_enable(HAL_AFE_NUM); // 开启AFE设置
for(i = 0; i < CH_NUM; i++) // 依次采样使能的通道
{
hal_afe_sample_chx(HAL_AFE_NUM, i);
adc_value = hal_afe_data_get(HAL_AFE_NUM, i);
hal_afe_rawdata = adc_value + s_adc_accumulate; // 计算增加累加量修正的采样值
}
//// hal_afe_disable(HAL_AFE_NUM); // 关闭AFE设置
}
Slide the potentiometer, the value of hal_afe_rawdata[0] changes from 6000 to 16000, and the jump is roughly 4 values
In addition to the HAL library implementation, there is another implementation in the routine. The acquisition channel is AIN0
In the following example, Action 1 is for initialization configuration, and Action 2 is for collection (running a state machine)
VS_VOID Action1 (VS_VOID)
{
static unsigned int aa=0;
/*延时函数初始化,调用后可使用延时函数*/
delay_init();
afe2_config_t afe2_initStruct;
nvic_config_t nvic_config;
/*PA0复用为VS */
gpio_mf_config(GPIO_GROUP_A,GPIO_PIN_NUM0,GPIO_MUX_FUNC_1);
rcc_apb_periph_clock_enable_ctrl(RCC_APBPeriph_AFE2,ENABLE);
/*24MHz内部高速时钟源2分频作为ADC时钟模块*/
rcc_clkout_config(RCC_MCU_CLKOUT_HIRC24_12,RCC_CLKOUT_PDIV_1); //使能内部12Mhz 高速振荡器
/*使能AFE采样完成中断 */
nvic_config.nvic_channel_priority = 0x00;
nvic_config.nvic_enable_flag = ENABLE;
nvic_config.nvic_IRQ_channel = IRQn_AFE2;
nvic_init(&nvic_config);
/*ADC中断使能 */
afe2_adc_interrupt_enable_ctrl(ENABLE);
/*ADC采样通道选择通道*/
afe2_adc_channel_config(g_afe_channel[0],AFE2_AINP_SOURCE_FT1,AFE2_AINN_SOURCE_GND);
/*ADC采样通道使能*/
afe2_adc_channel_enable_ctrl(g_afe_channel[0],ENABLE);
/*设置为有限周期扫描模式 */
afe2_initStruct.conv_mode = AFE2_CONV_MODE_SINGLE_SCAN;
/*格式为无符号数*/
afe2_initStruct.data_format = AFE2_DATA_FORMAT_UNSIGNED;
/*有效转换周期次数128*/
afe2_initStruct.average_num = AFE2_AVERAGE_NUM_128;
/*通道采样计数器129 ADC时钟*/
afe2_initStruct.sample_time = AFE2_SAMPLE_TIME_128Cycles;;
/*AFE转换结束,置起中断*/
afe2_initStruct.stop_mode = AFE2_STOP_MODE_INTERRUPT;
/*ADC通道建立时间选择80us*/
afe2_initStruct.setup_time_int = AFE2_SETUP_TIME_120Cycles;
/*去掉最大值和最小值进行平均处理*/
afe2_initStruct.average_mode = AFE2_AVERAGE_MODE_TRIM;
/*全速模式*/
afe2_initStruct.power_mode = AFE2_POWER_MODE_FS;
/*ADC初始化*/
afe2_init(&afe2_initStruct);
/*PGA选择*/
afe2_pga_gain_set(g_afe_channel[0],AFE2_PGA_GAIN_1);
/*不使能PGA*/
afe2_pga_enable_ctrl(ENABLE);////
/*PGA1、PGA2均bypass*/
afe2_pga_bypass_set(g_afe_channel[0],AFE2_PGA_BYPASS_DISBALE);////
/*VS0配置2.6V*/
afe2_vs_voltage_set(AFE2_VS_VOLTS_2V6);
/*VS工作在正常模式*/
afe2_vs_limit_enable_ctrl(DISABLE);
delay_us(50);
/*使能VS*/
afe2_vs_enable_ctrl(ENABLE);
delay_us(50);
/*使能ADC*/
afe2_adc_enable_ctrl(ENABLE);
delay_us(20);
/*使能ADC校准*/
////afe2_adc_calibration_start();
/*等待ADC校准完成*/
//// while(!afe2_flag_status_get(AFE2_FLAG_CALF));
/*关闭ADC校准*/
//// afe2_adc_calibration_stop();
delay_us(50);
/*关闭ADC*/
afe2_adc_enable_ctrl(DISABLE);
/*关闭VS */
afe2_vs_enable_ctrl(DISABLE);
}
VS_VOID Action2 (VS_VOID)
{
static unsigned int aa;
int ret;
aa++;
/*VS使能 */
afe2_vs_enable_ctrl(ENABLE);
/*PGA使能 */
afe2_pga_enable_ctrl(ENABLE);
/*ADC使能 */
afe2_adc_enable_ctrl(ENABLE);
/*设置采样通道 */
afe2_adc_channel_enable_ctrl(g_afe_channel[0],ENABLE);
/*启动采样 */
afe2_adc_conversion_start();
/*进入普通睡眠模式*/
pwr_sleep_mode_enter(PWR_SLEEP_MODE_NORMAL,PWR_SLEEP_ENTRY_WFI);
/*等待采样完成,AFE0_Finish只在中断中置位 */
while(AFE0_Finish == 0);
/*清除采样完成标志位 */
AFE0_Finish = 0;
/*读采样值 */
afe_rawdata[0] = afe2_adc_conversion_value_get(g_afe_channel[0]);
/*关闭PGA */
// afe2_vs_enable_ctrl(DISABLE);
/*关闭ADC*/
// afe2_adc_enable_ctrl(DISABLE);
/*关闭VS */
// afe2_vs_enable_ctrl(DISABLE);
/*ADC采样数据有效标志获取*/
ret = afe2_adc_result_valid_flag_get(g_afe_channel[0]);
/*采样的ADC码值*/
if(ret == 1)
{
SensorADC = afe_rawdata[0];
//printf("tempSensorADC : %d,%x\r\n ",ret,SensorADC);
}
/*延迟200ms*/
delay_ms(500);
SEQ_AddEvent(Event1);
}
|