Hi, Ari,
I have developed an example which can demo the capture function of eFlexPWM module. I post it here.
On the demo code, I generate PWM signals with SM0/SM1/SM2 of PWMA module, I connect the PWMA_0A signal to the PWMA_1X pin so that I can check if the captured value is correct or not.
software configuration:
1)enable all module clock
void CLOCK_init(void)
{
SIM_PCE3|=0x00FF; //enable PWMA and PWMB all channels
SIM_PCE0|=0xFF7F; //enable all Timer and GPIO A/B/C/D/E/G/F
SIM_PCE2|=0x180; //enable SAR ADC clock and Cyclic ADC clock
return;
}
2)I configure the PWMA_1X pin GPIO_G9 as PWMA_1X with the code:
//GPIOG9 PWMB_1X PWMA_1X TA3 XB_OUT11, pin 30 on J503 on TWR-8400 board
//connect the measured signal to PWMA_1A pin GPIOG9, test the duty cycle of the signal
void pinPWMA_1X(void)
{
//let PWMA_1X signal output
GPIOG_PER|=0x200;
SIM_GPSGH&=~(0x30);
SIM_GPSGH|=0x01<<2; //set G9 bits to be 01 in binary:01 Function = PWMA_1X;
PWMA_OUTEN&=~(0x0F); //disable all PWMA_X pins
}
configure the GPIO_G9 as PWMA_1X: clear the corresponding bit in PWMA_OUTEN, the PWMA_1X will be a capture signal input, set the orresponding bit in PWMA_OUTEN, the PWMA_1X will be an PWM signal output.
3)configure capture function:
void captureFuncPWMA_1A(void)
{
//bit setting:
//EDGCNTX_EN=1 Edge counter enabled
//INP_SELX=0 Raw PWM_X input signal selected as source.
//EDGX1=01 Capture falling edges
//EDGX0=10 Capture rising edges
//ONESHOTX=0 Free running mode is selected.
PWMA_SM1CAPTCTRLX&=0x00;
PWMA_SM1CAPTCTRLX=0x98;
PWMA_SM1CAPTCTRLX|=0x01; //start capture function
PWMA_OUTEN&=~(0x0F); //disable all PWMA_X pins
}
4)interrupt mode configuration:
void PWM_ISR_SETTING()
{
//interrupt priority setting
INTC_IPR8|=0xC000; //set PWMA_CAP interrupt source priority
//enable the PWM_SM0 reload interrupt
PWMA_SM1INTEN|=0x80; //edge1(falling edge) trigger interrupt
return;
}
5)ISR function of capture:
#pragma interrupt on
void captureISR(void)
{
static unsigned int i=0;
varEdge0[i]=PWMA_SM1CVAL0;
varEdge1[i]=PWMA_SM1CVAL1;
varDiff[i]=varEdge1-varEdge0;
varsm0cnt[i]=PWMA_SM0CNT;
varsm1cnt[i]=PWMA_SM1CNT;
i++;
if(i>=50)
{
asm(debughlt);
}
//clear the interrupt flag
PWMA_SM1STS|=0xC0; //clear the CFX1 and CFX0 bits
asm(nop);
GPIOA_DR^=0x100; //toggle the A8 bit
}
Hardware connection:
I developed the project on CodeWarrior for mcu ver10.6 and TWR-8400 board. Connect the PWMA_0A(pin 40 of primary A connector) to GPIO_G9(pin 30 of J503), it is okay. you'd better connect a 2~5K ohm serial resistor when you test in case you configure both pin as output by mistake.
BR
Xiangjun Rong