最近在编用
STM32驱动舵机的程序,但在应用中发现一个非常怪的现象。
STM32端口初始化和PWM设置的程序如下:
端口初始化:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
PWM设置的程序如下:
tiM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 20000;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM3->CCMR1|=7<<4;
TIM3->CCMR1|=1<<3;
TIM3->CCER|=1<<0;
TIM3->CR1=0x8000; TIM3->CCR1 = 2000;
TIM3->CCMR1|=7<<12;
TIM3->CCMR1|=1<<11;
TIM3->CCER|=1<<4;
TIM3->CCR2 = 1600;
TIM3->CR1=0x8000;
TIM3->CCMR2|=7<<4;
TIM3->CCMR2|=1<<3;
TIM3->CCER|=1<<8;
TIM3->CCR3 = 1200;
TIM3->CR1=0x8000;
TIM3->CCMR2|=7<<12;
TIM3->CCMR2|=1<<11;
TIM3->CCER|=1<<12;
TIM3->CCR4 = 1000;
TIM3->CR1=0x8000;
TIM3->CR1|=0x01;
/* TIM enable counter */
TIM_CtrlPWMOutputs(TIM3,ENABLE);
现在的现象是,当我改变TIM3->CCR1 = 2000;这一行的值的时候,可以用示波器看到输出波形在变化,但舵机却始终只运行到一个固定的位置,就不再动了。这个位置也不随TIM3->CCR1的值变化而变动。
找了半天没找到原因,也百思不得其解,
0