256 lines
5.7 KiB
C
256 lines
5.7 KiB
C
|
/*
|
|||
|
* user_main.c
|
|||
|
*
|
|||
|
* Created on: 2023年6月25日
|
|||
|
* 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;
|
|||
|
}
|
|||
|
|
|||
|
//电压标定值
|
|||
|
reference = *(int16_t*)(FLASH485_ADDR+14);
|
|||
|
if(reference== 0x0000 ||reference == -1)
|
|||
|
{
|
|||
|
reference = 0;
|
|||
|
}
|
|||
|
|
|||
|
//电压阈值
|
|||
|
voltage_thresh = *(int16_t*)(FLASH485_ADDR+16);
|
|||
|
if(voltage_thresh== 0x0000 ||voltage_thresh == -1)
|
|||
|
{
|
|||
|
voltage_thresh = 60;
|
|||
|
}
|
|||
|
|
|||
|
//获取电机运行时间
|
|||
|
//v5.3取消电机运行时间功能,注释write_runtime函数,软件定时器2功能,
|
|||
|
//MODBUS_06H中的运行时间设置功能
|
|||
|
/* 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);
|
|||
|
bsp_StartAutoTimer(6, 6000);
|
|||
|
|
|||
|
|
|||
|
bsp_StartAutoTimer(7, 5000);
|
|||
|
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))
|
|||
|
{
|
|||
|
HAL_IWDG_Refresh(&hiwdg); //喂狗
|
|||
|
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
|||
|
if(reference!=0 && reference!=-1 && reference!=1 )
|
|||
|
{
|
|||
|
voltage = 110.0*adc_max/abs(reference);
|
|||
|
if(voltage>220)voltage = 220;
|
|||
|
else if(voltage<-220)voltage = -220;
|
|||
|
if(adc_state ==0)voltage=0;
|
|||
|
|
|||
|
if(abs(voltage)<voltage_thresh)user_temp++;
|
|||
|
else user_temp=0;
|
|||
|
if(user_temp>10000)user_temp=100;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
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(bsp_CheckTimer(6))
|
|||
|
{
|
|||
|
calculate_voltage();
|
|||
|
}
|
|||
|
|
|||
|
if(bsp_CheckTimer(7)) //动态校准
|
|||
|
{
|
|||
|
static int16_t last_voltage = 110,last_voltage2 = 110;
|
|||
|
static uint8_t temp = 0;
|
|||
|
static uint8_t cnt =0;
|
|||
|
if(voltage!=0)
|
|||
|
{
|
|||
|
cnt++;
|
|||
|
if(cnt>24)
|
|||
|
{
|
|||
|
cnt =0;
|
|||
|
if(abs(last_voltage-voltage)*(uint32_t)100/abs(last_voltage)<=5)
|
|||
|
{
|
|||
|
reference = 110.0*adc_max/last_voltage;
|
|||
|
voltage = 110.0*adc_max/abs(reference);
|
|||
|
}
|
|||
|
last_voltage = voltage;
|
|||
|
}
|
|||
|
|
|||
|
if(abs(last_voltage2+voltage)<abs(last_voltage2)+abs(voltage))
|
|||
|
{
|
|||
|
if(abs(voltage)>=50)
|
|||
|
{
|
|||
|
reference = 0;
|
|||
|
write_flash();
|
|||
|
}
|
|||
|
temp = 1;
|
|||
|
}
|
|||
|
else {
|
|||
|
if(temp == 1 && abs(voltage)>=50)
|
|||
|
{
|
|||
|
reference = 0;
|
|||
|
write_flash();
|
|||
|
}
|
|||
|
temp = 0;
|
|||
|
}
|
|||
|
last_voltage2 = voltage;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
if(bsp_CheckTimer(8))
|
|||
|
{
|
|||
|
HAL_GPIO_WritePin(TX485_EN_GPIO_Port, TX485_EN_Pin, GPIO_PIN_RESET);//使能485芯片接收
|
|||
|
}
|
|||
|
if(usart_count >0)
|
|||
|
{
|
|||
|
Usart_Receive();
|
|||
|
usart_count = 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|