%get and plot IV curve;
clear all
compliance = 100e-3;
Vstart = 0;
Vstop = -7;
Vstep = -0.1;
pse = 0.15;
Vvec_up = Vstart:Vstep:Vstop;
Vvec_down = Vstop:-Vstep:Vstart;
meterGPIB = 30;

%initialize source meter
address = strcat('GPIB0::', num2str(meterGPIB), '::INSTR');
source = visa('ni', address);
fopen(source)
voltage = Vvec_up(1);
fprintf(source, ':SENS:CURR:PROT %e', compliance);
fprintf(source, ':SOUR:VOLT:LEV %e', voltage);
fprintf(source, ':OUTP ON');
fprintf(source, ':READ?')
current_holder = str2num(fscanf(source));
current = current_holder(2);

%step through voltages: go up:
for i = 1:length(Vvec_up)
    voltage = Vvec_up(i);
    fprintf(source, ':SOUR:VOLT:LEV %e; *WAI', voltage);
    fprintf(source, ':READ?; *WAI')
    current_holder = str2num(fscanf(source));
    current_up(i) = current_holder(2);
    pause(pse);
end

%step through voltages: go down:
for i = 1:length(Vvec_down)
    voltage = Vvec_down(i);
    fprintf(source, ':SOUR:VOLT:LEV %e; *WAI', voltage);
    fprintf(source, ':READ?; *WAI')
    current_holder = str2num(fscanf(source));
    current_down(i) = current_holder(2);
    pause(pse);
end

fprintf(source, ':SOUR:VOLT:LEV 0');
fclose(source)
clear source

plot(Vvec_up, current_up*1e3, Vvec_down, current_down*1e3)
ylabel('Current (mA)')
xlabel('Voltage (V)')

uisave()

    

