例程给的是用ADC_ExternalTrigConv_T1_CC4作为触发源。
但是我想用ADC_ExternalTrigConv_T3_TRGO触发,谁有相关程序?可否发我一份,或者贴出相关代码也行。谢啦!
下面是我的代码,虽然ADC也能采样,但是采样时间间隔不是我设定的频率。
#include "stm32f0xx.h"
__IO uint16_t ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
__IO uint32_t ADCmvoltp = 0 ;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void ADC_TIM_Config(void);
int main(void)
{
ADC_TIM_Config();
while (1)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
}
}
void ADC_TIM_Config(void)
{
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* ADC1 and TIM1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
/* TIM1 Configuration */
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Output Compare PWM Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; /* low edge by default */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0x01;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
/* TIM1 enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM3, ENABLE);
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO ;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable ADCperipheral[PerIdx] */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADCEN falg */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
} |