【GMT43智能液晶模块】例程十一:通用定时器实验——定时点亮LED
实验原理:
通过STM32的一个GPIO口来驱动LED灯,设定GPIO为推挽输出模式,采用灌电流的方式与LED连接,
输出高电平LED灭,输出低电平LED亮,通过通用定时器TIM3实现500ms定时,每500ms变换一次LED的状态。
示例截图:
源代码下载链接:
链接:https://pan.baidu.com/s/1eTSNL54 密码:1ypb
GMT43购买链接:
核心代码:
static int initialize(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); TIM_TimeBaseInitStructure.TIM_Period = 4999; TIM_TimeBaseInitStructure.TIM_Prescaler = 8399; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); return 0; }
void TIM3_IRQHandler(void) { static int timer_counter = 0; if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) { if(timer_counter % 2 == 0){ LED_OFF; }else{ LED_ON; } }else{ LED_OFF; } timer_counter ++; TIM_ClearITPendingBit(TIM3,TIM_IT_Update); }