-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathads1115_rpi.c
109 lines (93 loc) · 1.98 KB
/
ads1115_rpi.c
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
/*
* ads1115_rpi.c
*
* Created on: 14 de fev de 2019
* Author: giobauermeister
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "ads1115_rpi.h"
//char *bus = "/dev/i2c-1";
int i2cFile;
//int deviceAddr = 0x48;
unsigned char writeBuf[3] = {0};
int openI2CBus(char *bus)
{
if ((i2cFile = open(bus, O_RDWR)) < 0)
{
printf("Failed to open the bus. \n");
return -1;
} else {
printf("Bus open \n");
return 1;
}
}
int setI2CSlave(unsigned char deviceAddr)
{
if(ioctl(i2cFile, I2C_SLAVE, deviceAddr) < 0)
{
printf("Failed to set I2C_SLAVE at address: 0x%x. \n", deviceAddr);
return -1;
} else {
printf("I2C_SLAVE set at address: 0x%x \n", deviceAddr);
return 1;
}
}
float readVoltage(int channel)
{
unsigned char readBuf[2] = {0};
unsigned int analogVal;
float voltage;
unsigned int config = 0;
config = CONFIG_REG_OS_SINGLE |
CONFIG_REG_PGA_4_096V |
CONFIG_REG_MODE_SINGLE |
CONFIG_REG_DR_128SPS |
CONFIG_REG_CMODE_TRAD |
CONFIG_REG_CPOL_ACTIV_LOW |
CONFIG_REG_CLATCH_NONLATCH |
CONFIG_REG_CQUE_NONE;
void configDevice(unsigned int config)
{
writeBuf[0] = 0x01;
writeBuf[1] = config >> 8;
writeBuf[2] = config & 0xFF;
write(i2cFile, writeBuf, 3);
}
switch (channel) {
case 0:
config |= CONFIG_REG_MUX_CHAN_0;
break;
case 1:
config |= CONFIG_REG_MUX_CHAN_1;
break;
case 2:
config |= CONFIG_REG_MUX_CHAN_2;
break;
case 3:
config |= CONFIG_REG_MUX_CHAN_3;
break;
default:
printf("Give a channel between 0-3\n");
}
configDevice(config);
while (readBuf[0] >> 7 != 1) {
read(i2cFile, readBuf, 2);
}
writeBuf[0] = 0x00;
write(i2cFile, writeBuf, 1);
if(read(i2cFile, readBuf, 2) != 2) // read data and check error
{
printf("Error : Input/Output Error \n");
}
else
{
analogVal = readBuf[0] << 8 | readBuf[1];
voltage = (float)analogVal*4.096/32767.0;
}
return voltage;
}