Files
ldc1612_cmake_vscode/Src/sensor_example.c
2025-08-17 02:58:32 +08:00

225 lines
9.1 KiB
C

//
// Sensor Usage Example
// 传感器使用示例代码
//
#include "ldc1612.h"
// #include "tmp112.h"
#include "i2c.h"
/*!
\brief 传感器初始化示例
\param[in] none
\param[out] none
\retval none
*/
void sensors_init_example(void) {
ldc1612_status_t ldc_status;
// tmp112a_status_t tmp_status;
/* 初始化I2C总线 */
i2c_status_t i2c_status = i2c_config();
if (i2c_status != I2C_STATUS_SUCCESS) {
// 使用串口发送错误信息
const char* error_msg = "I2C init failed\r\n";
for (uint8_t i = 0; error_msg[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, error_msg[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
return;
}
/* 扫描I2C总线 */
// i2c_scan();
/* 初始化LDC1612 */
ldc_status = ldc1612_init();
if (ldc_status == LDC1612_STATUS_SUCCESS) {
const char* msg = "LDC1612 init success\r\n";
for (uint8_t i = 0; msg[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, msg[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
/* 配置通道0 */
ldc_status = ldc1612_config_single_channel(LDC1612_CHANNEL_0);
if (ldc_status != LDC1612_STATUS_SUCCESS) {
const char* error = "LDC1612 config failed\r\n";
for (uint8_t i = 0; error[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, error[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
}
} else {
const char* error = "LDC1612 init failed: ";
for (uint8_t i = 0; error[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, error[i]);
}
const char* status_str = ldc1612_get_status_string(ldc_status);
for (uint8_t i = 0; status_str[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, status_str[i]);
}
const char* newline = "\r\n";
for (uint8_t i = 0; newline[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, newline[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
}
/* 初始化TMP112A */
// tmp_status = tmp112a_init();
// if (tmp_status == TMP112A_STATUS_SUCCESS) {
// const char* msg = "TMP112A init success\r\n";
// for (uint8_t i = 0; msg[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, msg[i]);
// }
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
// /* 设置温度阈值 */
// tmp_status = tmp112a_set_thresholds(-10.0f, 50.0f);
// if (tmp_status != TMP112A_STATUS_SUCCESS) {
// const char* error = "TMP112A threshold config failed\r\n";
// for (uint8_t i = 0; error[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, error[i]);
// }
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
// }
// } else {
// const char* error = "TMP112A init failed: ";
// for (uint8_t i = 0; error[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, error[i]);
// }
// const char* status_str = tmp112a_get_status_string(tmp_status);
// for (uint8_t i = 0; status_str[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, status_str[i]);
// }
// const char* newline = "\r\n";
// for (uint8_t i = 0; newline[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, newline[i]);
// }
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
// }
}
/*!
\brief 传感器读取示例
\param[in] none
\param[out] none
\retval none
*/
void sensors_read_example(void) {
ldc1612_result_t ldc_result;
// tmp112a_result_t tmp_result;
ldc1612_status_t ldc_status;
// tmp112a_status_t tmp_status;
/* 读取LDC1612数据 */
ldc_status = ldc1612_read_channel(LDC1612_CHANNEL_0, &ldc_result);
if (ldc_status == LDC1612_STATUS_SUCCESS) {
if (!ldc_result.error_flag) {
const char* msg = "LDC1612 Data: 0x";
for (uint8_t i = 0; msg[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, msg[i]);
}
/* 发送32位十六进制数据 */
uint8_t hex_chars[] = "0123456789ABCDEF";
for (int8_t i = 7; i >= 0; i--) {
uint8_t nibble = (ldc_result.frequency >> (i * 4)) & 0x0F;
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, hex_chars[nibble]);
}
const char* newline = "\r\n";
for (uint8_t i = 0; newline[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, newline[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
} else {
const char* error = "LDC1612 Error Code: 0x";
for (uint8_t i = 0; error[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, error[i]);
}
uint8_t hex_chars[] = "0123456789ABCDEF";
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, hex_chars[(ldc_result.error_code >> 4) & 0x0F]);
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, hex_chars[ldc_result.error_code & 0x0F]);
const char* newline = "\r\n";
for (uint8_t i = 0; newline[i] != '\0'; i++) {
while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
usart_data_transmit(RS485_PHY, newline[i]);
}
while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
}
}
/* 读取TMP112A数据 */
// tmp_status = tmp112a_read_temperature(&tmp_result);
// if (tmp_status == TMP112A_STATUS_SUCCESS) {
// const char* msg = "Temperature: ";
// for (uint8_t i = 0; msg[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, msg[i]);
// }
// /* 简单的温度显示(整数部分) */
// int16_t temp_int = (int16_t)tmp_result.temperature_c;
// if (temp_int < 0) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, '-');
// temp_int = -temp_int;
// }
// if (temp_int >= 100) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, '0' + (temp_int / 100));
// temp_int %= 100;
// }
// if (temp_int >= 10) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, '0' + (temp_int / 10));
// temp_int %= 10;
// }
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, '0' + temp_int);
// const char* unit = " C";
// for (uint8_t i = 0; unit[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, unit[i]);
// }
// if (tmp_result.alert_flag) {
// const char* alert = " [ALERT]";
// for (uint8_t i = 0; alert[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, alert[i]);
// }
// }
// const char* newline = "\r\n";
// for (uint8_t i = 0; newline[i] != '\0'; i++) {
// while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {}
// usart_data_transmit(RS485_PHY, newline[i]);
// }
// while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {}
// }
}