2024-11-06 22:26:06 +08:00
|
|
|
#include "servo.h"
|
|
|
|
#include "tim.h"
|
2024-11-07 11:59:20 +08:00
|
|
|
#include "lcd.h"
|
2024-11-06 22:26:06 +08:00
|
|
|
|
2024-11-07 11:59:20 +08:00
|
|
|
uint8_t Servo_Speed = 1, Servo_goal = 0, Servo_position = 0;
|
2024-11-06 22:26:06 +08:00
|
|
|
|
|
|
|
void Servo_SetAngle(int goal)
|
|
|
|
{
|
2024-11-07 11:59:20 +08:00
|
|
|
Servo_position = goal;
|
|
|
|
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_2, 2500 * goal / 180 - 500);
|
2024-11-06 22:26:06 +08:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:59:20 +08:00
|
|
|
// 往哪个方向转
|
2024-11-06 22:26:06 +08:00
|
|
|
int Servo_toward(void)
|
|
|
|
{
|
2024-11-07 11:59:20 +08:00
|
|
|
return Servo_goal > Servo_position;
|
2024-11-06 22:26:06 +08:00
|
|
|
}
|
2024-11-07 11:59:20 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
// }
|