actuator/Core/Src/main.c

361 lines
9.6 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) 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 "cmd_queue.h"
#include "atcom.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;//编码器初始值152*65535 + 38680 = 10 000 000
int size = 0; //串口屏指令长度
uint8_t Device_ID[2];//设备ID
uint8_t *check_str = 0;
/* 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();
MX_TIM10_Init();
MX_TIM11_Init();
/* USER CODE BEGIN 2 */
ee_io_init(); //eeprom初始化
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);
HAL_UART_Receive_IT(&huart6, (uint8_t *)&u6_RxBuffer, 1); //开启接收中断(透传)
HAL_UART_Receive_IT(&huart2, (uint8_t *)&u2_RxBuffer, 1); //开启接收中断(气密)
HAL_UART_Receive_IT(&huart3, (uint8_t *)&u3_RxBuffer, 1); //开启接收中断(温度)
ee_readbytes(Device_ID,0,2); //读取设备ID
HoldReg[37] = Device_ID[0] << 8 | Device_ID[1];
//网络通讯预设信息读取
ee_readbytes((uint8_t*)wifiname,100,100);
if((uint8_t)wifiname[0] == 0xff)
wifiname[0] = '\0';
ee_readbytes((uint8_t*)wifipassword,200,100);
if((uint8_t)wifipassword[0] == 0xff)
wifipassword[0] = '\0';
ee_readbytes((uint8_t*)hisip,300,100);
if((uint8_t)hisip[0] == 0xff)
hisip[0] = '\0';
ee_readbytes((uint8_t*)port,400,100);
if((uint8_t)port[0] == 0xff)
port[0] = '\0';
// 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 */
//串口屏任务处理
size = queue_find_cmd(cmd_buffer,CMD_MAX_SIZE); //从缓冲区中获取一条指令
if(size>0 && cmd_buffer[1]!=0x07) //接收到指令 ,及判断是否为开机提示
{
ProcessMessage((PCTRL_MSG)cmd_buffer, size); //指令处理
}
if(it_100ms_flag)
{
UpdateUI(); //UI界面更新
it_100ms_flag = 0;
}
//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);
}
//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)//编码器计数最大值为65535通过前后两次计数差值判断溢出方向
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;
//设备ID赋值与保存
Device_ID[0] = HoldReg[37] >> 8;
Device_ID[1] = HoldReg[37] & 0xFF;
ee_writebytes(Device_ID,0,2);
}
/* 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 */