Target
Tiva TM4C1294NCPDT Launchpad (EK-TM4C1294XL)
Note - XTAL (Y1) is 25MHz
Problem
The code below is designed to generate a 25 kHz PWM signal (T=40us) but is observed with a period of 31.1us.
Code & Scope Capture
int main(void) { //Clock Init SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_OSC), 25000000); /* 25MHz external XTAL (Y1) */ //******************************************************************************************************************************// // GPIO MODULE INIT // //******************************************************************************************************************************// SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); /* enable GPIO Port F */ GPIOPinConfigure(GPIO_PF2_M0PWM2); /* set pin-muxes */ GPIOPinConfigure(GPIO_PF3_M0PWM3); GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2); /* set gpio-modules to push-pull output */ GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); //******************************************************************************************************************************// // PWM MODULE INIT // //******************************************************************************************************************************// //Init PWM Module SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); /* Enable the PWM0 peripheral */ while(!SysCtlPeripheralReady(SYSCTL_PERIPH_PWM0)); /* Wait for the PWM0 module to be ready */ PWMGenConfigure(PWM0_BASE, PWM_GEN_1, /* Configure the PWM generator for count down with */ PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); /* with immediate updates to the parameters */ //Init PWM0 Gen1 PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, 500); /* 100% (40us) Tpwm_clk=80ns, 80n*500 = 40us (25kHz) */ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, 250); /* 50% (20us) */ PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, 125); /* 25% (10us) */ //Start PWM Module PWMGenEnable(PWM0_BASE, PWM_GEN_1); /* Start the timers in Generator Module 1 */ PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT | PWM_OUT_3_BIT), true); /* Enable the outputs */ //spin for(;;) { _nop(); } }
See below for measured results, Ch1(Red): PWM3, Ch2(Yellow): PWM2 -
Question
Why do I see a period of 31us, not 40us?
Calculations Used
Fclk = 25MHz, Fclk_pwm = 12.5 MHz
Tclk_pwm = 80ns
N_per_pwm = 500
T_per_pwm = 40us (but 31.1us observed?)