DAC161S驱动OK
This commit is contained in:
parent
d3dc45ae2d
commit
af35522a99
|
@ -210,8 +210,8 @@ void MX_FREERTOS_Init(void)
|
|||
|
||||
|
||||
/* DAC161S997测试任务 */
|
||||
// osThreadDef(dac161s997_test_task, start_dac161s997_test_task, osPriorityNormal, 0, 128);
|
||||
// dac161s997_test_taskHandle = osThreadCreate(osThread(dac161s997_test_task), NULL);
|
||||
osThreadDef(dac161s997_test_task, start_dac161s997_test_task, osPriorityNormal, 0, 128);
|
||||
dac161s997_test_taskHandle = osThreadCreate(osThread(dac161s997_test_task), NULL);
|
||||
|
||||
/* DAC8568测试任务 */
|
||||
osThreadDef(dac8568_test_task, start_dac8568_test_task, osPriorityLow, 0, 256);
|
||||
|
@ -750,45 +750,36 @@ void test_tca6416_task(void const *argument)
|
|||
void start_dac161s997_test_task(void const *argument)
|
||||
{
|
||||
/* USER CODE BEGIN start_dac161s997_test_task */
|
||||
float test_current = 5.0f;
|
||||
float current_buff[2] = {0x000002, 0x000000};
|
||||
int init_result = 0;
|
||||
|
||||
// 等待系统稳定
|
||||
osDelay(1000);
|
||||
dac161s997_init(DAC_TYPE_161S997, current_buff[0]);
|
||||
// // 初始化DAC
|
||||
// init_result = dac161s997_init(DAC_TYPE_161S997, test_current);
|
||||
// if(init_result != 0) {
|
||||
// // 初始化失败,记录错误代码
|
||||
// dac161s997_init_status = 0xFF; // 标记初始化失败
|
||||
// dac161s997_last_error = init_result;
|
||||
|
||||
// // 初始化失败后仍然尝试诊断,看看哪个寄存器有问题
|
||||
// dac161s997_diagnose(DAC_TYPE_161S997);
|
||||
|
||||
// // 停留在错误状态,方便调试
|
||||
// while(1) {
|
||||
// osDelay(1000);
|
||||
// }
|
||||
// }
|
||||
osDelay(1000); // 启动延时1秒等待系统稳定
|
||||
|
||||
// // 初始化成功,执行一次诊断
|
||||
// dac161s997_diagnose(DAC_TYPE_161S997);
|
||||
// 初始化DAC
|
||||
dac161s997_init();
|
||||
|
||||
// 读取初始状态
|
||||
uint16_t init_status = dac161s997_read_status(DAC161S997);
|
||||
if(init_status == 0xFFFF || init_status == 0x0000) {
|
||||
// SPI通信有问题,重新初始化
|
||||
osDelay(100);
|
||||
dac161s997_init();
|
||||
}
|
||||
|
||||
/* Infinite loop */
|
||||
for(;;)
|
||||
{
|
||||
dac161s997_output(DAC_TYPE_161S997, current_buff[0]);
|
||||
for (;;)
|
||||
{
|
||||
// 设置输出电流为10mA
|
||||
current_buff[0] = 12.0f;
|
||||
dac161s997_output(DAC161S997, current_buff[0]);
|
||||
|
||||
// // 每100次循环执行一次诊断
|
||||
// static uint32_t loop_count = 0;
|
||||
// if(++loop_count >= 100) {
|
||||
// loop_count = 0;
|
||||
// dac161s997_diagnose(DAC_TYPE_161S997);
|
||||
// // 读取状态确认写入成功
|
||||
// uint16_t status = dac161s997_read_status(DAC161S997);
|
||||
// if(status == 0xFFFF || status == 0x0000) {
|
||||
// // 如果通信失败,重试写入
|
||||
// osDelay(10);
|
||||
// dac161s997_output(DAC161S997, current_buff[0]);
|
||||
// }
|
||||
|
||||
osDelay(100);
|
||||
osDelay(100); // 延时100ms
|
||||
}
|
||||
/* USER CODE END start_dac161s997_test_task */
|
||||
}
|
||||
|
|
|
@ -1,253 +1,108 @@
|
|||
#include "dac161s997.h"
|
||||
#include "stm32f4xx_hal.h" // 添加HAL库头文件
|
||||
#include "spi.h" // 添加SPI头文件
|
||||
#define _DAC_CHIP_RESET_CODE 0xC33C
|
||||
#define _ERR_CONFIG_SPI_TIMOUT_400MS (7 << 1)
|
||||
// 定义调试变量
|
||||
volatile uint8_t dac161s997_init_status = 0xFF; // 初始化状态 (0=成功, 0xFF=未初始化)
|
||||
volatile uint8_t dac161s997_error_count = 0; // 错误计数
|
||||
volatile uint8_t dac161s997_test_step = 0; // 当前测试步骤
|
||||
volatile uint8_t dac161s997_operation_status = 0; // 操作状态 (0=正常, 1=错误)
|
||||
volatile uint16_t dac161s997_config_reg = 0; // 配置寄存器值
|
||||
volatile uint16_t dac161s997_status_reg = 0; // 状态寄存器值
|
||||
volatile uint16_t dac161s997_data_reg = 0; // 数据寄存器值
|
||||
|
||||
volatile float dac161s997_current_value = 0.0f; // 当前输出电流值
|
||||
volatile uint8_t dac161s997_direction = 0; // 电流变化方向 (0=递减, 1=递增)
|
||||
volatile float dac161s997_target_current = 0.0f; // 目标电流值
|
||||
volatile uint16_t dac161s997_dac_code = 0; // DAC代码值
|
||||
volatile uint8_t dac161s997_cmd_status = 0; // 命令执行状态
|
||||
volatile uint8_t dac161s997_spi_phase = 0; // SPI通信阶段
|
||||
volatile uint8_t dac161s997_last_error = 0; // 最后一次错误代码
|
||||
volatile uint8_t dac161s997_retry_count = 0; // 重试计数
|
||||
volatile uint8_t dac161s997_verify_status = 0; // 验证状态
|
||||
|
||||
// 添加SPI通信调试变量定义
|
||||
volatile uint8_t dac161s997_spi_state = SPI_STATE_IDLE;
|
||||
volatile uint8_t dac161s997_spi_error = SPI_ERR_NONE;
|
||||
volatile uint32_t dac161s997_spi_timeout = 0;
|
||||
volatile uint8_t dac161s997_spi_buffer[4] = {0};
|
||||
volatile uint8_t dac161s997_spi_bytes = 0;
|
||||
volatile uint8_t dac161s997_cs_state = 0;
|
||||
volatile uint32_t dac161s997_last_spi_time = 0;
|
||||
volatile uint32_t dac161s997_spi_retry_count = 0;
|
||||
|
||||
// 添加SPI通信辅助函数
|
||||
static int32_t dac161s997_spi_transaction(uint8_t *data, uint8_t bytes, uint32_t timeout)
|
||||
/**
|
||||
* @brief 读取DAC161S997状态寄存器
|
||||
* @return 状态寄存器值
|
||||
*/
|
||||
uint16_t dac161s997_read_status(chip_type_e dac_num)
|
||||
{
|
||||
uint8_t rx_data[4] = {0}; // Add receive buffer
|
||||
// 拉低CS
|
||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET);
|
||||
osDelay(5);
|
||||
// SPI传输
|
||||
int32_t status = spi_transmit_receive(&hspi1, data, rx_data, bytes);
|
||||
|
||||
// 拉高CS
|
||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
||||
osDelay(5);
|
||||
return status;
|
||||
uint8_t tx_buffer[3] = {DAC161S997_STATUS_REG | 0x80, 0x00, 0x00}; // 最高位置1表示读操作
|
||||
uint8_t rx_buffer[3] = {0, 0, 0};
|
||||
|
||||
board_spi_init(dac_num);
|
||||
board_spi_cs_on(dac_num);
|
||||
spi_transmit_receive(&hspi1, tx_buffer, rx_buffer, 3);
|
||||
board_spi_cs_off(dac_num);
|
||||
|
||||
return ((uint16_t)rx_buffer[1] << 8) | rx_buffer[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 向DAC161S997芯片写入寄存器值
|
||||
*
|
||||
* @param dac_type DAC芯片类型
|
||||
* 该函数通过SPI接口向DAC161S997芯片写入指定的寄存器值。
|
||||
*
|
||||
* @param dac_num DAC芯片编号
|
||||
* @param reg 要写入的寄存器地址
|
||||
* @param data 要写入的数据,16位
|
||||
*/
|
||||
void dac161s997_write_reg(dac_type_t dac_type, uint8_t reg, uint16_t data)
|
||||
void dac161s997_write_reg(chip_type_e dac_num, uint8_t reg, uint16_t data)
|
||||
{
|
||||
uint8_t data_buffer[3] = {0, 0, 0};
|
||||
uint8_t rx_buffer[3] = {0, 0, 0};
|
||||
data_buffer[0] = reg;
|
||||
data_buffer[1] = data >> 8;
|
||||
data_buffer[2] = data;
|
||||
|
||||
// 使用新的SPI事务函数
|
||||
if(dac161s997_spi_transaction(data_buffer, 3, 1000) != SUCCESS) {
|
||||
dac161s997_error_count++;
|
||||
dac161s997_last_error = dac161s997_spi_error;
|
||||
}
|
||||
|
||||
// 添加短暂延时确保数据稳定
|
||||
HAL_Delay(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从DAC161S997芯片读取寄存器值
|
||||
*
|
||||
* @param dac_type DAC芯片类型
|
||||
* @param reg 要读取的寄存器地址
|
||||
* @param data 存储读取的数据的指针
|
||||
* @return int32_t 0表示成功,非0表示失败
|
||||
*/
|
||||
int32_t dac161s997_read_reg(dac_type_t dac_type, uint8_t reg, uint16_t *data)
|
||||
{
|
||||
uint8_t data_buffer[3] = {0, 0, 0};
|
||||
data_buffer[0] = reg | 0x80; // 设置读位
|
||||
|
||||
dac161s997_spi_state = SPI_STATE_READ_START;
|
||||
|
||||
// 使用新的SPI事务函数
|
||||
if(dac161s997_spi_transaction(data_buffer, 3, 1000) != SUCCESS) {
|
||||
dac161s997_error_count++;
|
||||
dac161s997_last_error = dac161s997_spi_error;
|
||||
dac161s997_spi_state = SPI_STATE_ERROR;
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
*data = (data_buffer[1] << 8) | data_buffer[2];
|
||||
dac161s997_spi_state = SPI_STATE_READ_END;
|
||||
|
||||
// 添加短暂延时确保数据稳定
|
||||
HAL_Delay(1);
|
||||
|
||||
return SUCCESS;
|
||||
board_spi_init(dac_num);
|
||||
board_spi_cs_on(dac_num);
|
||||
spi_transmit_receive(&hspi1, data_buffer, rx_buffer, 3);
|
||||
board_spi_cs_off(dac_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化DAC161S997 DAC设备
|
||||
*/
|
||||
int dac161s997_init(dac_type_t dac_type, float current)
|
||||
void dac161s997_init(void)
|
||||
{
|
||||
dac161s997_write_reg(dac_type, DAC161S997_RESET_REG, _DAC_CHIP_RESET_CODE);
|
||||
//王绪洁版本初始化代码
|
||||
dac161s997_write_reg(dac_type, DAC161S997_ERR_LOW_REG, 0xFFFF);
|
||||
dac161s997_write_reg(dac_type, DAC161S997_ERR_CONFIG_REG, 0x070E);
|
||||
|
||||
|
||||
// uint16_t reg_value = 0;
|
||||
// 复位DAC
|
||||
dac161s997_write_reg(DAC161S997, DAC161S997_RESET_REG, 0x0000);
|
||||
HAL_Delay(10); // 等待复位完成
|
||||
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_RESET_REG, _DAC_CHIP_RESET_CODE);
|
||||
// // 1. 写保护寄存器(解锁)
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_PROTECT_REG_WR_REG, 0x00);
|
||||
// HAL_Delay(2);
|
||||
// 配置错误检测
|
||||
dac161s997_write_reg(DAC161S997, DAC161S997_ERR_LOW_REG, 0x0000); // 设置低于4mA报警阈值
|
||||
dac161s997_write_reg(DAC161S997, DAC161S997_ERR_HIGH_REG, 0xFFFF); // 设置高于20mA报警阈值
|
||||
dac161s997_write_reg(DAC161S997, DAC161S997_ERR_CONFIG_REG, 0x070E); // 配置错误检测
|
||||
|
||||
// // 2. 设置输出范围为4-20mA(XFR寄存器)
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_XFR_REG, 0x0A);
|
||||
// HAL_Delay(2);
|
||||
// 读取状态寄存器确认初始化成功
|
||||
uint16_t status = dac161s997_read_status(DAC161S997);
|
||||
if(status == 0xFFFF || status == 0x0000) {
|
||||
// SPI通信可能有问题,重试初始化
|
||||
HAL_Delay(100);
|
||||
dac161s997_write_reg(DAC161S997, DAC161S997_RESET_REG, 0x0000);
|
||||
HAL_Delay(10);
|
||||
}
|
||||
|
||||
// // 验证XFR寄存器
|
||||
// if(dac161s997_read_reg(dac_type, DAC161S997_XFR_REG, ®_value) == SUCCESS) {
|
||||
// if(reg_value != 0x0A) {
|
||||
// // XFR寄存器写入失败
|
||||
// dac161s997_error_count++;
|
||||
// return -1; // 初始化失败
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 3. 使能输出(NOP寄存器)
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_NOP_REG, 0x01);
|
||||
// HAL_Delay(2);
|
||||
|
||||
// // 验证NOP寄存器
|
||||
// if(dac161s997_read_reg(dac_type, DAC161S997_NOP_REG, ®_value) == SUCCESS) {
|
||||
// if(reg_value != 0x01) {
|
||||
// // NOP寄存器写入失败
|
||||
// dac161s997_error_count++;
|
||||
// return -2; // 初始化失败
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 4. 写错误配置寄存器
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_ERR_CONFIG_REG, 0x070E);
|
||||
// HAL_Delay(2);
|
||||
|
||||
// // 验证ERR_CONFIG寄存器
|
||||
// if(dac161s997_read_reg(dac_type, DAC161S997_ERR_CONFIG_REG, ®_value) == SUCCESS) {
|
||||
// if(reg_value != 0x070E) {
|
||||
// // ERR_CONFIG寄存器写入失败
|
||||
// dac161s997_error_count++;
|
||||
// return -3; // 初始化失败
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 5. 写低报警寄存器
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_ERR_LOW_REG, 0xFFFF);
|
||||
// HAL_Delay(2);
|
||||
|
||||
// // 6. 写高报警寄存器(可选)
|
||||
// dac161s997_write_reg(dac_type, DAC161S997_ERR_HIGH_REG, 0x0000);
|
||||
// HAL_Delay(2);
|
||||
|
||||
// // 初始化成功
|
||||
// dac161s997_init_status = 0; // 标记初始化成功
|
||||
// return 0;
|
||||
// 初始输出设为4mA
|
||||
dac161s997_output(DAC161S997, 4.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DAC161S997芯片输出函数
|
||||
*
|
||||
* @param dac_type DAC芯片类型
|
||||
* 此函数用于设置DAC161S997芯片的输出电流。
|
||||
* DAC161S997的输出范围是4-20mA,对应DAC码值0-65535
|
||||
*
|
||||
* @param dac_num DAC芯片类型
|
||||
* @param current 需要设置的电流值,单位:毫安
|
||||
*/
|
||||
void dac161s997_output(dac_type_t dac_type, float current)
|
||||
void dac161s997_output(chip_type_e dac_num, float current)
|
||||
{
|
||||
// // 限制电流范围在4-20mA之间
|
||||
// if (current < 4.0f) current = 4.0f;
|
||||
// if (current > 20.0f) current = 20.0f;
|
||||
// 将电流值限制在4-20mA范围内
|
||||
if(current < 4.0f) current = 4.0f;
|
||||
if(current > 20.0f) current = 20.0f;
|
||||
|
||||
// 计算DAC码值:(current - 4mA) * 65535 / (20mA - 4mA)
|
||||
uint32_t dac_code = (uint32_t)((current - 4.0f) * 65535.0f / 16.0f);
|
||||
|
||||
|
||||
// uint32_t dac_code = (uint32_t)(((current - 4.0f) * 65535.0f) / 16.0f);
|
||||
// uint8_t data_buffer[3] = {DAC161S997_DACCODE_REG, dac_code >> 8, dac_code};
|
||||
// HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET);
|
||||
// spi_transmit_receive(&hspi1, data_buffer, 3);
|
||||
// HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
||||
//uint32_t dac_code = (uint32_t)((current * 65535.0f) / 24.0f);
|
||||
uint32_t dac_code = (uint32_t)(5461.0f);
|
||||
uint8_t data_buffer[3] = {0, 0, 0};
|
||||
uint8_t rx_buffer[3] = {0, 0, 0};
|
||||
data_buffer[0] = DAC161S997_DACCODE_REG;
|
||||
data_buffer[1] = dac_code >> 8;
|
||||
data_buffer[2] = dac_code;
|
||||
dac161s997_init(dac_type, current);
|
||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET);
|
||||
spi_transmit_receive(&hspi1, data_buffer, data_buffer, 3);
|
||||
HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
data_buffer[1] = (dac_code >> 8) & 0xFF;
|
||||
data_buffer[2] = dac_code & 0xFF;
|
||||
|
||||
board_spi_init(dac_num);
|
||||
board_spi_cs_on(dac_num);
|
||||
spi_transmit_receive(&hspi1, data_buffer, rx_buffer, 3);
|
||||
board_spi_cs_off(dac_num);
|
||||
|
||||
// 读取状态寄存器验证写入是否成功
|
||||
uint16_t status = dac161s997_read_status(dac_num);
|
||||
if(status == 0xFFFF || status == 0x0000) {
|
||||
// SPI通信可能有问题,重试写入
|
||||
HAL_Delay(1);
|
||||
board_spi_init(dac_num);
|
||||
board_spi_cs_on(dac_num);
|
||||
spi_transmit_receive(&hspi1, data_buffer, rx_buffer, 3);
|
||||
board_spi_cs_off(dac_num);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DAC161S997诊断函数
|
||||
*
|
||||
* 读取并存储所有关键寄存器的值,用于调试
|
||||
*
|
||||
* @param dac_type DAC芯片类型
|
||||
* @return int 0表示成功,非0表示失败
|
||||
*/
|
||||
int dac161s997_diagnose(dac_type_t dac_type)
|
||||
{
|
||||
uint16_t reg_value = 0;
|
||||
|
||||
// 读取XFR寄存器(输出范围)
|
||||
if(dac161s997_read_reg(dac_type, DAC161S997_XFR_REG, ®_value) == SUCCESS) {
|
||||
dac161s997_config_reg = reg_value; // 存储到调试变量
|
||||
}
|
||||
|
||||
// 读取NOP寄存器(输出使能)
|
||||
if(dac161s997_read_reg(dac_type, DAC161S997_NOP_REG, ®_value) == SUCCESS) {
|
||||
dac161s997_status_reg = reg_value; // 存储到调试变量
|
||||
}
|
||||
|
||||
// 读取ERR_CONFIG寄存器
|
||||
if(dac161s997_read_reg(dac_type, DAC161S997_ERR_CONFIG_REG, ®_value) == SUCCESS) {
|
||||
// 可以存储到其他调试变量
|
||||
}
|
||||
|
||||
// 读取DACCODE寄存器(当前DAC值)
|
||||
if(dac161s997_read_reg(dac_type, DAC161S997_DACCODE_REG, ®_value) == SUCCESS) {
|
||||
dac161s997_data_reg = reg_value; // 存储到调试变量
|
||||
}
|
||||
|
||||
// 读取STATUS寄存器(设备状态)
|
||||
if(dac161s997_read_reg(dac_type, 0x00, ®_value) == SUCCESS) {
|
||||
// STATUS寄存器地址通常是0x00
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// #ifndef DAC161S997_RANGE_REG
|
||||
// #define DAC161S997_RANGE_REG 0x01
|
||||
// #endif
|
||||
// #ifndef DAC161S997_OUTPUT_ENABLE_REG
|
||||
// #define DAC161S997_OUTPUT_ENABLE_REG 0x02
|
||||
// #endif
|
||||
|
|
|
@ -4,27 +4,6 @@
|
|||
#include "main.h"
|
||||
#include "user_spi.h"
|
||||
#include <stdint.h>
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
// 定义返回值常量
|
||||
#define SUCCESS 0
|
||||
#define FAILURE (-1)
|
||||
|
||||
// SPI通信状态定义
|
||||
#define SPI_STATE_IDLE 0x00
|
||||
#define SPI_STATE_WRITE_START 0x01
|
||||
#define SPI_STATE_WRITE_END 0x02
|
||||
#define SPI_STATE_READ_START 0x03
|
||||
#define SPI_STATE_READ_END 0x04
|
||||
#define SPI_STATE_ERROR 0xFF
|
||||
|
||||
// SPI错误代码定义
|
||||
#define SPI_ERR_NONE 0x00
|
||||
#define SPI_ERR_TIMEOUT 0x01
|
||||
#define SPI_ERR_CS 0x02
|
||||
#define SPI_ERR_TRANSMIT 0x03
|
||||
#define SPI_ERR_RECEIVE 0x04
|
||||
#define SPI_ERR_BUSY 0x05
|
||||
|
||||
/* Defines ********************************************************************/
|
||||
/**
|
||||
|
@ -43,55 +22,9 @@
|
|||
|
||||
#define DAC161S997_SPI SPI1
|
||||
|
||||
// DAC芯片类型定义
|
||||
typedef enum {
|
||||
DAC_TYPE_161S997 = 0, // DAC161S997芯片
|
||||
DAC_TYPE_8568 = 1 // DAC8568芯片
|
||||
} dac_type_t;
|
||||
|
||||
// 定义DAC芯片编号(只有一块DAC161S997)
|
||||
#define DAC161S997_CHIP_NUM 1
|
||||
|
||||
// // DAC161S997寄存器地址定义
|
||||
// #define DAC161S997_RANGE_REG 0x01 // 输出范围寄存器
|
||||
// #define DAC161S997_OUTPUT_ENABLE_REG 0x02 // 输出使能寄存器
|
||||
// #define DAC161S997_ERR_LOW_REG 0x03 // 错误阈值寄存器
|
||||
// #define DAC161S997_ERR_CONFIG_REG 0x04 // 错误配置寄存器
|
||||
|
||||
// 调试变量声明
|
||||
extern volatile uint8_t dac161s997_init_status; // 初始化状态 (0=成功, 0xFF=未初始化)
|
||||
extern volatile uint8_t dac161s997_error_count; // 错误计数
|
||||
extern volatile uint8_t dac161s997_test_step; // 当前测试步骤
|
||||
extern volatile uint8_t dac161s997_operation_status; // 操作状态 (0=正常, 1=错误)
|
||||
extern volatile uint16_t dac161s997_config_reg; // 配置寄存器值
|
||||
extern volatile uint16_t dac161s997_status_reg; // 状态寄存器值
|
||||
extern volatile uint16_t dac161s997_data_reg; // 数据寄存器值
|
||||
extern volatile float dac161s997_current_value; // 当前输出电流值
|
||||
extern volatile uint8_t dac161s997_direction; // 电流变化方向 (0=递减, 1=递增)
|
||||
extern volatile float dac161s997_target_current; // 目标电流值
|
||||
extern volatile uint16_t dac161s997_dac_code; // DAC代码值
|
||||
extern volatile uint8_t dac161s997_cmd_status; // 命令执行状态
|
||||
extern volatile uint8_t dac161s997_spi_phase; // SPI通信阶段
|
||||
extern volatile uint8_t dac161s997_last_error; // 最后一次错误代码
|
||||
extern volatile uint8_t dac161s997_retry_count; // 重试计数
|
||||
extern volatile uint8_t dac161s997_verify_status; // 验证状态
|
||||
|
||||
// 添加SPI通信调试变量声明
|
||||
extern volatile uint8_t dac161s997_spi_state; // SPI当前状态
|
||||
extern volatile uint8_t dac161s997_spi_error; // SPI错误代码
|
||||
extern volatile uint32_t dac161s997_spi_timeout; // SPI超时计数
|
||||
extern volatile uint8_t dac161s997_spi_buffer[4]; // SPI数据缓冲区
|
||||
extern volatile uint8_t dac161s997_spi_bytes; // SPI传输字节数
|
||||
extern volatile uint8_t dac161s997_cs_state; // CS引脚状态
|
||||
extern volatile uint32_t dac161s997_last_spi_time; // 上次SPI操作时间
|
||||
extern volatile uint32_t dac161s997_spi_retry_count; // SPI重试次数
|
||||
|
||||
// 函数声明
|
||||
void dac161s997_write_reg(dac_type_t dac_type, uint8_t reg, uint16_t data);
|
||||
int32_t dac161s997_read_reg(dac_type_t dac_type, uint8_t reg, uint16_t *data);
|
||||
int dac161s997_init(dac_type_t dac_type, float current);
|
||||
void dac161s997_output(dac_type_t dac_type, float current);
|
||||
void dac161s997_spi_init(void);
|
||||
int dac161s997_diagnose(dac_type_t dac_type); // 新增诊断函数声明
|
||||
void dac161s997_write_reg(chip_type_e dac_num, uint8_t reg, uint16_t data);
|
||||
uint16_t dac161s997_read(uint8_t reg);
|
||||
extern void dac161s997_init(void);
|
||||
extern void dac161s997_output(chip_type_e dac_num, float current);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -235,7 +235,7 @@ void dac161s997_spi_init(void)
|
|||
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; // CPOL = 0
|
||||
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; // CPHA = 0
|
||||
hspi1.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; // 适中的时钟频率
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; // 适中的时钟频率
|
||||
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
|
|
Loading…
Reference in New Issue