This is an example on how to use hardware units.
//----------------------------------------------------------- // Includes // ------------------------------------------------------------- #include "ExampleHrdw.h" // includes for our particular project //----------------------------------------------------------- // Define //----------------------------------------------------------- #define VBUS 50 // A/D #defines #define IUR_CH 0 // iu (RECTIFIER) A/D channel #define IVR_CH 0 // iv (RECTIFIER) A/D channel #define VUVR_CH 2 // vuv (RECTIFIER) A/D channel #define VVWR_CH 2 // vvw (RECTIFIER) A/D channel #define VBUS1_CH 1 // Vbus A/D channel #define IUI_CH 3 // iu (INVERTER) A/D channel #define IVI_CH 3 // iv (INVERTER) A/D channel #define VUVI_CH 4 // vuv (INVERTER) A/D channel #define VVWI_CH 4 // vvw (INVERTER) A/D channel //----------------------------------------------------------- // Implemented Functions // ------------------------------------------------------------- interrupt void ADInterrupt(void); interrupt void ePWMAInterrupt(void); interrupt void ePWMBInterrupt(void); interrupt void QEP1Interrupt(void); interrupt void Timer0Interrupt(void); //----------------------------------------------------------- // Globals //----------------------------------------------------------- const Uint16 HSPCLK = 1; // high speed preescaler divider const Uint16 LSPCLK = 1; // low speed preescaler divider const _iq Vbus = _IQ(VBUS); // bus voltage const Uint16 TPWMA_US = 100; // EPWMA period (us) const Uint16 TPWMB_US = 100; // EPWMB period (us) const Uint16 DB_US = 3; // PWM dead time (us) const _iq F_AD = _IQ(18.75); // ADC clk (Mhz) const Uint16 T_SH = 500; // ADC Sample & Hold window width (ns) const Uint16 T_QEP = 10000; // QEP UTE period (us) const Uint16 ENCODER_LINES=1024;// encoder lines const Uint16 POLE_PAIRS=2; // machine pole pairs const Uint32 T_T0=1000; // Timer 0 period (us) // A/D variables volatile _iq iuI, ivI; volatile _iq iuR, ivR, vuvI, vvwI, icL; // measured currents volatile _iq vuvL, vvwL; // measured voltages volatile _iq vbus; // measured vbus // encoder Uint16 qep1Counter; //----------------------------------------------------------- interrupt void ADInterrupt(void) { ToggleGPIOPort(_GPIO32); // reading of AD channels vbus = ReadADInputWriteRaw(ADCINA0); iuR = ReadADInputWriteRaw(ADCINA1); ivR = ReadADInputWriteRaw(ADCINB1); iuI = ReadADInputWriteRaw(ADCINA2); ivI = ReadADInputWriteRaw(ADCINB2); vuvL = ReadADInputWriteRaw(ADCINA3); vvwL = ReadADInputWriteRaw(ADCINB3); vuvI = ReadADInputWriteRaw(ADCINA4); vvwI = ReadADInputWriteRaw(ADCINB4); qep1Counter = (Uint16)(qep1.eQEPRegs->QPOSCNT); WriteData(&qep1Counter); WriteData(&(qep1.lastPos)); WriteData(&(qep1.eQEPRegs->QEPSTS.all)); // prepare next adquisition UpdatePWMADuty3ph(_IQ(0.4),_IQ(0.6),_IQ(0.8)); ReinitADSEQ1(); } //----------------------------------------------------------- //----------------------------------------------------------- // ISR on PWMA //----------------------------------------------------------- interrupt void ePWMAInterrupt(void) { // process ..... ToggleGPIOPort(_GPIO18); UpdatePWMADuty3ph(_IQ(0.1),_IQ(0.3),_IQ(0.5)); // Acknowledge this interrupt EpwmAPIEAck(); } //----------------------------------------------------------- //----------------------------------------------------------- // ISR on PWMB //----------------------------------------------------------- interrupt void ePWMBInterrupt(void) { // process ..... //ToggleGPIOPort(_GPIO19); UpdatePWMBDuty3ph(_IQ(0.2),_IQ(0.4),_IQ(0.6)); // Acknowledge this interrupt EpwmBPIEAck(); } //----------------------------------------------------------- // ---------------------------------------------------------------------------- // ISR on QEP1 // ---------------------------------------------------------------------------- interrupt void QEP1Interrupt(void) { // process ..... ToggleGPIOPort(_GPIO19); // calculate velocity and position QEP1VelocityAndPosition(); Eqep1PIEAck(); } //----------------------------------------------------------- // ---------------------------------------------------------------------------- // ISR on Timer0 // ---------------------------------------------------------------------------- interrupt void Timer0Interrupt(void) { //ToggleGPIOPort(_GPIO19); // Acknowledge this interrupt to receive more interrupts from group 1 Timer0PIEAck(); } // ---------------------------------------------------------------------------- //----------------------------------------------------------- // main // ------------------------------------------------------------- void main(void) { // Enable GPIO outputs on GPIO18, 19, 30, 32 set it high // GPIO18 ConfigureGPIOPort(_GPIO18, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH); // GPIO19 ConfigureGPIOPort(_GPIO19, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH); // GPIO30 ConfigureGPIOPort(_GPIO30, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH); // GPIO32 ConfigureGPIOPort(_GPIO32, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH); // SYSCLKOUT = 150Mhz // HSPCLK = SYSCLKOUT // LSPCLK = SYSCLKOUT InitCPU(OSCCLK, DSP28_CLKINDIV, DSP28_PLLCR, HSPCLK, LSPCLK); // Init three phase PWMA Init3phPWM(ePWMAInterrupt, EPWMA, TPWMA_US, DB_US, TRIANGLE, SYMMETRICAL, CTR_ZERO, Vbus, SOCA, DB_ACTV_HIC); // Init three phase PWMB Init3phPWM(ePWMBInterrupt, EPWMB, TPWMB_US, DB_US, TRIANGLE, SYMMETRICAL, CTR_ZERO, Vbus, SOCNULL, DB_ACTV_LOC); // AD initialization ConfigureAD(ADInterrupt, F_AD, T_SH, 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(ADCINA1, IUR_CH, _IQ(6.389431840973869), _IQ(0.0388196)); // iu (RECTIFIER) current AddChannel(ADCINB1, IVR_CH, _IQ(6.071838412173394), _IQ(0.0388196)); // iv (RECTIFIER) current AddChannel(ADCINA2, IUI_CH, _IQ(4.4377), _IQ(0.03319049)); // iu (INVERTER) current AddChannel(ADCINB2, IVI_CH, _IQ(5.2879), _IQ(0.03319049)); // iv (INVERTER) current AddChannel(ADCINA0, VBUS1_CH, _IQ(5.081364916335725e+02), _IQ(0.647901387693130)); // vbus 1 voltage AddChannel(ADCINA3, VUVR_CH, _IQ(55.984477040846890), _IQ(0.321979324581005)); // vuv (RECTIFIER) voltage AddChannel(ADCINB3, VVWR_CH, _IQ(52.629935447699701), _IQ(0.321979324581005)); // vvw (RECTIFIER) voltage AddChannel(ADCINA4, VUVI_CH, _IQ(48.583587866666690), _IQ(0.3228)); // vuv (INVERTER) voltage AddChannel(ADCINB4, VVWI_CH, _IQ(50.492447733333343), _IQ(0.3228)); // vvw (INVERTER) voltage // Enable trigger to store AD samples SetTrigger(); // Configure Memory to store AD samples (OVERWRITE/NOVERWRITE) InitDataStorageBuffer(NOVERWRITE); // QEP1 ConfigureQEP1(QEP1Interrupt, T_QEP, PCRM_IE, ENCODER_LINES, POLE_PAIRS); // CPU timers // Timer structure, T(us), timer ISR ConfigureCpuTimer(CPUTIMER0, T_T0, Timer0Interrupt); // Enable QEP EnableQEP1(); // Start timer StartCpuTimer0(); // Enable PWM clock EnableEPWMModuleTBCLK(); // enable PIE module and CPU interrupts EnableInterrupts(); // <-- PABLO: This call MUST BE the last one // main loop while(1){ IDLE; } }