📄 main.c
字号:
/**************** (c) 2004 STMicroelectronics **********************
PROJECT : ST7MC demokit
COMPILER : ST7 METROWERKS C (HIWARE) / COSMIC
MODULE : main.c
LIBRARY VERSION : 1.0.2
CREATION DATE : 04.2005
AUTHOR : Jiang Jian-guo / Microcontroller Competence center / ST Shang Hai
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : Main Routine
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
******************************************************************************
THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS
SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES
WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE.
******************************************************************************
******************************************************************************/
#include "version.h"
#include "lib.h"
#include "mtc.h"
#include "ST7MC_hr.h"
#include "ports.h"
#include "opamp.h"
#include "timer.h"
#include "it_ST7MC.h"
#include "LinSCI.h"
#include "misc.h"
#include "wwdg.h"
#include "spi.h"
#include "regul.h"
#include "adc.h"
#include "MTC_Settings_Sensorless.h"
#define CRC 0x54 // just declare a data byte to be written in EEPROM
typedef enum
{
IDLE, START, RUN, STOP, BRAKE, WAIT, FAULT
} SystStatus_t;
static SystStatus_t State;
u8 KEY_CODE = IDLE_KEY;
u8 start_counter; // variable for starting counter
u8 stall_counter; // variable for stalling cunter in starting stage
void main(void)
{
while ( !WWD_Init() );
ST7_IntPrioritySetUp();
State = IDLE;
Power_Motor_Status = 0;
start_counter = 0;
stall_counter = 0;
SCI_Config();
PORTS_Init(); // initialize I/O
TIMA_Config(); // initialize TIMER B peripheral
SPI_Init();
MTC_InitPeripheral(); // Initialize peripheral for Sensorless BLDC drive
EnableInterrupts();
PORTS_LedOn();
while(1) // main loop
{
if (Chk_Timer_WDG_Elapsed() == TRUE) WWD_Refresh();
/* SCI TX/RX & process the received datas ----------*/
if (SCI_IsReceptionCompleted() == TRUE) Resert_SCIRX_Watchdog();
// Refresh SCI RX watchdog
SCI_Send_Data(); // SCI TX
if ((Get_TargetSpeed() < Freq_Min) || (Get_TargetSpeed() > Freq_Max))
{
MotorCmd = STOP_CMD;
MotorStatus=0;
MotorStatus_Backup = 0;
start_counter = 0; // prepare for the start counter
stall_counter = 0; // prepare for the stall counter
timer_after_start = 1000; // prepare for the start-up failure
}
else
{
if ((MotorStatus & FAULT_MSK) == 0)
MotorCmd = START_CMD;
}
/* --------------------------------------------------*/
Chk_Power_Motor_Status();
if ((MotorStatus & FAULT_MSK) != 0) State = FAULT; // MOTOR_STALLED
// or SCI_FAILURE or EMERGENCY_STOP?
switch (State)
{
case IDLE:
if ((MotorCmd == START_CMD) && (Timer_RestartTimerBegin() == FALSE))
{
State = START;
}
break;
case START:
if (MTC_StartMotor() == TRUE) State = RUN;
// PORTS_LedOn();
break;
case RUN:
// PORTS_LedOff();
if (GetMotorStatus() & START_UP_FAILED) // start is failed
{
start_counter++;
if (start_counter >= 0x03) // restarting times is high than 3
{
State = FAULT;
start_counter = 0; // prepare for the start counter
}
else
{
MTC_StopMotor(); // prepare for starting again
if ((u8)(GetMotorStatus() & FAULT_MSK) == 0) State = IDLE;
}
break;
}
Chk_Motor_Stalled();
if ((GetMotorStatus() & MOTOR_STALLED) != 0) // motor stalled
{
if (timer_after_start != 0) // motor stalled in starting stage(in 10 seconds)
{
stall_counter++;
if (stall_counter < 0x03) // motor stalling time is low than 3
{
MTC_StopMotor(); // prepare for starting again
SetMotorStatus((u8)(GetMotorStatus() & (u8)(~MOTOR_STALLED)));
if ((u8)(GetMotorStatus() & FAULT_MSK) == 0) State = IDLE;
}
else
{ // start-up failed
stall_counter = 0; // prepare for stall counter
SetMotorStatus((u8)(GetMotorStatus() & (u8)(~MOTOR_STALLED)));
SetMotorStatus((u8)(GetMotorStatus() | (u8)(START_UP_FAILED)));
}
}
break;
}
else
{
if (timer_after_start == 0) stall_counter = 0;
}
if (MotorStatus & AUTO_SWITCH)
{
Change_Speed(); // acceleration rate = 1 Hz/0.2Sec
if (ValBit(Flag_MTC,SAMP_EVT)) // update PWM?
{
//u32 temp;
ClrBit(Flag_MTC,SAMP_EVT);
#if (FEEDBACK_TYPE == CLOSED_LOOP)
/* temp = (u8)(Get_RV1()); // target_freq = (Get_RV1 * (Freq_Max-Freq_Min))/256 + Freq_Min
temp *= (u32)(Freq_Max-Freq_Min);
temp >>= 8;
temp += Freq_Min;
*/
Set_Target_Electrical_Frequency(Get_CurrentTargetSpeed());
#elif (FEEDBACK_TYPE == OPEN_LOOP)
Falling_bemf = (u8)(Get_RV3()); // read RV3 & set falling Bemf coefficient accordingly
Rising_bemf = (u8)(Get_RV2()); // read RV2 & set rising Bemf coefficient accordingly
temp = (u8)(Get_RV1());
#if (DRIVING_MODE == VOLTAGE_MODE)
temp *= (u32)(Max_Duty); // temp = (Max_Duty * Get_RV1)/256
#elif (DRIVING_MODE == CURRENT_MODE)
temp *= (u32)(PWM_FREQUENCY); // temp = (PWM_FREQUENCY * Get_RV1)/256
#else
#error"Wrong Driving Mode Parameter!"
#endif
temp >>= 8;
Set_Duty((u16)(temp));
#else
#error"Wrong Feedback Type Parameter!"
#endif
}
}
if (MotorCmd == STOP_CMD) State = BRAKE;
break;
case BRAKE:
if (active_brake(Brake_Duty,Brake_Time) == TRUE) State = STOP;
break;
case STOP:
MTC_StopMotor();
State = IDLE;
Set_RestartTimer(18000); // 3 Min delay
break;
case WAIT:
break;
case FAULT:
default:
MotorStatus_Backup = MotorStatus;
MTC_StopMotor();
start_counter = 0; // prepare for the start counter
stall_counter = 0; // prepare for the stall counter
if ((Timer_RestartTimerBegin() == FALSE) && (MotorCmd != STOP_CMD))
Set_RestartTimer(18000); // 3 Min delay
if ((MotorStatus & FAULT_MSK) == 0)
{
State = IDLE;
}
MotorCmd = STOP_CMD;
break;
}
}
} // main loop
/*** (c) 2004 STMicroelectronics ****************** END OF FILE ***/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -