Other Parts Discussed in Thread: CONTROLSUITE
Hey all,
it's me again :D
I am currently playing with the F28377S launchpad, and I am quite amazed so far. However, I am struggling with adapting my ideas into the code. In particular, I use controlsuite and its example "epwm_deadband". And I can adjust the duty cycles of my PWM channels according to what I want. However, I cannot manage to change the PWM frequency according to my calculations. The Technical Reference Manual says for Up-Down-Count mode:
T_PWM = 2*TPRD*TBCLK
I assume a processor clock frequency of 200MHz, and I would like to have a 16kHz PWM. Thus, the value for TPRD should be
TPRD = T_PWM/(2*TBCLK)=f_CLK/(2*f_PWM)=200MHz/(2*16kHz)=6250
I am measuring the pins on ePWM2A and ePWM2B, which have the PRD assigned to 6250 with a duty cycle of 50% (i.e. PRD/2). On the oscilloscope, I do measure a duty cycle of 50%, exactly as wished. However, the frequency of my PWM signal is 4kHz instead of 16kHz - which is a factor of 4 smaller meaning that my microcontroller is running at 50MHz instead of 200MHz.
According to my best knowledge, the code as posted below should result in a PWM frequency of 16kHz when PRD is set to 6250, which means that the clock frequency is scaled down somewhere else.
But I cannot figure out where I have the option to scale my clock frequency. Could you maybe assist me with that?
Thank you.
The code:
//########################################################################### // FILE: epwm_deadband_c28.c // TITLE: Check PWM Dead-Band // //! \addtogroup cpu01_example_list //! <h1> EPWM dead band control (epwm_deadband)</h1> //! //! During the test, monitor ePWM1, ePWM2, and/or ePWM3 outputs //! on a scope. //! //! - ePWM1A is on GPIO0 //! - ePWM1B is on GPIO1 //! - ePWM2A is on GPIO2 //! - ePWM2B is on GPIO3 //! - ePWM3A is on GPIO4 //! - ePWM3B is on GPIO5 //! //! This example configures ePWM1, ePWM2 and ePWM3 for: //! - Count up/down //! - Deadband //! //! 3 Examples are included: //! - ePWM1: Active low PWMs //! - ePWM2: Active low complementary PWMs //! - ePWM3: Active high complementary PWMs //! //! Each ePWM is configured to interrupt on the 3rd zero event. //! When this happens the deadband is modified such that //! 0 <= DB <= DB_MAX. That is, the deadband will move up and //! down between 0 and the maximum value. //! //! View the EPWM1A/B, EPWM2A/B and EPWM3A/B waveforms //! via an oscilloscope // // //########################################################################### // $TI Release: F2837xS Support Library v180 $ // $Release Date: Fri Nov 6 16:27:58 CST 2015 $ // $Copyright: Copyright (C) 2014-2015 Texas Instruments Incorporated - // http://www.ti.com/ ALL RIGHTS RESERVED $ //########################################################################### #include "F28x_Project.h" // Device Headerfile and Examples Include File #include <math.h> // Prototype statements for functions found within this file. void InitEPwm2Example(void); void InitEPwm6Example(void);
__interrupt void epwm2_isr(void); volatile int i; int amplitude; int steps; int offset; double Pi; void main(void) { i=0; amplitude = 1425; steps = 16000/50; offset = 1875; Pi = 3.1416; // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2837xS_SysCtrl.c file. InitSysCtrl(); // Step 2. Initialize GPIO: // This example function is found in the F2837xS_Gpio.c file and // illustrates how to set the GPIO to its default state. // InitGpio(); //allocate PWM1, PWM2 and PWM3 to CPU1 // CpuSysRegs.CPUSEL0.bit.EPWM1 = 0; // CpuSysRegs.CPUSEL0.bit.EPWM2 = 0; // CpuSysRegs.CPUSEL0.bit.EPWM3 = 0; // enable PWM1, PWM2 and PWM3 CpuSysRegs.PCLKCR2.bit.EPWM2=1; // For this case just init GPIO pins for ePWM1, ePWM2, ePWM3 // These functions are in the F2837xS_EPwm.c file InitEPwm2Gpio(); InitEPwm6Gpio(); // Step 3. Clear all interrupts and initialize PIE vector table: // Disable CPU interrupts DINT; // Initialize the PIE control registers to their default state. // The default state is all PIE interrupts disabled and flags // are cleared. // This function is found in the F2837xS_PieCtrl.c file. InitPieCtrl(); // Disable CPU interrupts and clear all CPU interrupt flags: IER = 0x0000; IFR = 0x0000; // Initialize the PIE vector table with pointers to the shell Interrupt // Service Routines (ISR). // This will populate the entire table, even if the interrupt // is not used in this example. This is useful for debug purposes. // The shell ISR routines are found in F2837xS_DefaultIsr.c. // This function is found in F2837xS_PieVect.c. InitPieVectTable(); // Interrupts that are used in this example are re-mapped to // ISR functions found within this file. EALLOW; // This is needed to write to EALLOW protected registers PieVectTable.EPWM2_INT = &epwm2_isr; EDIS; // This is needed to disable write to EALLOW protected registers // Step 4. Initialize the Device Peripherals: // For this example, only initialize the ePWM EALLOW; CpuSysRegs.PCLKCR0.bit.TBCLKSYNC =0; EDIS; InitEPwm2Example(); InitEPwm6Example(); EALLOW; CpuSysRegs.PCLKCR0.bit.TBCLKSYNC =1; EDIS; // Enable CPU INT3 which is connected to EPWM1-3 INT: IER |= M_INT3; // Enable EPWM INTn in the PIE: Group 3 interrupt 1-3 PieCtrlRegs.PIEIER3.bit.INTx1 = 1; PieCtrlRegs.PIEIER3.bit.INTx2 = 1; PieCtrlRegs.PIEIER3.bit.INTx3 = 1; // Enable global Interrupts and higher priority real-time debug events: EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM // Step 6. IDLE loop. Just sit and loop forever (optional): for(;;) { asm (" NOP"); } } __interrupt void epwm2_isr(void) { i++; if(i==399) { i=0; } EPwm2Regs.CMPA.bit.CMPA = 6250/2; EPwm6Regs.CMPA.bit.CMPA=amplitude*sin(i*2*Pi/steps)+offset; // Clear INT flag for this timer EPwm2Regs.ETCLR.bit.INT = 1; // Acknowledge this interrupt to receive more interrupts from group 3 PieCtrlRegs.PIEACK.all = PIEACK_GROUP3; } void InitEPwm2Example() { EPwm2Regs.TBPRD = 6250; // Set timer period EPwm2Regs.TBPHS.bit.TBPHS = 0x0000; // Phase is 0 EPwm2Regs.TBCTR = 0x0000; // Clear counter // Setup TBCLK EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up and down EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // Clock ratio to SYSCLKOUT EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV1; // Slow just to observe on the // scope // Setup compare EPwm2Regs.CMPA.bit.CMPA = 6250/2; // Set actions EPwm2Regs.AQCTLA.bit.CAU = AQ_SET; // Set PWM2A on Zero EPwm2Regs.AQCTLA.bit.CAD = AQ_CLEAR; EPwm2Regs.AQCTLB.bit.CAU = AQ_CLEAR; // Set PWM2A on Zero EPwm2Regs.AQCTLB.bit.CAD = AQ_SET; // Active Low complementary PWMs - setup the deadband EPwm2Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; EPwm2Regs.DBCTL.bit.POLSEL = DB_ACTV_LOC; EPwm2Regs.DBCTL.bit.IN_MODE = DBA_ALL; EPwm2Regs.DBRED.bit.DBRED = 0; EPwm2Regs.DBFED.bit.DBFED = 0; // Interrupt where we will modify the deadband EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_PRDZERO; // Select INT on Zero event EPwm2Regs.ETSEL.bit.INTEN = 1; // Enable INT EPwm2Regs.ETPS.bit.INTPRD = ET_1ST; // Generate INT on 1st event } void InitEPwm6Example() { EPwm6Regs.TBPRD = 6250; // Set timer period EPwm6Regs.TBPHS.bit.TBPHS = 0x0000; // Phase is 0 EPwm6Regs.TBCTR = 0x0000; // Clear counter // Setup TBCLK EPwm6Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading EPwm6Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // Clock ratio to SYSCLKOUT EPwm6Regs.TBCTL.bit.CLKDIV = TB_DIV1; // Slow so we can observe on // the scope // Setup compare // EPwm6Regs.CMPA.bit.CMPA = 3000; // Set actions EPwm6Regs.AQCTLA.bit.CAU = AQ_SET; // Set PWM3A on Zero EPwm6Regs.AQCTLA.bit.CAD = AQ_CLEAR; EPwm6Regs.AQCTLB.bit.CAU = AQ_CLEAR; // Set PWM3A on Zero EPwm6Regs.AQCTLB.bit.CAD = AQ_SET; // Active high complementary PWMs - Setup the deadband EPwm6Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; EPwm6Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; EPwm6Regs.DBCTL.bit.IN_MODE = DBA_ALL; EPwm6Regs.DBRED.bit.DBRED = 50; EPwm6Regs.DBFED.bit.DBFED = 50; // EPwm6_DB_Direction = DB_UP; }

