STM32F10XXX 雙通道ADC數(shù)據(jù)采集,TIM2觸發(fā)采集
*STM32F10XXX 雙通道ADC數(shù)據(jù)采集
*環(huán)境:keil for arm mdk 4.71a 官方V3.5庫(kù)
*STM32數(shù)據(jù)采集部分
* 1、ADC1/ADC2實(shí)現(xiàn)并行同時(shí)數(shù)據(jù)采集,12BIT最高可達(dá)1MSPS采樣速度并通過(guò)STM32的DMA傳輸放入內(nèi)存中
* 2、TIM2 CC2實(shí)現(xiàn)對(duì)ADC采樣的觸發(fā),ADC_Sample_Frequency_Set函數(shù)實(shí)現(xiàn)自定義TIM2 CC2頻率輸出,
* 3、采樣的頻率和采樣個(gè)數(shù)通過(guò)接收到的UDP控制命令來(lái)指定
* 采樣的頻率為20HZ~1MHZ;
* 采樣深度為1~4000個(gè)數(shù)據(jù)(受限于STM32內(nèi)存20KB容量,一個(gè)數(shù)據(jù)為2個(gè)12bitADC通道讀數(shù),需一個(gè)word)
*******************************************************************************/
#define ADC1_DR_addr ((u32)0x4001244C) //AD轉(zhuǎn)換結(jié)果存放地址定義
/*******************************************************************************
* Function Name : DMA_ADC_Transfer_Reset
* Description : ADC1/ADC2 DMA傳輸通道復(fù)位,準(zhǔn)備下一次DMA傳輸
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void DMA_ADC_Transfer_Reset(void)
{
//開(kāi)始DMA傳輸
DMA_Channel1->CCR &= ~(1<<0); //禁用DMA_Channel,EN是CCR1寄存器的0位
DMA->IFCR |= 0x0000000F;//清除CHANNEL1的4個(gè)標(biāo)志
DMA_Channel1->CNDTR = (u16)SampleDepth;//重新設(shè)置要設(shè)置的DMA傳輸數(shù)據(jù)量
DMA_Channel1->CCR |= (1<<0);//重新使能DMA_channel1
while(!(DMA->ISR & DMA_FLAG_TC1));
DMA_Channel1->CCR &= ~(1<<0); //禁用DMA_Channel,EN是CCR1寄存器的0位
}
/*******************************************************************************
* Function Name : DMA_ADC_Transfer_Init
* Description : ADC1/ADC2 DMA傳輸通道初始化
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void DMA_ADC_Transfer_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DR_Address;//ADC數(shù)據(jù)寄存器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&Data_Buffer[0];//目標(biāo)緩沖區(qū)地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外設(shè)是源
DMA_InitStructure.DMA_BufferSize = 0;//設(shè)置DMA讀取長(zhǎng)度為
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設(shè)地址不遞增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//目標(biāo)緩沖區(qū)地址遞增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//外設(shè)數(shù)據(jù)寬度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//目標(biāo)緩沖區(qū)數(shù)據(jù)寬度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:Circular循環(huán)模式/Normal普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//優(yōu)先級(jí)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//內(nèi)存到內(nèi)存模式不使能
DMA_Init(DMA_Channel1, &DMA_InitStructure);
/* Enable DMA channel1 */
//DMA_Cmd(DMA_Channel1, ENABLE);
}
/*******************************************************************************
* Function Name : ADC_Initial
* Description : ADC1/ADC2初始化
*******************************************************************************/
void ADC_Initial(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1, ENABLE ); //使能ADC通道時(shí)鐘
ADC_InitTypeDef ADC_InitStructure;
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;//ADC1/ADC2同時(shí)并行采樣模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//多通道掃描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//單次轉(zhuǎn)換模式(轉(zhuǎn)換后即停止)
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;//觸發(fā)模式
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//數(shù)據(jù)右對(duì)齊
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1 , &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_1Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* ADC2 configuration ---配置同ADC1--------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC2 regular channels configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5);
/* Enable ADC2 external trigger conversion */
ADC_ExternalTrigConvCmd(ADC2, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register 校準(zhǔn)ADC1*/
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
/* Enable ADC2 reset calibaration register 校準(zhǔn)ADC2*/
ADC_ResetCalibration(ADC2);
/* Check the end of ADC2 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC2));
/* Start ADC2 calibaration */
ADC_StartCalibration(ADC2);
/* Check the end of ADC2 calibration */
while(ADC_GetCalibrationStatus(ADC2));
/* Test on channel1 transfer complete flag */
//while(!DMA_GetFlagStatus(DMA_FLAG_TC1));
/* Clear channel1 transfer complete flag */
//DMA_ClearFlag(DMA_FLAG_TC1);
ADC_ExternalTrigConvCmd( ADC1, ENABLE);
}
/*******************************************************************************
* Function Name : ADC_Sample_Frequency_Set
* Description : 根據(jù)輸入的頻率設(shè)置,TIM2_CC2產(chǎn)生相應(yīng)的頻率
* 用來(lái)控制ADC的采樣,F(xiàn)requency=1000000/(Prescaler+1)來(lái)產(chǎn)生
* 因此有些頻率計(jì)算不準(zhǔn)確,一般頻率為2或5的倍數(shù)才準(zhǔn)確
* 頻率范圍為16Hz~1000,000Hz
* Input : u16 Frequency:輸入所需要的采樣頻率
*******************************************************************************/
void ADC_Sample_Frequency_Set(u32 Frequency)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE );
TIM_Cmd(TIM2, DISABLE);//先停止TIM2時(shí)鐘,以準(zhǔn)備下面的設(shè)置
/* -----------------------------------------------------------------------
TIM2 配置: 產(chǎn)生TIM2_CC2時(shí)鐘控制信號(hào)用于控制ADC采樣
TIM2CLK =72 MHz
TIM2 ARR Register = 35 => TIM3 Frequency = (TIM3 counter clock/(ARR + 1))/2
TIM2 Frequency = 1000 KHz.
----------------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 35; //APR寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 1000000/Frequency-1; //預(yù)分頻值,用來(lái)調(diào)整頻率,分頻系數(shù)=1000khz/(prescaler+1)
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM_OCMode_Toggle Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;//TIM_OCMode_Toggle,TIM_OCMode_PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period; //CCR2寄存器
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* TIM使能 */
TIM_Cmd(TIM2, ENABLE);
/* TIM 輸出使能 */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
評(píng)論