zoukankan      html  css  js  c++  java
  • adc 测量子系统

            #include <stdio.h>
            #include <stdlib.h>
            #include <fcntl.h>
            #include <unistd.h>
            #include <math.h> 

            char iiotype[16][32] = {
                    'in_voltage1_raw',
                    'in_voltage2_raw',
            };
                  

          
           
            // return = 0: voltage value from AIN1, unit: mV
            // < 0: failed
            int read_AIN1(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[1]);
                    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;

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

                    // read AIN2
                    ret = read_AIN2(&fvalue);
                    if(ret < 0)
                    {
                            return ret;
                    }
        
            }

  • 相关阅读:
    Go反射原理
    并发控制--context篇
    Go并发控制--WaitGroup篇
    Go依赖管理--module
    正睿培训 8.4 学习笔记
    bitset
    7.18 学习笔记
    7.17 学习笔记
    P6835 [Cnoi2020]线形生物
    UVA11300 Spreading the Wealth 思维题
  • 原文地址:https://www.cnblogs.com/zym0805/p/5984337.html
Copyright © 2011-2022 走看看