#include "TIM_Init.h"void TIM1_Configuration (uint Period,uint Pulse)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
本文引用地址:http://m.butianyuan.cn/article/201611/321627.htmRCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//使能由APB2時鐘控制的外設中的TIM1端口
TIM_DeInit(TIM1); //復位定時器1所有寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 0; //預分頻數(shù)為0,不分頻
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//計數(shù)方式為順序計數(shù)模式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//設置計數(shù)器溢出后的重載初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//設置計數(shù)器溢出后的重載初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //配置時鐘分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循環(huán)計數(shù)次數(shù)值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //用以上參數(shù)初始化定時器時間基礎模塊
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pulse;//127;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 5;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
//清中斷
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
//TIM1中斷源設置,開啟相應通道的捕捉比較中斷
TIM_Cmd(TIM1, ENABLE); //使能定時器1
TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能定時器1的PWM輸出
}
void TIM2_Configuration (uint Period)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//用以上參數(shù)初始化定時器時間基礎模塊
TIM_DeInit(TIM2); //復位定時器1所有寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 0; //預分頻數(shù)為0,不分頻
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//計數(shù)方式為順序計數(shù)模式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//設置計數(shù)器溢出后的重載初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//設置計數(shù)器溢出后的重載初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //配置時鐘分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循環(huán)計數(shù)次數(shù)值
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//清中斷
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
//TIM1中斷源設置,開啟相應通道的捕捉比較中斷
TIM_ITConfig(//使能或者失能指定的TIM中斷
TIM2, //TIM2
TIM_IT_Update|//TIM 中斷源
TIM_IT_Trigger,//TIM 觸發(fā)中斷源
ENABLE//使能
);
TIM_Cmd(TIM2, ENABLE); //使能定時器1
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdefVECT_TAB_RAM//中斷向量表存放位置選擇
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //中斷向量表存放于RAM中0x00
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);//中斷向量表存放于FLASH中0x00
#endif
//中斷NVIC設置:允許中斷,設置優(yōu)先級//中斷向量設置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置優(yōu)先級分組:先占優(yōu)先級2位,從優(yōu)先級2位
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//更新事件
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//搶占優(yōu)先級0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//響應優(yōu)先級1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//允許中斷
NVIC_Init(&NVIC_InitStructure);//寫入設置
}
評論