问题:我是按着江协科技的PWM控制舵机来改变引脚的,但是舵机没有反应,这应该怎么改参数啊!
PWM.c文件
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM4);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCIniStructure;
TIM_OCStructInit(&TIM_OCIniStructure);
TIM_OCIniStructure.TIM_OCMode =TIM_OCMode_PWM1; //输出比较模式
TIM_OCIniStructure.TIM_OCPolarity =TIM_OCPolarity_High;
TIM_OCIniStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCIniStructure.TIM_Pulse =0; //设置CCR
TIM_OC4Init(TIM4, &TIM_OCIniStructure);//通道2
// TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_Cmd(TIM4,ENABLE);
}
void PWM_SetCompare4(uint16_t Compare)
{
TIM_SetCompare4(TIM4, Compare);
}
PWM.h文件
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_SetCompare4(uint16_t Compare);
#endif
舵机模块文件Servo.c
#include "stm32f10x.h" // Device header
#include "PWM.h"
void Servo_Init(void)
{
PWM_Init();
}
void Servo_SetAngle(float Angle)
{
PWM_SetCompare4(Angle / 180*2000 +500);
}
Servo.h如下:
#ifndef __SERVO_H
#define __SERVO_H
void Servo_Init(void);
void Servo_SetAngle(float Angle);
#endif
main函数如下
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Servo.h"
#include "Key.h"
uint8_t KeyNum;
float Angle;
int main(void)
{
OLED_Init();
Servo_Init();
Key_Init ();
OLED_ShowString (1,1,"Angle:");
Servo_SetAngle(90);
while(1)
{
// KeyNum = Key_GetNum ();
// if(KeyNum == 1)
// {
// Angle+=30;
// if(Angle>=180)
// {
// Angle =0;
// }
// }
// for(Angle=0;Angle<=180;)
// {
// Servo_SetAngle (Angle);
// Delay_ms (1000);
// Angle+=30;
// if(Angle>180)
// {
// Angle =0;
// }
// OLED_ShowNum (1,7,Angle ,3);
// }
}
}
代码就是这样的,我是刚学的,然后就到这里不知道了,主文件里只要有双斜杠的就是注释了的,求解答。