-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptemp2.c
124 lines (105 loc) · 2.49 KB
/
ptemp2.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define SENSOR_ADDR 0x39
int i2c_open(int *fd, char *filename, int addr)
{
//open I2C device file
if((*fd=open(filename, O_RDWR))<0)
{
fprintf(stderr, "i2c_open: Faild to open i2c port\n");
return -1;
}
//set device address
if(ioctl(*fd, I2C_SLAVE, addr)<0)
{
fprintf(stderr, "i2c_open: Failed to talk to slave\n");
return -2;
}
return 0;
}
int i2c_close(char fd)
{
//close I2C device file
if((close(fd))<0)
{
fprintf(stderr, "i2c_close: Faild to close i2c port\n");
return -1;
}
return 0;
}
int i2c_write(int fd, unsigned char regaddr, unsigned char data)
{
unsigned char buf[2];
buf[0]=regaddr;
buf[1]=data;
if((write(fd,buf,2))!=2){
fprintf(stderr, "i2c_write: Error writing to i2c slave\n");
return -1;
}
return 0;
}
int i2c_read(int fd, unsigned char regaddr)
{
unsigned char buf[1];
buf[0]=regaddr;
if((write(fd,buf,1))!=1){ //note: write an address once
fprintf(stderr, "i2c_read: Error writing to i2c slave\n");
return -1;
}
if(read(fd,buf,1)!=1){
fprintf(stderr, "i2c_read: Error reading from i2c slave\n");
return -2;
}
return buf[0];
}
int main(int argc, char **argv)
{
char *i2cFileName="/dev/i2c-0"; //Device address
int i2c_fd_sensor; //I2C device file descriptor
int i2c_addr_sensor=SENSOR_ADDR; //I2C device address
int ret;
unsigned char th, tl;
double temperature;
//Open I2C device: Temperature sensor STTS751
ret=i2c_open(&i2c_fd_sensor, i2cFileName, i2c_addr_sensor);
if(ret<0){
fprintf(stderr, "main: Failed i2c_open\n");
exit(EXIT_FAILURE);
}
//Write configuration to STTS751
ret=i2c_write(i2c_fd_sensor, 0x03, 0x0C);
if(ret<0){
fprintf(stderr, "main: Failed i2c_write\n");
exit(EXIT_FAILURE);
}
//Read address 00 from STTS751
ret=i2c_read(i2c_fd_sensor, 0x00);
if(ret<0){
fprintf(stderr, "main: Failed i2c_read (address 00)\n");
exit(EXIT_FAILURE);
}
th=(unsigned char)ret;
//Read address 02 from STTS751
ret=i2c_read(i2c_fd_sensor, 0x02);
if(ret<0){
fprintf(stderr, "main: Failed i2c_read (address 02)\n");
exit(EXIT_FAILURE);
}
tl=(unsigned char)ret;
//Close device: STTS751
ret=i2c_close(i2c_fd_sensor);
if(ret<0){
fprintf(stderr, "main: Failed i2c_close\n");
exit(EXIT_FAILURE);
}
//Calculate temperature
temperature=(double)th+((double)(tl>>4)*0.0625);
printf("%.1lf", temperature);
return 0;
}