控制舵机时只能执行TIM_SetCompare1(TIM1,5),不能执行到TIM_SetCompare2(TIM1,25)
主函数:
int main(void)
{
kongbai_Init();
TIM1_PWM_Init(199,7199);
while (1)
{
TIM_SetCompare1(TIM1,5);
TIM_SetCompare2(TIM1,5);
TIM_SetCompare3(TIM1,5);
TIM_SetCompare4(TIM1,5);
delay_ms(1000);
TIM_SetCompare1(TIM1,25);
TIM_SetCompare2(TIM1,25);
TIM_SetCompare3(TIM1,25);
TIM_SetCompare4(TIM1,25);
delay_ms(1000);
}
}
定时器及i/o口初始化 void IO_Init()//初始化所需GPIO口及使能时钟
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA和GPIOB的时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;//初始化PA1,PA2,PA3,PA6,PA7
PA8,PA9,PA10,PA11,PA15 I/O口
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //设置复用推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //设置频率50HZ
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA所需端口
}
void TIM1_PWM_Init(u16 arr,u16 psc)//使能TIM1定时器及初始化设置
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_DeInit(TIM1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//使能TIM1时钟
TIM_TimeBaseInitStructure.TIM_Period=arr;//自动装载值
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;//预分频系数
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//初始化TIM1
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//PWM模式2----CNT大于ccrx为有效
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//使能状态输出
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//di电平为有效电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补通道极性高
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//互补通道输出使能
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //非工作状态下通道输出低
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset; //非工作状态下互补通道输出低
TIM_OC1Init(TIM1, &TIM_OCInitStructure);//初始化TIM1_CH1设置输出模式
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);//使能输出比较预装载
TIM_OC2Init(TIM1, &TIM_OCInitStructure);//初始化TIM1_CH2设置输出模式
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);//使能输出比较预装载
TIM_OC3Init(TIM1, &TIM_OCInitStructure);//初始化TIM1_CH3设置输出模式
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);//使能输出比较预装载
TIM_OC4Init(TIM1, &TIM_OCInitStructure);//初始化TIM1_CH4设置输出模式
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);//使能输出比较预装载
TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Disable;//运行模式下工作选择
TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Disable;//空闲模式下工作选择
TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF;//锁定设置
TIM_BDTRInitStructure.TIM_DeadTime=0x03;//死区时间
TIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable;//刹车功能
TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_Low;//刹车功能输入极性
TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;//自动输出使能
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);//死区和刹车功能寄存器配置
TIM_CtrlPWMOutputs(TIM1,ENABLE); //主输出使能 (只有高级定时器,如TIM1和TIM8需要)
TIM_Cmd(TIM1,ENABLE);//使能TIM1
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>