Files
CPY_PowerBus/OSU/Core/Src/main.c
T
2026-07-01 09:38:00 +08:00

518 lines
15 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 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 temp;
static uint16_t leve;
//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);
static void MX_NVIC_Init(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 == USART2){
HAL_UART_Receive_IT(huart, modbusRxBuf, 1); // 重新启动接收
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if(huart->Instance == USART2){
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 = leve; //
modbusTxBuf[3] = deep >> 8; // 高字芿
modbusTxBuf[4] = deep & 0xFF; // 低字芿
AppendCRC(modbusTxBuf, 5); // 添加CRC校验
HAL_UART_Transmit(&huart2, modbusTxBuf, 7, 100); // 发鿿7字节响应
}
if(regnum == 0x02)
{
modbusTxBuf[0] = SLAVE_ADDR; // 从机地址
modbusTxBuf[1] = 0x03; // 功能砿
modbusTxBuf[2] = 0x04; // 返回字节敿(2字节)
uint16_t deep = leve; // 模拟温度倿(实际应从传感器读叿)
modbusTxBuf[3] = deep >> 8; // 高字芿
modbusTxBuf[4] = deep & 0xFF; // 低字芿
uint16_t temp = temp_value[3]; // 模拟温度倿(实际应从传感器读叿)
modbusTxBuf[5] = temp >> 8; // 高字芿
modbusTxBuf[6] = temp & 0xFF; // 低字芿
AppendCRC(modbusTxBuf, 7); // 添加CRC校验
HAL_UART_Transmit(&huart2, 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 = temp_value[3]; // 模拟温度倿(实际应从传感器读叿)
modbusTxBuf[3] = temp >> 8; // 高字芿
modbusTxBuf[4] = temp & 0xFF; // 低字芿
AppendCRC(modbusTxBuf, 5); // 添加CRC校验
HAL_UART_Transmit(&huart2, modbusTxBuf, 7, 100); // 发鿿7字节响应
}
break;
// 可以添加其他功能码处琿
}
memset(modbusTxBuf, 0, sizeof(modbusTxBuf)); // 清空发鿁缓冲区
}
//re_def
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (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;
}
//adc adv
void float_to_int(void)
{
for(int i=0;i<4;i++)
{
temp_value[i]=(floor)(getTemp(thermistor[i])*10);
}
}
void adc_to_Rt(void)
{
float vol[4] = {0,0,0,0};
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&adc_value, 4);
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]);
}
}
void get_level(void)
{
int difference_1 = abs(temp_value[2]-temp_value[3]);
int difference_2 = abs(temp_value[1]-temp_value[3]);
int difference_3 = abs(temp_value[0]-temp_value[3]);
if( difference_1>=250){
leve = 0;
}
else if(difference_2>=250){
leve = 1;
}
else if(difference_3>=250){
leve = 2;
}
else {
leve = 3;
}
// printf("%d,",difference_1);
// printf("%d,",difference_2);
// printf("%d,",difference_3);
// printf("%d",leve);
}
int addr_test(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
HAL_Delay(10);
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_RESET) {
return 1;
}
return 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();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
//Set GPIO NMOS COLSE
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_HEATER,GPIO_PIN_RESET);
HAL_UART_Receive_IT(&huart2, modbusRxBuf, 1); // 重新启动接收
int a = 0;
for (int i = 0; i < 10; i++) {
HAL_IWDG_Refresh(&hiwdg);
HAL_Delay(10);
}
HAL_IWDG_Refresh(&hiwdg);
if (addr_test()) {
gucAddr++;
}
/* 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(&huart2);
uartRecvCnt = 0;
HAL_UART_Receive_IT(&huart2, modbusRxBuf, 1); // 重新启动接收
}
if(rxFlag){ // 棿查是否接收到完整Modbus数据帿
rxFlag = 0; // 清除标志
Process_Modbus(); // 处理Modbus命令
}
if(a == 0)
{
adc_to_Rt();
adc_to_Rt();
float_to_int();
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_HEATER,GPIO_PIN_SET);
}
if (a == 15000) // 加热15秒
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_HEATER,GPIO_PIN_RESET);
adc_to_Rt();
float_to_int();
get_level();
}
if (a % 900 == 0) //每秒读一次温度
{
adc_to_Rt();
float_to_int();
printf("Temp_value:");
printf("%d,",a/100);
for(int i=0;i<4;i++)
{
printf("%d,",temp_value[i]);
}
printf("%d,",leve);
printf("\r\n");
}
if(a++ > 50000)
{
a = 0;
}
HAL_Delay(1);
}
/* 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();
}
}
/**
* @brief NVIC Configuration.
* @retval None
*/
static void MX_NVIC_Init(void)
{
/* USART2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
/* 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 */