zoukankan      html  css  js  c++  java
  • iio adc转换应用编写

            #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! ', 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! ', 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! ', 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! ', 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! ', 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 ');

                    // read AIN1
                    ret = read_AIN0(&fvalue);
                    if(ret < 0)
                    {
                            return ret;
                    }
                    printf('AIN1 = %.2f mV ', fvalue);

                    // read AIN2
                    ret = read_AIN2(&fvalue);
                    if(ret < 0)
                    {
                            return ret;
                    }
                    printf('AIN2 = %.2f mV ', fvalue);

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

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

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

     

  • 相关阅读:
    Springboot 之 自定义配置文件及读取配置文件
    SQLSERVER系统视图 sql server系统表详细说明
    MySQL Workbench建表时 PK NN UQ BIN UN ZF AI 的含义
    使用Ecplise git commit时出现"There are no stages files"
    maven添加sqlserver的jdbc驱动包
    java将XML文档转换成json格式数据
    java将XML文档转换成json格式数据
    cannot be resolved. It is indirectly referenced from required .class files
    org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2012-12-12 12:01:01': not a valid representation (error: Can not parse date "2012-12-
    @Autowired注解和静态方法 NoClassDefFoundError could not initialize class 静态类
  • 原文地址:https://www.cnblogs.com/zym0805/p/5235050.html
Copyright © 2011-2022 走看看