Jump to content
  • 0

Reading PWM signal


Abdulrahmann

Question

I have USB-ctr08 and i want to use c++ code to read the duty cycle of the signal 

Here is a sample of the code looking for your support. 

The problem is the raisingedge1 and raisingedge2 and failingedge are always the same number 

Im using the 8 counter channel only. 


#define _AFXDLL
#define TIME_INTERVAL 0.01; // Time in seconds for which the count is measured
#include "stdafx.h"

/* Include files */
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <thread>
#include "cbw.h"



void readPWMChannels(int boardNum) {
    for (int  counterChannel = 0; counterChannel < 8; ++counterChannel) {
        int boardNum = 0;           // Board number
        USHORT risingEdge1 = 0, risingEdge2 = 0;  // Store counts for two rising edges
        USHORT fallingEdge = 0;                   // Store count for falling edge
        double timeBase = 0.0;       // Timer tick in seconds
        int FreqencyInMHZ = 0;
       // cbCLoad(boardNum, counterChannel, 0);
       
        // Configure the counter to detect edges (rising/falling)
        cbCConfigScan(boardNum, counterChannel, TOTALIZE, CTR_DEBOUNCE_NONE, CTR_TRIGGER_AFTER_STABLE, CTR_RISING_EDGE, CTR_TICK20833PT3ns, 0);

        // 1. Detect the first rising edge
        cbCIn(boardNum, counterChannel, &risingEdge1);

        // 2. Detect the falling edge (high time ends)
        cbCConfigScan(boardNum, counterChannel, TOTALIZE, CTR_DEBOUNCE_NONE, CTR_TRIGGER_AFTER_STABLE, CTR_FALLING_EDGE, CTR_TICK20833PT3ns, 0);
        cbCIn(boardNum, counterChannel, &fallingEdge);

        // 3. Detect the second rising edge (end of period)
        cbCConfigScan(boardNum, counterChannel, TOTALIZE, CTR_DEBOUNCE_NONE, CTR_TRIGGER_AFTER_STABLE, CTR_RISING_EDGE, CTR_TICK20833PT3ns,0);
        cbCIn(boardNum, counterChannel, &risingEdge2);

        // Get the timer tick resolution (in seconds)
        cbGetConfig(BOARDINFO, boardNum, 0, BICLOCK,&FreqencyInMHZ );
        timeBase= FreqencyInMHZ / 1e6;
        // Calculate pulse width (high time) in seconds
        double pulseWidth = (fallingEdge - risingEdge1) * timeBase;

        // Calculate the period in seconds
        double period = (risingEdge2 - risingEdge1) * timeBase;

        // Calculate the duty cycle as a percentage
        double dutyCycle = (pulseWidth / period) * 100.0;

        // Display the result
        std::cout << "Pulse Width: " << pulseWidth << " seconds" << std::endl;
        std::cout << "Period: " << period << " seconds" << std::endl;
        std::cout << "Duty Cycle: " << dutyCycle << " %" << std::endl;

       
    }
   

   
}

int main() {
    int boardNum = 0; // Use the appropriate board number
    while (true) {
        readPWMChannels(boardNum);
        std::this_thread::sleep_for(std::chrono::microseconds(10));
    }

    return 0;
}

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...