/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 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 "spi.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* 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 ---------------------------------------------------------*/ /* 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 */ void sys_init() { MS5803Init(); ADS1256_Init(); // hc595_init(&hc595); _74hc165_init(); dac7311_init(); modbus_tim_init(); modbus_init(); adc1_start_dma(); laser_init(usart2_laser_send); io_output(); // encode_init(); } char step_cnt = 0; int i = 0; int bub_flag = 0;//气泡检测状态 float AO1_data = 0;//AO1输出电流值 float AO2_data = 0;//AO2输出电流值 uint16_t dis_temp = 0;//DI输入存储中间量 int flow_state = 0; int OVER_TIMES = 65535; uint16_t press_array1[10] = {0}; uint16_t press_array2[10] = {0}; uint16_t press_array3[10] = {0}; uint32_t press_array1_sum = 0; uint32_t press_array2_sum = 0; uint32_t press_array3_sum = 0; int a1 = 0,a2 = 0,a3 = 0; void sys_task(void) { //ao1_set(8); //AO(0-25mA)输出,每当寄存器内数值更新时输出电流同步更新 if(!(AO1_data == (float)(HoldReg[0]) / 1000)) { AO1_data = (float)(HoldReg[0]) / 1000; if(AO1_data > 25) AO1_data = 25; dac7311_init(); ao1_set(AO1_data); } if(!(AO2_data == (float)(HoldReg[1]) / 1000)) { AO2_data = (float)(HoldReg[1]) / 1000; if(AO2_data > 25) AO2_data = 25; dac7311_init(); ao2_set(AO2_data); } adc_task(i); //ADS1256数据采样 i++; if(i > 7) i = 0; ms5803_task(); //ms5803数据采样 laser_task();//获取一次激光传感器数据 get_adc_value();//得到温度和AO反馈值 io_output();//实时更新IO输出状态 //DI数据存入离散量寄存器,气泡检测 bub_flag = DisState[0] & 0x04;//获取气泡检测状态 dis_temp =_74hc165_read_byte(); DisState[1] = ~((unsigned char)(dis_temp & 0x00ff)); //第二级74HC165的数据 DisState[0] = ~((unsigned char)((dis_temp >> 8) & 0x00ff));//第一级74HC165的数据 if(bub_flag != (DisState[0] & 0x04)) { HoldReg[2] ++; if(HoldReg[2] > 65500) HoldReg[2] = 65500; } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ step_cnt = 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_SPI3_Init(); MX_TIM7_Init(); MX_USART3_UART_Init(); MX_ADC1_Init(); MX_SPI1_Init(); MX_USART6_UART_Init(); MX_SPI2_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_TIM4_Init(); MX_TIM3_Init(); /* USER CODE BEGIN 2 */ //delay_init(168); LL_TIM_EnableCounter(TIM3); //使能计数 LL_TIM_EnableIT_UPDATE(TIM3); //使能更新中断 LL_TIM_EnableAllOutputs(TIM3); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if(it_1000ms_falg == 1) { switch(step_cnt) { case 0 : { LL_GPIO_ResetOutputPin(SENSOR_SELECT_GPIO_Port,SENSOR_SELECT_Pin);//压力传感器板选择 SetCS();//拉高压力传感器所有片选 it_1000ms_falg = 0; step_cnt++; } break; case 1 : { step_cnt++; it_1000ms_falg = 0; } break; case 2 : { sys_init();//硬件初始化 OVER_TIMES = 64; it_1000ms_falg = 0; step_cnt++; } break; case 3 : { sys_task();//系统任务流程 } break; } } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { LL_FLASH_SetLatency(LL_FLASH_LATENCY_5); while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_5) { } LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); LL_RCC_HSE_Enable(); /* Wait till HSE is ready */ while(LL_RCC_HSE_IsReady() != 1) { } LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_4, 168, LL_RCC_PLLP_DIV_2); LL_RCC_PLL_Enable(); /* Wait till PLL is ready */ while(LL_RCC_PLL_IsReady() != 1) { } while (LL_PWR_IsActiveFlag_VOS() == 0) { } LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_4); LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_2); LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); /* Wait till System clock is ready */ while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { } LL_SetSystemCoreClock(168000000); /* Update the time base */ if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ static uint16_t g_fac_ms = 0; // ms延时倍乘数,在os下,代表每个节拍的ms数 static uint32_t g_fac_us = 0; /* us延时倍乘数 */ void SysTick_Init(void) { NVIC_SetPriority(SysTick_IRQn, 3); LL_SYSTICK_EnableIT(); } /** * @brief 初始化延迟函数 * @param sysclk: 系统时钟频率, 即CPU频率(rcc_c_ck) * @retval 无 */ void delay_init(uint16_t sysclk) { #if SYS_SUPPORT_OS /* 如果需要支持OS */ uint32_t reload; #endif SysTick_Init(); LL_SetSystemCoreClock(LL_SYSTICK_CLKSOURCE_HCLK); /* SYSTICK使用内核时钟源,同CPU同频率 */ g_fac_us = sysclk; /* 不论是否使用OS,g_fac_us都需要使用 */ g_fac_ms = g_fac_ms; #if SYS_SUPPORT_OS /* 如果需要支持OS. */ reload = sysclk; /* 每秒钟的计数次数 单位为M */ reload *= 1000000 / configTICK_RATE_HZ; /* 根据delay_ostickspersec设定溢出时间,reload为24位 * 寄存器,最大值:16777216,在168M下,约合0.099s左右 */ g_fac_ms = 1000 / configTICK_RATE_HZ; // 代表OS可以延时的最少单位 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; /* 开启SYSTICK中断 */ SysTick->LOAD = reload; /* 每1/delay_ostickspersec秒中断一次 */ SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; /* 开启SYSTICK */ #endif } /** * @brief 延时nus * @param nus: 要延时的us数. * @note 注意: nus的值,不要大于34952us(最大值即2^24 / g_fac_us @g_fac_us = 168) * @retval 无 */ void delay_us(uint32_t nus) { uint32_t ticks; uint32_t told, tnow, tcnt = 0; uint32_t reload = SysTick->LOAD; /* LOAD的值 */ ticks = nus * g_fac_us; /* 需要的节拍数 */ told = SysTick->VAL; /* 刚进入时的计数器值 */ while (1) { tnow = SysTick->VAL; if (tnow != told) { if (tnow < told) { tcnt += told - tnow; /* 这里注意一下SYSTICK是一个递减的计数器就可以了 */ } else { tcnt += reload - tnow + told; } told = tnow; if (tcnt >= ticks) { __NOP(); break; /* 时间超过/等于要延迟的时间,则退出 */ } } } } /** * @brief 延时nms * @param nms: 要延时的ms数 (0< nms <= 65535) * @retval 无 */ void delay_ms(uint16_t nms) { uint32_t repeat = nms / 30; /* 这里用30,是考虑到可能有超频应用 */ uint32_t remain = nms % 30; while (repeat) { delay_us(30 * 1000); /* 利用delay_us 实现 1000ms 延时 */ repeat--; } if (remain) { delay_us(remain * 1000); /* 利用delay_us, 把尾数延时(remain ms)给做了 */ } } /** * @brief 延时函数,用于模拟硬件延时。 * @param {uint32_t} ticks * @return {*} * @note: 请注意,这个函数仅用于模拟硬件延时,实际应用中可能需要使用其他延时函数,如HAL_Delay或rt_delay。 */ void delay_tick(uint32_t ticks) { while (ticks--) { __NOP(); } } /* 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 */