STM32的ADC實(shí)測(cè)
編譯平臺(tái):IAR5.40
測(cè)試概述:使用片內(nèi)振蕩器時(shí)鐘HSI,雙ADC同步采樣,ADC1采用DMA1存放,ADC2采用直接讀取。
每讀取十個(gè)采樣值計(jì)數(shù)一次,定時(shí)一秒鐘,通過串口將采樣次數(shù)送出,測(cè)試ADC采樣速率。
一、主頻設(shè)為:56MHz
使用HSI,STM32F103最高頻率可達(dá)72MHz,主頻在56MHz時(shí)AD速率最大。
RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_14); // 8 ÷ 2 × 14 = 56MHz
二、ADC工作頻率:14MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
// 56 ÷ 4 = 14MHz
三、TIM2設(shè)置1s中斷
TIM_TimeBaseStructure.TIM_Period = 2240; // 1s// for 103
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; // for 103
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM2, 25000-1, TIM_PSCReloadMode_Immediate);// for 103
// TIM2CLK = 56 ÷2 = 28MHz
// 2240 × 25000 ÷ (28MHz × 2) = 1s
四、DMA設(shè)置
只有ADC1和ADC3有DMA功能,而STM32F103只有ADC1和ADC2,所以DMA只設(shè)置存放ADC1的數(shù)據(jù),ADC2直接讀取。
緩存大小設(shè)為10個(gè)單位數(shù)據(jù),每存放10個(gè)數(shù)據(jù)FLAG_TC1置高,通知記錄采樣次數(shù)。
#define ADC1_DR_Address ((uint32_t)ADC1_BASE+0x4C)
#define ADC2_DR_Address ((uint32_t)ADC2_BASE+0x4C)
__IO uint32_t ADC1_ConvertedValueTab[2];
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_ConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 10;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
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);
DMA_Cmd(DMA1_Channel1, ENABLE);
五、ADC設(shè)置
雙AD同步采樣模式,不掃描,連續(xù)采樣,1個(gè)規(guī)則通道
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC2, ADC_Channel_5, 1, ADC_SampleTime_1Cycles5);
ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
六、主程序
while(1)
{
while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC)==RESET);
DMA_ClearFlag(DMA1_FLAG_TC1);
dd = (u16)ADC2->DR;
ADC_ClearITPendingBit(ADC2, ADC_IT_EOC);
min_cnt++;
}
七、中斷處理
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
send_data(min_cnt); // 通過串口檢查數(shù)據(jù)
min_cnt = 0;
}
TIM2->SR = 0x00;
}
八、測(cè)試結(jié)果
據(jù)手冊(cè)所示,STM32F2的AD速率可達(dá)2MSPS,STM32F4的AD速率可高達(dá)2.4MSPS。
評(píng)論