1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <QMessageBox>
#include <stdio.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define EinAMP 1 //gpio 1
#define Last 4 //gpio 2
#define StepUp 5 // Step Up Einschalten
int fd;
// Note PCF8591 defaults to 0x48!
int asd_address = 0x48;
unsigned int val;
unsigned char writeBuf[3];
unsigned char readBuf[2];
float myfloat;
const float Schwelle0 = 1.13, Schwelle1 = 1.785, Schwelle2 = 2.5;
const float HYSTERESE =0.1;
int s0Flag = 0, s1Flag = 0, s2Flag = 0;
int flag=0;
const float VPS = 4.096 / 32768.0; // volts per step
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPalette Pal;
Pal.setColor(QPalette::Normal, QPalette::WindowText, Qt::green);
Pal.setColor(QPalette::Normal, QPalette::Window, Qt::blue);
ui->lcdNumber->setAutoFillBackground(true);
ui->lcdNumber->setPalette(Pal);
ui->lcdNumber->display("1234");
ui->label->setFont(QFont("Helvetica", 14, QFont::Bold, true));
Pal.setColor(QPalette::Normal, QPalette::WindowText, Qt::red);
ui->label->setText("abcde");
ui->label->setPalette(Pal);
if (wiringPiSetup () < 0) // WiringPi GPIO numbers
{ // ACHTUNG: QwtTest root privileges
QMessageBox msgBox;
msgBox.setText(QApplication::translate("Main", "Fehler mit WiringPiSetupGpio"));
msgBox.exec();
}
pinMode(EinAMP, OUTPUT);
pinMode(Last, OUTPUT);
pinMode(StepUp, OUTPUT);
fd = wiringPiI2CSetup(asd_address);
if (fd < 0)
{
QMessageBox msgBox;
msgBox.setText(QApplication::translate("Main", "Fehler mit wiringPiI2CSetup(Adr)"));
msgBox.exec();
}
// set config register and start conversion
// AIN0 and GND, 4.096v, 128s/s
// Refer to page 19 area of spec sheet
writeBuf[1] = 0b11000010; // 0xC2 single shot off
// bit 15 flag bit for single shot not used here
// Bits 14-12 input selection:
// 100 ANC0; 101 ANC1; 110 ANC2; 111 ANC3
// Bits 11-9 Amp gain. Default to 010 here 001 P19
// Bit 8 Operational mode of the ADS1115.
// 0 : Continuous conversion mode
// 1 : Power-down single-shot mode (default)
writeBuf[0] = 0b10000101; // bits 7-0 0x85
// Bits 7-5 data rate default to 100 for 128SPS
// Bits 4-0 comparator functions see spec sheet.
// begin conversion
val = writeBuf[0] << 8 | writeBuf[1]; // ACHTUNG: zuerst wird LOW-Byte, dann HIGH-Byte von val gesendet
wiringPiI2CWriteReg16(fd,1, (int)val);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(Zeitgeber()));
timer->start(500);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Zeitgeber()
{
char hstring [10];
// read conversion register
val= wiringPiI2CReadReg16(fd,0); // ACHTUNG: zuerst wird LOW-Byte, dann HIGH-Byte fuer val emphangen
val = ((((val & 0x00FF)<<8) | ((val & 0xFF00)>>8))); // Byte-swapping ist notwendig!
myfloat = val * VPS; // convert to voltage
sprintf (hstring,"%4.2f", myfloat);
ui->lcdNumber->display(hstring);
if(flag)
{
return;
}
if ((!s0Flag && myfloat <= Schwelle0) || (s0Flag && myfloat <= Schwelle0 - HYSTERESE))
//if (myfloat <= Schwelle0) || schwelle für 0 bis 1.13 Volt
{
s0Flag = 0;
s1Flag = 0;
s2Flag = 0;
ui->label->setText("AUS");
digitalWrite(EinAMP, LOW);
digitalWrite(Last, LOW);
digitalWrite(StepUp, LOW);
return;
}
s0Flag = 1;
if ((!s1Flag && myfloat <= Schwelle1) || (s1Flag && myfloat <= Schwelle1 - HYSTERESE))
{
s1Flag = 0;
s2Flag = 0;
ui->label->setText("1 AMP");
digitalWrite(EinAMP, HIGH);
digitalWrite(Last, HIGH);
digitalWrite(StepUp, HIGH);
return;
}
s1Flag = 1;
if((!s2Flag && myfloat <= Schwelle2) || (s2Flag && myfloat <= Schwelle2 - HYSTERESE))
{
s2Flag = 0;
ui->label->setText("4 AMP");
digitalWrite(EinAMP, LOW);
digitalWrite(Last, HIGH);
digitalWrite(StepUp, HIGH);
return;
}
s2Flag = 1;
ui->label->setText("AUS");
digitalWrite(EinAMP, LOW);
digitalWrite(Last, LOW);
digitalWrite(StepUp, LOW);
}
void MainWindow::on_pushButton_clicked() // AUS Button
{
ui->label->setText("AUS");
digitalWrite(EinAMP, LOW);
digitalWrite(Last, LOW);
digitalWrite(StepUp, LOW);
flag=1;
}
void MainWindow::on_pushButton_2_clicked() //Ein button
{
flag=0;
}
void MainWindow::on_lcdNumber_overflow()
{
}
|