This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.
mfps/App/Src/app.c

122 lines
3.2 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.

#include "app.h"
//绿灯 NOR ;蓝灯 ERR
float X_ads1220 = 0; //读到的电压值
float X_ads1220_L = 15.0; //电压值上限
float X_ads1220_H = 1320.0; //电压值下限
float X_ads1220_prc = 0; //电压百分比(即位移百分比)
float TEMP_M1820 = 0; //温度
uint32_t move_step_5mm = 0x0000C800;
uint32_t move_step_1mm = 0x00002800;
char oled_init_flag = 0,oled_init_result = 0;
//int i2c_error_temp = 0;
uint8_t magnet_tx[7] = {0x05,0x01,0x00,0x01,0x00,0x32,0xFF};
HAL_StatusTypeDef hal_check_tx;
uint8_t flash_rd_flag = 0;
signed int first_xa_32 = 0;//记录上电时电机位置
uint16_t load_check = 0;
uint8_t TSTEP_CK[5] = {0};
uint32_t tstep_check = 0;
void app_act (void)
{
if(flash_rd_flag == 0) //只读取一次
{
STMFLASH_Read(FLASH_SAVE_ADDR,flash_rd,6); //读取当前flash内容
// magnet_middle = flash_rd[0]; //默认以#25的中点为标准
flash_rd_flag = 1;
}
TMC5160_SPIReadInt(0x12,TSTEP_CK); //读取TSTEP,为0说明灵敏度过小
tstep_check = Raw_32(TSTEP_CK);
TMC5160_SPIReadInt(DRV_STAT_ADDR,DRV_STAT); //读取驱动器状态
DRV_STAT_32 = Raw_32(DRV_STAT);
SG_RESULT_16 = ((uint16_t)DRV_STAT_32) & 0x03FF; //驱动器状态共32bit其中0-9bit为负载值读到的负载值越低负载越高0代表最高负载
if(SG_RESULT_16 != 0) load_check = SG_RESULT_16;
//1 按键操作
// Key_Scan();//按键扫描(已放入定时器中断)
key_act(); //按键执行功能
//2 定时器
// if(it_5000ms_flag == 1)
// {
// it_5000ms_flag = 0;
//
//
//
// }
//3 串口通信测试
// uart_test();
//4 数据采集
if(it_100ms_flag)
{
it_100ms_flag = 0;
HAL_GPIO_WritePin(RS485_EN1_GPIO_Port,RS485_EN1_Pin,GPIO_PIN_SET); //使能485发送发送结束后在回调函数中拉低
hal_check_tx = HAL_UART_Transmit_IT(&huart2, magnet_tx ,7); //485发送
TEMP_M1820 = M1820_Get_Temp(); //温度采集
OLED_MenuTest(); //OLED显示,菜单
X_ads1220 = Xads1220_filter(25,75); //电阻尺滤波结果电压值mv
// X_ads1220_prc =(X_ads1220 - X_ads1220_L)/(X_ads1220_H - X_ads1220_L);
// 串口计时
process_ttl_receive_timer();
}
//5 电阻尺 位移传感器
Xads1220_record();//采集数据用于滤波
//6 电机
motor_protect_ads(100,1900);
Deal_Motor();
//7 串口数据处理
Deal_Uart_Data_For_Module();
//9 LED灯状态指示
if( Motor_Run == 0 )
{
HAL_GPIO_WritePin(LED_NOR_GPIO_Port, LED_NOR_Pin, GPIO_PIN_SET);//停止状态两灯熄灭
HAL_GPIO_WritePin(LED_ERR_GPIO_Port, LED_ERR_Pin, GPIO_PIN_SET);
}
if( (Motor_Run == 1) && (motor_direc == 1) )
{
HAL_GPIO_WritePin(LED_NOR_GPIO_Port, LED_NOR_Pin, GPIO_PIN_RESET);//正向转动绿灯亮
HAL_GPIO_WritePin(LED_ERR_GPIO_Port, LED_ERR_Pin, GPIO_PIN_SET);
}
if( (Motor_Run == 1) && (motor_direc == 2) )
{
HAL_GPIO_WritePin(LED_NOR_GPIO_Port, LED_NOR_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_ERR_GPIO_Port, LED_ERR_Pin, GPIO_PIN_RESET);//反向转动蓝灯亮
}
if( ( Motor_Run == 2 ) || ( Motor_Run == 3 ) )
{
HAL_GPIO_WritePin(LED_NOR_GPIO_Port, LED_NOR_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_ERR_GPIO_Port, LED_ERR_Pin, GPIO_PIN_RESET);//复位移动时两灯亮
}
}