110VCGQ/project/110VCGQV5.2/user/Src/user_main.c

189 lines
4.4 KiB
C
Raw Permalink Normal View History

2024-11-18 10:09:39 +08:00
/*
* user_main.c
*
* Created on: 2023625
* Author: wyf
*/
#include "user.h"
void user_main(void)
{
user_init();
RS485ADDR = *(uint16_t*)FLASH485_ADDR; //获取从机地址
if(RS485ADDR == 0x0000 || RS485ADDR == 0xFFFF)
{
RS485ADDR = 1;
}
pwm_pulse = *(uint16_t*)(FLASH485_ADDR+2); //获取脉宽
if(pwm_pulse == 0x0000 || pwm_pulse == 0xFFFF)
{
pwm_pulse = 700;
}
__HAL_TIM_SET_COMPARE(&htim14,TIM_CHANNEL_1,pwm_pulse); //设置脉宽
adcValue_factor = *(uint16_t*)(FLASH485_ADDR+4); //获取ADC修正系数
if(adcValue_factor == 0x0000 || adcValue_factor == -1)
{
adcValue_factor = 10;
}
adcValue_addnum = *(int16_t*)(FLASH485_ADDR+6); //获取ADC修正系数
if(adcValue_addnum == 0x0000 || adcValue_addnum == -1)
{
adcValue_addnum = 0;
}
baud_rate = *(int16_t*)(FLASH485_ADDR+8);
if(baud_rate == 0x0000 || baud_rate == -1)
{
baud_rate = 0;
}
Uart_SetBaud(baud_rate);
estimate_value = *(int16_t*)(FLASH485_ADDR+10);
if(estimate_value== 0x0000 ||estimate_value == -1)
{
estimate_value = 40;
}
threshold_value = *(int16_t*)(FLASH485_ADDR+12);
if(threshold_value== 0x0000 ||threshold_value == -1)
{
threshold_value = 0;
}
//获取电机运行时间
current_addr = FLASHmotor_runtime;
while(1)
{
motor_runtime = *(uint16_t*)current_addr;
uint16_t temp = *(uint16_t*)(current_addr+8);
if(motor_runtime == 0 || motor_runtime == 0xFFFF)motor_runtime =0;
if(temp ==0 || temp == 0xFFFF) temp = 0;
//最大地址 77D8,地址大于77D8从头再写
if(temp > motor_runtime && current_addr <= 0x080077D0)
{
motor_runtime = temp;
current_addr += 8;
}
else
{
// bad_runtime = *(uint16_t*)(current_addr+2); //恶劣环境运行时间
break;
}
}
// if(motor_runtime == 0 || motor_runtime == 0xFFFF)motor_runtime =0;
HAL_TIM_Base_Start_IT(&htim14);
HAL_TIM_PWM_Start_IT(&htim14, TIM_CHANNEL_1); //开启PWM波输出
HAL_UART_DMAStop(&huart1);//复位DMA //串口DMA接受
HAL_UART_Receive_DMA(&huart1,(uint8_t *)RxBuff,sizeof(RxBuff));
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)RegularConvData_Tab, ADCBUFF_LEN); //开启ADC转换
//DE发射器使能高电平有效RE接收器使能低电平有效
HAL_GPIO_WritePin(TX485_EN_GPIO_Port, TX485_EN_Pin, GPIO_PIN_RESET);
bsp_StartAutoTimer(0,50); //计算波形峰峰值
bsp_StartAutoTimer(1,500); //led1闪烁,
bsp_StartAutoTimer(3,5000); //同步引脚信号超时定时器
bsp_StartAutoTimer(4,1000); //未检测到电机信号
bsp_StartAutoTimer(5,MYI2C_Tick);
uint16_t runtime_count = 0;
while(1)
{
if(bsp_CheckTimer(0))
{
HAL_ADC_Stop_DMA(&hadc1);
ADC_MAX(); //计算波形峰峰
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)RegularConvData_Tab, ADCBUFF_LEN);
}
if(bsp_CheckTimer(1))
{
user_temp=0;
// HAL_IWDG_Refresh(&hiwdg); //喂狗
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
}
if(bsp_CheckTimer(2)) //在外部中断回调函数中开启在定时器4中关闭
{
runtime_count++;
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
//每500ms加一次一小时7200次
if(runtime_count>7200)
{
motor_runtime++;
if(motor_runtime == 65535)motor_runtime=0;
//判断是否为恶劣环境去除此功能删除下面的代码和write_runtime中的代码
if(tempe<=30)
{
if(humidness>=tempe*2.85)bad_runtime++;
}
else if(tempe>30)
{
if(humidness>=-tempe*2.85+180)bad_runtime++;
}
write_runtime();
runtime_count = 0;
}
}
if(bsp_CheckTimer(3))
{
adc_state = 2; //状态未知
}
if(bsp_CheckTimer(4))
{
bsp_StopTimer(2);
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
motor_state = 1;
}
//读取温湿度
if(bsp_CheckTimer(5))
{
AHT20_Handle(&SENx);
if(SENx.Step == SENSOR_COMPLETE)
{
humidness = (uint8_t)SENx.RH;
tempe = (uint8_t)SENx.T;
}
}
if(usart_count >0)
{
Usart_Receive();
usart_count = 0;
}
// HAL_Delay(5000);
// HAL_ADC_Stop_DMA(&hadc1);
// HAL_GPIO_WritePin(TX485_EN_GPIO_Port, TX485_EN_Pin, GPIO_PIN_SET);//使能485芯片发送
// for(int i = 0; i< ADCBUFF_LEN;i++)
// {
// sprintf(TXBuff,"%d\r\n",RegularConvData_Tab[i]);
// HAL_UART_Transmit_DMA(&huart1,(uint8_t*)TXBuff,strlen(TXBuff));
// HAL_Delay(10);
// }
// HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
// while(1);
}
}