博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iio adc转换应用编写
阅读量:5328 次
发布时间:2019-06-14

本文共 3408 字,大约阅读时间需要 11 分钟。

        #include <stdio.h>

        #include <stdlib.h>
        #include <fcntl.h>
        #include <unistd.h>
        #include <math.h> 
        char iiotype[16][32] = {
                ''in_voltage0_raw'',
                'in_voltage1_raw',
                'in_voltage2_raw',
                'in_voltage3_raw',
                'in_voltage4_raw',
                'in_voltage5_raw',
                'in_voltage6_raw',
                'in_voltage7_raw'
    };
              
        // return = 0: voltage value from AIN0, unit: mV
        // < 0: failed
        int read_AIN0(float *fvoltage)
        {
                int value, ret = 0;
                char filename[80];
                FILE *fp;
                char buf[20];
                sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[0]);
                fp = fopen(filename, 'rt' );
                if( fp==NULL )
                {
                        printf('open %s fail!\n', filename);
                        *fvoltage = 0.0;
                        ret = -1;
                        return ret;
                }
                fread( buf, 1, sizeof(buf), fp );
                fclose(fp);
        
                // convert to integer
                sscanf( buf, '%d', &value );
                *fvoltage = 0.8 * value;
                return ret;
        }
        // return = 0: voltage value from AIN2, unit: mV
        // < 0: failed
        int read_AIN2(float *fvoltage)
        {
                int value, ret = 0;
                char filename[80];
                FILE *fp;
                char buf[20];
                sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[2]);
                fp = fopen(filename, 'rt' );
                if( fp==NULL )
                {
                        printf('open %s fail!\n', filename);
                        *fvoltage = 0.0;
                        ret = -1;
                        return ret;
                }
                fread( buf, 1, sizeof(buf), fp );
                fclose(fp);
                // convert to integer
                sscanf( buf, '%d', &value );
                *fvoltage = 0.8 * value;
                return ret;
        }

   // return = 0: voltage value from AIN3, unit: mV

        // < 0: failed
        int read_AIN3(float *fvoltage)
        {
                int value, ret = 0;
                char filename[80];
                FILE *fp;
                char buf[20];
                sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[3]);
                fp = fopen(filename, 'rt' );
                if( fp==NULL )
                {
                        printf('open %s fail!\n', filename);
                        *fvoltage = 0.0;
                        ret = -1;
                        return ret;
                }
                fread( buf, 1, sizeof(buf), fp );
                fclose(fp);
                // convert to integer
                sscanf( buf, '%d', &value );
                *fvoltage = 0.8 * value;
                return ret;
        }

 

        // return = 0: voltage value from AIN4, unit: mV

        // < 0: failed
        int read_AIN4(float *fvoltage)
        {
                int value, ret = 0;
                char filename[80];
                FILE *fp;
                char buf[20];
                sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[4]);
                fp = fopen(filename, 'rt' );
                if( fp==NULL )
                {
                        printf('open %s fail!\n', filename);
                        *fvoltage = 0.0;
                        ret = -1;
                        return ret;
                }
                fread( buf, 1, sizeof(buf), fp );
                fclose(fp);
                // convert to integer
                sscanf( buf, '%d', &value );
                *fvoltage = 0.8 * value;
                return ret;
        }

        // return = 0: voltage value from AIN5, unit: mV

        // < 0: failed
        int read_AIN5(float *fvoltage)
        {
                int value, ret = 0;
                char filename[80];
                FILE *fp;
                char buf[20];
                sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[5]);
                fp = fopen(filename, 'rt' );
                if( fp==NULL )
                {
                        printf('open %s fail!\n', filename);
                        *fvoltage = 0.0;
                        ret = -1;
                        return ret;
                }
                fread( buf, 1, sizeof(buf), fp );
                fclose(fp);
                // convert to integer
                sscanf( buf, '%d', &value );
                *fvoltage = 0.8 * value;
                return ret;
        }
      
        int main(int argc, char** argv)
        {
                int ret = 0;
                float fvalue;
                printf('A5 IIO AD test v2.0\n');
                // read AIN1
                ret = read_AIN0(&fvalue);
                if(ret < 0)
                {
                        return ret;
                }
                printf('AIN1 = %.2f mV\n', fvalue);
                // read AIN2
                ret = read_AIN2(&fvalue);
                if(ret < 0)
                {
                        return ret;
                }
                printf('AIN2 = %.2f mV\n', fvalue);

 

                // read AIN3

                ret = read_AIN3(&fvalue);
                if(ret < 0)
                {
                        return ret;
                }
                printf('AIN3 = %.2f mV\n', fvalue);

 

                // read AIN4

                ret = read_AIN4(&fvalue);
                if(ret < 0)
                {
                        return ret;
                }
                printf('AIN4 = %.2f mV\n', fvalue);

 

                 // read AIN5

                ret = read_AIN5(&fvalue);
                if(ret < 0)
                {
                        return ret;
                }
                printf('AIN5 = %.2f mV\n', fvalue);
              
                return ret;
        }
 

转载于:https://www.cnblogs.com/zym0805/p/5235050.html

你可能感兴趣的文章
随手练——HDU 5015 矩阵快速幂
查看>>
Maven之setting.xml配置文件详解
查看>>
SDK目录结构
查看>>
malloc() & free()
查看>>
HDU 2063 过山车
查看>>
高精度1--加法
查看>>
String比较
查看>>
Django之Models
查看>>
CSS 透明度级别 及 背景透明
查看>>
Linux 的 date 日期的使用
查看>>
PHP zip压缩文件及解压
查看>>
SOAP web service用AFNetWorking实现请求
查看>>
Java变量类型,实例变量 与局部变量 静态变量
查看>>
mysql操作命令梳理(4)-中文乱码问题
查看>>
Python环境搭建(安装、验证与卸载)
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
关于这次软件以及pda终端的培训
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>