直接请教版主了。不舍近求远
问题: 定时器tim2的CC2触发ADC转换,采样512个点,利用DMA,DMA传输完后中断,进行相应的处理。存在的问题是,修改CC2的捕获值,采样率没有发生改变,不知问题出在哪里?
程序如下
//定时器设置
#include "stm32lib//stm32f10x.h"
//#define samplerate250
//#define samplerate500
#define samplerate1000
// #define samplerate1
void Tim2_Config(void)
{
TIM_TimeBaseInitTypeDef Tim_TimeBaseStruct;
TIM_OCInitTypeDef Tim_OcIniStruct;
__IO uint16_t CCR2_val = 40 ;
//开启tim2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
//不同的采样率
#ifdef samplerate500
Tim_TimeBaseStruct.TIM_Period = 20;
#endif
#ifdef samplerate250
Tim_TimeBaseStruct.TIM_Period = 40;
#endif
#ifdef samplerate1000
Tim_TimeBaseStruct.TIM_Period = 10;
#endif
#ifdef samplerate1
Tim_TimeBaseStruct.TIM_Period = 10000;
#endif
Tim_TimeBaseStruct.TIM_Prescaler = 7199;
Tim_TimeBaseStruct.TIM_ClockDivision = 0;
Tim_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&Tim_TimeBaseStruct);
//比较通道配置,因为ADC的时候定时器触发转换使用的是CC2,所以对OC2进行配置
Tim_OcIniStruct.TIM_OCMode = TIM_OCMode_Timing;
Tim_OcIniStruct.TIM_Pulse = CCR2_val;
Tim_OcIniStruct.TIM_OCPolarity = TIM_OCPolarity_High;
Tim_OcIniStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM2,&Tim_OcIniStruct);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable); //禁止重装
/*使能预装载*/
TIM_ARRPreloadConfig(TIM2, ENABLE);
/*预先清除所有中断位*/
TIM_ClearITPendingBit(TIM2,TIM_IT_Update|TIM_IT_CC2);
//中断开启
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
//////ADC和DMA的设置
#include "stm32lib//stm32f10x.h"
#define smpnum 256
#define channelCnt 2 //ad转换的通道数目
#define buffsize smpnum*channelCnt //dma缓冲区大小
//为了进行FFT转换,每个通道的采样点数应为(2^n),采样点数取64、256或512
//缓冲区大小应该为通道数*采样点数
u16 ADCCov[buffsize];
static DMA_InitTypeDef DMA_InitStructure;
static ADC_InitTypeDef adc_initStruct;
void adcIni(void);
uint readAdc(void);
void adcIni(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
ADC_DeInit(ADC1);
adc_initStruct.ADC_Mode = ADC_Mode_Independent;
adc_initStruct.ADC_ContinuousConvMode = ENABLE;
adc_initStruct.ADC_ScanConvMode = ENABLE;
adc_initStruct.ADC_DataAlign = ADC_DataAlign_Right;
adc_initStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; //设置定时器触发AD转换
adc_initStruct.ADC_NbrOfChannel = channelCnt; //
ADC_Init(ADC1,&adc_initStruct);
ADC_RegularChannelConfig(ADC1,ADC_Channel_14,1,ADC_SampleTime_28Cycles5); //PC4 adc1 in14
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,2,ADC_SampleTime_28Cycles5); //PC5 adc1 in15
//ADC_RegularChannelConfig(ADC1,ADC_Channel_4,3,ADC_SampleTime_71Cycles5); //PA4 adc1 in4
//ADC_RegularChannelConfig(ADC1,ADC_Channel_5,4,ADC_SampleTime_239Cycles5); //PA5 adc1 in5
//ADC_RegularChannelConfig(ADC1,ADC_Channel_6,5,ADC_SampleTime_71Cycles5); //PA6 adc1 in6
//ADC_RegularChannelConfig(ADC1,ADC_Channel_7,6,ADC_SampleTime_239Cycles5); //PA7 adc1 in7
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
//ADC_SoftwareStartConvCmd(ADC1,ENABLE); //软件触发转换
ADC_ExternalTrigConvCmd(ADC1,ENABLE); //外部触发ADC转换,使用TIM2的CC2进行触发
/*重置校准寄存器 */
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
/*开始校准状态*/
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
/* 人工打开ADC转换.*/
ADC_ExternalTrigConvCmd( ADC1, ENABLE); //使能或者失能ADCx的经外部触发启动转换功能
}
//DMA的配置
void DMA_Configuration(void)
{
/* 允许 DMA1 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA通道1*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)( &(ADC1->DR)); //ADC1数据寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADCCov; //获取ADC的数组
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //片内外设作源头
DMA_InitStructure.DMA_BufferSize = buffsize; //每次DMAbuffsize个数据
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);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //DMA通道1传输完成中断
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
}
///DMA中断
volatile bool ADC_Ok;
void DMA1_Channel1_IRQHandler(void) //NVIC、stm32f10x_it.h、startup_stm32f10x_md.s中的名称要一致
{
if(DMA_GetITStatus(DMA1_IT_TC1))
{
DMA_ClearITPendingBit(DMA1_IT_GL1); //清除全部中断标志
ADC_Ok=TRUE;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update|TIM_IT_CC2);
}
} |