void collect_block_advanced_triggered () { int i; int trigger_sample; long time_interval; short time_units; short oversample; long no_of_samples = BUFFER_SIZE; FILE *fp; long time_indisposed_ms; short overflow; int threshold_mv =1500; long max_samples; short ch; printf ( "Collect block triggered...\n" ); printf ( "Collects when value rises past %dmV\n", threshold_mv ); printf ( "Press a key to start...\n" ); getch (); set_defaults (); set_trigger_advanced (); /* find the maximum number of samples, the time interval (in time_units), * the most suitable time units, and the maximum oversample at the current timebase */ oversample = 1; while (!ps2000_get_timebase ( unitOpened.handle, timebase, no_of_samples, &time_interval, &time_units, oversample, &max_samples)) timebase++; /* Start it collecting, * then wait for completion */ ps2000_run_block ( unitOpened.handle, BUFFER_SIZE, timebase, oversample, &time_indisposed_ms ); printf ( "Waiting for trigger..." ); printf ( "Press a key to abort\n" ); while (( !ps2000_ready ( unitOpened.handle )) && ( !kbhit () )) { Sleep ( 100 ); } if (kbhit ()) { getch (); printf ( "data collection aborted\n" ); } else { ps2000_stop ( unitOpened.handle ); /* Get the times (in units specified by time_units) * and the values (in ADC counts) */ ps2000_get_times_and_values ( unitOpened.handle, times, unitOpened.channelSettings[PS2000_CHANNEL_A].values, unitOpened.channelSettings[PS2000_CHANNEL_B].values, NULL, NULL, &overflow, time_units, BUFFER_SIZE ); /* Print out the first 10 readings, * converting the readings to mV if required */ printf ("Ten readings around trigger\n"); printf ("Time\tValue\n"); printf ("(ns)\t(%s)\n", adc_units (time_units)); /* This calculation is correct for 10% pre-trigger */ trigger_sample = BUFFER_SIZE / 10; for (i = trigger_sample - 5; i < trigger_sample + 5; i++) { for (ch = 0; ch < unitOpened.noOfChannels; ch++) { if(unitOpened.channelSettings[ch].enabled) { printf ( "%d\t", adc_to_mv ( unitOpened.channelSettings[ch].values[i], unitOpened.channelSettings[ch].range) ); } } printf("\n"); } fp = fopen ( "data.txt","w" ); if (fp != NULL) { for ( i = 0; i < BUFFER_SIZE; i++ ) { fprintf ( fp,"%ld ", times[i]); for (ch = 0; ch < unitOpened.noOfChannels; ch++) { if(unitOpened.channelSettings[ch].enabled) { fprintf ( fp, ",%d, %d,", unitOpened.channelSettings[ch].values[i], adc_to_mv ( unitOpened.channelSettings[ch].values[i], unitOpened.channelSettings[ch].range) ); } } fprintf(fp, "\n"); } fclose(fp); } else printf("Cannot open the file data.txt for writing. \nPlease ensure that you have permission to access. \n"); } }
void collect_fast_streaming_triggered (void) { unsigned long i; FILE *fp; short overflow; int ok; short ch; unsigned long nPreviousValues = 0; short values_a[BUFFER_SIZE_STREAMING]; short values_b[BUFFER_SIZE_STREAMING]; unsigned long triggerAt; short triggered; unsigned long no_of_samples; double startTime = 0; fprintf (stderr, "Ready to stream...\n" ); printf (stderr, "Press a key to start\n" ); getch (); set_defaults (); set_trigger_advanced (); unitOpened.trigger.advanced.autoStop = 0; unitOpened.trigger.advanced.totalSamples = 0; unitOpened.trigger.advanced.triggered = 0; //Enable and set channel A with AC coupling at a range of +/-2V if(ps2000_set_channel(unitOpened.handle,PS2000_CHANNEL_A,TRUE,FALSE,PS2000_2V)) fprintf(stderr,"Channel A enabled and set..\n"); else fprintf(stderr,"Unable to set channel A...\n"); //Disable channel B if(ps2000_set_channel(unitOpened.handle,PS2000_CHANNEL_B,FALSE,FALSE,1)) fprintf(stderr,"Channel B disabled..\n"); else fprintf(stderr,"Unable to set channel B...\n"); /* Collect data at 200ns intervals * 100000 points with an agregation of 1 : 1 * Auto stop after the 100000 samples * Start it collecting, */ if(!ps2000_run_streaming_ns ( unitOpened.handle, 200, 2, BUFFER_SIZE_STREAMING, 1, 1, 40000 )) fprintf(stderr,"There was a problem running streaming...\n"); /* From here on, we can get data whenever we want... */ while (!unitOpened.trigger.advanced.autoStop) { ps2000_get_streaming_last_values (unitOpened.handle, ps2000FastStreamingReady); if (nPreviousValues != unitOpened.trigger.advanced.totalSamples) { nPreviousValues = unitOpened.trigger.advanced.totalSamples; } Sleep (0); } ps2000_stop (unitOpened.handle); no_of_samples = ps2000_get_streaming_values_no_aggregation (unitOpened.handle, &startTime, // get samples from the beginning values_a, // set buffer for channel A values_b, // set buffer for channel B NULL, NULL, &overflow, &triggerAt, &triggered, BUFFER_SIZE_STREAMING); fprintf(stderr,"%d samples collected, representing %d mains cycles",no_of_samples,no_of_samples/99000); // if the unit triggered print out ten samples either side of the trigger point // otherwise print the first 20 readings /*for ( i = (triggered ? triggerAt - 10 : 0) ; i < ((triggered ? triggerAt - 10 : 0) + 20); i++) { for (ch = 0; ch < unitOpened.noOfChannels; ch++) { if (unitOpened.channelSettings[ch].enabled) { printf ("%d, ", adc_to_mv ((!ch ? values_a[i] : values_b[i]), unitOpened.channelSettings[ch].range) ); } } printf ("\n"); } */ //fp = fopen ( "data.txt", "w" ); for ( i = 0; i < no_of_samples; i++ ) { printf ("%d ", adc_to_mv (values_a[i], unitOpened.channelSettings[0].range) ); } printf ("\n"); //fclose ( fp ); getch (); }
void collect_fast_streaming_triggered (void) { unsigned long i; FILE *fp; short overflow; int ok; short ch; unsigned long nPreviousValues = 0; short values_a[BUFFER_SIZE_STREAMING]; short values_b[BUFFER_SIZE_STREAMING]; unsigned long triggerAt; short triggered; unsigned long no_of_samples; double startTime = 0; printf ( "Collect fast streaming triggered...\n" ); printf ( "Data is written to disk file (data.txt)\n" ); printf ( "Press a key to start\n" ); getch (); set_defaults (); set_trigger_advanced (); unitOpened.trigger.advanced.autoStop = 0; unitOpened.trigger.advanced.totalSamples = 0; unitOpened.trigger.advanced.triggered = 0; /* Collect data at 10us intervals * 100000 points with an agregation of 100 : 1 * Auto stop after the 100000 samples * Start it collecting, */ ok = ps2000_run_streaming_ns ( unitOpened.handle, 10, PS2000_US, BUFFER_SIZE_STREAMING, 1, 100, 30000 ); printf ( "OK: %d\n", ok ); /* From here on, we can get data whenever we want... */ while (!unitOpened.trigger.advanced.autoStop) { ps2000_get_streaming_last_values (unitOpened.handle, ps2000FastStreamingReady); if (nPreviousValues != unitOpened.trigger.advanced.totalSamples) { printf ("Values collected: %ld\n", unitOpened.trigger.advanced.totalSamples - nPreviousValues); nPreviousValues = unitOpened.trigger.advanced.totalSamples; } Sleep (0); } ps2000_stop (unitOpened.handle); no_of_samples = ps2000_get_streaming_values_no_aggregation (unitOpened.handle, &startTime, // get samples from the beginning values_a, // set buffer for channel A values_b, // set buffer for channel B NULL, NULL, &overflow, &triggerAt, &triggered, BUFFER_SIZE_STREAMING); // if the unit triggered print out ten samples either side of the trigger point // otherwise print the first 20 readings for ( i = (triggered ? triggerAt - 10 : 0) ; i < ((triggered ? triggerAt - 10 : 0) + 20); i++) { for (ch = 0; ch < unitOpened.noOfChannels; ch++) { if (unitOpened.channelSettings[ch].enabled) { printf ("%d, ", adc_to_mv ((!ch ? values_a[i] : values_b[i]), unitOpened.channelSettings[ch].range) ); } } printf ("\n"); } fp = fopen ( "data.txt", "w" ); if (fp != NULL) { for ( i = 0; i < no_of_samples; i++ ) { for (ch = 0; ch < unitOpened.noOfChannels; ch++) { if (unitOpened.channelSettings[ch].enabled) { fprintf ( fp, "%d, ", adc_to_mv ((!ch ? values_a[i] : values_b[i]), unitOpened.channelSettings[ch].range) ); } } fprintf (fp, "\n"); } fclose ( fp ); } else printf("Cannot open the file data.txt for writing. \nPlease ensure that you have permission to access. \n"); getch (); }