Files
linear-Slide/Interface/Int_TMC2209.h

52 lines
1.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef __INT_TMC2209_H__
#define __INT_TMC2209_H__
#include "main.h"
#include "tim.h"
#include "gpio.h"
#include "usart.h"
#include "Com_type.h"
#include "Com_debug.h"
// TMC2209串口从机地址
#define TMC2209_USART_SLAVE_ADDR 0x03
#define TMC2209_GCONF_REG_ADDR 0x00
// ---------------- 机械参数定义 ----------------
// 步进角 1.8° (200步/圈)
#define STEPPER_CIRCLE_STEPS 200
// 细分设置 (8细分)
#define STEPPER_SPLIT 8
// 丝杆导程 (转一圈前进 8mm)
#define STICK_LEAD 8.0f
// [核心] 计算每毫米需要的脉冲数
// 公式: (一圈步数 * 细分) / 导程
// 例如: (200 * 8) / 8 = 200 步/mm
#define STEPS_PER_MM ((float)(STEPPER_CIRCLE_STEPS * STEPPER_SPLIT) / STICK_LEAD)
// 定时器时钟频率 (4MHz)
#define TIM_CLOCK_FREQ 4000000
// ---------------- 计算公式修改 ----------------
// [修改] 计算ARR的宏根据线速度(mm/s)计算ARR值
// 逻辑推导:
// 1. 每秒脉冲数 (Hz) = 速度(mm/s) * 每毫米步数(steps/mm)
// 2. 翻转频率 (Toggle Hz) = 每秒脉冲数 * 2 (因为一个脉冲包含高低电平两次中断)
// 3. ARR = 定时器时钟 / 翻转频率 - 1
// 传入参数 speed_mm_s 单位mm/s
#define CALC_ARR(speed_mm_s) ((uint32_t)((TIM_CLOCK_FREQ) / ((speed_mm_s) * STEPS_PER_MM * 2.0f) - 1))
/* 物理参数定义 */
#define SOFT_LIMIT_MAX_MM 230.0f /* 丝杆有效量程上限 (mm) */
#define SOFT_LIMIT_MIN_MM 0.0f /* 丝杆有效量程下限 (mm) */
void Int_TMC2209_init(void);
void Int_TMC2209_start(Stepper_t *stepper, Encoder_t *encoder);
void Int_TMC2209_stop(void);
#endif /* __INT_TMC2209_H__ */