目錄:
1:概述
2:常用中斷功能
3:PWM輸出
1:概述
在開發(fā)中,定時器的應(yīng)用很廣泛,簡單總結(jié)為三個方面:
1.1:中斷功能的應(yīng)用,常用的是利用定時器中斷,實現(xiàn)定時、記時、延時、超時判斷,前面博文中有總結(jié)過內(nèi)核定時器的各種使用的方法;
1.2:比較輸出,常用應(yīng)用就是PWM輸出,用脈寬調(diào)制來實現(xiàn)led、電機等的控制;
1.3:輸入捕獲,可以抓取輸入的方波信號,統(tǒng)計波形周期和占空比,最常見利用就是門控,將外部模擬量轉(zhuǎn)化為數(shù)字量(定時器的計數(shù)值);
2:常用中斷功能
定時器的初始化詳細參考手冊,主要的一點是確定定時器的溢出周期,利用公式:Tout = (arr+1)*(psc+1)/Tclk,可計算出定時器的溢出頻率;
如下代碼實現(xiàn):在中斷函數(shù)中實現(xiàn)led翻轉(zhuǎn),翻轉(zhuǎn)頻率1HZ,既led閃爍頻率1HZ;
timer_driver.c
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
-
voidTIM3_Init(unsignedintarr,unsignedintpsc)
-
{
-
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
-
NVIC_InitTypeDefNVIC_InitStructure;
-
TIM_OCInitTypeDefTIM_OCInitStruct;
-
-
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//將TIM3的CH2映射到PB5上
-
-
//定時器TIM3初始化
-
TIM_TimeBaseStructure.TIM_Period=arr;
-
TIM_TimeBaseStructure.TIM_Prescaler=psc;
-
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
-
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
-
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能指定的TIM3中斷,允許更新中斷
-
-
//中斷優(yōu)先級NVIC設(shè)置
-
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中斷
-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//先占優(yōu)先級0級
-
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//從優(yōu)先級3級
-
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道被使能
-
NVIC_Init(&NVIC_InitStructure);//初始化NVIC寄存器
-
-
TIM_Cmd(TIM3,ENABLE);//使能TIMx
-
}
-
-
unsignedcharflag=0;
-
voidTIM3_IRQHandler(void)
-
{
-
flag=!flag;
-
if(flag==0)
-
{
-
LED1_ON;
-
LED2_ON;
-
LED3_ON;
-
}
-
else
-
{
-
LED1_OFF;
-
LED2_OFF;
-
LED3_OFF;
-
}
-
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
-
}
main.c
-
#include"stm32f10x.h"
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
#include"systick_driver.h"
-
#include"key_board.h"
-
-
unsignedintled_pwm_ctr_cnt=0;
-
unsignedintled_pwm=0;
-
-
/*外設(shè)時鐘初始化*/
-
voidRCC_PeriphClock_Config(void)
-
{
-
/*GPIO口時鐘初始化*/
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD,ENABLE);
-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
-
}
-
-
intmain()
-
{
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級分組
-
SysTick_Init(INT_10MS,SysTick_CLKSource_HCLK_Div8);//內(nèi)核滴答定時器配置,用于led任務(wù)輪循
-
RCC_PeriphClock_Config();
-
Led_Init();//led燈初始化
-
TIM3_Init(9999,7199);//定時器3初始化,溢出頻率為1hz
-
-
while(1)
-
{
-
;
-
}
-
}
3:PWM輸出
pwm輸出需確定兩點:PWM輸出波形的周期,PWM輸出波形的占空比,具體需要設(shè)置PWM波形的有效電平和PWM輸出模式;
如下代碼實現(xiàn)1khz的pwm波形輸出,通過安檢可以改變pwm波的占空比;
timer_driver.c
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
-
voidTIM3_Init(unsignedintarr,unsignedintpsc)
-
{
-
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
-
TIM_OCInitTypeDefTIM_OCInitStruct;
-
-
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//將TIM3的CH2映射到PB5上
-
-
//定時器TIM3初始化
-
TIM_TimeBaseStructure.TIM_Period=arr;
-
TIM_TimeBaseStructure.TIM_Prescaler=psc;
-
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
-
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
-
//PWMCH2輸出初始化
-
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;/*開發(fā)板高電平led燈亮,因為設(shè)置向上計數(shù)方式,并且設(shè)置OC1有效電平為高電平,根據(jù)手冊,設(shè)置為PWM模式2*/
-
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;/*設(shè)置OC2輸出使能*/
-
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;/*設(shè)置OC2有效電平為低電平*/
-
TIM_OC2Init(TIM3,&TIM_OCInitStruct);
-
-
////PWMCH1輸出初始化
-
//TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;/*開發(fā)板高電平led燈亮,因為設(shè)置向上計數(shù)方式,并且設(shè)置OC1有效電平為高電平,根據(jù)手冊,設(shè)置為PWM模式2*/
-
//TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;/*設(shè)置OC1輸出使能*/
-
//TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;/*設(shè)置OC1有效電平為低電平*/
-
//TIM_OC1Init(TIM3,&TIM_OCInitStruct);
-
-
TIM_Cmd(TIM3,ENABLE);//使能TIMx
-
-
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);/*開啟輸出比較2的預(yù)裝載功能*/
-
//TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);/*開啟輸出比較2的預(yù)裝載功能*/
-