想编辑了一个用按键使舵机旋转的代码(用stm32f103zet6单面机库函数),但是舵机不动.
pwm文件
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stdint.h"
#include "led.h"
void TIM2_PWM_init(uint16_t arr,uint16_t psc)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE);
GPIO_InitTypeDef GPIO_InitHHH;
GPIO_InitHHH.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitHHH.GPIO_Pin=GPIO_Pin_3;
GPIO_InitHHH.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitHHH);
TIM_InternalClockConfig(TIM2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef PPPP;
PPPP.NVIC_IRQChannel=TIM2_IRQn;
PPPP.NVIC_IRQChannelCmd=ENABLE;
PPPP.NVIC_IRQChannelPreemptionPriority=0;
PPPP.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&PPPP);
TIM_TimeBaseInitTypeDef HHHH;
HHHH.TIM_ClockDivision=TIM_CKD_DIV1;
HHHH.TIM_CounterMode=TIM_CounterMode_Up;
HHHH.TIM_Period=arr;
HHHH.TIM_Prescaler=psc;
HHHH.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&HHHH);
TIM_OCInitTypeDef OOOO;
TIM_OCStructInit(&OOOO);
OOOO.TIM_OCMode=TIM_OCMode_PWM2;
OOOO.TIM_OCPolarity=TIM_OCPolarity_Low;
OOOO.TIM_OutputState=TIM_OutputNState_Enable;
OOOO.TIM_Pulse=0;
TIM_OC2Init(TIM2,&OOOO);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_Cmd(TIM2,ENABLE);
}
舵机文件
#include "stm32f10x.h"
#include "PWM.h"
#include "stm32f10x.h" // Device header
#include "stm32f10x_tim.h"
void steering_init(void)
{
TIM2_PWM_init(199,7199);
}
void steering_angle(float angle)
{
TIM_SetCompare2(TIM2,angle/180*2000+500);
}
主函数
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "stdint.h"
#include "stm32f10x_tim.h"
#include "steering.h"
#include "key.h"
float angle;
uint8_t key;
int main(void)
{
key_init();
steering_init();
steering_angle(90);
uint8_t key1();
while(1)
{
key = key1();
if(key==1)
{
angle += 30;
if (angle>=180)
{
angle=0;
}
}
steering_angle(angle);key = key1();
if(key==1)
{
angle += 30;
if (angle>=180)
{
angle=0;
}
}
steering_angle(angle);
}
}
各位看看我错哪了qwq