Files
Motor_v2/Robot_V2/device/tle5012b.cpp
T
cbb 180a46271c ```
feat(DengFOC):
新增了基于三相电流采样的dq轴电流计算函数`cal_Iq_Id`
调整了开环控制中的电压限制逻辑
更新了默认电源电压值为24V。
KHT7823测试代码
```
2025-10-30 20:20:14 +08:00

135 lines
3.9 KiB
C++
Raw 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.
#include "tle5012b.h"
#include "core_cmInstr.h"
#include "gd32c10x_gpio.h"
#include "gd32c10x_spi.h"
#include "stdbool.h"
#include "systick.h"
#define SPI_TX_OFF() \
do { \
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, \
GPIO_PIN_5); \
} while (0)
#define SPI_TX_ON() \
do { \
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5); \
} while (0)
#define SPI_CS_ENABLE() gpio_bit_reset(GPIOA, GPIO_PIN_15)
#define SPI_CS_DISABLE() gpio_bit_set(GPIOA, GPIO_PIN_15)
/**
* @brief Send or read half word through SPI
*
* @param[in] spi_periph SPI peripheral
* @param[in] i_HalfWord data
*/
uint16_t spi_rw_half_word(uint32_t spi_periph, const uint16_t i_HalfWord) {
uint32_t to;
uint16_t data = 0xFFFF;
bool ok = true;
/* ---------- Send data ---------- */
to = 200;
while ((RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE)) && --to)
;
if (to == 0)
ok = false;
if (ok) {
if (RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE)) {
(void)spi_i2s_data_receive(spi_periph); /* throw Dummy */
}
spi_i2s_data_transmit(spi_periph, i_HalfWord);
/* wait rx succ*/
to = 200;
while ((RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE)) && --to)
;
if (to == 0)
ok = false;
else
data = spi_i2s_data_receive(spi_periph); /* throw Dummy */
}
/* ---------- wait bsu idle ---------- */
if (ok) {
to = 200;
while ((SET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TRANS)) &&
SET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE) && --to)
;
if (to == 0)
ok = false;
}
return ok ? data : 0xFFFF;
}
void KHT7823_gpio_init(void) {
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI0);
rcu_periph_clock_enable(RCU_AF);
/* 关闭 JTAG,仅保留 SWD(释放 PA15、PB3、PB4) */
gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
/* -------- SPI0 全 remap:PB3/4/5 + PA15 -------- */
gpio_pin_remap_config(GPIO_SPI0_REMAP, ENABLE);
/* PA15 → NSS, 推挽输出 */
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
/* PB3 → SCK, PB5 → MOSI, 推挽输出 */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ,
GPIO_PIN_3 | GPIO_PIN_5);
/* PB4 → MISO, 浮空输入 */
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ,
GPIO_PIN_4 | GPIO_PIN_10);
}
/**
* @brief Initialize SPI module
*
* @details Configures SPI hardware and initializes necessary settings
*/
void KHT7823_spi_init(void) {
spi_parameter_struct spi_init_str;
spi_struct_para_init(&spi_init_str);
/* SPI参数:16bit, 主机, 全双工, CPOL=0 CPHA=1, MSB first */
spi_init_str.device_mode = SPI_MASTER;
spi_init_str.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_str.frame_size = SPI_FRAMESIZE_16BIT;
spi_init_str.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_str.nss = SPI_NSS_SOFT;
spi_init_str.endian = SPI_ENDIAN_MSB;
spi_init_str.prescale = SPI_PSC_16; /* 8MHz @ 128MHz */
spi_init(SPI0, &spi_init_str);
spi_nss_output_enable(SPI0);
spi_nssp_mode_enable(SPI0);
spi_enable(SPI0);
}
void KHT7823_init(void) {
KHT7823_gpio_init();
KHT7823_spi_init();
}
float KHT7823_readAngle() {
u16 u16Command = 0x4000;
float ang = 0;
SPI_CS_ENABLE();
// 2. 发送命令1,同时接收回答1
SPI_DATA(SPI0) = u16Command;
while (RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE))
; // 等待发送缓冲区空
while (RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE))
; // 等待接收缓冲区满
u16 u16data = SPI_DATA(SPI0); // 读取回答1
SPI_CS_DISABLE();
// 将15位角度值转换为角度(0-360度)
ang = ((float)u16data * 360.0f) / 32767.0f;
return ang;
}