PWM Frequency and Duty Measurement

This example shows how to measure PWM signal frequency and duty using Simulink Desktop Real-Time™. The measured signal is connected to gate pins of two counter inputs of your data acquisition board. The first Counter Input block is configured to measure signal duty by measuring the time between rising and falling edge of the signal. The other Counter Input block is configured to measure signal period by measuring the time between two rising edges of the signal. Both the counters use internal on-board clock as the clock source. PWM frequency and duty are then computed based on these values.

Note: To run this model, you must have a supported data acquisition board connected to your computer.

Run Model in Normal Mode

This model is pre-set to be used with the National Instruments PCIe-6323 board. You can use it with any board from the PCIe-63xx series. To change the board, open both the Counter Input blocks and select your data acquisition board. You may need to register the board by clicking the Install new board button first.

Alternatively, you can also use any board from the PCI-62xx series or the PCI-60xx series. For these boards, please change the Counter base frequency from 100 MHz to 80 MHz or 20 MHz, respectively.

  1. Connect your PWM signal to both CTR 0 GATE and CTR 1 GATE input pins. To measure both PWM frequency and duty, two counter channels are required.

  2. To switch to normal mode if needed, double-click the orange button in the model.

  3. To start the real-time execution, on the Simulation tab, click Run.

Run Model in External Mode

This model is pre-set to be used with the National Instruments PCIe-6323 board. You can use it with any board from the PCIe-63xx series. To change the board, open both the Counter Input blocks and select your data acquisition board. You may need to register the board by clicking the Install new board button first.

Alternatively, you can also use any board from the PCI-62xx series or the PCI-60xx series. For these boards, please change the Counter base frequency from 100 MHz to 80 MHz or 20 MHz, respectively.

  1. Connect your PWM signal to both CTR 0 GATE and CTR 1 GATE input pins. To measure both PWM frequency and duty, two counter channels are required.

  2. To switch to external mode if needed, double-click the orange button in the model.

  3. To start the real-time execution, on the Desktop Real-Time tab, click Start. The model builds, connects to external mode, and starts.

Open the Model

warning('off','sldrt:blkgui:boardnotonlist');
open_system(fullfile(matlabroot,'toolbox','sldrt','sldrtexamples','sldrtex_pwmmeasure'));

Close Open Scopes

close_system(find_system(gcs ,'BlockType', 'Scope'));

Clean Up Model

clear
close all
bdclose all

See Also