b49c9ab0c6
完整实现了以下模块: - 系统数据总线(sys_data_bus)模块间通信 - UART/USART/LPUART驱动 + RS485半双工 - I2C驱动(AS5600角度传感器 + AT24C02 EEPROM) - Modbus RTU从站协议 - 步进电机微步进控制(正弦/余弦换相+S曲线加减速) - 到位开关驱动(DI_MIN/DI_MAX) - PGA+ADC电流检测 - 系统监控任务(内存/串口/传感器/任务状态) - LED/Key外设驱动 - 基于CMake + arm-none-eabi-gcc构建系统
78 lines
1.5 KiB
C
78 lines
1.5 KiB
C
#include "pga_driver.h"
|
|
#include "adc_driver.h"
|
|
#include "py32md530.h"
|
|
#include "config.h"
|
|
#include "sys_data_bus.h"
|
|
#include <stddef.h>
|
|
|
|
static uint8_t pga_gain = PGA_GAIN_1;
|
|
static float pga_shunt = PGA_SHUNT_RESISTOR;
|
|
static float pga_vref = PGA_VREF;
|
|
static uint8_t pga_initialized = 0;
|
|
|
|
ret_code_t pga_init(const pga_config_t *config)
|
|
{
|
|
if (config == NULL) {
|
|
return RET_INVALID_PARAM;
|
|
}
|
|
|
|
pga_gain = config->gain;
|
|
pga_shunt = config->shunt_resistor;
|
|
pga_vref = config->vref;
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
|
|
|
|
COMP->CR &= ~COMP_CR_EN_Msk;
|
|
|
|
COMP->CR = COMP_CR_EN_Msk
|
|
| COMP_CR_PGA_MODE_Msk
|
|
| ((uint32_t)pga_gain << COMP_CR_PGA_GAIN_Pos);
|
|
|
|
while (!(COMP->CSR & COMP_CSR_OUT_Msk));
|
|
|
|
pga_initialized = 1;
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
ret_code_t pga_deinit(void)
|
|
{
|
|
COMP->CR &= ~COMP_CR_EN_Msk;
|
|
pga_initialized = 0;
|
|
return RET_OK;
|
|
}
|
|
|
|
uint16_t pga_read_raw(void)
|
|
{
|
|
if (!pga_initialized) {
|
|
return 0;
|
|
}
|
|
|
|
return adc_read_channel(PGA_ADC_CH);
|
|
}
|
|
|
|
float pga_read_current(void)
|
|
{
|
|
uint16_t raw;
|
|
float voltage;
|
|
float current;
|
|
|
|
if (!pga_initialized) {
|
|
return 0.0f;
|
|
}
|
|
|
|
raw = adc_read_channel(PGA_ADC_CH);
|
|
|
|
voltage = (float)raw * pga_vref / PGA_ADC_RESOLUTION;
|
|
|
|
current = voltage / ((float)pga_gain * pga_shunt);
|
|
|
|
{
|
|
sys_data_bus_value_t val;
|
|
val.f = current;
|
|
sys_data_bus_write(SYS_DATA_BUS_MOTOR_CURRENT, val);
|
|
}
|
|
|
|
return current;
|
|
}
|