您的位置 首页 FPGA

STM32F10XXX 双通道ADC数据收集,TIM2触发收集

/**********(C)COPYRIGHT2007STMicroelectronics************STM32F10XXX双通道ADC数据采集*环境:keilforarmmdk4.71a

/********** (C) COPYRIGHT 2007 STMicroelectronics ***********

*STM32F10XXX 双通道ADC数据收集
*环境:keil for arm mdk 4.71a 官方V3.5库
*STM32数据收集部分
* 1、ADC1/ADC2完成并行一起数据收集,12BIT最高可达1MSPS采样速度并经过STM32的DMA传输放入内存中
* 2、TIM2 CC2完成对ADC采样的触发,ADC_Sample_Frequency_Set函数完成自界说TIM2 CC2频率输出,
* 3、采样的频率和采样个数经过接收到的UDP操控指令来指定
* 采样的频率为20HZ~1MHZ;
* 采样深度为1~4000个数据(受限于STM32内存20KB容量,一个数据为2个12bitADC通道读数,需一个word)
*******************************************************************************/
#define ADC1_DR_addr ((u32)0x4001244C) //AD转化成果寄存地址界说
/*******************************************************************************
* Function Name : DMA_ADC_Transfer_Reset
* Description : ADC1/ADC2 DMA传输通道复位,预备下一次DMA传输
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void DMA_ADC_Transfer_Reset(void)
{
//开端DMA传输
DMA_Channel1->CCR &= ~(1<<0); //禁用DMA_Channel,EN是CCR1寄存器的0位
DMA->IFCR |= 0x0000000F;//铲除CHANNEL1的4个标志
DMA_Channel1->CNDTR = (u16)SampleDepth;//从头设置要设置的DMA传输数据量
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数据寄存器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&Data_Buffer[0];//方针缓冲区地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设是源
DMA_InitStructure.DMA_BufferSize = 0;//设置DMA读取长度为
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_Normal;//DMA形式:Circular循环形式/Normal一般形式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//内存到内存形式不使能
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通道时钟
ADC_InitTypeDef ADC_InitStructure;
/* ADC1 configuration ——————————————————*/
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;//ADC1/ADC2一起并行采样形式
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(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 校准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 校准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 : 依据输入的频率设置,TIM2_CC2发生相应的频率
* 用来操控ADC的采样,Frequency=1000000/(Prescaler+1)来发生
* 因而有些频率计算不精确,一般频率为2或5的倍数才精确
* 频率规模为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时钟,以预备下面的设置
/* ———————————————————————–
TIM2 装备: 发生TIM2_CC2时钟操控信号用于操控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; //预分频值,用来调整频率,分频系数=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);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/fpga/268331.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部