-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
55 lines (47 loc) · 1.27 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
int read_ADvalue(char * device_name, float *value){
int count,ret,fd,tmp;
char buf[20];
char filename[80];
sprintf(filename,"/sys/bus/iio/devices/iio:device0/%s",device_name);
memset(buf,0,sizeof(buf));
fd =open(filename,O_RDONLY);
count = read(fd, buf, sizeof(buf));
close(fd);
if(count > 0 ){
sscanf( buf, "%d", &tmp );
tmp = tmp & 0x0FFF;
//printf("AD : %s \n", buf);
*value = ((float)tmp/4096.0)*1.8;
return 1;
}else{
printf("get AD error\n");
return -1;
}
}
int main(int argc ,char ** argv){
float ad_value;
int fd = open("./dat.txt",O_RDWR|O_CREAT|O_TRUNC);
char buf[20];
if(fd<0)return -1;
else{
lseek(fd,0,SEEK_SET);
}
while(1){
/**/
//printf("<-----Start Scaning AD chanel------>\n");
read_ADvalue("in_voltage0_raw",&ad_value);
sprintf(buf,"%f\n",ad_value);
write(fd,buf,strlen(buf));
//usleep(2);
}
return 0;
}