From 8d698e0ae05a4bdf65f5252a9178fb545e8f0b89 Mon Sep 17 00:00:00 2001
From: Michael Dryden <mdryden@chem.utoronto.ca>
Date: Wed, 4 Feb 2015 17:51:24 -0500
Subject: [PATCH] Implemented SWV using timed single ADC conversions.

---
 DSTAT-temp/src/experiment.c | 54 ++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/DSTAT-temp/src/experiment.c b/DSTAT-temp/src/experiment.c
index 4786026..53070d1 100644
--- a/DSTAT-temp/src/experiment.c
+++ b/DSTAT-temp/src/experiment.c
@@ -801,35 +801,31 @@ void swv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h
 		direction = 0;
 	
 	tc_enable(&TCF0);
+	tc_enable(&TCC0);
 	
 	frequency *= 2; //compensate for half-period triggers
 	
 	//calculate time to ADC trigger
 	period = ceil((1/(double)frequency)*F_CPU);
-	
-	if (direction == 1)
-		max5443_set_voltage1(dacindex+dacindex_pulse_height);
-	else
-		max5443_set_voltage1(dacindex-dacindex_pulse_height);
+	uint32_t adc_period = ceil(((1/(double)frequency)-(double)(sample_delay_ms_100div/1e5))*F_CPU);
 	set_timer_period(period, &TCF0);
+	set_timer_period(adc_period, &TCC0);
 	
 	ads1255_wakeup();
-	ads1255_rdatac();
-	ads1255_sync();
+	ads1255_standby();
 	
 	volt_exp_start();
 	
 	do{
 		TCF0.CNT = 0;
-		while (!tc_is_overflow(&TCF0));
-		ads1255_wakeup(); //synchronize ADC
-		TCF0.CNT = 0;
+		TCC0.CNT = 0;
 	
 		if (_swv_singledir(dacindex_start, dacindex_stop, dacindex_pulse_height, dacindex_step, direction))
 			goto aborting; //function will return non-zero if abort called over USB
 		
 		if (scans > 0){ //non-cyclic mode skips out after one direction
 			TCF0.CNT = 0;
+			TCC0.CNT = 0;
 			if (_swv_singledir(dacindex_stop, dacindex_start, dacindex_pulse_height, dacindex_step, !direction)) //swap start and stop and invert direction for second half of scan
 				goto aborting;
 		}
@@ -845,6 +841,9 @@ void swv_experiment(int16_t start, int16_t stop, uint16_t step, uint16_t pulse_h
 		tc_write_clock_source(&TCF0, TC_CLKSEL_OFF_gc);
 		tc_disable(&TCF0);
 		TCF0.CNT = 0;
+		tc_write_clock_source(&TCC0, TC_CLKSEL_OFF_gc);
+		tc_disable(&TCC0);
+		TCC0.CNT = 0;
 		ads1255_standby();
 	
 		return;
@@ -871,34 +870,45 @@ uint8_t _swv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t daci
 	
 	while ((dacindex <= dacindex_stop && direction == 1) || (dacindex >= dacindex_stop && direction == 0)){
 		tc_clear_overflow(&TCF0);
+		tc_clear_overflow(&TCC0);
 				
-		while (!tc_is_overflow(&TCF0)){ //convert continuously until tc overflow - datum is last collected point
-			while (ioport_pin_is_low(IOPORT_CREATE_PIN(PORTD, 5)));  //wait for next valid datum
-			while (ioport_pin_is_high(IOPORT_CREATE_PIN(PORTD, 5)));
-			forward = ads1255_read_fast24();
+		while (!tc_is_overflow(&TCC0)){ //ADC tc overflow
 			if (udi_cdc_is_rx_ready()){ //check for abort signal over USB
 				if (getchar() == 'a')
 					return 1;
 			}
 		}
-				
+		
+		ads1255_wakeup();
+		while (ioport_pin_is_high(IOPORT_CREATE_PIN(PORTD, 5)));
+		forward = ads1255_read_single24();
+		ads1255_standby();
+		
+		while (!tc_is_overflow(&TCF0)); //wait for end of half-cycle
+		TCC0.CNT = 0;
+		
 		if (direction == 1) //switch voltage to other half of cycle
 			max5443_set_voltage1(dacindex-dacindex_pulse_height);
 		else
 			max5443_set_voltage1(dacindex+dacindex_pulse_height);
 
 		tc_clear_overflow(&TCF0); //reset timer OVF
+		tc_clear_overflow(&TCC0);
 				
-		while (!tc_is_overflow(&TCF0)){ //wait for tc overflow
-			while (ioport_pin_is_low(IOPORT_CREATE_PIN(PORTD, 5)));  //wait for next valid datum
-			while (ioport_pin_is_high(IOPORT_CREATE_PIN(PORTD, 5)));
-			reverse = ads1255_read_fast24();
-					
-			if (udi_cdc_is_rx_ready()){
+		while (!tc_is_overflow(&TCC0)){ //ADC tc overflow
+			if (udi_cdc_is_rx_ready()){ //check for abort signal over USB
 				if (getchar() == 'a')
-					return 1;
+				return 1;
 			}
 		}
+		
+		ads1255_wakeup();
+		while (ioport_pin_is_high(IOPORT_CREATE_PIN(PORTD, 5)));
+		reverse = ads1255_read_single24();
+		ads1255_standby();
+		
+		while (!tc_is_overflow(&TCF0)); //wait for end of half-cycle
+		TCC0.CNT = 0;
 			
 		lastindex = dacindex;
 			
-- 
GitLab