想用TIM2触发ADC,不能用~~大家看一看,拜托了,困惑好久了~~void TIM2_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //0 bits for pre-emption priority,4 bits for subpriority
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //28
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*TIM_Period--1000 TIM_Prescaler--719 -->中断周期为10ms*/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure ;
TIM_DeInit(TIM2); //不初始化到默认参数
TIM_TimeBaseStructure.TIM_Period=1000; /* 自动重装载寄存器周期的值(计数值), 累计 TIM_Period个频率后产生一个更新或者中断 */
TIM_TimeBaseStructure.TIM_Prescaler= (720 - 1);/* 时钟预分频数 72M/72 */
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /* 采样分频 */
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /* 向上计数模式 */
TIM_OCInitStructure. TIM_OCMode= TIM_OCMode_PWM1;
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable ;
TIM_OCInitStructure. TIM_Pulse = 500; //the pulse value to be loaded into the Capture Compare Register.
TIM_OCInitStructure. TIM_OCNPolarity= TIM_OCNPolarity_Low;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); /* 清除溢出中断标志 */
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //enable TIM update Interrupt source
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_InternalClockConfig(TIM2);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
//TIM_UpdateDisableConfig(TIM2,DISABLE);
TIM_CtrlPWMOutputs(TIM2,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_Cmd(TIM2, ENABLE); /* 开启时钟 */
}
void timer_gpio_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB.03 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM2_Init(void)
{
TIM2_NVIC_Configuration();
TIM2_Configuration();
timer_gpio_config();
}
|