基于stm32 TSIC506f带校验测温程序
时间: 2023-08-11 07:06:32 浏览: 78
基于stm32的温度测量系统.doc
以下是一个基于STM32的TSIC506F带校验的测温程序示例,使用了HAL库:
```
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define TSIC506F_TIMEOUT 100
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
uint8_t rx_buffer[4];
uint8_t rx_index = 0;
uint32_t start_time = 0;
uint16_t temperature = 0;
while (1)
{
if (HAL_UART_Receive(&huart1, &rx_buffer[rx_index], 1, 100) == HAL_OK) {
if (rx_buffer[0] == 0x02) {
rx_index = 1;
start_time = HAL_GetTick();
} else if (rx_buffer[0] == 0x03 && rx_index == 3) {
temperature = ((rx_buffer[1] << 8) | rx_buffer[2]) * 0.01;
uint8_t checksum = (rx_buffer[1] + rx_buffer[2]) & 0xFF;
if (checksum == rx_buffer[3]) {
char buf[32];
sprintf(buf, "Temperature: %dC\r\n", temperature);
HAL_UART_Transmit(&huart1, (uint8_t*)buf, strlen(buf), 1000);
}
rx_index = 0;
} else if (rx_index == 3) {
rx_index = 0;
} else {
rx_index++;
}
} else if (HAL_GetTick() - start_time > TSIC506F_TIMEOUT) {
rx_index = 0;
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
```
这个程序使用了STM32的HAL库,通过UART接收TSIC506F的温度数据,并进行校验。如果校验通过,则将温度数据输出到串口上。
需要注意的是,TSIC506F的数据帧中包含了起始字节、结束字节和校验位,需要进行相应的处理才能得到正确的温度数据。在这个程序中,如果收到起始字节0x02,则将接收到的数据存储到缓冲区中,并记录开始时间;如果在一定时间内没有收到完整的数据帧,则清空缓冲区。如果收到结束字节0x03,则将缓冲区中的数据解析成温度数据,并进行校验。如果校验通过,则将温度数据输出到串口上。
阅读全文