⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc_read.c

📁 _计算实用教程Visual C++6.0实用教程
💻 C
字号:
/*adc_read.c*/
/*For weather data logging system*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

/* Personal includes and defines */
#include "uadc.h"
#define USAGE "sampling_rate(in seconds)(0-300) num_samples(1-20) channel(0-7)"
/* End Personal includes */

/* Prototypes */
int adc(int,int);
/* End Prototypes */

int main(int argc,char *argv[])
{
/* Sampling rate (in seconds) passed to sleep system call.
   and Number of samples */
int sampling_rate,num_samples;

/* Channel number of adc read from */
int channel;

/* Number of points in data file */
int num_point=1;

/* The integer value returned by adc */
int data_point=0;

/* The temperature obtained  by rescaling the integer value returned by adc */
float temp;

/* An index variable */
int i;

/* File pointers for input , output and log files */
FILE * infile;
FILE * outfile;
FILE * logfile;

/* POSIX type time_t for time system call */
time_t init_time,current_time;

/* A string for ctime system call to convert time_t type to a friendlier format */
char *timestr;

/* A string to read into and a char * to check the status of fgets */
char instring[80];
char *fget_stat;


/* Parse and assign command line arguments */
if (argc !=4 )
{
fprintf(stderr,"%s %s\n",argv[0],USAGE);exit(-2);
}
sampling_rate=atoi(argv[1]);
num_samples=atoi(argv[2]);
channel=atoi(argv[3]);

/* Debugging purposes only */
#ifdef DEB
fprintf(stderr,"Channel Number:%d\t Sampling Rate:%d Seconds \t Number of Samples : %d \n",channel,sampling_rate,num_samples);
#endif 


/* Get Starting time (Returns time as seconds elapsed since 00:00:00 January 1,1970 */
init_time=time(NULL);

/* ctime converts time to a string of form Sun Jun 28 00:00:00 1996  */
timestr=ctime((const time_t *)&init_time);

/* Write starting time into logfile */
logfile=fopen("adcerr.log","w");
fprintf(logfile,"Start Time: %s",timestr);
fflush(logfile);

/* Open the output file (temperature.dat) */
outfile=fopen("temperature.dat","w");

/* Start The infinite loop */
for(;;)

{
/* Open adc device */
if(adc(channel,DEVICE_OPEN) != -4)
     {

      /* Read a data value from adc */
      data_point=adc(channel,DEVICE_READ);

      /* Set  time of data read */
      current_time=time(NULL);

      /* Convert to an  easier format */
      timestr=ctime((const time_t *)&current_time);

      /* Rescale the data as per sensor requirements */
      temp=(float)(data_point-2730)/10.0;

     /* Debugging purposes only */
#ifdef DEB
      fprintf(stderr,"Temperature:%3.1f \t Time:%s\n ",temp,timestr);
#endif

     /* Close the adc device */
     adc(channel,DEVICE_CLOSE);
      }
else
   {
    /* If device could not be opened log error and exit */

    fprintf(logfile,"Could not open device now \n");
    fclose(logfile); 
    fclose(outfile);
    exit(-1);
    }

/*  Initially at startup write num_samples data points (Sampled at a sampling_rate(in seconds) interval )
    to the output file . This is read by another program which averages , finds minimum and maximum,
    and outputs a HTML file containing the relevant data */

if (num_point<=num_samples)
   {
   /*  Just a precaution in case outfile is open to flush it */
   fclose(outfile);

   /* Open output file in append mode */
   outfile=fopen("temperature.dat","a");

   /* Output the temperature as a float and time of reading as a string */
   fprintf(outfile,"%3.1f %s",temp,timestr);

   fclose(outfile);

   /* Increment number of points written to file */
   num_point++;
   }
else
  {

  /* If the number of points in file is greater than num_samples */

  if (num_point>num_samples)
     {
     /* Open the output file (temperature.dat) as read only 
        and a temporary file (temperature.datt) . Copy the last fourteen 
        lines  of temperature.dat to the temporary file */

     infile=fopen("temperature.dat","r");
     outfile=fopen("temperature.datt","w");
     for(i=0;i<=14;i++)
     {
     fgets(instring,32,infile);
     if (i>0) fprintf(outfile,"%s",instring);
     }
     fclose(infile);
     fclose(outfile);

     /* Delete original file (temperature.dat) and copy the temporary file to temperature.dat */
     infile=fopen("temperature.datt","r");
     outfile=fopen("temperature.dat","w");
     while ((fget_stat=fgets(instring,32,infile))!=NULL)
     {
     fprintf(outfile,"%s",instring);
     }
     fclose(infile);

     /* Delete tempfile */
     unlink("temperature.datt");

     /* Write the current data point and time stamp to temperature.dat .
        This ensures that temperature.dat always contains the most recent 
        fifteen temperature values */
     fprintf(outfile,"%3.1f %s",temp,timestr);

     /* Flush and close output stream */
     fflush(outfile);
     fclose(outfile);
    } /*  End if (num_points>num_samples) */
} /* End Else */
/* Sleep for sixty seconds ( Sampling at one minute intervals) */
sleep(sampling_rate);
} /* Infinite Loop */
} /* Close Main */


                  /*Listing 1*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -