Stm32Project/Src/servo.c

52 lines
1.0 KiB
C

#include "servo.h"
#include "tim.h"
#include "lcd.h"
uint8_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)
{
return Servo_goal > Servo_position;
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
static uint8_t times = 0,tt=0;
if (htim == &htim7)
{
// if (++times == 250)
// {
// LCD_ShowxNum(50, 50, ++tt, 4, 16, 0);
// times = 0;
// }
// else
// {
// return;
// }
if (Servo_position == Servo_goal)
{
return;
}
if (Servo_Speed == 1)
{
Servo_position = Servo_goal;
}
if (Servo_Speed == 2)
{
Servo_position += Servo_toward() * 9;
}
if (Servo_Speed == 3)
{
Servo_position += Servo_toward();
}
Servo_SetAngle(Servo_position);
}
}