Hi, i have created a test code for ADC library in Qt. I have wrapped the Toradex ADC library and wrote the code for mapping temperature 0 to 70°C to 0 to 3.3V. The code is compiled and the results are as expected. But after minute or two, the Test GUI freezes and any voltage variation on the ADC pin is not updated on the GUI
Additionally please find the test project folder here:
https://drive.google.com/open?id=15v69oKXGbJogWiWy7RabPmcy4aEMhdDu
Any help is greatly appreciated
/**************ADCLib.h*************/
#include"adc.h"
class ADCLib
{
public:
ADCLib();
virtual ~ADCLib();
int getADCValue(int adc_channel_number);
float getTemperatureInCelsius(int adc_channel_number);
float getTemperatureInFahrenheit(int adc_channel_number);
private:
HANDLE adcHandle; // Handle to ADC library
DWORD bit; // ADC bits variable
DWORD sample; // ADC samples variable
};
/**************ADCLib.cpp*************/
#include"stdafx.h"
#include"adc.h"
#include"ADCLib.h"
ADCLib::ADCLib()
{
adcHandle = NULL;
bit = 8; // adc bits
sample = 12; // adc samples
}
ADCLib::~ADCLib()
{
Adc_Close(adcHandle); // Close ADC channel
Adc_Deinit(adcHandle); // Deinit ADC channel
}
/*----------------------------------------------------------------------------
* getADCValue(int adc_channel_number)
*
* adc_channel_number : adc 0 to 3 according to carrier board Datasheet
*
* Return value : raw data from adc
*
* Description : takes adc channel number and returns its raw value after reading
*----------------------------------------------------------------------------*/
int ADCLib::getADCValue(int adc_channel_number)
{
DWORD adcData = 0; // Variable to get adcData from adc pin
if(adc_channel_number == 0)
adcHandle = Adc_Init(L"ADC1"); // Initialize adc0, without affecting hardware registers
if(adc_channel_number == 1)
adcHandle = Adc_Init(L"ADC2"); // Initialize adc1, without affecting hardware registers
if(adc_channel_number == 2)
adcHandle = Adc_Init(L"ADC3"); // Initialize adc2, without affecting hardware registers
if(adc_channel_number == 3)
adcHandle = Adc_Init(L"ADC4"); // Initialize adc3, without affecting hardware registers
Adc_SetConfigInt(adcHandle, L"BitResolution", bit, StoreVolatile); // Set a bit resolution configuration parameter.
Adc_SetConfigInt(adcHandle, L"AvgSamples", sample, StoreVolatile); // Set a average sample configuration parameter.
Adc_Open(adcHandle);
Adc_Read(adcHandle, &adcData, sizeof(DWORD)); // Local Function Read ADC channel in loop
return adcData;
}
/*----------------------------------------------------------------------------
* getTemperatureInCelsius(int adc_channel_number)
*
* adc_channel_number : adc 0 to 3 according to carrier board Datasheet
*
* Return value : temperature in degree celsius upto 70°C
*
* Description : reads adc channel and returns temperature in degree celsius
*----------------------------------------------------------------------------*/
float ADCLib::getTemperatureInCelsius(int adc_channel_number)
{
float temperatureIn°C,adcValue;
adcValue = getADCValue(adc_channel_number);
temperatureIn°C = (adcValue/3296480)*70;
return temperatureIn°C;
}
/**************adc_lib_test*************/
#ifndef ADC_LIB_TEST_H
#define ADC_LIB_TEST_H
#include <QtGui/QMainWindow>
#include "ui_adc_lib_test.h"
#include "ADCLib.h"
class adc_lib_test : public QMainWindow
{
Q_OBJECT
public:
adc_lib_test(QWidget *parent = 0, Qt::WFlags flags = 0);
~adc_lib_test();
ADCLib ADC;
private:
Ui::adc_lib_testClass ui;
public slots:
void update_labels(); // update labels at 500ms interval
};
#endif // ADC_LIB_TEST_H
/************adc_lib_test.cpp************/
#include "stdafx.h"
#include "adc_lib_test.h"
adc_lib_test::adc_lib_test(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), SLOT(update_labels())); // for instant label updating
timer->start(500);
}
adc_lib_test::~adc_lib_test()
{
}
void adc_lib_test::update_labels()
{
double volts = ADC.getADCValue(0)/ 1000000.0;
ui.volts->setNum(volts);
ui.adc_raw->setNum(ADC.getADCValue(0));
ui.degree_c->setNum(ADC.getTemperatureInCelsius(0));
}
/*******stdafx.h*******/
#include <QtGui>
/*******stdafx.cpp*******/
#include "stdafx.h"
/*******main.cpp*******/
#include "stdafx.h"
#include "adc_lib_test.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
adc_lib_test w;
w.show();
return a.exec();
}