- /***************************************************************************
- zhushi : (C) by kangear ^_^
- email : kangear@163.com
- ***************************************************************************/
- /***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
- #include <stdio.h> //sterr sscanf(存储的数据,格式控制字符串,选择性设定字符串)
- #include <unistd.h> //鸡肋
- #include <stdlib.h> //鸡肋
- #include <sys/types.h> //鸡肋
- #include <sys/stat.h> //鸡肋
- #include <sys/ioctl.h> //鸡肋
- #include <fcntl.h> //open();read();close;
- #include <linux/fs.h> //鸡肋
- #include <errno.h> //鸡肋
- #include <string.h> //鸡肋
- int main(void)
- {
- fprintf(stderr, "press Ctrl-C to stop\n"); //相当于printf;
- int fd = open("/dev/adc", 0); //文件编程中的 文件打开
- if (fd < 0) //文件打开失败
- {
- perror("open ADC device:"); //错误信息打印出来
- return 1;
- }
- for(;;) //呃……这个从功能上说是循环,但是有点不懂……!-_-!
- {
- char buffer[30]; //定义一数组
- int len = read(fd, buffer, sizeof buffer -1); //文件编程中的 文件读 成功返回长度
- if (len > 0) { //读成功
- buffer[len] = '\0'; //在末尾添加“\0”结束符
- int value = -1;
- sscanf(buffer, "%d", &value); //将得到的信息中提取整型数
- printf("ADC Value: %d\n", value); //打印
- }
- else //读失败
- {
- perror("read ADC device:"); //输出错误信息
- return 1;
- }
- usleep(500* 1000); //和sleep()功能一样不过是μ秒(相当于睡眠0.5秒)
- }
-
- close(fd); //文件编程中的 文件关闭
- }