while(1)
{
if (CpuTimer1.InterruptCount >= 20000)
{
CpuTimer1.InterruptCount = 0;
LED_BLINK
}
if (GpioDataRegs.GPADAT.bit.GPIO22 == 1)
{
if (SciaRegs.SCIRXST.bit.RXRDY == 1)
rxdata = SciaRegs.SCIRXBUF.bit.RXDT;
if (rxdata == 0xFF)
SciaRegs.SCICTL1.bit.TXENA = 1;
if (rxdata < 50)
SpeedRef = _I2Q(rxdata / 100.0);
}
else
SpeedRef = _IQ(0.15);
if ((RunMotor == FALSE) && (SpeedRef > _IQ(0.05)))
{
GATE_ON
DELAY_US(50000);
DRV8301_Configuration();
Module_Init();
RunMotor = TRUE;
EALLOW;
EPwm1Regs.TZCLR.bit.OST=1;
EPwm2Regs.TZCLR.bit.OST=1;
EPwm3Regs.TZCLR.bit.OST=1;
EDIS;
}
if ((RunMotor == TRUE) && (SpeedRef < _IQ(0.05)))
{
GATE_OFF
speed1.SpeedRpm = 0;
RunMotor = FALSE;
EALLOW;
EPwm1Regs.TZFRC.bit.OST=1;
EPwm2Regs.TZFRC.bit.OST=1;
EPwm3Regs.TZFRC.bit.OST=1;
EDIS;
}
if (CpuTimer2.InterruptCount >= 60)
{
CpuTimer2.InterruptCount = 0;
TxBuf[0] = 0xFE;
TxBuf[1] = 0xFE;
TxBuf[2] = (Uint16)(AdcResult.ADCRESULT4 * VCC_3V3 * 5L) >> 8; // Ua * 4(V)
TxBuf[3] = (Uint16)(AdcResult.ADCRESULT5 * VCC_3V3 * 5L) >> 8; // Ub * 4(V)
TxBuf[4] = (Uint16)(AdcResult.ADCRESULT6 * VCC_3V3 * 5L) >> 8; // Uc * 4(V)
TxBuf[5] = (int16)((2048L – AdcResult.ADCRESULT1) * VCC_3V3 / SAMP_RES / DRV_GAIN) >> 8; // Ib * 16(A)
TxBuf[6] = (int16)((2048L – AdcResult.ADCRESULT2) * VCC_3V3 / SAMP_RES / DRV_GAIN) >> 8; // Ic * 16(A)
TxBuf[7] = (Uint16)(fabs(speed1.SpeedRpm)) >> 4; // MotorSpeed / 16(RPM)
TxBuf[8] = (Uint16)(AdcResult.ADCRESULT3 * VCC_3V3 * 5L) >> 8; // SupplyVoltage * 4(V)
TxBuf[9] = GetTemperatureC(AdcResult.ADCRESULT7); // ChipTemperature(℃)
for(i=0; i<TXBUFLEN; i++)
comPutChar(TxBuf[i]);
}
comTXD();
}
}
interrupt void MainISR(void)
{
CpuTimer1.InterruptCount++;
CpuTimer2.InterruptCount++;
if (RunMotor == TRUE)
{
// Initial Rotor Alignment Process
if (AlignFlag != 0)
{
mod1.Counter = 0;
pwm1.CmtnPointer = 0;
BLDCPWM_MACRO(pwm1)
if (VirtualTimer > 0x7FFE)
{
if (LoopCount != LOOP_CNT_MAX)
LoopCount++;
else
{
AlignFlag = 0;
VirtualTimer = 0;
VirtualTimer++;
VirtualTimer &= 0x00007FFF;
}
}
else
{
VirtualTimer++;
VirtualTimer &= 0x00007FFF;
}
}
else
{
// =============================== LEVEL 1 ======================================
// Open loop and Bemf measurement
// ==============================================================================
#if (BUILDLEVEL==LEVEL1)
// ——————————————————————————
// ADC conversion and offset adjustment
// ——————————————————————————
cmtn1.Va = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
cmtn1.Vb = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
cmtn1.Vc = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
DCbus_current = _IQ15toIQ((AdcResult.ADCRESULT1<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
// ——————————————————————————
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ——————————————————————————
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)
// ——————————————————————————
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ——————————————————————————
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)
// ——————————————————————————
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ——————————————————————————
mod1.TrigInput = impl1.Out;
MOD6CNT_MACRO(mod1)
// ——————————————————————————
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ——————————————————————————
#if MOTOR_DIR == 0
pwm1.CmtnPointer = (int16)mod1.Counter;
#elif MOTOR_DIR == 1
pwm1.CmtnPointer = 5 – (int16)mod1.Counter;
#endif
pwm1.DutyFunc = DfuncTesting;
BLDCPWM_MACRO(pwm1)
#ifdef SARAM
// ——————————————————————————
// Connect inputs of the DATALOG module
// ——————————————————————————
DlogCh1 = (int16)mod1.Counter;
DlogCh2 = _IQtoQ15(cmtn1.Va);
DlogCh3 = _IQtoQ15(cmtn1.Vb);
DlogCh4 = _IQtoQ15(cmtn1.Vc);
#endif
#endif // (BUILDLEVEL==LEVEL1)
// =============================== LEVEL 2 ======================================
// Closed speed loop and current measurement
// ==============================================================================
#if (BUILDLEVEL==LEVEL2)
// ——————————————————————————
// ADC conversion and offset adjustment
// ——————————————————————————
DCbus_current = _IQ15toIQ((AdcResult.ADCRESULT1<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
// ——————————————————————————
// Connect inputs of the RMP module and call the Ramp control macro.
// ——————————————————————————
rc1.TargetValue = SpeedRef;
RC_MACRO(rc1)
// ——————————————————————————
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ——————————————————————————
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)
// ——————————————————————————
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ——————————————————————————
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)
// ——————————————————————————
// Connect inputs of the RMP2 module and call the Ramp control 2 macro.
// ——————————————————————————
rmp2.DesiredInput = DFuncDesired;
RC2_MACRO(rmp2)
// ——————————————————————————
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ——————————————————————————
// Switch from open-loop to closed-loop operation by Ramp3DoneFlag variable
if (rmp3.Ramp3DoneFlag == FALSE)
mod1.TrigInput = impl1.Out; // open-loop operation
else
mod1.TrigInput = cmtn1.CmtnTrig; // closed-loop operation
MOD6CNT_MACRO(mod1)
// ——————————————————————————
// Connect inputs of the PID_REG3 module and call the PID speed controller
// macro.
// ——————————————————————————
pid1_spd.term.Ref = rc1.SetpointValue;
pid1_spd.term.Fbk = speed1.Speed;
PID_GR_MACRO(pid1_spd)
// ——————————————————————————
// Set the speed closed loop flag once the speed is built up to a desired value.
// ——————————————————————————
if ( rc1.EqualFlag == 0x7FFFFFFF )
{
SpeedLoopFlag = TRUE;
// rc1.RampDelayMax = 30;
}
// ——————————————————————————
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ——————————————————————————
// Switch from fixed duty-cycle or controlled Speed duty-cycle by SpeedLoopFlag variable
if (SpeedLoopFlag == FALSE)
{
pwm1.DutyFunc = rmp2.Out; // fixed duty-cycle
pid1_spd.data.ui=0;
pid1_spd.data.i1=0;
}
else
{
pwm1.DutyFunc = (int16)_IQtoQ15(pid1_spd.term.Out); // controlled Speed duty-cycle
}
pwm1.CmtnPointer = (int16)mod1.Counter;
BLDCPWM_MACRO(pwm1)
// ——————————————————————————
// Connect inputs of the COM_TRIG module and call the Commutation trigger macro.
// ——————————————————————————
cmtn1.Va = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
cmtn1.Vb = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
cmtn1.Vc = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
cmtn1.CmtnPointer = mod1.Counter;
cmtn1.VirtualTimer = VirtualTimer;
CMTN_TRIG_MACRO(cmtn1)
// ——————————————————————————
// Connect inputs of the SPEED_REV_PR module and call the speed calculation macro.
// ——————————————————————————
speed1.EventPeriod = cmtn1.RevPeriod;
SPEED_PR_MACRO(speed1)
#ifdef SARAM
// ——————————————————————————
// Connect inputs of the DATALOG module
// ——————————————————————————
DlogCh1 = (int16)mod1.Counter;
DlogCh2 = _IQtoQ15(cmtn1.Va);
DlogCh3 = _IQtoQ15(cmtn1.Vb);
DlogCh4 = _IQtoQ15(cmtn1.Vc);
#endif
#endif // (BUILDLEVEL==LEVEL4)
// =============================== LEVEL 3 ======================================
// Closed current(torque) loop
// ==============================================================================
#if (BUILDLEVEL==LEVEL3)
// ——————————————————————————
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ——————————————————————————
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)
// ——————————————————————————
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ——————————————————————————
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)
// ——————————————————————————
// Connect inputs of the RMP2 module and call the Ramp control 2 macro.
// ——————————————————————————
rmp2.DesiredInput = DFuncDesired;
RC2_MACRO(rmp2)
// ——————————————————————————
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ——————————————————————————
// Switch from open-loop to closed-loop operation by Ramp3DoneFlag variable
if (rmp3.Ramp3DoneFlag == FALSE)
mod1.TrigInput = impl1.Out; // open-loop operation
else
mod1.TrigInput = cmtn1.CmtnTrig; // closed-loop operation
MOD6CNT_MACRO(mod1)
// ——————————————————————————
// Connect inputs of the PID_GRANDO_CONTROLLER module and call the PID controller macro.
// ——————————————————————————
tempIdc=pid1_idc.term.Fbk;
pid1_idc.term.Ref = CurrentSet;
pid1_idc.term.Fbk = _IQ15toIQ((AdcResult.ADCRESULT1<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
// pid1_idc.term.Fbk = -(_IQ15toIQ((AdcResult.ADCRESULT1<<3)-IDC_offset)<<1); //1.65V offset added on DRV8312 board.
if(pid1_idc.term.Fbk<0)
pid1_idc.term.Fbk=tempIdc; // Eliminate negative values
PID_GR_MACRO(pid1_idc)
// ——————————————————————————
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ——————————————————————————
// Switch from fixed duty-cycle or controlled Idc duty-cycle by ILoopFlag variable
if (ILoopFlag == FALSE)
{
pwm1.DutyFunc = rmp2.Out; // fixed duty-cycle
pid1_idc.data.ui=0;
pid1_idc.data.i1=0;
}
else
{
pwm1.DutyFunc = _IQtoQ15(pid1_idc.term.Out); // controlled Idc duty-cycle
}
pwm1.CmtnPointer = (int16)mod1.Counter;
BLDCPWM_MACRO(pwm1)
// ——————————————————————————
// Connect inputs of the COM_TRIG module and call the Commutation trigger macro.
// ——————————————————————————
cmtn1.Va = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
cmtn1.Vb = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
cmtn1.Vc = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
cmtn1.CmtnPointer = mod1.Counter;
cmtn1.VirtualTimer = VirtualTimer;
CMTN_TRIG_MACRO(cmtn1)
// ——————————————————————————
// Connect inputs of the SPEED_REV_PR module and call the speed calculation macro.
// ——————————————————————————
speed1.EventPeriod = cmtn1.RevPeriod;
SPEED_PR_MACRO(speed1)
#ifdef SARAM
// ——————————————————————————
// Connect inputs of the DATALOG module
// ——————————————————————————
DlogCh1 = _IQtoQ15(cmtn1.DebugBemf);
DlogCh2 = _IQtoQ15(cmtn1.Va);
DlogCh3 = _IQtoQ15(cmtn1.Vb);
DlogCh4 = _IQtoQ15(cmtn1.Vc);
#endif
#endif // (BUILDLEVEL==LEVEL3)
Eric Ma:
C:\ti\controlSUITE\development_kits\DRV8312-C2-KIT_v128\BLDC_Sensored
你可以看看这个文档更靠谱。
ERIC