使用STD库–en.stm32f0_stdperiph_lib_v1.6.0。
ADC 的功能是将模拟信号采样得到数字信号,而有些时候,我们需要使用到定时采样,比如在计算一个采集的波形的频率的时候,我们需要精确的知道采样频率,也就是 1 s 内采集的点数,这个时候,就需要使用到定时采集。定时采样有如下三种方法:
本文,将采用第三种方法进行 AD 采集,使用 TIM 定时器触发 AD 采集,然后 DMA 搬运至内存。
最大的采样周期是239.5个周期,那么最小采样频率:14M/(239.5+12.5)=55.5KHz
如果我要AD 50Hz 采集5路信号,那么14MHZ ADC CLK下,单独的最大的采样周期是239.5个周期,则5个通道一次全部采集完成的时间= 5×单个通道采集完成的时间
单个通道是(239.5+12.5)ADC CLK=252CLK
5个通道一次全部采集完成的时间= 5x252CLK=1260CLK
总共的采集消耗时间非常少,时间冗余很多了。
这种情况下,假定输入信号是50Hz (周期为20ms),初步定为1周期1000个采样点,每2个 采样点间隔为 20ms /1000= 20 us。
ADC可编程的通道采样时间 我们选239.5周期,则 ADC 转换周期一周期大小为
20us /(239.5+12.5) 。 ADC时钟频率约为 12.6 MHz。默认14MHZ是满足要求的。
默认来自异步时钟系统 14MHZ
PCLK_Frequency=48000000 ,所以ADC的最大时钟可以是24MHZ。
RCC_ADCCLKConfig();
举例 用ADC1 规则通道的顺序为CH0,CH1,CH2,CH3,
不启动扫描模式:
在单次转换模式下:
启动ADC1,则
1.开始转换CH0(ADC_SQR的第一通道)
转换完成后停止,等待ADC的下一次启动,继续从第一步开始转换
在连续转换模式下:
启动ADC1,则
1.开始转换CH0(ADC_SQR的第一通道)
转换完成后回到第一步,继续转换
启动扫描模式下
在单次转换模式下:
启动ADC1,则
1.开始转换CH0、
2.转换完成后自动开始转换CH1
3.转换完成后自动开始转换CH2
4.转换完成后自动开始转换CH3
5.转换完成后停止,等待ADC的下一次启动下一次ADC启动后从第一步开始转换
在连续转换模式下:
启动ADC1,则
1.开始转换CH0
2.转换完成后自动开始转换CH1
3.转换完成后自动开始转换CH2
4.转换完成后自动开始转换CH3
5.转换完成后返回第一步,继续转换
采样时间是你通过寄存器告诉STM32采样模拟量的时间,设置越长越精确。
可以多次采集取平均 提高准确度。
/**
* @brief ADC configuration
* @param None
* @retval None
*/
static void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC Channel11 and channel10 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5 ;
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//ENABLE ;//DISABLE;//ENABLE;
//ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
#if 1
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC4;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;//ADC_ExternalTrigConvEdge_None;
#else
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
#endif
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, ADC_Channel_3 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_4 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_5 , ADC_SampleTime_239_5Cycles);
/* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_239_5Cycles);
ADC_TempSensorCmd(ENABLE);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles);
ADC_VrefintCmd(ENABLE);
// /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles);
// ADC_VbatCmd(ENABLE);
//index=0 v=0 mv=6 RegularConvData_Tab= 867, ADC1ConvertedVoltage 634 mV
//index=1 v=1 mv=4 RegularConvData_Tab= 1987, ADC1ConvertedVoltage 1457 mV
//index=2 v=1 mv=2 RegularConvData_Tab= 1774, ADC1ConvertedVoltage 1298 mV
//index=3 v=1 mv=4 RegularConvData_Tab= 2041, ADC1ConvertedVoltage 1496 mV
// /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles);
// ADC_VrefintCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
#if 1
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1,ADC_DMAMode_OneShot);// ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
#endif
/* Enable the ADC peripheral */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADRDY flag */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
}
/**
* @brief DMA channel1 configuration
* @param None
* @retval None
*/
static void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* DMA1 Channel1 Config */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = ADC_CHANNEL_NUMS;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* DMA1 Channel1 enable */
// DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1,DMA1_IT_TC1,ENABLE);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
单次多通道采集完成后 产生中断,通知主任务获取数据
/**
* @brief TIM configuration
* @param None
* @retval None
*/
static void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure={0};
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
/* TIM1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/* TIM1 Configuration */
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1000*20-1;//2000-1;//20-1 ;//0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 48-1;//48-1;//48000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
#if 1
TIM_OCStructInit(&TIM_OCInitStructure);
// /* Output Compare PWM Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /* low edge by default */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0x01;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
#else
//TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
TIM_ClearITPendingBit(TIM1, TIM_IT_Trigger);
NVIC_InitStruct.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM1,TIM_IT_Trigger,ENABLE);
#endif
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
先配置ADC,再配置DMA,最后配置TIM来触发启动ADC
int adc_main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
uint32_t v=0,mv=0;
uint8_t index;
static uint8_t times=0;
/* ADC1 configuration */
ADC_Config();
/* DMA configuration */
DMA_Config();
/* Infinite loop */
#if 1
/* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
#endif
/* TIM1 configuration */
TIM_Config();
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
printf("statrt while\r\n");
while (1)
{
#if 0
/* Test DMA1 TC flag */
while((DMA_GetFlagStatus(DMA1_FLAG_TC1)) == RESET );
/* Clear DMA TC flag */
DMA_ClearFlag(DMA1_FLAG_TC1);
printf("get adc data\r\n");
#endif
#if 1
extern volatile unsigned int ADC_ok;
if(ADC_ok ==1)
{
printf("%d %d\r\n",get_curtime(),get_adctime());
if(RegularConvData_Tab[2] < 2000)
{
printf("vref 1.5 error,2=%d ,4=%d \r\n",RegularConvData_Tab[2],RegularConvData_Tab[4]);
}
else
{
//for(index=0;index<(ADC_CHANNEL_NUMS-2);index++)
for(index=0;index<(ADC_CHANNEL_NUMS-0);index++)
{
v=((RegularConvData_Tab[index]* 3000) / 0xFFF) /1000;
mv = ((RegularConvData_Tab[index]* 3000) / 0xFFF)%1000;
//v=((RegularConvData_Tab[index]* 3000) / 0xFFF) / 1000;
//mv = (((RegularConvData_Tab[index]* 3000) / 0xFFF)%1000)/100;
/* Compute the voltage */
ADC1ConvertedVoltage = (RegularConvData_Tab[index] *3000)/0xFFF;
printf("index=%d times=%d RegularConvData_Tab= %d, ADC1ConvertedVoltage %d mV\r\n",index,times,RegularConvData_Tab[index],ADC1ConvertedVoltage);
// printf("index=%d v=%d mv=%d RegularConvData_Tab= %d, ADC1ConvertedVoltage %d mV\r\n",index,v,mv,RegularConvData_Tab[index],ADC1ConvertedVoltage);
}
times++;
if(times>=50)
{
times= 0;
}
}
ADC_ok=0;
}
#endif
#if 0
for(index=0;indexCHSELR |= (uint32_t)0;
switch(index)
{
case 0:
ADC_ChannelConfig(ADC1, ADC_Channel_3 , ADC_SampleTime_239_5Cycles);
ADC1->CHSELR = ADC_Channel_3;
break;
case 1:
ADC_ChannelConfig(ADC1, ADC_Channel_4 , ADC_SampleTime_239_5Cycles);
ADC1->CHSELR = ADC_Channel_4;
break;
case 2:
ADC_ChannelConfig(ADC1, ADC_Channel_5 , ADC_SampleTime_239_5Cycles);
ADC1->CHSELR = ADC_Channel_5;
break;
case 3:
/* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_239_5Cycles);
ADC1->CHSELR = ADC_Channel_TempSensor;
break;
case 4:
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles);
ADC1->CHSELR = ADC_Channel_Vrefint;
break;
default:
printf("adc channel error\r\n");
break;
}
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY) == RESET);
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
/* Test EOC flag */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
RegularConvData_Tab[index] =ADC_GetConversionValue(ADC1);
v=((RegularConvData_Tab[index]* 3000) / 0xFFF) ;
mv = ((RegularConvData_Tab[index]* 3000) / 0xFFF)%1000;
/* Compute the voltage */
ADC1ConvertedVoltage = (RegularConvData_Tab[index] *3000)/0xFFF;
printf("index=%d int mv=%d f mv=%d RegularConvData_Tab= %d, ADC1ConvertedVoltage %d mV\r\n",index,v,mv,RegularConvData_Tab[index],ADC1ConvertedVoltage);
//ADC_StopOfConversion(ADC1);
}
#endif
#if 0
/* Test EOC flag */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get ADC1 converted data */
ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
/* Compute the voltage */
ADC1ConvertedVoltage = (ADC1ConvertedValue *3000)/0xFFF;
printf("ADC1ConvertedVoltage %d, %d mV\r\n",ADC1ConvertedValue,ADC1ConvertedVoltage);
#endif
}
}
[2022-11-07 12:38:52] 58 3
[2022-11-07 12:38:52] index=0 RegularConvData_Tab= 0, ADC1ConvertedVoltage 0 mV
[2022-11-07 12:38:52] index=1 RegularConvData_Tab= 34, ADC1ConvertedVoltage 24 mV
[2022-11-07 12:38:52] index=2 RegularConvData_Tab= 112, ADC1ConvertedVoltage 82 mV
[2022-11-07 12:38:52] index=3 RegularConvData_Tab= 1942, ADC1ConvertedVoltage 1422 mV
[2022-11-07 12:38:52] index=4 RegularConvData_Tab= 1672, ADC1ConvertedVoltage 1224 mV
[2022-11-07 12:38:52] 78 4
[2022-11-07 12:38:52] index=0 RegularConvData_Tab= 0, ADC1ConvertedVoltage 0 mV
[2022-11-07 12:38:52] index=1 RegularConvData_Tab= 33, ADC1ConvertedVoltage 24 mV
[2022-11-07 12:38:52] index=2 RegularConvData_Tab= 111, ADC1ConvertedVoltage 81 mV
[2022-11-07 12:38:52] index=3 RegularConvData_Tab= 1942, ADC1ConvertedVoltage 1422 mV
[2022-11-07 12:38:52] index=4 RegularConvData_Tab= 1673, ADC1ConvertedVoltage 1225 mV
ADC按照通道顺序循环采样并转换数据,然后DMA自动将对应的数据搬运至RegularConvData_Tab[]数组中。使用该方法得到的ADC值有时候波动会比较大,如果不做滤波就直接采用的话,有可能会因为数据波动造成程序误判。如果将ADC值做中值滤波处理,即使有个别数据波动,对程序的影响则大幅度降低。因为DMA搬运新数据时会将旧数据覆盖掉,这里采用DMA中断处理,每发生一次DMA中断时将新的数据缓存起来,存够指定数量后再做中值滤波!
单次多通道采集完成后 产生中断,通知主任务获取数据
void DMA_Config(void)函数加上DMA中断配置的代码如下
//ADC DMA数据传输完成
void DMA1_Channel1_IRQHandler(void)
{
/* Check the status of the specified DMAy flag */
if ((DMA1->ISR & DMA1_FLAG_TC1) != (uint32_t)RESET)
{
ADC_DMA_INTERRUPT_HANDLER();
//清除标志位
DMA1->IFCR = DMA1_FLAG_TC1;
}
}
void ADC_DMA_INTERRUPT_HANDLER(void)
{
u8 i;
static u8 times=0;
static u16 buffer[ADC_DMA_BUFFER_SIZE];
for (i=0;i= 8) { //取8次平均值
for (i=0;i>3;
buffer[i] = 0; //清零
}
times = 0;
}
}
CalAverConvData_Tab 作为ADC采集的平均数据用于后续的数据处理。
本文实现的一个功能便是使用 TIM 触发 ADC 多通道采集,并使用 DMA 进行搬运,通过这样子就可以精确地控制 ADC 的采样频率,也就是控制 1 s 钟可以采集多少个点。最后,而这个采样频率就是 TIM 控制的PWM 的频率,但是为了更加精确的计算其真实的采样频率还应该加上 ADC 通道的转换一个数据的转换时间,这样才是最为精确的采样频率。
A_BUFFER_SIZE;i++) {
buffer[i] += RegularConvData_Tab[i];
}
if (++times >= 8) { //取8次平均值
for (i=0;i
buffer[i] = 0; //清零
}
times = 0;
}
}
CalAverConvData_Tab 作为ADC采集的平均数据用于后续的数据处理。
## 4.总结
本文实现的一个功能便是使用 TIM 触发 ADC 多通道采集,并使用 DMA 进行搬运,通过这样子就可以精确地控制 ADC 的采样频率,也就是控制 1 s 钟可以采集多少个点。最后,而这个采样频率就是 TIM 控制的PWM 的频率,但是为了更加精确的计算其真实的采样频率还应该加上 ADC 通道的转换一个数据的转换时间,这样才是最为精确的采样频率。
##