www.pudn.com > MS_SAR_ADC_Sim_ver03_02.zip > MS_SAR_ADC_Sim_ver03_02.m, change:2016-09-08,size:4857b


%% SAR ADC Simulation Code by Jiwon Lee 
 
function adc_output = diff_sar_adc(ADC_p_input, ADC_n_input, N_bits, V_supply, C_ideal, ... 
    switching, C_error_pos_upper, C_error_neg_upper, C_error_pos_lower, C_error_neg_lower) 
 
 
C_real_pos = zeros(2,N_bits-1); 
C_real_neg = zeros(2,N_bits-1); 
 
C_temp = 0; 
 
Start_idx_pos_upper = 1; 
Start_idx_neg_upper = 1; 
Start_idx_pos_lower = 1; 
Start_idx_neg_lower = 1; 
 
 
for k = 1:(N_bits-2) 
    C_error = C_error_pos_upper(Start_idx_pos_upper:Start_idx_pos_upper+2^(N_bits-1-k)/2-1); 
    for l = 1:2^(N_bits-1-k)/2 
        C_unit = C_ideal + C_error(l); 
        C_temp = C_temp + C_unit; 
    end 
    Start_idx_pos_upper = Start_idx_pos_upper + 2^(N_bits-1-k)/2; 
    C_real_pos(1,k) = C_temp; 
    C_temp = 0; 
     
    C_error = C_error_neg_upper(Start_idx_neg_upper:Start_idx_neg_upper+2^(N_bits-1-k)/2-1); 
    for l = 1:2^(N_bits-1-k)/2 
        C_unit = C_ideal + C_error(l); 
        C_temp = C_temp + C_unit; 
    end 
    Start_idx_neg_upper = Start_idx_neg_upper + 2^(N_bits-1-k)/2; 
    C_real_neg(1,k) = C_temp; 
    C_temp = 0; 
     
    C_error = C_error_pos_lower(Start_idx_pos_lower:Start_idx_pos_lower+2^(N_bits-1-k)/2-1); 
    for l = 1:2^(N_bits-1-k)/2 
        C_unit = C_ideal + C_error(l); 
        C_temp = C_temp + C_unit; 
    end 
    Start_idx_pos_lower = Start_idx_pos_lower + 2^(N_bits-1-k)/2; 
    C_real_pos(2,k) = C_temp; 
    C_temp = 0; 
     
    C_error = C_error_neg_lower(Start_idx_neg_lower:Start_idx_neg_lower+2^(N_bits-1-k)/2-1); 
    for l = 1:2^(N_bits-1-k)/2 
        C_unit = C_ideal + C_error(l); 
        C_temp = C_temp + C_unit; 
    end 
    Start_idx_neg_lower = Start_idx_neg_lower + 2^(N_bits-1-k)/2; 
    C_real_neg(2,k) = C_temp; 
     
    C_unit = 0; 
    C_temp = 0; 
     
end 
 
C_error = C_error_pos_upper(Start_idx_pos_upper); 
C_real_pos(1,N_bits-1) = C_ideal + C_error; 
 
C_error = C_error_pos_upper(Start_idx_neg_upper); 
C_real_neg(1,N_bits-1) = C_ideal + C_error; 
 
C_error = C_error_pos_lower(Start_idx_pos_lower); 
C_real_pos(2,N_bits-1) = C_ideal + C_error; 
 
C_error = C_error_neg_lower(Start_idx_neg_lower); 
C_real_neg(2,N_bits-1) = C_ideal + C_error; 
 
C_sum_upper = sum(C_real_pos(2,:))+sum(C_real_neg(2,:)); 
C_sum_lower = sum(C_real_pos(2,:))+sum(C_real_neg(2,:)); 
 
 
 
 
ADC_output = zeros(1,N_bits); 
 
temp_upper = ADC_p_input; 
temp_lower = ADC_n_input; 
temp_result = 0; 
 
toggle = 1; 
 
if switching == 1 
    toggle = -1; 
end 
 
for k = 1:N_bits 
     
    toggle = toggle * (-1); 
     
    if temp_upper > temp_lower 
        ADC_output(k) = 1; 
         
        if k~=N_bits 
            if switching == 1 
                temp_upper = temp_upper - V_supply * C_real_neg(1,k) / C_sum_upper; 
                temp_lower = temp_lower + V_supply * C_real_pos(2,k) / C_sum_lower; 
            else 
                if k == 1 
                    if toggle == 1 
                        temp_upper = temp_upper - V_supply * C_real_neg(1,k) / C_sum_upper; 
                        temp_lower = temp_lower + V_supply * C_real_pos(2,k) / C_sum_lower; 
                    else 
                        temp_upper = temp_upper - V_supply * C_real_pos(1,k) / C_sum_upper; 
                        temp_lower = temp_lower + V_supply * C_real_neg(2,k) / C_sum_lower; 
                    end 
                else 
                    temp_upper = temp_upper + V_supply * C_real_pos(1,k) / C_sum_upper; 
                    temp_lower = temp_lower - V_supply * C_real_neg(2,k) / C_sum_lower;                            
                end 
            end 
             
        else 
            break; 
        end 
    else 
        ADC_output(k) = 0; 
         
        if k~=N_bits 
            if switching == 1 
                temp_upper = temp_upper + V_supply * C_real_pos(1,k) / C_sum_upper; 
                temp_lower = temp_lower - V_supply * C_real_neg(2,k) / C_sum_lower; 
            else 
                if k == 1 
                    if toggle == 1 
                        temp_upper = temp_upper + V_supply * C_real_pos(1,k) / C_sum_upper; 
                        temp_lower = temp_lower - V_supply * C_real_neg(2,k) / C_sum_lower; 
                    else 
                        temp_upper = temp_upper + V_supply * C_real_neg(1,k) / C_sum_upper; 
                        temp_lower = temp_lower - V_supply * C_real_pos(2,k) / C_sum_lower; 
                    end 
                else 
                    temp_upper = temp_upper + V_supply * C_real_pos(1,k) / C_sum_upper; 
                    temp_lower = temp_lower - V_supply * C_real_neg(2,k) / C_sum_lower;                     
                end 
            end 
        else 
            break; 
        end 
    end 
     
    temp_result = temp_result + 2^(N_bits-k)*ADC_output(k); 
     
end 
 
adc_output = temp_result;