This technical tutorial demonstrates how to dynamically modulate the duty cycle of a Pulse Width Modulation (PWM) signal by altering the compare one value of a high-resolution timer (Timer B). The demonstration uses a model-based design environment where a sine wave generator provides the input for the modulation. A key aspect of the implementation is using a high-frequency, interrupt-driven repetition block that updates the timer's compare register every 16 PWM cycles. The process covers the logic setup, data type conversion, code generation, and a final check of the resulting modulated PWM signal on an oscilloscope.
Modulating a PWM Signal with a High-Resolution Timer
0:00:08 Objective: Modulating PWM Duty Cycle: The video's goal is to show how to change the compare one value of a high-resolution timer (Timer B) to dynamically alter the duty cycle of a PWM signal.
0:00:35 PWM Configuration: The timer's period is set to 18,432. The compare one value dictates the point within this period where the output pin state changes, thereby setting the duty cycle.
0:00:57 Modulation Using a Sine Wave: A sine wave is used as the input source to generate the changing values for the compare register. This creates a smooth, cyclical modulation of the PWM duty cycle.
01:40 Data Type Conversion: To ensure compatibility with the timer's register, the floating-point output of the sine wave generator is converted into a 16-bit integer (int16).
02:45 High-Frequency Updates via Interrupts: The duty cycle is not updated in the main program loop. Instead, a "repetition" block is used, which functions as an interrupt. This interrupt is configured to trigger every 16 cycles of the PWM period, allowing for rapid and precise updates to the duty cycle.
03:24 Code Generation and Deployment: The creator builds the model, which automatically generates the necessary code and deploys it to the target hardware. The generated code now includes the new logic to set the compare unit one for Timer B.
04:10 Verifying the Output: The final step is to use an oscilloscope to visualize the output and confirm that the PWM signal's duty cycle is being successfully modulated in a sinusoidal pattern as intended.