153 lines
3.5 KiB
C
153 lines
3.5 KiB
C
/*
|
||
* user_main.c
|
||
*
|
||
* Created on: Sep 24, 2024
|
||
* Author: 10425
|
||
*/
|
||
#include "user.h"
|
||
|
||
|
||
uint32_t updata_appaddr;
|
||
void user_main()
|
||
{
|
||
uint8_t aa[2] = {0,0};
|
||
uint8_t APP_flag = *(uint8_t*)app_flag;
|
||
|
||
if(APP_flag == 0xFF)
|
||
{
|
||
uint8_t parameters[18];
|
||
memset(parameters,0xFF,sizeof(parameters));
|
||
AT24C16_WriteBytes(0, parameters, sizeof(parameters));
|
||
uint64_t a = 0;
|
||
write_code(app_flag, (uint8_t*)&a, 8);
|
||
}
|
||
user_init();
|
||
|
||
|
||
bsp_StartAutoTimer(0,50); //计算波形峰峰值
|
||
bsp_StartAutoTimer(1,500); //计算电压
|
||
bsp_StartAutoTimer(3,5000); //同步引脚信号超时定时器
|
||
bsp_StartAutoTimer(4,1000); //未检测到电机信号
|
||
bsp_StartAutoTimer(5,MYI2C_Tick);
|
||
bsp_StartAutoTimer(6, 6000);
|
||
bsp_StartAutoTimer(7, 5000);
|
||
bsp_StartAutoTimer(8, 500); //亮灯
|
||
AT24C16_WriteBytes(64, aa, sizeof(aa));
|
||
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(dev_reg.reference!=0 && dev_reg.reference!=-1 && dev_reg.reference!=1 )
|
||
{
|
||
dev_reg.voltage = 110.0*dev_reg.adc_max/abs(dev_reg.reference);
|
||
if(dev_reg.voltage>220)dev_reg.voltage = 220;
|
||
else if(dev_reg.voltage<-220)dev_reg.voltage = -220;
|
||
if(dev_reg.adc_state ==0)dev_reg.voltage=0;
|
||
|
||
if(abs(dev_reg.voltage)<dev_reg.voltage_thresh)user_temp++;
|
||
else user_temp=0;
|
||
if(user_temp>10000)user_temp=100;
|
||
}
|
||
}
|
||
|
||
if(bsp_CheckTimer(2)) //在外部中断回调函数中开启,在定时器4中关闭
|
||
{
|
||
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
||
}
|
||
|
||
if(bsp_CheckTimer(3))
|
||
{
|
||
dev_reg.adc_state = 2; //状态未知
|
||
}
|
||
if(bsp_CheckTimer(4))
|
||
{
|
||
bsp_StopTimer(2);
|
||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
|
||
dev_reg.motor_state = 1;
|
||
}
|
||
//读取温湿度
|
||
if(bsp_CheckTimer(5))
|
||
{
|
||
AHT20_Handle(&SENx);
|
||
if(SENx.Step == SENSOR_COMPLETE)
|
||
{
|
||
dev_reg.humidness = (uint8_t)SENx.RH;
|
||
dev_reg.tempe = (uint8_t)SENx.T;
|
||
}
|
||
}
|
||
if(bsp_CheckTimer(6))
|
||
{
|
||
calculate_voltage();
|
||
}
|
||
|
||
if(bsp_CheckTimer(7)) //动态校准.每5秒调用一次
|
||
{
|
||
static int16_t last_voltage = 110,last_voltage2 = 110;
|
||
static uint8_t temp = 0;
|
||
static uint8_t cnt =0;
|
||
if(dev_reg.voltage!=0)
|
||
{
|
||
cnt++;
|
||
if(cnt>24) //每2分钟动态校准一次
|
||
{
|
||
cnt =0;
|
||
if(abs(last_voltage-dev_reg.voltage)*(uint32_t)100/abs(last_voltage)<=10)
|
||
{
|
||
dev_reg.reference = 110.0*dev_reg.adc_max/last_voltage;
|
||
dev_reg.voltage = 110.0*dev_reg.adc_max/abs(dev_reg.reference);
|
||
}
|
||
last_voltage = dev_reg.voltage;
|
||
}
|
||
|
||
//发生极性翻转时,且电压大于50,变为110
|
||
if(abs(last_voltage2+dev_reg.voltage)<abs(last_voltage2)+abs(dev_reg.voltage))
|
||
{
|
||
if(abs(dev_reg.voltage)>=50)
|
||
{
|
||
dev_reg.reference = 0;
|
||
write_flash(write_reference,dev_reg.reference);
|
||
}
|
||
temp = 1;
|
||
}
|
||
else {
|
||
if(temp == 1 && abs(dev_reg.voltage)>=50)
|
||
{
|
||
dev_reg.reference = 0;
|
||
write_flash(write_reference,dev_reg.reference);
|
||
}
|
||
temp = 0;
|
||
}
|
||
last_voltage2 = dev_reg.voltage;
|
||
}
|
||
|
||
}
|
||
|
||
if(bsp_CheckTimer(8))
|
||
{
|
||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||
// HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
||
// HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
|
||
|
||
}
|
||
|
||
if(usart_count >0)
|
||
{
|
||
Usart_Receive();
|
||
usart_count = 0;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|