342 lines
8.6 KiB
C
342 lines
8.6 KiB
C
/* 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 "i2c.h"
|
||
#include "spi.h"
|
||
#include "tim.h"
|
||
#include "usart.h"
|
||
#include "gpio.h"
|
||
|
||
/* Private includes ----------------------------------------------------------*/
|
||
/* USER CODE BEGIN Includes */
|
||
#include "gpios.h"
|
||
#include "adcs.h"
|
||
#include "ADS1256.h"
|
||
#include "ads1220.h"
|
||
#include "dac7311.h"
|
||
#include "wf5803.h"
|
||
#include "ds18b20.h"
|
||
#include "eeprom.h"
|
||
/* USER CODE END Includes */
|
||
|
||
/* Private typedef -----------------------------------------------------------*/
|
||
/* USER CODE BEGIN PTD */
|
||
|
||
|
||
//4-20mA电流输出
|
||
float ao_dwq = 0; //AO输出电流值(定位器)
|
||
float ao_blf1 = 0; //AO输出电流值(比例阀)
|
||
float ao_blf2 = 0; //AO输出电流值(比例阀)
|
||
|
||
//编码器相关参数
|
||
int last_encode_num = 0;
|
||
int curr_encode_num = 0;
|
||
uint32_t encode_num = 0;
|
||
int cnt_update = 152;
|
||
|
||
/* 本地网络信息:IP地址、网关地址、子网掩码和端口号 */
|
||
uint8_t ch395_addr[14] = {192,168,114,194,192,168,114,110,255,255,255,0,31,144};
|
||
/* 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 */
|
||
|
||
/* 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 */
|
||
delay_ms(1000);
|
||
/* USER CODE END SysInit */
|
||
|
||
/* Initialize all configured peripherals */
|
||
MX_GPIO_Init();
|
||
MX_DMA_Init();
|
||
MX_ADC1_Init();
|
||
MX_I2C1_Init();
|
||
MX_SPI2_Init();
|
||
MX_TIM1_Init();
|
||
MX_UART4_Init();
|
||
MX_USART1_UART_Init();
|
||
MX_USART2_UART_Init();
|
||
MX_USART3_UART_Init();
|
||
MX_USART6_UART_Init();
|
||
MX_TIM6_Init();
|
||
MX_TIM7_Init();
|
||
/* USER CODE BEGIN 2 */
|
||
HAL_TIM_Base_Start_IT(&htim6); //主要定时
|
||
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL); //定时器1编码器启动
|
||
__HAL_TIM_SetCounter(&htim1, 0x9718); //编码器初始值一千万
|
||
adcs_init();
|
||
ads1256_init();
|
||
delay_ms(200);
|
||
ads1220_init();
|
||
delay_ms(200);
|
||
dac7311_init();
|
||
delay_ms(200);
|
||
wf5803_setup();
|
||
ds18b20_init();
|
||
_74hc165_init();
|
||
modbus_rtu_init();
|
||
//laser_init(usart3_laser_send);
|
||
delay_ms(200);
|
||
|
||
ee_readbytes(ch395_addr,0,14);
|
||
for(int n = 0;n < 12;n++)
|
||
{
|
||
HoldReg[40 + n] = ch395_addr[n];
|
||
}
|
||
HoldReg[52] = ch395_addr[12] << 8 | ch395_addr[13];
|
||
//ee_writebytes(ch395_addr,0,14);
|
||
|
||
ch395_hardware_init(); //ch395硬件初始化
|
||
ch395_init(); //以太网初始化
|
||
/* USER CODE END 2 */
|
||
|
||
/* Infinite loop */
|
||
/* USER CODE BEGIN WHILE */
|
||
while (1)
|
||
{
|
||
/* USER CODE END WHILE */
|
||
|
||
/* USER CODE BEGIN 3 */
|
||
ch395q_handler(); //网络通讯
|
||
|
||
//4-20mA电流输出控制
|
||
if( !(ao_dwq == (float)(HoldReg[0]) / 1000) || !(ao_blf1 == (float)(HoldReg[1]) / 1000) || !(ao_blf2 == (float)(HoldReg[2]) / 1000))
|
||
{
|
||
ao_dwq = (float)(HoldReg[0]) / 1000;
|
||
ao_blf1 = (float)(HoldReg[1]) / 1000;
|
||
ao_blf2 = (float)(HoldReg[2]) / 1000;
|
||
if(ao_dwq > 25) ao_dwq = 25;
|
||
if(ao_blf1 > 25) ao_blf1 = 25;
|
||
if(ao_blf2 > 25) ao_blf2 = 25;
|
||
ao_dwq_set(ao_dwq);
|
||
ao_blf1_set(ao_blf1);
|
||
ao_blf2_set(ao_blf2);
|
||
}
|
||
|
||
if(it_50ms_flag)
|
||
{
|
||
wf5803_task();
|
||
it_50ms_flag = 0;
|
||
}
|
||
|
||
//wf5803_loop(); //气压输入
|
||
ads1220_get_data(); //比例阀输出反馈和比例阀控制电流输出反馈
|
||
ads1256_get_data(); //模拟量输入
|
||
adcs_data(); //定位器控制电流输出反馈
|
||
digital_ctrl(); //数字量输入输出,ds18b20温度采集与运行指示灯同步进行
|
||
|
||
//编码器
|
||
last_encode_num = curr_encode_num;
|
||
curr_encode_num = __HAL_TIM_GET_COUNTER(&htim1);
|
||
if((curr_encode_num - last_encode_num) < -50000)
|
||
cnt_update++;
|
||
else if((curr_encode_num - last_encode_num) > 50000)
|
||
cnt_update--;
|
||
encode_num = curr_encode_num + cnt_update * 0xFFFF;//编码器,返回计数脉冲值
|
||
InputReg[20] = (encode_num >> 16) & 0xffff;
|
||
InputReg[21] = encode_num & 0xffff;
|
||
|
||
if(HoldReg[53])
|
||
{
|
||
for(int n = 0;n < 12;n++)
|
||
{
|
||
ch395_addr[n] = HoldReg[40 + n];
|
||
}
|
||
ch395_addr[12] = HoldReg[52] >> 8;
|
||
ch395_addr[13] = HoldReg[52] & 0xFF;
|
||
ee_writebytes(ch395_addr,0,14);
|
||
HoldReg[53] = 0;
|
||
set_ipv4();
|
||
}
|
||
}
|
||
/* 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_RCC_PWR_CLK_ENABLE();
|
||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
|
||
|
||
/** Initializes the RCC Oscillators according to the specified parameters
|
||
* in the RCC_OscInitTypeDef structure.
|
||
*/
|
||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
||
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
||
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
||
RCC_OscInitStruct.PLL.PLLM = 4;
|
||
RCC_OscInitStruct.PLL.PLLN = 168;
|
||
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
||
RCC_OscInitStruct.PLL.PLLQ = 4;
|
||
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_CLOCKTYPE_PCLK2;
|
||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
|
||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
||
|
||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
|
||
{
|
||
Error_Handler();
|
||
}
|
||
}
|
||
|
||
/* USER CODE BEGIN 4 */
|
||
#define CPU_FREQUENCY_MHZ 168 // STM32时钟主频
|
||
void delay_us(__IO uint32_t delay)
|
||
{
|
||
int last, curr, val;
|
||
int temp;
|
||
|
||
while (delay != 0)
|
||
{
|
||
temp = delay > 900 ? 900 : delay;
|
||
last = SysTick->VAL;
|
||
curr = last - CPU_FREQUENCY_MHZ * temp;
|
||
if (curr >= 0)
|
||
{
|
||
do
|
||
{
|
||
val = SysTick->VAL;
|
||
}
|
||
while ((val < last) && (val >= curr));
|
||
}
|
||
else
|
||
{
|
||
curr += CPU_FREQUENCY_MHZ * 1000;
|
||
do
|
||
{
|
||
val = SysTick->VAL;
|
||
}
|
||
while ((val <= last) || (val > curr));
|
||
}
|
||
delay -= temp;
|
||
}
|
||
}
|
||
|
||
void delay_ms(__IO uint32_t delay)
|
||
{
|
||
HAL_Delay(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 */
|