811 lines
23 KiB
C
811 lines
23 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body - Modbus RTU从机实现
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include <stdio.h> // 用于printf函数
|
|
#include <string.h> // 用于memset函数
|
|
#include "math.h"
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
ADC_HandleTypeDef hadc1;
|
|
DMA_HandleTypeDef hdma_adc1;
|
|
|
|
IWDG_HandleTypeDef hiwdg;
|
|
|
|
TIM_HandleTypeDef htim1;
|
|
|
|
UART_HandleTypeDef huart1;
|
|
UART_HandleTypeDef huart2;
|
|
|
|
/* USER CODE BEGIN PV */
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
static void MX_GPIO_Init(void);
|
|
static void MX_DMA_Init(void);
|
|
static void MX_ADC1_Init(void);
|
|
static void MX_TIM1_Init(void);
|
|
static void MX_USART2_UART_Init(void);
|
|
static void MX_USART1_UART_Init(void);
|
|
static void MX_IWDG_Init(void);
|
|
static void MX_NVIC_Init(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
#define SLAVE_ADDR 0x20 // Modbus从机地址
|
|
#define FLOW_REG_ADDR 0x0000 // 流�??
|
|
#define TEMP_REG_ADDR 0x0001 // 温度
|
|
#define REGNUM_01 0x01 // 温度
|
|
#define REGNUM_02 0x02 // 温度
|
|
|
|
/* 全局变量 */
|
|
uint8_t modbusRxBuf[256]; // Modbus接收缓冲�?
|
|
uint8_t modbusTxBuf[256]; // Modbus发�?�缓冲区
|
|
volatile uint8_t rxFlag = 0; // 接收完成标志
|
|
|
|
uint16_t adc_value;
|
|
uint32_t RNTC_value;
|
|
float temp_value;
|
|
uint16_t temperature_int;
|
|
|
|
static struct
|
|
{
|
|
uint16_t usCnt;
|
|
uint16_t usFreq;
|
|
uint32_t usLastPulseTime;
|
|
uint32_t usStartPulseTime;
|
|
uint16_t usFlowRate;
|
|
} waterSensorData;
|
|
|
|
/**
|
|
* @brief 重定向printf函数到串�??1
|
|
* @param ch: 要发送的字符
|
|
* @param f: 文件指针(未使�??)
|
|
* @return 发�?�的字符
|
|
*/
|
|
int fputc(int ch, FILE *f) {
|
|
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 100); // 超时时间100ms
|
|
return ch;
|
|
}
|
|
|
|
/**
|
|
* @brief 串口错误回调函数
|
|
* @param huart: 串口句柄
|
|
*/
|
|
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
printf("error\r\n");
|
|
if(huart->Instance == USART2){
|
|
HAL_UART_Receive_IT(huart, modbusRxBuf, 1); // 重新启动接收
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 串口接收完成回调函数
|
|
* @param huart: 串口句柄
|
|
* @note 实现Modbus数据的字节接�??
|
|
*/
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
|
|
|
|
static uint8_t cnt = 0;
|
|
if(huart->Instance == USART2){
|
|
|
|
if(cnt == 0 && modbusRxBuf[0] != SLAVE_ADDR){
|
|
HAL_UART_Receive_IT(huart, modbusRxBuf, 1); // 非本机地�??,重置接�??
|
|
return;
|
|
}
|
|
if(++cnt >= 8){ // 接收�??8字节,触发处�??
|
|
cnt = 0;
|
|
rxFlag = 1; // 设置接收完成标志
|
|
}
|
|
HAL_UART_Receive_IT(huart, &modbusRxBuf[cnt], 1); // 继续接收下一个字�??
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief CRC16校验计算函数
|
|
* @param buf: 数据缓冲�??
|
|
* @param len: 数据长度
|
|
* @return CRC校验�??
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @brief 在数据后添加CRC校验�??
|
|
* @param buf: 数据缓冲�??
|
|
* @param len: 数据长度(不包含CRC)
|
|
*/
|
|
void AppendCRC(uint8_t* buf, uint8_t len)
|
|
{
|
|
uint16_t crc = CRC16(buf, len);
|
|
buf[len] = crc & 0xFF; // 低字节在�?
|
|
buf[len+1] = crc >> 8; // 高字节在�?
|
|
}
|
|
|
|
/**
|
|
* @brief Modbus指令处理函数
|
|
* @note 处理接收到的Modbus命令并生成响�??
|
|
*/
|
|
void Process_Modbus(void)
|
|
{
|
|
|
|
/* 第一层地�?过滤 */
|
|
if(modbusRxBuf[0] != SLAVE_ADDR) return; // 非本机地�?,直接忽�?
|
|
|
|
/* 第二层CRC验证 */
|
|
uint16_t crc = CRC16(modbusRxBuf, 6);
|
|
if(*(uint16_t*)&modbusRxBuf[6] != crc) return; // CRC校验失败,忽�?
|
|
|
|
switch(modbusRxBuf[1]){ // 根据功能码处�?
|
|
case 0x03: // 读保持寄存器
|
|
if(*(uint16_t*)&modbusRxBuf[3] == FLOW_REG_ADDR) // �?查寄存器地址
|
|
{
|
|
uint16_t regnum = (modbusRxBuf[4] << 8) | modbusRxBuf[5];
|
|
if(regnum == 0x01)
|
|
{
|
|
modbusTxBuf[0] = SLAVE_ADDR; // 从机地址
|
|
modbusTxBuf[1] = 0x03; // 功能砿
|
|
modbusTxBuf[2] = 0x02; // 返回字节敿(2字节)
|
|
// if (waterSensorData.usFreq == 0) {
|
|
// waterSensorData.usFlowRate = 0;
|
|
// } else {
|
|
// waterSensorData.usFlowRate = 50 * 1000 * 60 * 10 / 660 / waterSensorData.usFreq;
|
|
// }
|
|
//uint16_t flow = waterSensorData.usFreq * 10 / 4 / 9;
|
|
uint16_t flow = 0;
|
|
__disable_irq();
|
|
if (waterSensorData.usFreq > 0) {
|
|
flow = (waterSensorData.usFreq + 10) * 1000UL / 108;
|
|
}
|
|
__enable_irq();
|
|
modbusTxBuf[3] = flow >> 8; // 高字芿
|
|
modbusTxBuf[4] = flow & 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字节)
|
|
// if (waterSensorData.usFreq == 0) {
|
|
// waterSensorData.usFlowRate = 0;
|
|
// } else {
|
|
// waterSensorData.usFlowRate = 50 * 1000 * 60 * 10 / 660 / waterSensorData.usFreq;
|
|
// }
|
|
//uint16_t flow = waterSensorData.usFreq * 10 / 4 / 9;
|
|
uint16_t flow = 0;
|
|
__disable_irq();
|
|
if (waterSensorData.usFreq > 0) {
|
|
flow = (waterSensorData.usFreq + 10) * 1000UL / 108;
|
|
}
|
|
__enable_irq();
|
|
modbusTxBuf[3] = flow >> 8; // 高字芿
|
|
modbusTxBuf[4] = flow & 0xFF; // 低字芿
|
|
uint16_t temp = temperature_int; // 模拟温度倿(实际应从传感器读叿)
|
|
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 = temperature_int; // 模拟温度�?(实际应从传感器读�?)
|
|
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)); // 清空发�?�缓冲区
|
|
}
|
|
|
|
|
|
|
|
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
|
|
{
|
|
if (GPIO_Pin == GPIO_PIN_2)
|
|
{
|
|
// if (waterSensorData.usCnt == 0)
|
|
// {
|
|
// waterSensorData.usStartPulseTime = HAL_GetTick();
|
|
// }
|
|
|
|
// waterSensorData.usLastPulseTime = HAL_GetTick();
|
|
|
|
// if (waterSensorData.usCnt >= 50)
|
|
// {
|
|
// HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);
|
|
// //waterSensorData.usFreq = 1000 * 10 / (waterSensorData.usLastPulseTime - waterSensorData.usStartPulseTime);
|
|
// //waterSensorData.usFlowRate = (waterSensorData.usFreq +10)*1000 / 108; //2fen 细管
|
|
// //waterSensorData.usFlowRate = (uint16_t)((1000UL * 50 * 60 * 5) / 660 / (waterSensorData.usLastPulseTime - waterSensorData.usStartPulseTime)); //4fen 粗管
|
|
// waterSensorData.usFreq = waterSensorData.usLastPulseTime - waterSensorData.usStartPulseTime;
|
|
// waterSensorData.usCnt = 0;
|
|
// waterSensorData.usStartPulseTime = waterSensorData.usLastPulseTime;
|
|
// }
|
|
|
|
++waterSensorData.usCnt;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
float get_temperature(float Rt)
|
|
{
|
|
const float Rp = 50000.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 adc_to_Rt(void)
|
|
{
|
|
int sum = 0;
|
|
int i;
|
|
for (i = 0; i < 10; i++) {
|
|
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&adc_value, 2);
|
|
sum += adc_value;
|
|
}
|
|
sum /= 10;
|
|
|
|
if (adc_value != 0) {
|
|
RNTC_value = (10000 * sum) / (4096 - sum);
|
|
temp_value = get_temperature((float)RNTC_value);
|
|
temperature_int = (int)(temp_value * 10);
|
|
}
|
|
}
|
|
|
|
int gpioInit(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 */
|
|
uint32_t ulLoopTime = 0;
|
|
/* 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_TIM1_Init();
|
|
MX_USART2_UART_Init();
|
|
MX_USART1_UART_Init();
|
|
MX_IWDG_Init();
|
|
|
|
/* Initialize interrupts */
|
|
MX_NVIC_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
//printf("System Initialized!\r\n"); // 系统初始化完成提�?
|
|
HAL_UART_Receive_IT(&huart2, modbusRxBuf, 1); // 启动串口接收中断
|
|
|
|
ulLoopTime = HAL_GetTick();
|
|
gpioInit();
|
|
|
|
waterSensorData.usFreq = 0;
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
if(rxFlag){ // �?查是否接收到完整Modbus数据�?
|
|
rxFlag = 0; // 清除标志
|
|
Process_Modbus(); // 处理Modbus命令
|
|
}
|
|
|
|
uint32_t ulTime = HAL_GetTick();
|
|
|
|
if (ulTime > ulLoopTime + 4000) {
|
|
__disable_irq();
|
|
if (waterSensorData.usCnt < 40) {
|
|
waterSensorData.usFreq = 0;
|
|
} else if (waterSensorData.usCnt < 2000) {
|
|
waterSensorData.usFreq = waterSensorData.usCnt / 4;
|
|
}
|
|
|
|
waterSensorData.usCnt = 0;
|
|
__enable_irq();
|
|
ulLoopTime = HAL_GetTick();
|
|
adc_to_Rt();
|
|
//printf("Flow Rate: %d mL/min\n", waterSensorData.usFlowRate);
|
|
printf("temp:%d\n", temperature_int);
|
|
|
|
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);
|
|
|
|
HAL_IWDG_Refresh(&hiwdg);
|
|
}
|
|
|
|
|
|
// ulTime = ulTime - waterSensorData.usLastPulseTime;
|
|
// if (ulTime > 10000)
|
|
// {
|
|
// waterSensorData.usFreq = 0;
|
|
// waterSensorData.usFlowRate = 0;
|
|
// }
|
|
|
|
|
|
}
|
|
/* 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_DIV2;
|
|
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)
|
|
{
|
|
/* EXTI2_3_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
|
|
}
|
|
|
|
/**
|
|
* @brief ADC1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_ADC1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC1_Init 0 */
|
|
|
|
/* USER CODE END ADC1_Init 0 */
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
|
*/
|
|
hadc1.Instance = ADC1;
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
hadc1.Init.LowPowerAutoWait = DISABLE;
|
|
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
|
|
hadc1.Init.ContinuousConvMode = DISABLE;
|
|
hadc1.Init.NbrOfConversion = 1;
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
hadc1.Init.DMAContinuousRequests = DISABLE;
|
|
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
|
|
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
|
|
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
|
|
hadc1.Init.OversamplingMode = ENABLE;
|
|
hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16;
|
|
hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_4;
|
|
hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
|
|
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_4;
|
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
|
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief IWDG Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_IWDG_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN IWDG_Init 0 */
|
|
|
|
/* USER CODE END IWDG_Init 0 */
|
|
|
|
/* USER CODE BEGIN IWDG_Init 1 */
|
|
|
|
/* USER CODE END IWDG_Init 1 */
|
|
hiwdg.Instance = IWDG;
|
|
hiwdg.Init.Prescaler = IWDG_PRESCALER_64;
|
|
hiwdg.Init.Window = 4095;
|
|
hiwdg.Init.Reload = 4095;
|
|
if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN IWDG_Init 2 */
|
|
|
|
/* USER CODE END IWDG_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief TIM1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_TIM1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN TIM1_Init 0 */
|
|
|
|
/* USER CODE END TIM1_Init 0 */
|
|
|
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
/* USER CODE BEGIN TIM1_Init 1 */
|
|
|
|
/* USER CODE END TIM1_Init 1 */
|
|
htim1.Instance = TIM1;
|
|
htim1.Init.Prescaler = 64-1;
|
|
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
htim1.Init.Period = 65535;
|
|
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
htim1.Init.RepetitionCounter = 0;
|
|
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
|
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
|
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN TIM1_Init 2 */
|
|
|
|
/* USER CODE END TIM1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART2 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART2_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART2_Init 0 */
|
|
|
|
/* USER CODE END USART2_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART2_Init 1 */
|
|
|
|
/* USER CODE END USART2_Init 1 */
|
|
huart2.Instance = USART2;
|
|
huart2.Init.BaudRate = 9600;
|
|
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart2.Init.StopBits = UART_STOPBITS_1;
|
|
huart2.Init.Parity = UART_PARITY_NONE;
|
|
huart2.Init.Mode = UART_MODE_TX_RX;
|
|
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART2_Init 2 */
|
|
|
|
/* USER CODE END USART2_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* Enable DMA controller clock
|
|
*/
|
|
static void MX_DMA_Init(void)
|
|
{
|
|
|
|
/* DMA controller clock enable */
|
|
__HAL_RCC_DMA1_CLK_ENABLE();
|
|
|
|
/* DMA interrupt init */
|
|
/* DMA1_Channel1_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin : PA0 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pin : PA1 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pin : PB2 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_2;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
}
|
|
|
|
/* 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 */
|