diff --git a/.vscode/settings.json b/.vscode/settings.json
index 5c7fd71..183643c 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -9,6 +9,7 @@
"usart.h": "c",
"stm32f4xx_it.h": "c",
"tca6416.h": "c",
- "stm32f4xx_hal.h": "c"
+ "stm32f4xx_hal.h": "c",
+ "dac161s997.h": "c"
}
}
\ No newline at end of file
diff --git a/Core/Inc/main.h b/Core/Inc/main.h
index f52de2b..2d441d6 100644
--- a/Core/Inc/main.h
+++ b/Core/Inc/main.h
@@ -188,7 +188,7 @@ extern "C"
#define HART2_RX_GPIO_Port GPIOD
#define HART2_RST_Pin GPIO_PIN_7
#define HART2_RST_GPIO_Port GPIOD
-
+//DAC161S997引脚定义
#define DAC1_CS_Pin GPIO_PIN_6
#define DAC1_CS_GPIO_Port GPIOB
#define DAC1_SCK_Pin GPIO_PIN_3
diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c
index d76378e..8a372c1 100644
--- a/Core/Src/freertos.c
+++ b/Core/Src/freertos.c
@@ -783,45 +783,45 @@ 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 = 4.0f; // 起始电流4mA
- float current_step = 0.5f; // 每次增加0.5mA
- uint8_t direction = 1; // 1表示递增,0表示递减
+ 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);
+ // }
+ // }
- // 初始化DAC161S997
- dac161s997_init();
+ // // 初始化成功,执行一次诊断
+ // dac161s997_diagnose(DAC_TYPE_161S997);
/* Infinite loop */
for(;;)
- {
- // 只输出到DAC1(因为只有一块DAC161S997)
- dac161s997_output(DAC161S997, test_current);
- osDelay(200);
+ {
+ dac161s997_output(DAC_TYPE_161S997, current_buff[0]);
- // 更新电流值
- if (direction == 1)
- {
- test_current += current_step;
- if (test_current >= 20.0f) // 达到20mA时开始递减
- {
- test_current = 20.0f;
- direction = 0;
- }
- }
- else
- {
- test_current -= current_step;
- if (test_current <= 4.0f) // 达到4mA时开始递增
- {
- test_current = 4.0f;
- direction = 1;
- }
- }
+ // // 每100次循环执行一次诊断
+ // static uint32_t loop_count = 0;
+ // if(++loop_count >= 100) {
+ // loop_count = 0;
+ // dac161s997_diagnose(DAC_TYPE_161S997);
+ // }
- // 每个周期延时1秒
- osDelay(800);
+ osDelay(100);
}
/* USER CODE END start_dac161s997_test_task */
}
\ No newline at end of file
diff --git a/Core/Src/main.c b/Core/Src/main.c
index da1d465..871d920 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -126,6 +126,7 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
//MX_DMA_Init();
+ dac161s997_spi_init();
//MX_TIM3_Init();
//MX_SPI1_Init();
MX_USART6_UART_Init();
diff --git a/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c b/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
index b3ce9bb..09123c4 100644
--- a/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
+++ b/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
@@ -407,7 +407,7 @@ GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/
-void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
+ void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));
diff --git a/MDK-ARM/controller_pcba.uvoptx b/MDK-ARM/controller_pcba.uvoptx
index 1d09d64..2ae31ec 100644
--- a/MDK-ARM/controller_pcba.uvoptx
+++ b/MDK-ARM/controller_pcba.uvoptx
@@ -153,147 +153,102 @@
0
1
- ad7124_analog[channel_nr],0x0A
+ dac161s997_init_status
1
1
- ad7124_analog[channel_nr].voltage
+ dac161s997_current_value
2
1
- ret
+ dac161s997_verify_status
3
1
- buffer[0]
+ dac161s997_dac_code
4
1
- FINESH_ID
+ dac161s997_config_reg
5
1
- ret
+ dac161s997_status_reg
6
1
- g_channel_config
+ dac161s997_init_status
7
1
- ad7124_regs[5].value
+ dac161s997_error_count
8
1
- g_adc_error
+ dac161s997_spi_state
9
1
- g_adc_data
+ dac161s997_test_step
10
1
- g_adc_state
+ dac161s997_operation_status
11
1
- g_adc_control
+ dac161s997_direction
12
1
- wr_buf[8]
+ dac161s997_target_current
13
1
- ad7124_analog[channel_nr].voltage
+ dac161s997_cmd_status
14
1
- huart6.gState
+ dac161s997_spi_phase
15
1
- rts_pin_state
+ dac161s997_last_error
16
1
- rst_pin_state
+ dac161s997_retry_count
17
1
- uart6_raw_data,0x10
+ dac161s997_verify_status
18
1
- uart6_full_data
+ dac161s997_config_reg
19
1
- uart6_dr_raw
-
-
- 20
- 1
- uart6_data_bits
-
-
- 21
- 1
- uart6_parity_bit
-
-
- 22
- 1
- uart6_stop_bits
-
-
- 23
- 1
- tca6416_init_result
-
-
- 24
- 1
- tca6416_write_data
-
-
- 25
- 1
- tca6416_read_data
-
-
- 26
- 1
- tca6416_error_count
-
-
- 27
- 1
- tca6416_test_pattern
-
-
- 28
- 1
- tca6416_i2c_ack_status
+ dac161s997_status_reg
diff --git a/Public/PCBA测试需求说明书 V1.1.docx b/Public/PCBA测试需求说明书 V1.1.docx
index cf876b8..f5ab83c 100644
Binary files a/Public/PCBA测试需求说明书 V1.1.docx and b/Public/PCBA测试需求说明书 V1.1.docx differ
diff --git a/Public/~WRL2044.tmp b/Public/~WRL2044.tmp
new file mode 100644
index 0000000..cf876b8
Binary files /dev/null and b/Public/~WRL2044.tmp differ
diff --git a/User/driver/dac161s997.c b/User/driver/dac161s997.c
index 54956a0..4d84978 100644
--- a/User/driver/dac161s997.c
+++ b/User/driver/dac161s997.c
@@ -1,64 +1,252 @@
#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)
+{
+ // 拉低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, bytes);
+
+ // 拉高CS
+ HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
+ osDelay(5);
+ return status;
+}
/**
* @brief 向DAC161S997芯片写入寄存器值
*
- * 该函数通过SPI接口向DAC161S997芯片写入指定的寄存器值。
- *
- * @param dac_num DAC芯片编号
+ * @param dac_type DAC芯片类型
* @param reg 要写入的寄存器地址
* @param data 要写入的数据,16位
*/
-void dac161s997_write_reg(uint8_t dac_num, uint8_t reg, uint16_t data)
+void dac161s997_write_reg(dac_type_t dac_type, uint8_t reg, uint16_t data)
{
uint8_t data_buffer[3] = {0, 0, 0};
data_buffer[0] = reg;
data_buffer[1] = data >> 8;
data_buffer[2] = data;
- HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET); // CS拉低
- HAL_SPI_Transmit(&hspi1, data_buffer, 3, 1000);
- HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET); // CS拉高
+ // 使用新的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;
}
/**
* @brief 初始化DAC161S997 DAC设备
*/
-void dac161s997_init(void)
+int dac161s997_init(dac_type_t dac_type, float current)
{
- // 初始化CS引脚
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- __HAL_RCC_GPIOB_CLK_ENABLE();
- GPIO_InitStruct.Pin = DAC1_CS_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- HAL_GPIO_Init(DAC1_CS_GPIO_Port, &GPIO_InitStruct);
- HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET);
+ 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;
- // 初始化DAC161S997芯片寄存器
- dac161s997_write_reg(1, DAC161S997_ERR_LOW_REG, 0xFFFF);
- dac161s997_write_reg(1, DAC161S997_ERR_CONFIG_REG, 0x070E);
+ // 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);
+
+ // // 2. 设置输出范围为4-20mA(XFR寄存器)
+ // dac161s997_write_reg(dac_type, DAC161S997_XFR_REG, 0x0A);
+ // HAL_Delay(2);
+
+ // // 验证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;
}
/**
* @brief DAC161S997芯片输出函数
*
- * 此函数用于设置DAC161S997芯片的输出电流。
- *
- * @param dac_num DAC芯片类型
+ * @param dac_type DAC芯片类型
* @param current 需要设置的电流值,单位:毫安
*/
-void dac161s997_output(uint8_t dac_num, float current)
+void dac161s997_output(dac_type_t dac_type, float current)
{
- uint32_t dac_code = (uint32_t)((current * 65535.0f) / 24.0f);
+ // // 限制电流范围在4-20mA之间
+ // if (current < 4.0f) current = 4.0f;
+ // if (current > 20.0f) current = 20.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};
data_buffer[0] = DAC161S997_DACCODE_REG;
data_buffer[1] = dac_code >> 8;
data_buffer[2] = dac_code;
-
- HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_RESET); // CS拉低
- HAL_SPI_Transmit(&hspi1, data_buffer, 3, 1000);
- HAL_GPIO_WritePin(DAC1_CS_GPIO_Port, DAC1_CS_Pin, GPIO_PIN_SET); // CS拉高
+ dac161s997_init(dac_type, current);
+ 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);
+
}
+
+/**
+ * @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
diff --git a/User/driver/dac161s997.h b/User/driver/dac161s997.h
index 5333fac..f4fd9a2 100644
--- a/User/driver/dac161s997.h
+++ b/User/driver/dac161s997.h
@@ -3,6 +3,29 @@
#include "main.h"
#include "user_spi.h"
+#include
+#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 ********************************************************************/
/**
* @defgroup DAC161S997_REGS
@@ -20,12 +43,55 @@
#define DAC161S997_SPI SPI1
-void dac161s997_write_reg(uint8_t dac_num, uint8_t reg, uint16_t data);
-uint16_t dac161s997_read(uint8_t reg);
-extern void dac161s997_init(void);
-extern void dac161s997_output(uint8_t dac_num, float current);
+// DAC芯片类型定义
+typedef enum {
+ DAC_TYPE_161S997 = 0, // DAC161S997芯片
+ DAC_TYPE_8568 = 1 // DAC8568芯片
+} dac_type_t;
// 定义DAC芯片编号(只有一块DAC161S997)
-#define DAC161S997 1
+#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); // 新增诊断函数声明
#endif
diff --git a/User/system/user_spi.c b/User/system/user_spi.c
index 517be1f..41969c5 100644
--- a/User/system/user_spi.c
+++ b/User/system/user_spi.c
@@ -48,17 +48,26 @@ SPI_HandleTypeDef hspi2;
#define SUCCESS 0
#define FAILURE -1
+// int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number)
+// {
+// // 使用 HAL_SPI_TransmitReceive 函数进行 SPI 通信
+// if (HAL_SPI_TransmitReceive(&hspi2, data_write, (uint8_t *)spi_rx_buffer, bytes_number, 1000) != HAL_OK)
+// {
+// // 如果通信失败,返回 FAILURE
+// return FAILURE;
+// }
+// /* 将 SPI 接收缓冲区复制到提供的数据缓冲区,以便返回给调用者 */
+// memcpy(data_write, spi_rx_buffer, bytes_number);
+// // 返回 SUCCESS 表示通信成功
+// return SUCCESS;
+// }
int32_t spi_transmit_receive(SPI_HandleTypeDef *hspi, uint8_t *data_write, uint8_t bytes_number)
{
- // 使用 HAL_SPI_TransmitReceive 函数进行 SPI 通信
- if (HAL_SPI_TransmitReceive(&hspi2, data_write, (uint8_t *)spi_rx_buffer, bytes_number, 1000) != HAL_OK)
+ if (HAL_SPI_TransmitReceive(hspi, data_write, (uint8_t *)spi_rx_buffer, bytes_number, 1000) != HAL_OK)
{
- // 如果通信失败,返回 FAILURE
return FAILURE;
}
- /* 将 SPI 接收缓冲区复制到提供的数据缓冲区,以便返回给调用者 */
memcpy(data_write, spi_rx_buffer, bytes_number);
- // 返回 SUCCESS 表示通信成功
return SUCCESS;
}
@@ -136,7 +145,7 @@ void dac161s997_spi_init(void)
// SCK (PB3), MISO (PB4), MOSI (PB5)
GPIO_InitStruct.Pin = DAC1_SCK_Pin | DAC1_MISO_Pin | DAC1_MOSI_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; // SPI1 alternate function
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
@@ -159,7 +168,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_32; // 适中的时钟频率
+ hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; // 适中的时钟频率
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
@@ -170,7 +179,7 @@ void dac161s997_spi_init(void)
Error_Handler();
}
- HAL_Delay(1);
+
}
void ad7124_cs_on(void)