本帖最后由 ouyanglei92 于 2021-5-15 18:24 编辑
/* 定义ADC_TEST,只是用单个通道持续规则转换 */
#define ADC_TEST 1
#if ADC_TEST
static uint32_t adc_test_value = 0;
uint32_t get_adc_test_value(void)
{
return adc_test_value;
}
#endif
//时钟引脚初始化
void adc_hw_init(void)
{
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_DMA0);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_ADC0);
gpio_init(RCU_GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, GPIO_PIN_0);
gpio_init(RCU_GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, GPIO_PIN_1);
gpio_init(RCU_GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, GPIO_PIN_4);
gpio_init(RCU_GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, GPIO_PIN_5);
gpio_init(RCU_GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_2MHZ, GPIO_PIN_6);
}
//复位ADC和DMA外设
static void adc0_reset(void)
{
rcu_periph_clock_enable(RCU_ADC0);
adc_disable(ADC0);
adc_deinit(ADC0);
adc_interrupt_disable(ADC0, ADC_INT_EOC);
adc_interrupt_disable(ADC0, ADC_INT_EOIC);
adc_interrupt_flag_clear(ADC0, ADC_INT_EOC);
adc_interrupt_flag_clear(ADC0, ADC_INT_EOIC);
#if !ADC_TEST
dma_interrupt_disable(DMA0, DMA_CH0, DMA_INT_FTF|DMA_INT_ERR);
dma_channel_disable(DMA0, DMA_CH0);
dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_FTF|DMA_INT_FLAG_ERR);
dma_deinit(DMA0, DMA_CH0);
#endif
}
void adc_init(void)
{
adc_hw_init();
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6); //108/6=18Mhz
adc0_reset();
delay_1ms(10);
adc_mode_config(ADC_MODE_FREE);
adc_resolution_config(ADC0, ADCBIT);
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
#if !ADC_TEST
adc_dma_mode_enable(ADC0);
#endif
adc_special_function_config(ADC0 , ADC_CONTINUOUS_MODE, ENABLE); //³ÖÐøģʽ
#if ADC_TEST
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_71POINT5);
#else
adc_special_function_config(ADC0 , ADC_SCAN_MODE, ENABLE);
adc0_receive_dma_config();
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 5);
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5);
adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_6, ADC_SAMPLETIME_55POINT5);
#endif
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);
adc_enable(ADC0);
delay_1ms(2);
adc_calibration_enable(ADC0);
delay_1ms(10);
first_cmp = 0;
NVIC_SetPriority(ADC0_1_IRQn, 9);
NVIC_EnableIRQ(ADC0_1_IRQn);
NVIC_SetPriority(DMA0_Channel0_IRQn, 10);
NVIC_EnableIRQ(DMA0_Channel0_IRQn);
adc_interrupt_enable(ADC0, ADC_INT_EOC);
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
}
//中断函数
void ADC0_1_IRQHandler(void)
{
if( SET==adc_interrupt_flag_get(ADC0, ADC_INT_FLAG_EOC) )
{
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
#if ADC_TEST
adc_test_value = ADC_RDATA(ADC0);//ADC_CONV(ADC_RDATA(ADC0));
#endif
}
adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_WDE|ADC_INT_FLAG_EOIC);
} |
下述代码已经调试可用,有需要的朋友可以直接粘贴到自己工程中使用
原因已经找到。谢谢各位朋友的热心帮助! 问题原因: 3.3V LDO芯片输出电压纹波(最大有400mv,波形震荡式的)比较大导致了,重换一个就好了。因为之前一直用这个LDO芯片,没太留意是降压稳压的问题。目前看用电阻降压的方式输入给ADC还算稳定,如果条件允许尽量在ADC输入前端设置一个电压跟随器吧,减少输出阻抗,保证所有的输入电压都加到了ADC输入端。