SPRUJ28E November 2021 – September 2024 AM68 , AM68A , TDA4AL-Q1 , TDA4VE-Q1 , TDA4VL-Q1
The mechanics of how to position an edge precisely in time has been demonstrated using the resources of the standard (EPWM_CMPA) and MEP (HRPWM_CMPAHR) registers. In a practical application, however, it is necessary to seamlessly provide the CPU a mapping function from a per-unit (fractional) duty cycle to a final integer (non-fractional) representation that is written to the [CMPA:CMPAHR] register combination.
To do this, first examine the scaling or mapping steps involved. It is common in control software to express duty cycle in a per-unit or percentage basis. This has the advantage of performing all needed math calculations without concern for the final absolute duty cycle, expressed in clock counts or high time in ns. Furthermore, it makes the code more transportable across multiple converter types running different PWM frequencies.
To implement the mapping scheme, a two-step scaling procedure is required.
Assumptions for this example:
System clock, FICLK | = | 10 ns (100 MHz) |
PWM frequency | = | 1.25 MHz (1/800 ns) |
Required PWM duty cycle, PWMDuty | = | 0.405 (40.5%) |
PWM period in terms of coarse steps, PWMperiod (800 ns/10 ns) | = | 80 |
Number of MEP steps per coarse step at 180 ps (10 ns/180 ps), MEP_SF | = | 55 |
Value to keep CMPAHR within the range of 1-255 and fractional rounding constant (default value) | = | 180h |
Step 1: Percentage Integer Duty value conversion for EPWM_CMPA register
EPWM_CMPA register value | = | int(PWMDuty × PWMperiod); int means integer part |
= | int(0.405 × 80) | |
= | int(32.4) | |
EPWM_CMPA register value | = | 32 (20h) |
Step 2: Fractional value conversion for HRPWM_CMPAHR register
HRPWM_CMPAHR register value | = | (frac(PWMDuty × PWMperiod) × MEP_SF) << 8) + 180h; frac means fractional part |
= | (frac(32.4) × 55 <<8) + 180h; Shift is to move the value as CMPAHR high byte | |
= | ((0.4 × 55) <<8) + 180h | |
= | (22 <<8) + 180h | |
= | 22 × 256 + 180h; Shifting left by 8 is the same multiplying by 256. | |
= | 5632 + 180h | |
= | 1600h + 180h | |
HRPWM_CMPAHR value | = | 1780h; HRPWM_CMPAHR value = 1700h, lower 8 bits will be ignored by hardware. |