#include "turn.h"
u8 led0_statelift;
u8 led0_stateright;
u8 pwm_high_counterlift;
u8 pwm_high_counterright;
//配置嵌套中断控制器
void tim3_nvic_config(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitTypeDef NVIC_Init_Struct;
NVIC_Init_Struct.NVIC_IRQChannel =TIM3_IRQn; //设置定时器 3中断
NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority =0 ;//抢占与优先级
NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 0; //zi优先级
NVIC_Init_Struct.NVIC_IRQChannelCmd =ENABLE; //shineng
NVIC_Init(&NVIC_Init_Struct);
}
//定时器初始化配置
void tim3_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
tim3_nvic_config();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseInitStruct.TIM_Prescaler = 36-1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 10-1; // 200khz翻转 100k个周期
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct); //初始化 TIM2 配置
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //清除溢出中断标志
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE); //开启时钟
}
//定时器中断函数处理
void set_pwm_highlift(u8 high_pd)
{
pwm_high_counterlift = high_pd;
}
void set_pwm_highright(u8 high_pd)
{
pwm_high_counterright = high_pd;
}
void TIM3_IRQHandler(void)
{
//进中断
if(TIM_GetITStatus(TIM3, TIM_IT_Update)!= RESET)
{
//left
led0_statelift ++;
if(led0_statelift <= pwm_high_counterlift)
{
LEFT0_ON();
LEFT1_OFF();
}
else if((led0_statelift > pwm_high_counterlift) && (led0_statelift <= 100))
{
LEFT0_OFF();
LEFT1_ON();
}
else
{
led0_statelift = 0;
}
TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
//right
led0_stateright ++;
if(led0_stateright <= pwm_high_counterright)
{
RIGHT0_ON();
RIGHT1_OFF();
}
else if((led0_stateright > pwm_high_counterright) && (led0_stateright <= 100))
{
RIGHT0_OFF();
RIGHT1_ON();
}
else
{
led0_stateright = 0;
}
}
TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
}
|