TIM_DeInit(TIM1); //复位定时器1一切寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频数为0,不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方法为次序计数模式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //装备时钟分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循环计数次数值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //用以上参数初始化定时器时刻根底模块
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);//用以上参数初始化定时器时刻根底模块
TIM_DeInit(TIM2); //复位定时器1一切寄存器
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频数为0,不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方法为次序计数形式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //装备时钟分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循环计数次数值
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 触发中止源
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设置:答应中止,设置优先级//中止向量设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置优先级分组:先占优先级2位,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//更新事情
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//呼应优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//答应中止
NVIC_Init(&NVIC_InitStructure);//写入设置
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/257893.html