M12-05P/app/user/MotorCtrl.c
2024-11-11 22:14:57 +08:00

156 lines
3.3 KiB
C

#include "MotorCtrl.h"
#include "hwCtrl.h"
#include "PINdef.h"
#include "appTask.h"
//#include "pfdl.h"
volatile uint8_t MotorState[6],MotorStateReal[6];
uint16_t current1;
uint8_t OC1flag;
uint16_t OC_Count1=0;
uint8_t act_step[6];
void MotorValueInit(void)
{
uint8_t i = 0;
for (i = 0; i < 6; i++)
{
MotorState[i] = ACT_NOACT;
MotorStateReal[i] = ACT_NOACT;
}
}
void setMotorState(uint8_t motorid,uint8_t act)
{
if (act <= ACT_XH && motorid < 6)
{
MotorState[motorid] = act;
act_step[motorid] = 0;
}
}
extern uint8_t key_p1_pressed, key_p2_pressed;
extern uint8_t key_p1_released, key_p2_released;
void MotorCtrl(void)//10ms
{
static uint8_t MotorRunState = 0;
static uint16_t p1_cnt, p2_cnt;
switch (MotorRunState)
{
case 0:
if (key_p1_pressed == 1 && p1_cnt < 0xffff)
{
p1_cnt++;
if (p1_cnt >= 150)
{
setMotorState(MOTOR1, ACT_XQ);
MotorRunState = 1;
//key_p1_released = 0;
key_p2_released = 0;
}
key_p1_released = 0;
}
if (key_p2_pressed == 1 && p2_cnt < 0xffff)
{
p2_cnt++;
if (p2_cnt >= 150)
{
setMotorState(MOTOR1, ACT_XH);
MotorRunState = 1;
key_p1_released = 0;
//key_p2_released = 0;
}
key_p2_released = 0;
}
if (key_p1_released == 1 && p1_cnt < 100 && p1_cnt > 0)
{
key_p1_released = 0;
setMotorState(MOTOR1, ACT_XQ);
MotorRunState = 2;
p1_cnt = 0;
}
if (key_p2_released == 1 && p2_cnt < 100 && p2_cnt > 0)
{
key_p2_released = 0;
setMotorState(MOTOR1, ACT_XH);
MotorRunState = 2;
p2_cnt = 0;
}
break;
case 1://长按
if (key_p1_released == 1 || key_p2_released == 1 || OC1flag)
{
key_p1_released = 0;
key_p2_released = 0;
setMotorState(MOTOR1, ACT_NOACT);
MotorRunState = 3;
OC1flag = 0;
}
break;
case 2://短按
if (key_p1_pressed == 1 || key_p2_pressed == 1 || OC1flag)
{
key_p1_pressed = 0;
key_p2_pressed = 0;
setMotorState(MOTOR1, ACT_NOACT);
MotorRunState = 3;
OC1flag = 0;
}
break;
case 3:
key_p1_pressed = 0;
key_p2_pressed = 0;
key_p1_released = 0;
key_p2_released = 0;
p1_cnt = 0;
p2_cnt = 0;
MotorRunState = 0;
break;
default:
break;
}
MotorStateReal[MOTOR1] = MotorState[MOTOR1];
MOTOR1Ctrl(MotorStateReal[MOTOR1]);
//MOTOR2Ctrl(MotorStateReal[MOTOR2]);
//MOTOR1Ctrl(MotorState[MOTOR1]);
}
uint16_t getOC_th(void)
{
return 100U;
}
void CurrentDetecte(void)
{
uint16_t OC_th;
current1 = getAdval(ADCH_RLY1);
OC_th = getOC_th();
if (current1 > OC_th && OC1flag == 0)
{
OC_Count1++;
if (OC_Count1 >= 200)
{
OC_Count1 = 0;
OC1flag = 1;
}
}
else
{
OC_Count1 = 0;
}
}