📄 main.c
字号:
/*****************************************************************************
*
* Freescale Semiconductor Inc.
* (c) Copyright 2004-2006 Freescale Semiconductor, Inc.
* (c) Copyright 2001-2004 Motorola, Inc.
* ALL RIGHTS RESERVED.
*
******************************************************************************
*
* $File Name: main.c$
*
* $Date: Jan-19-2007$
*
* $Version: 0.0.2.0$
*
* Description: Main file for BLDC Sensorless motor control using MC9S08AW60
*
*****************************************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC9S08AW60.h> /* include peripheral declarations */
#include "main.h"
#include "math.h"
/*****************************************************************************
*
* Constants Definition
*
*****************************************************************************/
const unsigned char adcFirstSample[3] =
{AD_DCB_V, AD_DCB_I, AD_TEMP};
unsigned char *pAdcSecondSample;
const unsigned char adcSecondSampleCD[6] =
{AD_BEMF_C, AD_BEMF_B, AD_BEMF_A, AD_BEMF_C, AD_BEMF_B, AD_BEMF_A};
const unsigned char adcSecondSampleCCD[6] =
{AD_BEMF_B, AD_BEMF_C, AD_BEMF_A, AD_BEMF_B, AD_BEMF_C, AD_BEMF_A};
/*****************************************************************************
*
* Variables Definition
*
*****************************************************************************/
#pragma DATA_SEG SHORT MY_ZEROPAGE
volatile static unsigned int duty_cycle, duty_cycle_demand, duty_cycle_alignment;
static unsigned int duty_cycle_speed, duty_cycle_torque;
static tDriveStatus driveStatus;
static tDriveDemand driveDemand,driveDemandRemote;
static tInterfaceStatus interfaceStatus;
static tProcessStatus processStatus;
static unsigned int actualPeriodZC;
volatile static unsigned int periodZC_F_PhA, periodZC_R_PhA;
volatile static unsigned int periodZC_F_PhB, periodZC_R_PhB;
volatile static unsigned int periodZC_F_PhC, periodZC_R_PhC;
volatile static unsigned int periodFalling, periodRaising, periodFiltered;
volatile static unsigned char pwmSector;
static unsigned int dcb_voltage;
static unsigned int dcb_voltage_ZC;
static unsigned int dcb_voltage_LT;
static unsigned int dcb_voltage_HT;
static signed int dcb_current;
volatile static signed torque_F_PhA,torque_F_PhB,torque_F_PhC;
volatile static signed torque_R_PhA,torque_R_PhB,torque_R_PhC;
volatile static signed torqueFiltered, torqueRequired;
static unsigned int phA_voltage;
static unsigned int phB_voltage;
static unsigned int phC_voltage;
static unsigned int temperature;
static signed int dcb_offset;
static unsigned char adcPeriod;
static unsigned char appState;
static unsigned int timeZC;
static unsigned int lastTimeZC;
static unsigned int periodNextCommutation;
static unsigned int timeCommutation;
static unsigned int timeZC_PhA;
static unsigned int timeZC_PhB;
static unsigned int timeZC_PhC;
static unsigned int timeZCstep;
static unsigned int actualSpeed;
static unsigned int desiredSpeed;
static unsigned int slopedSpeed;
static unsigned int *desiredValue;
static unsigned char stallCheckCounter;
static unsigned int alignmentTimer;
static unsigned int startCMTTimer;
static unsigned char startCMTCounter;
static sPIparams speedPIParams;
static sPIparams torquePIParams;
static unsigned char advanceAngle;
#pragma DATA_SEG DEFAULT
static unsigned char StallError;
static unsigned char direction;
static unsigned char controlMode;
static unsigned int desiredValueStep;
static unsigned int desiredValueMaxLimit;
static unsigned int desiredValueMinLimit;
/*****************************************************************************
*
* Functions definition
*
*****************************************************************************/
static void PWM_Disable(void);
static void PWM_Vector_0CD(void);
static void PWM_Vector_1CD(void);
static void PWM_Vector_2CD(void);
static void PWM_Vector_3CD(void);
static void PWM_Vector_4CD(void);
static void PWM_Vector_5CD(void);
static void PWM_Vector_0CCD(void);
static void PWM_Vector_1CCD(void);
static void PWM_Vector_2CCD(void);
static void PWM_Vector_3CCD(void);
static void PWM_Vector_4CCD(void);
static void PWM_Vector_5CCD(void);
static void PWM_Alignment(void);
static void PWMTransitionVectorCD(void);
static void PWMTransitionVectorCCD(void);
static tPointerFcn *PWMCommutationFcn;
static tPointerFcn PWMCommutationFcnCD[] = {
PWM_Vector_0CD,
PWM_Vector_5CD,
PWM_Vector_4CD,
PWM_Vector_3CD,
PWM_Vector_2CD,
PWM_Vector_1CD,
};
static tPointerFcn PWMCommutationFcnCCD[] = {
PWM_Vector_5CCD,
PWM_Vector_0CCD,
PWM_Vector_1CCD,
PWM_Vector_2CCD,
PWM_Vector_3CCD,
PWM_Vector_4CCD,
};
void (*PWMTransition)(void);
static void SaveDCBvoltage(void);
static void SaveDCBcurrent(void);
static void SaveDCBtemperature(void);
static tPointerFcn SaveFirstSample[] = {
SaveDCBtemperature,
SaveDCBvoltage,
SaveDCBcurrent
};
static void SaveBEMFA(void);
static void SaveBEMFB(void);
static void SaveBEMFC(void);
static void CheckManualInterface (tInterfaceStatus *Status);
static tPointerFcn *SaveSecondSample;
static tPointerFcn SaveSecondSampleCD[] = {
SaveBEMFC,
SaveBEMFB,
SaveBEMFA,
SaveBEMFC,
SaveBEMFB,
SaveBEMFA
};
static tPointerFcn SaveSecondSampleCCD[] = {
SaveBEMFB,
SaveBEMFC,
SaveBEMFA,
SaveBEMFB,
SaveBEMFC,
SaveBEMFA
};
static void ZCdetectPhAfalling(void);
static void ZCdetectPhAraising(void);
static void ZCdetectPhBfalling(void);
static void ZCdetectPhBraising(void);
static void ZCdetectPhCfalling(void);
static void ZCdetectPhCraising(void);
static tPointerFcn *ZCdetection;
static tPointerFcn ZCdetectionCD[] = {
ZCdetectPhCfalling,
ZCdetectPhBraising,
ZCdetectPhAfalling,
ZCdetectPhCraising,
ZCdetectPhBfalling,
ZCdetectPhAraising
};
static tPointerFcn ZCdetectionCCD[] = {
ZCdetectPhBfalling,
ZCdetectPhCraising,
ZCdetectPhAfalling,
ZCdetectPhBraising,
ZCdetectPhCfalling,
ZCdetectPhAraising
};
static void AppInit(void);
static void AppAlignment(void);
static void AppStart(void);
static void AppRun(void);
static void AppStop(void);
static void AppStabilization(void);
static void AppError(void);
static void AppInitToStop(void);
static void AppStopToAlignment(void);
static void AppStallErrorToAlignment(void);
static void AppAlignmentToStart(void);
static void AppRunToStop(void);
static void AppStartToStabilization(void);
static void AppStabilizationToRun(void);
static tPointerFcn AppStateMachine[] = {
AppInit,
AppAlignment,
AppStart,
AppRun,
AppStop,
AppStabilization,
AppError
};
static void AppBackGround(void);
static void PWM_Enable(void);
signed int rampGetValue16(signed int Increment, signed int ActValue, signed int ReqValue);
signed int controllerPI(signed int desiredValue, signed int measuredValue, sPIparams *pParams, signed int NegativePILimit, signed int PositivePILimit);
/*****************************************************************************
*
* Function: void SysInit(void)
*
* Description: System initialization function
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void SysInit(void)
{
ICGC1 = ICGC1_HGO_MASK | ICGC1_CLKS0_MASK | ICGC1_OSCSTEN_MASK;
// Internal clock (243kHz) + Enable FLL
ICGC2 = ICGC2_MFD2_MASK | ICGC2_MFD1_MASK | ICGC2_MFD0_MASK;
// ICGOUT frequency changed to 40MHz, bus clock 20Mhz
// 243kHz*/7*64*18/1; P=64, N=18, R=1
ICGTRM = 128;
// Trimming register set to middle
SOPT = 0x00;
// COP Watchdog timer disabled
// COP Watchdog long timeout period selected
// Stop Mode disabled
}
/*****************************************************************************
*
* Function: void TPM1Init(void)
*
* Description: Timer 1 initialization function
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void TPM1Init(void)
{
TPM1SC = TPM1SC_TOIE_MASK | TPM1SC_CPWMS_MASK | TPM1SC_CLKSA_MASK;
// Prescale divisor (divisor = 1)
// Clock source - BUS clock
// Center aligned PWM mode selected
// Enable Overflow Interrupt
TPM1MOD = PWM_MODULO;
// PWM frequency set to 16000
// 20MHz/2/16kHz = 625
// Note: rounded to be even value
}
/*****************************************************************************
*
* Function: void TPM2Init(void)
*
* Description: Timer 1 initialization function
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void TPM2Init(void)
{
TPM2SC = TPM2SC_CLKSA_MASK | TPM2SC_PS2_MASK | TPM2SC_PS1_MASK | TPM2SC_PS0_MASK;
// Prescale divisor (divisor = 128)
// Clock source - BUS clock
// Edge aligned PWM mode selected
TPM2MOD = 0;
// Free running timer
TPM2C0SC = TPM2C0SC_MS0A_MASK;
// Enable Output Compare interrupt
// Output Compare
// Software Output Compare only
TPM2C1V = 781;
// period 5 ms (20e6/128/5000)
TPM2C1SC = TPM2C1SC_CH1IE_MASK | TPM2C1SC_MS1A_MASK;
// Enable Output Compare interrupt
// Output Compare
// Software Output Compare only
}
/*****************************************************************************
*
* Function: void ADC_Init(void)
*
* Description: ADC module initialization function
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void ADC_Init(void)
{
AD1CFG = AD1CFG_MODE1_MASK;
// 10 bit conversion selected
APCTL1 = APCTL1_ADPC0_MASK | APCTL1_ADPC1_MASK | APCTL1_ADPC4_MASK | APCTL1_ADPC5_MASK | APCTL1_ADPC6_MASK;
// Enable input pins: DCB voltage, DCB current, Phase A, B, C voltages
}
/*****************************************************************************
*
* Function: void IO_PortsInit(void)
*
* Description: I/O ports initialization function
*
* Returns: None
*
* Global Data:None
*
* Arguments: None
*
* Range Issues: None
*
* Special Issues: None
*
*****************************************************************************/
void IO_PortsInit(void)
{
PTCDD_PTCDD6 = 1; //bit 6 of Port C as outputs - User LED
PTEDD = PTEDD_PTEDD2_MASK | PTEDD_PTEDD3_MASK;
PTFDD = PTFDD_PTFDD0_MASK | PTFDD_PTFDD1_MASK | PTFDD_PTFDD2_MASK | PTFDD_PTFDD3_MASK;
// Set PWM pins as output when timer channel disabled
PTGPE = PTGPE_PTGPE0_MASK | PTGPE_PTGPE1_MASK;
//Enable pull up resistors for UP/DOWN buttons;
PTCPE = PTGPE_PTGPE4_MASK;
//Enable pull up resistors for START/STOP switch;
//PTADD = PTADD_PTADD0_MASK | PTADD_PTADD1_MASK | PTADD_PTADD2_MASK | PTADD_PTADD3_MASK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -