Hi all, the HPWM command is quite useful for reconstructing analog control signals. It does have some limitations. The command: HPWM 1, Freq, Data takes 240 us to execute at 20 MHz. This limit puts an upper rate of 4.166 kHz sampling rate. For many applications this may be fine, however, for high-speed applications it is not. One way of getting around this is to call the routine one time, and then write to the PWM data register directly. As shown below.
Outside the main loop: HPWM 1, Freq, Data
Inside the main loop: CCPR1L = Data
This reduces the execution time to one instruction cycle or .2 us at 20 MHz. The only drawback to this approach is the GCB leaves the PWM with 7 bits of resolution (127), however it is still better than the 100 limit by GCB.
Cheers, Ed.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all, the HPWM command is quite useful for reconstructing analog control signals. It does have some limitations. The command: HPWM 1, Freq, Data takes 240 us to execute at 20 MHz. This limit puts an upper rate of 4.166 kHz sampling rate. For many applications this may be fine, however, for high-speed applications it is not. One way of getting around this is to call the routine one time, and then write to the PWM data register directly. As shown below.
Outside the main loop: HPWM 1, Freq, Data
Inside the main loop: CCPR1L = Data
This reduces the execution time to one instruction cycle or .2 us at 20 MHz. The only drawback to this approach is the GCB leaves the PWM with 7 bits of resolution (127), however it is still better than the 100 limit by GCB.
Cheers, Ed.