513 lines
16 KiB
C
513 lines
16 KiB
C
/* USER CODE BEGIN Header */
|
||
/**
|
||
******************************************************************************
|
||
* @file : main.c
|
||
* @brief : Main program body
|
||
******************************************************************************
|
||
* @attention
|
||
*
|
||
* Copyright (c) 2023 STMicroelectronics.
|
||
* All rights reserved.
|
||
*
|
||
* This software is licensed under terms that can be found in the LICENSE file
|
||
* in the root directory of this software component.
|
||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||
*
|
||
******************************************************************************
|
||
*/
|
||
/* USER CODE END Header */
|
||
/* Includes ------------------------------------------------------------------*/
|
||
#include "main.h"
|
||
#include "adc.h"
|
||
#include "dma.h"
|
||
#include "iwdg.h"
|
||
#include "usart.h"
|
||
#include "gpio.h"
|
||
|
||
/* Private includes ----------------------------------------------------------*/
|
||
/* USER CODE BEGIN Includes */
|
||
#include "stdio.h"
|
||
#include "stdlib.h"
|
||
#include "math.h"
|
||
#include <string.h> // 用于memset函数
|
||
|
||
//#include "modbus_core_crc.h"
|
||
/* USER CODE END Includes */
|
||
|
||
/* Private typedef -----------------------------------------------------------*/
|
||
/* USER CODE BEGIN PTD */
|
||
|
||
uint8_t gucAddr = 0x10;
|
||
#define SLAVE_ADDR gucAddr // Modbus从机地址
|
||
//#define SLAVE_ADDR 0x11 // Modbus从机地址
|
||
#define DEEP_REG_ADDR 0x0000 // 液位 0,1,2,3
|
||
#define TEMP_REG_ADDR 0x0001 // 油温 *10
|
||
#define REGNUM_01 0x01 // 温度
|
||
#define REGNUM_02 0x02 // 温度
|
||
#define T_HEARTER 10000 // 加热时间 ms
|
||
#define T_COOLER 10000 // 冷却时间 ms
|
||
|
||
uint8_t modbusRxBuf[16]; // Modbus接收缓冲匿
|
||
uint8_t modbusTxBuf[16]; // Modbus发鿁缓冲区
|
||
volatile uint8_t rxFlag = 0; // 接收完成标志
|
||
|
||
static uint16_t gusOilTemp = 0;
|
||
static uint16_t level;
|
||
//uint8_t UART1_Rx_flg = 0;
|
||
/* USER CODE END PTD */
|
||
|
||
/* Private define ------------------------------------------------------------*/
|
||
/* USER CODE BEGIN PD */
|
||
/* USER CODE END PD */
|
||
|
||
/* Private macro -------------------------------------------------------------*/
|
||
/* USER CODE BEGIN PM */
|
||
static float thermistor[4];
|
||
static uint16_t adc_value[4];
|
||
uint16_t temp_value[4];
|
||
|
||
float tem_compare[4];
|
||
|
||
/* USER CODE END PM */
|
||
|
||
/* Private variables ---------------------------------------------------------*/
|
||
|
||
/* USER CODE BEGIN PV */
|
||
|
||
/* USER CODE END PV */
|
||
|
||
/* Private function prototypes -----------------------------------------------*/
|
||
void SystemClock_Config(void);
|
||
/* USER CODE BEGIN PFP */
|
||
|
||
/* USER CODE END PFP */
|
||
|
||
/* Private user code ---------------------------------------------------------*/
|
||
/* USER CODE BEGIN 0 */
|
||
|
||
|
||
static uint8_t uartRecvCnt = 0;
|
||
static uint32_t ulUartRecvTime = 0;
|
||
|
||
void get_level(void);
|
||
|
||
|
||
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
||
{
|
||
if(huart->Instance == USART1){
|
||
HAL_UART_Receive_IT(huart, modbusRxBuf, 1); // 重新启动接收
|
||
}
|
||
}
|
||
|
||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
||
|
||
if(huart->Instance == USART1){
|
||
|
||
if(uartRecvCnt == 0 && modbusRxBuf[0] != SLAVE_ADDR){
|
||
HAL_UART_Receive_IT(huart, modbusRxBuf, 1); // 非本机地?,重置接?
|
||
return;
|
||
}
|
||
if(++uartRecvCnt >= 8){ // 接收?8字节,触发处?
|
||
uartRecvCnt = 0;
|
||
rxFlag = 1; // 设置接收完成标志
|
||
}
|
||
HAL_UART_Receive_IT(huart, &modbusRxBuf[uartRecvCnt], 1); // 继续接收下一个字?
|
||
ulUartRecvTime = 0;
|
||
}
|
||
}
|
||
|
||
|
||
uint16_t CRC16(uint8_t *buf, int len)
|
||
{
|
||
uint16_t crc = 0xFFFF;
|
||
for(int pos=0; pos<len; pos++){
|
||
crc ^= (uint16_t)buf[pos];
|
||
for(int i=8; i!=0; i--){
|
||
if((crc & 0x0001) != 0){
|
||
crc >>= 1;
|
||
crc ^= 0xA001; // Modbus CRC多项?
|
||
}else{
|
||
crc >>= 1;
|
||
}
|
||
}
|
||
}
|
||
return crc;
|
||
}
|
||
|
||
|
||
void AppendCRC(uint8_t* buf, uint8_t len)
|
||
{
|
||
uint16_t crc = CRC16(buf, len);
|
||
buf[len] = crc & 0xFF; // 低字节在剿
|
||
buf[len+1] = crc >> 8; // 高字节在吿
|
||
}
|
||
|
||
void Process_Modbus(void)
|
||
{
|
||
//printf("modbusr\r\n");
|
||
/* 第一层地坿过滤 */
|
||
if(modbusRxBuf[0] != SLAVE_ADDR) return; // 非本机地坿,直接忽畿
|
||
//printf("addr ok\r\n");
|
||
/* 第二层CRC验证 */
|
||
uint16_t crc = CRC16(modbusRxBuf, 6);
|
||
if(*(uint16_t*)&modbusRxBuf[6] != crc) return; // CRC校验失败,忽畿
|
||
//printf("crc ok\r\n");
|
||
switch(modbusRxBuf[1]){ // 根据功能码处琿
|
||
case 0x03: // 读保持寄存器
|
||
if(*(uint16_t*)&modbusRxBuf[3] == DEEP_REG_ADDR) // 棿查寄存器地址
|
||
{
|
||
uint16_t regnum = (modbusRxBuf[4] << 8) | modbusRxBuf[5];
|
||
if(regnum == 0x01)
|
||
{
|
||
modbusTxBuf[0] = SLAVE_ADDR; // 从机地址
|
||
modbusTxBuf[1] = 0x03; // 功能砿
|
||
modbusTxBuf[2] = 0x02; // 返回字节敿(2字节)
|
||
uint16_t deep = level; //
|
||
modbusTxBuf[3] = deep >> 8; // 高字芿
|
||
modbusTxBuf[4] = deep & 0xFF; // 低字芿
|
||
AppendCRC(modbusTxBuf, 5); // 添加CRC校验
|
||
HAL_UART_Transmit(&huart1, modbusTxBuf, 7, 100); // 发鿿7字节响应
|
||
}
|
||
if(regnum == 0x02)
|
||
{
|
||
modbusTxBuf[0] = SLAVE_ADDR; // 从机地址
|
||
modbusTxBuf[1] = 0x03; // 功能砿
|
||
modbusTxBuf[2] = 0x04; // 返回字节敿(2字节)
|
||
uint16_t deep = level; // 模拟温度倿(实际应从传感器读叿)
|
||
modbusTxBuf[3] = deep >> 8; // 高字芿
|
||
modbusTxBuf[4] = deep & 0xFF; // 低字芿
|
||
uint16_t temp = gusOilTemp; // 模拟温度倿(实际应从传感器读叿)
|
||
modbusTxBuf[5] = temp >> 8; // 高字芿
|
||
modbusTxBuf[6] = temp & 0xFF; // 低字芿
|
||
AppendCRC(modbusTxBuf, 7); // 添加CRC校验
|
||
HAL_UART_Transmit(&huart1, modbusTxBuf, 9, 100); // 发鿿7字节响应
|
||
}
|
||
}
|
||
if(*(uint16_t*)&modbusRxBuf[3] == TEMP_REG_ADDR) // 棿查寄存器地坿
|
||
{
|
||
|
||
modbusTxBuf[0] = SLAVE_ADDR; // 从机地址
|
||
modbusTxBuf[1] = 0x03; // 功能砿
|
||
modbusTxBuf[2] = 0x02; // 返回字节敿(2字节)
|
||
uint16_t temp = gusOilTemp; // 模拟温度倿(实际应从传感器读叿)
|
||
modbusTxBuf[3] = temp >> 8; // 高字芿
|
||
modbusTxBuf[4] = temp & 0xFF; // 低字芿
|
||
AppendCRC(modbusTxBuf, 5); // 添加CRC校验
|
||
HAL_UART_Transmit(&huart1, modbusTxBuf, 7, 100); // 发鿿7字节响应
|
||
}
|
||
break;
|
||
// 可以添加其他功能码处琿
|
||
}
|
||
memset(modbusTxBuf, 0, sizeof(modbusTxBuf)); // 清空发鿁缓冲区
|
||
}
|
||
|
||
//re_def
|
||
|
||
int fputc(int ch, FILE *f)
|
||
{
|
||
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff);
|
||
return ch;
|
||
}
|
||
|
||
|
||
//count temp
|
||
static float getTemp(float Rt)
|
||
{
|
||
const float Rp = 10000.0; ///< 10K NTC
|
||
const float T2 = (273.15+25.0); ///< 10K NTC
|
||
const float Bx = 3950.0; ///< 10K NTC
|
||
const float Ka = 273.15; ///< 10K NTC
|
||
|
||
float Temp = 0.0f;
|
||
Temp = Rt / Rp;
|
||
Temp = log(Temp);
|
||
Temp /=Bx;
|
||
Temp += (1 / T2);
|
||
Temp = 1 / Temp;
|
||
Temp -= Ka;
|
||
return Temp;
|
||
}
|
||
|
||
|
||
void readTemp(void)
|
||
{
|
||
float vol[4] = {0,0,0,0};
|
||
|
||
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_value, 4);
|
||
HAL_Delay(1);
|
||
// printf("readTemp %d,%d,%d,%d\r\n",adc_value[0],adc_value[1],adc_value[2],adc_value[3]);
|
||
for(int i=0 ; i<4 ;i++)
|
||
{
|
||
vol[i] = ((float)adc_value[i]/4095);
|
||
vol[i] = vol[i]*3.3 ;
|
||
thermistor[i] = (10000*vol[i])/(3.3-vol[i]);
|
||
temp_value[i]=(floor)(getTemp(thermistor[i])*10);
|
||
}
|
||
HAL_ADC_Stop_DMA(&hadc1);
|
||
}
|
||
|
||
const int iSampleNum = 15;
|
||
|
||
/*
|
||
通过温度变化计算油位
|
||
@param tempBuf 温度缓冲区
|
||
@return 1: 油位没过 0: 油位空缺
|
||
*/
|
||
int calculate(uint16_t *tempBuf)
|
||
{
|
||
for (int i = 0; i < iSampleNum; i++)
|
||
{
|
||
printf("%d ",tempBuf[i]);
|
||
}
|
||
printf("%d", tempBuf[iSampleNum - 1] - tempBuf[0]);
|
||
printf("\r\n");
|
||
if ((tempBuf[iSampleNum - 1] > tempBuf[0]) && (tempBuf[iSampleNum - 1] - tempBuf[0] <= 40))
|
||
{
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void check_level(void)
|
||
{
|
||
static int iLevelBuf[3] = {0};
|
||
static int iCheckIndex = 0;
|
||
static uint16_t usIndex = 0;
|
||
static uint32_t ulTick = 0;
|
||
static uint16_t ausTempBuf[4][iSampleNum] = {0};
|
||
|
||
switch(iCheckIndex){
|
||
case 0:
|
||
HAL_GPIO_WritePin(HEAT1_GPIO_Port, HEAT1_Pin,GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(HEAT2_GPIO_Port, HEAT2_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT3_GPIO_Port, HEAT3_Pin,GPIO_PIN_RESET);
|
||
break;
|
||
case 1:
|
||
HAL_GPIO_WritePin(HEAT1_GPIO_Port, HEAT1_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT2_GPIO_Port, HEAT2_Pin,GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(HEAT3_GPIO_Port, HEAT3_Pin,GPIO_PIN_RESET);
|
||
break;
|
||
case 2:
|
||
HAL_GPIO_WritePin(HEAT1_GPIO_Port, HEAT1_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT2_GPIO_Port, HEAT2_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT3_GPIO_Port, HEAT3_Pin,GPIO_PIN_SET);
|
||
break;
|
||
default:
|
||
HAL_GPIO_WritePin(HEAT1_GPIO_Port, HEAT1_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT2_GPIO_Port, HEAT2_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT3_GPIO_Port, HEAT3_Pin,GPIO_PIN_RESET);
|
||
break;
|
||
}
|
||
|
||
if(HAL_GetTick() < ulTick + 1000) return;
|
||
ulTick = HAL_GetTick();
|
||
|
||
readTemp();
|
||
// printf("%d,%d,%d,%d\r\n",temp_value[0],temp_value[1],temp_value[2],temp_value[3]);
|
||
|
||
ausTempBuf[0][usIndex] = temp_value[0];
|
||
ausTempBuf[1][usIndex] = temp_value[1];
|
||
ausTempBuf[2][usIndex] = temp_value[2];
|
||
ausTempBuf[3][usIndex] = temp_value[3];
|
||
usIndex++;
|
||
if(usIndex >= iSampleNum)
|
||
{
|
||
if (iCheckIndex < 3) {
|
||
printf("calculate %d\r\n", iCheckIndex);
|
||
iLevelBuf[iCheckIndex] = calculate(ausTempBuf[iCheckIndex + 1]);
|
||
|
||
if (iLevelBuf[0] && iLevelBuf[1] && iLevelBuf[2])
|
||
{
|
||
level = 3;
|
||
} else if (iLevelBuf[0] && iLevelBuf[1])
|
||
{
|
||
level = 2;
|
||
} else if (iLevelBuf[0])
|
||
{
|
||
level = 1;
|
||
} else
|
||
{
|
||
level = 0;
|
||
}
|
||
} else {
|
||
gusOilTemp = 0;
|
||
for(int i=0 ; i<iSampleNum;i++)
|
||
{
|
||
gusOilTemp += ausTempBuf[0][i];
|
||
}
|
||
gusOilTemp /= iSampleNum;
|
||
|
||
printf("temp %d,level %d\r\n", gusOilTemp, level);
|
||
}
|
||
|
||
usIndex = 0;
|
||
iCheckIndex++;
|
||
if(iCheckIndex >= 4) iCheckIndex = 0;
|
||
}
|
||
|
||
}
|
||
|
||
|
||
/* USER CODE END 0 */
|
||
|
||
/**
|
||
* @brief The application entry point.
|
||
* @retval int
|
||
*/
|
||
int main(void)
|
||
{
|
||
/* USER CODE BEGIN 1 */
|
||
|
||
/* USER CODE END 1 */
|
||
|
||
/* MCU Configuration--------------------------------------------------------*/
|
||
|
||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||
HAL_Init();
|
||
|
||
/* USER CODE BEGIN Init */
|
||
|
||
/* USER CODE END Init */
|
||
|
||
/* Configure the system clock */
|
||
SystemClock_Config();
|
||
|
||
/* USER CODE BEGIN SysInit */
|
||
|
||
/* USER CODE END SysInit */
|
||
|
||
/* Initialize all configured peripherals */
|
||
MX_GPIO_Init();
|
||
MX_DMA_Init();
|
||
MX_ADC1_Init();
|
||
MX_USART2_UART_Init();
|
||
MX_USART1_UART_Init();
|
||
MX_IWDG_Init();
|
||
/* USER CODE BEGIN 2 */
|
||
//Set GPIO NMOS COLSE
|
||
HAL_GPIO_WritePin(HEAT1_GPIO_Port, HEAT1_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT2_GPIO_Port, HEAT2_Pin,GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(HEAT3_GPIO_Port, HEAT3_Pin,GPIO_PIN_RESET);
|
||
|
||
HAL_UART_Receive_IT(&huart1, modbusRxBuf, 1); // 重新启动接收
|
||
|
||
HAL_IWDG_Refresh(&hiwdg);
|
||
|
||
/* USER CODE END 2 */
|
||
|
||
/* Infinite loop */
|
||
/* USER CODE BEGIN WHILE */
|
||
while (1)
|
||
{
|
||
HAL_IWDG_Refresh(&hiwdg);
|
||
/* USER CODE END WHILE */
|
||
|
||
/* USER CODE BEGIN 3 */
|
||
ulUartRecvTime++;
|
||
if ((uartRecvCnt > 0) && (ulUartRecvTime > 3)) {
|
||
HAL_UART_AbortReceive_IT(&huart1);
|
||
|
||
uartRecvCnt = 0;
|
||
HAL_UART_Receive_IT(&huart1, modbusRxBuf, 1); // 重新启动接收
|
||
}
|
||
|
||
if(rxFlag){ // 棿查是否接收到完整Modbus数据帿
|
||
rxFlag = 0; // 清除标志
|
||
printf("modbusRxBuf ");
|
||
for (size_t i = 0; i < 8; i++)
|
||
{
|
||
printf("%02X ", modbusRxBuf[i]);
|
||
}
|
||
printf("\r\n");
|
||
|
||
Process_Modbus(); // 处理Modbus命令
|
||
}
|
||
|
||
HAL_Delay(1);
|
||
check_level();
|
||
}
|
||
/* USER CODE END 3 */
|
||
}
|
||
|
||
/**
|
||
* @brief System Clock Configuration
|
||
* @retval None
|
||
*/
|
||
void SystemClock_Config(void)
|
||
{
|
||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||
|
||
/** Configure the main internal regulator output voltage
|
||
*/
|
||
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
||
|
||
/** Initializes the RCC Oscillators according to the specified parameters
|
||
* in the RCC_OscInitTypeDef structure.
|
||
*/
|
||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
|
||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
|
||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
||
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
|
||
RCC_OscInitStruct.PLL.PLLN = 8;
|
||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV11;
|
||
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||
{
|
||
Error_Handler();
|
||
}
|
||
|
||
/** Initializes the CPU, AHB and APB buses clocks
|
||
*/
|
||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||
|RCC_CLOCKTYPE_PCLK1;
|
||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
||
|
||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
||
{
|
||
Error_Handler();
|
||
}
|
||
}
|
||
|
||
/* USER CODE BEGIN 4 */
|
||
|
||
/* USER CODE END 4 */
|
||
|
||
/**
|
||
* @brief This function is executed in case of error occurrence.
|
||
* @retval None
|
||
*/
|
||
void Error_Handler(void)
|
||
{
|
||
/* USER CODE BEGIN Error_Handler_Debug */
|
||
/* User can add his own implementation to report the HAL error return state */
|
||
__disable_irq();
|
||
while (1)
|
||
{
|
||
}
|
||
/* USER CODE END Error_Handler_Debug */
|
||
}
|
||
|
||
#ifdef USE_FULL_ASSERT
|
||
/**
|
||
* @brief Reports the name of the source file and the source line number
|
||
* where the assert_param error has occurred.
|
||
* @param file: pointer to the source file name
|
||
* @param line: assert_param error line source number
|
||
* @retval None
|
||
*/
|
||
void assert_failed(uint8_t *file, uint32_t line)
|
||
{
|
||
/* USER CODE BEGIN 6 */
|
||
/* User can add his own implementation to report the file name and line number,
|
||
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
||
/* USER CODE END 6 */
|
||
}
|
||
#endif /* USE_FULL_ASSERT */
|