|
给你贴个我的程序,定时1秒扫描AD的,希望对你有用
void SetupTIM (void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // NVIC_InitTypeDef NVIC_InitStructure;
/* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 720; TIM_TimeBaseStructure.TIM_Prescaler = 50000; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM2->CR1|=(1<<3)|(1<<2); //单脉冲模式
/* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);
} void StartTIM(void) { /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); } void SetupNVIC(void){ NVIC_InitTypeDef NVIC_InitStructure; NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //中断向量表 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; //tim2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM2_IRQHandler(void) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//必须加 StartTIM(); } 好了,主程序中别忘了开启TIM的时钟,剩下的自己写吧。 |
|