TIMotorLIBExample2/main.c

This example adds PWM generation for V/HZ control to Example 1.

//-----------------------------------------------------------
// Includes
// -------------------------------------------------------------
#include "Example2.h"  // includes for our particular project

//-----------------------------------------------------------
// Define
//-----------------------------------------------------------
#define VBUS                  50
// A/D #defines
#define IA_CH                 0                   // ia A/D channel
#define IB_CH                 0                   // ib A/D channel

//-----------------------------------------------------------
// Implemented Functions
// -------------------------------------------------------------
interrupt void ADInterrupt(void);
interrupt void pwmInterrupt(void);

//-----------------------------------------------------------
// Globals
//-----------------------------------------------------------
const _iq Vbus = _IQ(VBUS);   // bus voltage
const _iq Vmax = _IQ(VBUS>>1);          // max output
const _iq Fmax = _IQ(50);     // max frequency
const Uint16 DB_US = 3;                 // dead time (us)
// A/D variables
volatile _iq ia, ib;                    // measured currents
// pwm variables
volatile _iq vas, vbs, vcs;   // phase voltages
volatile _iq f;                         // frequency


//-----------------------------------------------------------

interrupt void ADInterrupt(void)
{
  ia = ReadADInputWriteRaw(ADCINA2);
  ib = ReadADInputWriteRaw(ADCINB2);
  // preload PWM values to be updated in next sample
  VHz(f, &vas, &vbs, &vcs);
  UpdatePWM3ph(vas,vbs,vcs,1);
  // prepare next adquisition
  ReinitAD();
}

//-----------------------------------------------------------

//-----------------------------------------------------------
// ISR on PWM
//-----------------------------------------------------------
interrupt void pwmInterrupt(void)
{
  // process .....
  // Acknowledge this interrupt to receive more interrupts from group 3
  PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
  // PABLO: Interrupt ACK should go to UpdatePWM function
  // Clear INT flag for this timer
  EPwm1Regs.ETCLR.bit.INT = 1;
}

//-----------------------------------------------------------

//-----------------------------------------------------------
// main
// -------------------------------------------------------------
void main(void)
{
  // SYSCLKOUT = 150Mhz
  // HSPCLK SYSCLKOUT/8 = 18.75MHz
  // LSPCLK= SYSCLKOUT/4
  InitCPU(8, 4);

  // Init three phase PWM
  Init3phPWM(pwmInterrupt, EPWMA, 100, DB_US, Vbus, SOCA);
  
  // AD initialization
  ConfigureAD(ADInterrupt, CASCADED_SEQ_MODE, 
    SIMULTANEOUS_MODE, PWM_SOC, 0);

  // ------------------ Add AD channels
  // ------------------- Current Sensor data -----------------------------
  // loops = 2;
  // rt = 1/1000;
  // Rm = 150;     (143 measured between input and GND)
  // op_gain = 0.5;
  // MAXBITS = 4096;
  // Vmax = 3
  // GAIN: % 1/(loops*rt_sensor*Rm*op_gain*MAXBITS/Vmax)
  // ------------------- Current Sensor data------ ------------------------

  AddChannel(ADCINA2, IA_CH, _IQ(0.03493696), _IQ(0.00512));                    // ia current
  AddChannel(ADCINB2, IB_CH, _IQ(-0.104635519999999), _IQ(0.00512));            // ib current

  // Configure Memory to store AD samples (OVERWRITE/NOVERWRITE)
  InitDataStorageBuffer(NOVERWRITE);

  //---------------------------------------------------------
  // Control routines         
  //---------------------------------------------------------
  InitVHz(_IQdiv((Uint32)Vbus>>1,Fmax), TWOPIDT); // Pablo: 2nd parameter should be gone

  SetTrigger();     // enable trigger to store AD samples

  // Enable PWM clock
  EnableEPWMModuleTBCLK();
  // enable PIE module and CPU interrupts
  EnableInterrupts();         // <-- PABLO: This call MUST BE the last one
  // main loop
  while(1){
    IDLE;
  }
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Generated on Mon Apr 23 12:59:20 2012 for TIMotorLIB by  doxygen 1.6.3