Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Time from HIGH to LOW



  • Hey everyone,

    I have a project with a LM555 timer and need to calculate how much time it takes for an input to go from LOW to HIGH and then to LOW again.

    Basically, I want to know the period of a square wave generated by the LM555 timer.

    I did it with Arduino using the function Pulseln. But I need to do this using a Beaglebone and programming in C++ (Using QtCreator).

    I create this code but it is not working:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QElapsedTimer>
    #include "SimpleGPIO.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    
           QElapsedTimer tempo;
           qint64 nanoSeg = 0;
           QString seg;
    
           unsigned int PINO = 60;   // GPIO1_28 = (1x32) + 28 = 60
           gpio_export(PINO);    // The LED is in pin 60
           gpio_set_dir(PINO, INPUT_PIN);   // The LED is an INPUT
    
         //  unsigned int valor = LOW;
    
    
    
           if(gpio_get_value(PINO)==HIGH){
    
               tempo.start();
    
           }
    
           while(gpio_get_value(PINO)==HIGH){
                ui->textEdit_2->setText("HIGH");
           }
    
           while(gpio_get_value(PINO)==LOW){
    
           }
    
           nanoSeg = tempo.nsecsElapsed();
    
           seg = QString::number(nanoSeg);  // Converte o inteiro 'nanoSeg' em string 'seg'
           ui->textEdit->setText(seg);
    
    }
    

    It compiles alright and when I run it I don't get an error messages, it just crashes.

    I have already checked and the libraries are OK. The problem I think is with the while functions. Do you guys have any ideia of what might be the problem?

    Thank you in advance.



  • Hi! You start with the following:

    
           if(gpio_get_value(PINO)==HIGH){
    
               tempo.start();
    
           }
    

    So with some bad luck (everytime you come here and gpio_get_value(PIN0) is low) your timer doesn't get started. And in that case, nsecsElapsed's behaviour is undefined.


Log in to reply