clear 

%Open a session
a = arduino('/dev/cu.usbmodem1411','Uno');

dly=0.00001;

% MSB is shifted in first
%Cap1 
%     Value per trim setting: 112pF
%     Enables: 50pF, 11x100pF
%     Bits 0:11
% 0 112 224 336 448 560 672 784 896 1008 1120

%Cap2 
%     Value per trim setting: 270pF
%     Enables: 50pF, 100pF, 13x200pF
%     Bits 0:11
% 0 270 540 819 1080 1350 1620 1890 2160 2430 2700

trim_values= [
  0 0 0 0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0    %1  trim -5
  0 1 0 0 0 0 0 0 0 0 0 0   1 0 1 0 0 0 0 0 0 0 0 0 0 0 0    %2  trim -4
  1 1 1 0 0 0 0 0 0 0 0 0   1 1 1 1 0 0 0 0 0 0 0 0 0 0 0    %3  trim -3
  1 1 1 1 0 0 0 0 0 0 0 0   0 0 1 1 1 1 0 0 0 0 0 0 0 0 0    %4  trim -2
  1 1 1 1 1 0 0 0 0 0 0 0   0 1 1 1 1 1 1 0 0 0 0 0 0 0 0    %5  trim -1
  1 1 1 1 1 1 0 0 0 0 0 0   1 1 1 1 1 1 1 1 0 0 0 0 0 0 0    %6  trim 0
  1 1 1 1 1 1 1 0 0 0 0 0   0 0 1 1 1 1 1 1 1 1 0 0 0 0 0    %7  trim +1
  0 1 1 1 1 1 1 1 1 0 0 0   0 1 1 1 1 1 1 1 1 1 1 0 0 0 0    %8  trim +2
  0 1 1 1 1 1 1 1 1 1 0 0   1 1 1 1 1 1 1 1 1 1 1 1 0 0 0    %9  trim +4
  0 1 1 1 1 1 1 1 1 1 1 0   1 0 1 1 1 1 1 1 1 1 1 1 1 1 0    %10 trim +4
  0 1 1 1 1 1 1 1 1 1 1 1   0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]; %11 trim +5

for i=1:1000

%Get idexed trim data
%tidx = input('enter trim idx (1:11) = ');
tidx=mod(i,11)+1
pause(0.01);
d=trim_values(tidx,:);

%output sld=0 along with clock pulse
writeDigitalPin(a, 'D4', 0);


%Output each bit in the control word along with clock pulse
for i=1:length(d)
  writeDigitalPin(a, 'D3', d(length(d)-i+1));
  pause(dly)
  writeDigitalPin(a, 'D2', 1);
  pause(dly)
  writeDigitalPin(a, 'D2', 0);
  pause(dly)

    
end

% output sld=1 along with clock pulse
writeDigitalPin(a, 'D4', 1);
pause(dly)
writeDigitalPin(a, 'D2', 1);
pause(dly)
writeDigitalPin(a, 'D2', 0);
pause(dly)
writeDigitalPin(a, 'D4', 0);

end

