137 lines
3.2 KiB
C
137 lines
3.2 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 = 1000;
|
||
}
|
||
__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;
|
||
}
|
||
|
||
//获取电机运行时间
|
||
current_addr = FLASHmotor_runtime;
|
||
while(1)
|
||
{
|
||
motor_runtime = *(int16_t*)current_addr;
|
||
int16_t temp = *(int16_t*)(current_addr+8);
|
||
|
||
//最大地址 77D8,地址大于77D8,从头再写
|
||
if(temp > motor_runtime && current_addr <= 0x080077D0)
|
||
{
|
||
motor_runtime = temp;
|
||
current_addr += 8;
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
if(motor_runtime <= 0)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); //未检测到电机信号
|
||
|
||
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))
|
||
{
|
||
// 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++;
|
||
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(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);
|
||
}
|
||
|
||
}
|