今天学习的是定时器定时器的计数方式有向上向下以及中间,向上是从0到设计数,向下是设计数到0;中间是0到设计数然后再到0;我设计的是1s就是1000*7200/72M
int time_Init()
{
TIM_TimeBaseInitTypeDef GPIO_TIM;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_TIM.TIM_ClockDivision = TIM_CKD_DIV1;//分频
GPIO_TIM.TIM_CounterMode = TIM_CounterMode_Up;//向上
GPIO_TIM.TIM_Period = 10000-1;
GPIO_TIM.TIM_Prescaler = 7200-1;
GPIO_TIM.TIM_RepetitionCounter = TIM_IT_Update;
TIM_TimeBaseInit(TIM2,&GPIO_TIM);
TIM_Cmd(TIM2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef GPIO_NVIC;
GPIO_NVIC.NVIC_IRQChannel = TIM2_IRQn;
GPIO_NVIC.NVIC_IRQChannelCmd = ENABLE;
GPIO_NVIC.NVIC_IRQChannelPreemptionPriority = 1;
GPIO_NVIC.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&GPIO_NVIC);
}
这个是配置的函数TIM_IT_Update是允许更新中断源;
看一下主函数
int main
{
int time_Init();
int LED_INIT();
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
while(1)
{
}
}
void TIM2_IRQHandler()
{
static uint16_t Temp = 0;
if(TIM_GetFlagStatus(TIM2,TIM_IT_Update) != RESET)
{
if(Temp++ %2 == 1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
TIM_ClearFlag(TIM2,TIM_IT_Update);
}
}
可以实现点灯