我想通过通用定时2实现1KHZ的ADC采样。让定时器去驱动ADC采集(1ms一次),采集到的数据在通过DMA送到内存,当用数据传送的时候 进入DMA中断,最终现象是定时器1ms中断正常。但是DMA中断就不正常了差不多20us就进入一次。搞不懂是不是那里配置错了。请各位帮一下忙看看
#include "adc.h"
#include "stm32f0xx.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address 0x40012440
__IO uint16_t RegularConvData_Tab;
void ADC_Timer_Init()
{
TIM_TimeBaseInitTypeDef timer_init_structure;
NVIC_InitTypeDef nvic_init_structure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //????TIM2?±??
nvic_init_structure.NVIC_IRQChannel = TIM2_IRQn; //????TIM2?????¨??
nvic_init_structure.NVIC_IRQChannelCmd = ENABLE; //????TIM2????
nvic_init_structure.NVIC_IRQChannelPriority = 0; //????????0
NVIC_Init(&nvic_init_structure);
TIM_DeInit(TIM2); //????TIM2
TIM_TimeBaseStructInit(&timer_init_structure); //??????TIMBASE?á????
timer_init_structure.TIM_ClockDivision = TIM_CKD_DIV1; //?????±??????·?????48M
timer_init_structure.TIM_CounterMode = TIM_CounterMode_Up; //?ò??????????
timer_init_structure.TIM_Period = 1000; //??1ms??·???????????????ADC
timer_init_structure.TIM_Prescaler = 48-1; //?????±???¤·?????f??1M??systick??1 uS
timer_init_structure.TIM_RepetitionCounter = 0x00; //·??ú0+1??update?????ú?ú????
TIM_TimeBaseInit(TIM2, &timer_init_structure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //????TIM2????
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); //????TIM2??update?????ü??????·???
TIM_Cmd(TIM2, ENABLE); //????TIM2
}
void ADC1_DMA_Init(void)
{
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
/* ADC1 DeInit */
ADC_DeInit(ADC1);
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA1 clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
/* Configure PA.00 as analog input */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //Fast speed
GPIO_Init(GPIOA, &GPIO_InitStruct); //
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel1_IRQn ;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0x1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/* DMA1 Channel1 Config */
DMA_DeInit(DMA1_Channel1);
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize =4;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Disable ;//??????·?????
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStruct);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);//?????á???§???????è????????°?????????
DMA_ClearITPendingBit(DMA_IT_TC); //??????????????
/* DMA1 Channel1 enable */
DMA_Cmd(DMA1_Channel1, ENABLE);
// /* ADC DMA request in circular mode */
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
/* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStruct);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConv_T2_TRGO; //??????·??è????TIM2
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStruct);
// /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);
// ADC_TempSensorCmd(ENABLE);
/* Convert the ADC1 Vref with 55.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_0 , ADC_SampleTime_55_5Cycles);
ADC_VrefintCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
ADC_DMACmd(ADC1, ENABLE);
//ADC_ITConfig(ADC1, ADC_IT_EOC ,ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
ADC_Timer_Init();
}
/////////////////////下面是中断的代码/////////////////////////////////////
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update)) //????·??úupdate????????
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //????update????????±ê??
}
}
#include "hal_led.h"
void DMA1_Channel1_IRQHandler(void){
static uint8_t ff ;
if(DMA_GetITStatus(DMA1_IT_TC1))
{
if(ff == 0){
GPIO_SetBits(LED1_PORT, LED1_PIN );
ff = 1 ;
}else{
GPIO_ResetBits(LED1_PORT, LED1_PIN );
ff = 0 ;
}
}
DMA_ClearITPendingBit(DMA1_IT_GL1); //???????????ù??????
}
转载 |