Skip to content
experiment.c 28.9 KiB
Newer Older
	
	if (_dpv_singledir(dacindex_start, dacindex_stop, dacindex_pulse_height, dacindex_step, direction))
		goto aborting; //function will return non-zero if abort called over USB
	
	printf("D\n\r"); //signal end of experiment
	
	aborting:
		tc_write_clock_source(&TCF0, TC_CLKSEL_OFF_gc);
		tc_disable(&TCF0);
		TCF0.CNT = 0;
		ads1255_standby();
	
		return;
}

uint8_t _dpv_singledir (uint16_t dacindex, uint16_t dacindex_stop, uint16_t dacindex_pulse_height, uint16_t dacindex_step, uint8_t direction){
	/**
	 * Internal function that performs a single direction sweep for DPV
	 *
	 * @param dacindex Starting voltage as dac index
	 * @param dacindex_stop Stop voltage in dac index.
	 * @param dacindex_step Step voltage in dac index.
	 * @param dacindex_pulse_height Pulse amplitude in dac index.
	 * @param direction Scan direction - 1 for up, 0 for down.
	 */
	int32_t forward = 0;
	int32_t reverse = 0;
	uint16_t lastindex = 0;
	
	if (direction == 1)
		max5443_set_voltage1(dacindex+dacindex_pulse_height);
	else
		max5443_set_voltage1(dacindex-dacindex_pulse_height);
	
	while ((dacindex <= dacindex_stop && direction == 1) || (dacindex >= dacindex_stop && direction == 0)){
		tc_clear_overflow(&TCF0);
		tc_clear_cc_interrupt(&TCF0, TC_CCA);
				
		while (!tc_is_cc_interrupt(&TCF0, TC_CCA)){ //convert continuously until tc CCA match - 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();
			if (udi_cdc_is_rx_ready()){ //check for abort signal over USB
				if (getchar() == 'a')
					return 1;
			}
		}
		
		//switch voltage to baseline
		max5443_set_voltage1(dacindex);
				
		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()){
				if (getchar() == 'a')
					return 1;
			}
		}
			
		lastindex = dacindex;
			
		//increment dacindex
		if (direction == 1){
			dacindex += dacindex_step;
			max5443_set_voltage1(dacindex+dacindex_pulse_height);
		}
		
		else{
			dacindex -= dacindex_step;
			max5443_set_voltage1(dacindex-dacindex_pulse_height);
		}
			
		//data output
		printf("B\n");
		send_data_uint16(lastindex);
		send_data_int32(forward);
		send_data_int32(reverse);
		printf("\n");
	}
	
	return 0;
}

ISR(PORTD_INT0_vect){
	if (portd_int0_callback) {
		portd_int0_callback();
	}