-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCF8591.cpp
More file actions
69 lines (55 loc) · 1.86 KB
/
PCF8591.cpp
File metadata and controls
69 lines (55 loc) · 1.86 KB
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
/**
* @brief methods to read analog values using PCF8591 chip
* @author Alvaro Llamas alvarollamasg@gmail.com
* @date 2016/08/04
* @copyright Apache License
* @verbatim
* 8-bit A/D and D/A converter
* http://www.nxp.com/documents/data_sheet/PCF8591.pdf
* @endverbatim
*/
#include <stdio.h>
#include <wiringPi.h>
//#include <wiringPiI2C.h>
#include <pcf8591.h>
#include "PCF8591.hpp"
int PCF8591::fd;
unsigned PCF8591::rawValue[4];
//--------------------------------------------------------------------------------
bool PCF8591::init () {
// http://stackoverflow.com/questions/28750703/raspberry-pi-and-i2c-in-c-with-wiringpi-for-pcf8591
/*if ((fd = wiringPiI2CSetup(0x48)) < 0) {
puts ("wiringPiI2CSetup failed");
return false;
}*/
if (pcf8591Setup(PINBASE,0x48) < 0) {
puts ("pcf8591Setup failed");
return false;
}
for (unsigned i=0; i<4; i++)
rawValue[i] = 0;
return true;
}
//--------------------------------------------------------------------------------
int PCF8591::readAnalogInput (unsigned AD_channel) {
if (AD_channel>3)
return -1;
// http://stackoverflow.com/questions/28750703/raspberry-pi-and-i2c-in-c-with-wiringpi-for-pcf8591
// Previous value
//rawValue[AD_channel] = wiringPiI2CReadReg8(fd,0x40|AD_channel);
// force new A/D conversion
//rawValue[AD_channel] = wiringPiI2CReadReg8(fd,0x40|AD_channel);
rawValue[AD_channel] = analogRead(PINBASE + AD_channel);
return rawValue[AD_channel];
}
//--------------------------------------------------------------------------------
int PCF8591::getLastAnalogValue (unsigned AD_channel) {
if (AD_channel>3)
return -1;
return rawValue[AD_channel];
}
//--------------------------------------------------------------------------------
float PCF8591::rawToVolts (unsigned rawValue) {
return rawValue*3.3/255;
}
//--------------------------------------------------------------------------------