function [] = RmonitoringEM_v20170802()
%Resistance monitoring of electromigration
% Stress a sample with high current and monitor electrical resistance with 
% an Agilent 34401A as a voltmeter, and Keithley 2700 as an ampmeter. 
% Temperature near sample is monitored with a Pt100 connected to a Keithley
% 2700. This code can be used for similar purposes with necessary modifications.
% By Di Erick Xu, University of Waterloo, 2017.Aug.02

% Hardware: GPIB-USB cable, an Agilent 34401A, and two Keithley 2700

% Tags: GPIB, Digital multimeter, Multiplexer, Resistance monitoring,
% Electromigration, Current stressing, Temperature monitoring,

%% initialzation
instrreset;     % Clear and delete all instrament references
clear;          % Clear local memory of any pre-existing objects
close all;      % Close all outstanding objects

% Create a folder named with the current date and a given sample name
sample = 'Demo4MathworksCentral';
sampleName = [datestr(now, 'yyyymmdd-HH-MM') '_' sample];
mkdir(sampleName);

% Gpib: define measurement instraments
dmmV = gpib('agilent',7, 23);
dmmI = gpib('agilent', 7, 20);
dmmT = gpib('agilent', 7, 25);

% Constants
nplc = 10;
pp = 0.01;
time = 0;
% Constants for converting Pt100 resistance to temperature
A = 3.9083E-3;
B = -5.775E-7;
R0 = 100;

% Establish communication with instraments and configure them
fopen(dmmI);
fopen(dmmV);
fopen(dmmT);
fprintf(dmmI, 'conf:curr');
fprintf(dmmV, 'conf:volt');
fprintf(dmmT, 'conf:fres');
fprintf(dmmI, ['sens:curr:dc:nplc ' num2str(nplc)]);
fprintf(dmmV, ['sens:volt:dc:nplc ' num2str(nplc)]);
fprintf(dmmT, ['sens:fres:dc:nplc ' num2str(nplc)]);
fprintf(dmmI, ['trig:del ' num2str(pp)]);
fprintf(dmmV, ['trig:del ' num2str(pp)]);
fprintf(dmmT, ['trig:del ' num2str(pp)]);

% Set voltage and current of power supply and output
% the current is low in the beginning to make measurements before aging
dos('Vantek.exe -u0500 -com7');
dos('Vantek.exe -i0100 -com7');
dos('Vantek.exe -on -com7');
pause(2)

% recording starts
currI = 100;
voltage = 0;
tic;
while voltage < 4
    
    % after 124 min, switch the current level from 0.1A to 2.2A
    if fix(time/60) > 124  
        if(currI == 100)
            dos('Vantek.exe -i2200 -com7');
            currI = 2200;
        end
    end
    
    % read values from three multimeters, and save them to variables
    fprintf(dmmV, 'read?');
    fprintf(dmmI, 'read?');
    fprintf(dmmT, 'read?');
    voltage = str2double( fscanf(dmmV) );
    current = str2double( fscanf(dmmI) );
    TempR = str2double( fscanf(dmmT) ); % Pt100 resistance
    
    % calculate temperature from the resistance of Pt100
    temperature = (-A + sqrt(A^2 - 4*B*(1 - TempR/R0)))/(2*B);
    
    % current time since tic;
    time = toc;

    % save data to file
    file = -1;% Make sure file is opened and something is recorded
    while (file == -1)
        file = fopen([sampleName,'\', sampleName],'a');
        fprintf(file,  ['my V, I and T at, ', num2str(time), ...
            ' s ,is = ,', num2str(voltage), ' V, ', num2str(current), ...
            ' A and ', num2str(temperature), ' oC',  '\r\n']); 
    end
    
    % wait for 4 seconds before the next measurement
    pause(4)
    fclose(file);
    
end % end of while

% Close communications
fclose(dmmI);
fclose(dmmV);
fclose(dmmT);

end % end of function
