tim2触发adc ,结果dma传输到ram中,再将ram数据发送到上位机 在上位机中显示出曲线来。但是每次显示,刚开始的几个数据不对,百思不得其解啊 :void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// ADC_InitStructure.ADC_ScanConvMode = DISABLE; //¹Ø±ÕͨµÀɨÃèģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //×¢ÒⲻҪʹÓóÖÐøת»»Ä£Ê½£¬·ñÔòÖ»Òª´¥·¢Ò»´Î£¬//ºóÐøµÄת»»¾Í»áÓÀ²»Í£Ðª£¨³ý·ÇCONTÇå0£©£¬ÕâÑùµÚÒ»´ÎÒÔºóµÄADC£¬¾Í²»ÊÇÓÉTIM2_CC2À´´¥·¢ÁË
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// ADC_InitStructure.ADC_NbrOfChannel = 1;
switch(dev.ADinfo.AD_CHN)
{
case 0x01:
case 0x02:
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
break;
case 0x03:
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
break;
}
ADC_Init(ADC1, &ADC_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//ÅäÖÃʱÖÓ(12MHz),ÔÚRCCÀïÃ滹ӦÅäÖÃAPB2=AHBʱÖÓ72MHz£¬
switch(dev.ADinfo.AD_CHN)
{
case 0x01:
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_1Cycles5);
break;
case 0x02:
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_1Cycles5);
break;
case 0x03:
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 2, ADC_SampleTime_1Cycles5);
break;
}
ADC_Cmd(ADC1,ENABLE);
ADC_DMACmd(ADC1,ENABLE);
// ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1); //Start Calibration register
while(ADC_GetCalibrationStatus(ADC1));
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
}
void DMA_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)membuf1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 256;
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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Enable GPIOA, GPIOC, ADC1 and TIM1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//200Hz, 72M/200=360000
// TIM_TimeBaseStructure.TIM_Period = 500;//ÉèÖÃ5msÒ»´ÎTIM2±È½ÏµÄÖÜÆÚ
TIM_TimeBaseStructure.TIM_Period = dev.ADinfo.AD_sample * 10 - 1; //*100ºó±äΪms
TIM_TimeBaseStructure.TIM_Prescaler = 720-1;//ϵͳÖ÷Ƶ72M£¬ÕâÀïÏ൱ÓÚ100KµÄ¶¨Ê±Æ÷2ʱÖÓ 100KHz = 10us
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//ÏÂÃæÏêϸ˵Ã÷
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse = 5;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//Èç¹ûÊÇPWM1ҪΪLow£¬PWM2ÔòΪHigh
TIM_OC2Init(TIM2, & TIM_OCInitStructure);
// TIM_Cmd(TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_UpdateDisableConfig(TIM2, DISABLE);
}
|