Stm32Project/Src/servo.c

62 lines
1.2 KiB
C

#include "servo.h"
#include "tim.h"
#include "lcd.h"
#include "motor.h"
int16_t Servo_Speed = 1, Servo_goal = 0, Servo_position = 0;
void Servo_SetAngle(int goal)
{
Servo_position = goal;
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_2, ANGLETOCCR(goal));
}
// 往哪个方向转
int Servo_toward(void)
{
if (Servo_goal > Servo_position)
{
return 1;
}
return -1;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
static int times = 0;
if (htim == &htim7)
{
if(times==Motor_tag)
{
Motor_time++;
Motor_reset_ornot();
}
if(Motor_start)
{
Motor_tag=times;
Motor_start=0;//清除标志位!!!
}
if (++times == 1000)
{
times = 0;
}
if (Servo_position == Servo_goal)
{
return;
}
if (Servo_Speed == 1)
{
Servo_position = Servo_goal;
}
if (Servo_Speed == 2 && times % 5 == 0)
{
Servo_position += Servo_toward();
}
if (Servo_Speed == 3 && times % 50 == 0)
{
Servo_position += Servo_toward();
}
Servo_SetAngle(Servo_position);
}
}