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;
            }

     

  • 相关阅读:
    fiddler 抓取 安卓模拟器 https包
    heidiSQL使用简介
    weblogic重启脚本
    svn命令在linux下的使用
    LVS之NAT和DR服务脚本
    LVS之NAT模型、DR模型配置和持久连接
    apache ab压力测试报错apr_socket_recv
    LVS负载均衡模型及算法概述
    Orcale11g单机安装与卸载
    IPC相关的命令
  • 原文地址:https://www.cnblogs.com/zym0805/p/5235050.html
Copyright © 2011-2022 走看看