//Write one byte of control or Calendar/Clock data static void write_byte(uint8_t byte) { uint8_t i; //Port pins in write mode WRITE_MODE(); //Write one byte of control or Calendar/Clock data for(i = 0; i != 8; ++i) { //Start clock cycle with SCLK low SCLK_STROBE_LOW(); _delay_us(1); //Write bit value to I/O pin of DS1302 if(((1<<i)&byte) == 0) { IO_STROBE_LOW(); } else { IO_STROBE_HIGH(); } DATA_TO_CLK_SETUP(); //Data to clock setup //End clock cycle with SCLK high SCLK_STROBE_HIGH(); _delay_us(1); } }
int main( int argc, char *argv[] ) { int nproc, ifile, nframes=0; FILE **fparr, *ofp, *ologp; int *nxarr; int nx, nx_output, nx_total, nx_ave; int nvx, nvx_output=200; float dv; float *data_vec, *odata_vec; char fname[256], errmsg[512], ofname[256]; int write_binary=1; int not_eof=1; if ( argc!=5 ) { fprintf( stderr, "Usage: %s [num_processors] " " [NX] [a or b to write ascii or binary data]\n\n" "Note: ascii data write is intended for debugging one's diagnostics. The files" "produced can be huge.\n", argv[0] ); return 0; } /* Obtain runtime data from comand line. */ nproc=atoi(argv[1]); if ( nproc<=0 ) ERROR( "Bad number of input files requested.\n" ); nx_output=atoi(argv[2]); if ( nx_output<=0 ) ERROR( "Bad nx requested.\n" ); if ( argv[3][0]=='a' || argv[3][0]=='A' ) write_binary=0; /* Allocate space for input file handles. */ fparr=emalloc((size_t)nproc*sizeof(*fparr)); nxarr=emalloc((size_t)nproc*sizeof(*nxarr)); foreach_file fparr[ifile]=emalloc(sizeof(**fparr)); /************************************************************************* Write phase space movie data. **************************************************************************/ /* Open all of the input files. */ foreach_file { sprintf(fname, "movie_phase.%d", ifile); if ( !(fparr[ifile]=fopen(fname, "rb")) ) ERROR(("Cannot open file: %s\n", fname)); /* First element in each file is the num of cells in local x mesh */ fread( &nxarr[ifile], sizeof(int), 1, fparr[ifile] ); fread( &nvx, sizeof(int), 1, fparr[ifile] ); fread( &dv, sizeof(float), 1, fparr[ifile] ); } nx_total = 0; foreach_file nx_total += nxarr[ifile]; printf("Total x mesh points: %d\n", nx_total); nx_ave = nx_total/nx_output; if ( nx_total%nx_output ) fprintf(stderr, "Warning: requested number of x points doesn't " "evenly divide nx_total.\n" ); data_vec = (float *)emalloc((size_t)(nx_total*nvx_output)*sizeof(*data_vec)); odata_vec = (float *)emalloc((size_t)(nx_output*nvx_output)*sizeof(*odata_vec)); /* Write various movie parameters to log file movie_log.dat */ if ( !(ologp=fopen( "movie_log.dat", "w" )) ) ERROR(("Cannot open log file.")); fprintf( ologp, "%d\n", nframes ); fprintf( ologp, "%d\n", nx_output ); fprintf( ologp, "%d\n", nvx_output ); fprintf( ologp, "%e\n", dv ); /* Prepare to write movie data file. */ sprintf(ofname, (write_binary ? "%s.bin" : "%s.dat"), argv[2]); if ( !(ofp = fopen( ofname, WRITE_MODE(write_binary) )) ) ERROR(("Cannot open file: %s\n", ofname)); while ( not_eof ) { int ix=0; /* Read one line of data from each input file and join together into data_vec array. We have assumed that each output file has the same number of lines in it. */ printf("Reading frame %d... ", ++nframes); foreach_file { fread( &data_vec[ix], sizeof(float), nxarr[ifile]*nvx_output, fparr[ifile] ); ix += nxarr[ifile]*nvx_output; if ( feof(fparr[ifile]) ) not_eof=0; } /* Now create the output data array by averaging over values in data_vec */ for ( ix=0; ix<nx_output; ++ix ) { int ivx; for ( ivx=0; ivx<nvx_output; ++ivx ) { int ixa; odata_vec[ix*nvx_output+ivx] = 0; for ( ixa=0; ixa<nx_ave; ++ixa ) if ( ix*nx_ave+ixa<nx_total ) odata_vec[ix*nvx_output+ivx] += data_vec[(ix*nx_ave+ixa)*nvx_output+ivx]; } } /* Write odata_vec into output stream */ WRITE_VEC(ofp, odata_vec,nx_output*nvx_output,sizeof(float),write_binary); #if 0 if ( write_binary ) fwrite( odata_vec, sizeof(float), nx_output*nvx_output, ofp ); else /* ascii write */ for ( ix=0; ix<nx_output; ++ix ) { int ivx; for ( ivx=0; ivx<nvx_output; ++ivx ) fprintf( ofp, "%e ", odata_vec[ix*nvx_output+ivx] ); fprintf( ofp, "\n" ); } #endif printf("done.\n"); } fprintf( ologp, "%d\n", nframes ); fclose( ologp ); /* Close input and output data files */ foreach_file fclose(fparr[ifile]); fclose(ofp); /************************************************************************* Write field movie data. **************************************************************************/ return 0; }