【GMT43智慧液晶模組】例程十一:通用定時器實驗——定時點亮LED

XiaomaGee發表於2018-01-18

實驗原理:

  通過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);
}

 

相關文章