本帖最后由 zcguoji 于 2021-4-15 12:46 编辑
您好,我现在用航顺的HK32f103cbt6遇到了ADC不能用的情况,卡在了校准这里,while(ADC_GetCalibrationStatus(ADCx)); 在stm32上面是正常使用的,代码如下
#include "power-adc.h"
#define USED_ADC1 ADC1
#define USED_ADC_RCC RCC_APB2Periph_ADC1
#define USED_ADC_GPIO_RCC RCC_APB2Periph_GPIOA
#define USED_ADC_PORT GPIOA
#define USED_ADC_PIN GPIO_Pin_4
#define USED_ADC_CHANNEL ADC_Channel_4
float voltage;
//ADC时钟开启
void ADC_RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(USED_ADC_GPIO_RCC | USED_ADC_RCC, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
}
//ADC GPIO设置
void ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = USED_ADC_PIN;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(USED_ADC_PORT,&GPIO_InitStructure);
}
//ADC 设置
void ADC_Config(ADC_TypeDef* ADCx,uint8_t ADC_Channel)
{
ADC_InitTypeDef ADC_InitStructure;
//ADC_StructInit(&ADC_InitStructure);
ADC_DeInit(ADCx);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode =DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE ;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADCx,&ADC_InitStructure);
DELAY_SOME(10);
ADC_RegularChannelConfig(ADCx, ADC_Channel, 1, ADC_SampleTime_239Cycles5 );
DELAY_SOME(10);
ADC_Cmd(ADCx, ENABLE);
DELAY_SOME(10);
ADC_ResetCalibration(ADCx);
while(ADC_GetResetCalibrationStatus(ADCx));
DELAY_SOME(10);
ADC_StartCalibration(ADCx);
while(ADC_GetCalibrationStatus(ADCx));
}
//初始化ADC
void ADC_INIT()
{
//ADC时钟开启
ADC_RCC_Configuration();
//ADC GPIO设置
ADC_GPIO_Config();
//ADC 设置
ADC_Config(USED_ADC1,USED_ADC_CHANNEL);
}
//获取ADC的值
void get_ADC_Value()
{
ADC_ClearFlag(USED_ADC1,ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(USED_ADC1, ENABLE);//开启ADC
DELAY_SOME(10);
while(RESET == ADC_GetFlagStatus(USED_ADC1,ADC_FLAG_EOC));
voltage = 3.3*ADC_GetConversionValue(USED_ADC1)/4096;
ADC_ClearFlag(USED_ADC1,ADC_FLAG_EOC);
ADC_SoftwareStartConvCmd(USED_ADC1, DISABLE);//关闭ADC
}
|