Example #1
0
void initVariables (void)
{
	rising_edge_delay = DB_RED;
	falling_edge_delay = DB_FED;
	overlap = 0;
	duty = 0;
	period = INITIAL_PERIOD;
	first_run = 1;
	Input_Voltage_Q15 = 0;
	Bus_Voltage_Q15 = 0;
	Input_Current_Q15 = 0;
	VIN_SCALE = VIN_SCALE_INIT;
	VBUS_SCALE = VBUS_SCALE_INIT;
	I_SCALE = I_SCALE_INIT;
	I_OFFSET = I_OFFSET_INIT;
	control_gain = 0x8000;
	v_temporary = 0;
	//Vin_reference_Q15 = 0x8000;
	Vin_reference_Q15 = _IQ15(100);
	Min_Startup_Voltage_Q15 = MIN_STARTUP_VOLTAGE;
	Min_Operating_Voltage_Q15 = MIN_OPERATING_VOLTAGE;
	Max_Operating_Voltage_Q15 = _IQ15(INITIAL_MAX_OPERATING_VOLTAGE);
	Max_Voltage_Q15 = _IQ15(INITIAL_MAX_VOLTAGE);
	startup_flag = 0;
	step_dir = 0;
	input_current_prescale = 0;
	VIN_OFFSET = VIN_OFFSET_INIT;
	input_voltage_prescale = 0;
	Power_Good = 0;
	Output_Over_Voltage = 1;
	deadtime_after_Q1_off = INITIAL_DEADTIME_AFTER_Q1_OFF;
	deadtime_after_Q2_off = INITIAL_DEADTIME_AFTER_Q2_OFF;
	deadtime_after_Q1_off_hi_res = INITIAL_DEADTIME_AFTER_Q1_OFF_HI_RES;
	deadtime_after_Q2_off_hi_res = INITIAL_DEADTIME_AFTER_Q2_OFF_HI_RES;
	early_turn_on_Q2 = INITIAL_EARLY_TURN_ON_Q2;
	duty = INITIAL_EARLY_TURN_ON_Q2;
	Num_Power_Samples = INITIAL_POWER_SAMPLES;
	for (i = 0; i < MAX_POWER_SAMPLES; i++)
	{
		Power_Samples_Q15[i] = 0;
	}
	Power_Sample_Counter = 0;
	Power_Sample_Sum_Q15 = 0;
	MPPT_Step_Size_Q15 = INITIAL_MPPT_STEP_SIZE;
	Num_Power_Samples_Q15 = _IQ15(Num_Power_Samples);
	Hiccup_Current_Limit_Q15 = _IQ15(INITIAL_HICCUP_CURRENT_LIMIT);
	High_Voltage_Reference_Q15 = _IQ15(100);
	delay_flag = 0;
	Current_Limit_Q15 = _IQ15(INITIAL_CURRENT_LIMIT);
	duty_comp = 0;
	initialize_Period_Table();
	Sample_Advance = 0;
	i_sense_v_gain = 0;
	i_sense_v_shift = 0;
	clkdiv = 0;
}
// MainISR
interrupt void MainISR(void)
{
    // Verifying the ISR
    IsrTicker++;

    if(RunMotor)
    {
        // =============================== LEVEL 1 ======================================
        //   Checks target independent modules, duty cycle waveforms and PWM update
        //   Keep the motors disconnected at this level
        // ==============================================================================

#if (BUILDLEVEL==LEVEL1)

        // ------------------------------------------------------------------------------
        //  Connect inputs of the RMP module and call the ramp control macro
        // ------------------------------------------------------------------------------
        rc1.TargetValue = VRef1;
        RC_MACRO(rc1)

        // ------------------------------------------------------------------------------
        //  Connect inputs of the PWM_DRV module and call the PWM signal generation macro
        // ------------------------------------------------------------------------------
        pwm1.MfuncC1 = (int16)_IQtoIQ15(_IQabs(rc1.SetpointValue)); // MfuncC1 is in Q15
        PWM_MACRO(pwm1) // Calculate the new PWM compare values
        

        if(rc1.SetpointValue < 0)
        {
            Rotation1 = 0;
        }
        else
        {
            Rotation1 = 1;
        }

        if(Motor==1)
        {
            if(Rotation1==0)
            {
                EPwm1Regs.AQCSFRC.bit.CSFB = 0;    //Forcing Disabled on EPWM1B
                EPwm1Regs.AQCSFRC.bit.CSFA = 1;    //EPWM1A forced low
            }
            else if(Rotation1 == 1)
            {
                EPwm1Regs.AQCSFRC.bit.CSFA = 0;    //Forcing Disabled on EPWM1A
                EPwm1Regs.AQCSFRC.bit.CSFB = 1;    //EPWM1B forced low
            }
            else
            {
                EPwm1Regs.AQCSFRC.bit.CSFA = 1;    //EPWM1A forced low
                EPwm1Regs.AQCSFRC.bit.CSFB = 1;    //EPWM1B forced low
            }
            EPwm1Regs.CMPA.half.CMPA=pwm1.PWM1out;
        }
        //else if (Motor==2)
        //{
            if(Rotation1==0)
            {
                EPwm2Regs.AQCSFRC.bit.CSFB = 0;    //Forcing Disabled on EPWM2B
                EPwm2Regs.AQCSFRC.bit.CSFA = 1;    //EPWM2A forced low
            }
            else if(Rotation1 == 1)
            {
                EPwm2Regs.AQCSFRC.bit.CSFA = 0;    //Forcing Disabled on EPWM2A
                EPwm2Regs.AQCSFRC.bit.CSFB = 1;    //EPWM2B forced low
            }
            else
            {
                EPwm2Regs.AQCSFRC.bit.CSFA = 1;    //EPWM2A forced low
                EPwm2Regs.AQCSFRC.bit.CSFB = 1;    //EPWM2B forced low
            }
            EPwm2Regs.CMPA.half.CMPA=pwm1.PWM1out;
        //}

        // ------------------------------------------------------------------------------
        //    Connect inputs of the PWMDAC module
        // ------------------------------------------------------------------------------
        PwmDacCh1 = (int16)_IQtoIQ15(VRef1);
        PwmDacCh2 = (int16)_IQtoIQ15(rc1.SetpointValue);

        // ------------------------------------------------------------------------------
        //    Connect inputs of the DATALOG module
        // ------------------------------------------------------------------------------
        DlogCh1 = (int16)_IQtoIQ15(VRef1);
        DlogCh2 = (int16)_IQtoIQ15(rc1.SetpointValue);
        DlogCh3 = (int16)_IQtoIQ15(VRef1);
        DlogCh4 = (int16)_IQtoIQ15(rc1.SetpointValue);

#endif // (BUILDLEVEL==LEVEL1)

        // =============================== LEVEL 2 ======================================
        //   Level 2 verifies the analog-to-digital conversion, offset compensation
        // ==============================================================================

#if (BUILDLEVEL==LEVEL2)

// ------------------------------------------------------------------------------
//  Connect inputs of the RMP module and call the ramp control macro
// ------------------------------------------------------------------------------
        rc1.TargetValue = VRef1;
        RC_MACRO(rc1)

// ------------------------------------------------------------------------------
//  Measure phase currents, subtract the offset and normalize from (-0.5,+0.5) to (-1,+1).
// ------------------------------------------------------------------------------
        if(Motor==1)
        {
            IFdbk1b=_IQ15toIQ((AdcResult.ADCRESULT1<<3)-_IQ15(0.5))<<1;
            IFdbk1a=_IQ15toIQ((AdcResult.ADCRESULT0<<3)-_IQ15(0.5))<<1;
            IFdbk1 = (IFdbk1a - IFdbk1b) >> 1;
        }
        else if(Motor==2)
// ------------------------------------------------------------------------------
        rc1.TargetValue = VRef1;
        RC_MACRO(rc1)

// ------------------------------------------------------------------------------
//  Measure phase currents, subtract the offset and normalize from (-0.5,+0.5) to (-1,+1).
// ------------------------------------------------------------------------------
        if(Motor==1)
        {
            IFdbk1b=_IQ15toIQ((AdcResult.ADCRESULT1<<3)-_IQ15(0.5))<<1;
            IFdbk1a=_IQ15toIQ((AdcResult.ADCRESULT0<<3)-_IQ15(0.5))<<1;
            IFdbk1 = (IFdbk1a - IFdbk1b) >> 1;
        }
        else if(Motor==2)
        {
            IFdbk1b=_IQ15toIQ((AdcResult.ADCRESULT4<<3)-_IQ15(0.5))<<1;
            IFdbk1a=_IQ15toIQ((AdcResult.ADCRESULT3<<3)-_IQ15(0.5))<<1;
            IFdbk1 = (IFdbk1a - IFdbk1b) >> 1;
        }

// ------------------------------------------------------------------------------
//  Connect inputs of the PWM_DRV module and call the PWM signal generation macro
// ------------------------------------------------------------------------------
        pwm1.MfuncC1 = (int16)_IQtoIQ15(_IQabs(rc1.SetpointValue)); // MfuncC1 is in Q15
        PWM_MACRO(pwm1)                            // Calculate the new PWM compare values

        if(rc1.SetpointValue < 0)
        {
            Rotation1 = 0;
        }
        else
TEST(IQmath_ExtractInteger, IQ15int)
{
    LONGS_EQUAL(65535, _IQ15int(_IQ15(65535.999969482))) ;
    LONGS_EQUAL(-65536, _IQ15int(_IQ15(-65536.0))) ;
}