## Appendix A  
This appendix reports the C code developed to implement both the voltage controller and the calculation of the switching function \(\Psi\). The code was used in the microprocessor emulation available in PSIM, but the code can be also used, without major modifications, to implement those processes in any real microprocessor programmable in C language. ```
1 voidSimulationStep(doublet,doubledelta,double*in,double*out,int*pnError,char*szErrorMsg,void*reserved_UserData,intserved_ThreadIndex,void*
2{}{}{}_{\tt}
3{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{ \tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{}_{\tt}{} \)
4
5//VariablestocalculatethePerturbandObservealgorithm(P&O)
6 floatVpv=0;
7 floatPpv=0;
8 floatPpv=0;
9 staticfloatP_old=0;
10 floatDpv=0.2;//Perturbationmagnitude
11 staticfloatV=18;//OutputoftthePMO
12 floatTa=0.5e-3;//Perturbationperiod
13 staticfloatsign=1;//PerturbationdirectionofVpv(1:increase,-1:decrease)
14 staticfloatt_old=0;
15
16//Variablestocalculatetheswitchingfunction(Psi)
17 floatVp;
18 floati1;
19 floati2;* 20 floatir;
* 21 floatPsi;
* 22 floatd_1;
* 23 floatd_2;
* 24
* 25//Variablestocalculatethevoltagecontrolleroutput(ir)
* 26 floate;
* 27 staticfloate_old;
* 28 staticfloatir_old;
* 29 floatKp=1.3479;
* 20 floatKi=9.0847e+03;
* 21//VariablestoclimitthedynamicresponseofthePROreference(dVr/dt)
* 23 floatramp_inc=(0.46/1e-6)*delt;//incrementwiththedynamiclimitation
* 24 staticfloatramp=Vr;
* 25//AcquiringthecircuitvariablesusingADC
* 26 Vpv=in[0];
* 27 pv=in[1];
* 28 vDv=in[2];
* 29i12=in[4];
* 30//CalculatethePVpower
* 31 Ppv=Vpv*1 pv;
* 32
* 33//ExecutethePMOalgorithmeachTa(period)
* 34 if((t-t_old)>=Ta)
* 35
* 36//Changetheperturbationdirectionwhnthepowerdecreases
* 37 if(Ppv=P_old)
* 38 sign=sign*(-1);
* 39
* 30//Perturbationwithoutdynamiclimitation
* 31 Vr=Vr+Dpvo*sign;
* 32
* 34//Avoidreferencevolategesoutsidepracticalvalues[0,Voc]
* 35 if(Vr<0)
* 36 Vr=0;
* 37 if(Vr>2.1)
* 38 Vr=22.1;
* 39P_old=Ppv;//StoredtocetetthechangeonthePVpower
* 30t_old=t;//StoredtocetetwhenperiodTaisfinished
* 31
* 32
* 33//RamptolimitdVr/dt
* 34//Increasingrampwithslopemax(dVr/dt)
* 35 if(ramp<Vr)
* 36
* 37 ifamp=ramp+ramp_inc;
* 38 if(ramp>Vr)
* 39 if(ramp>Vr)
* 31
* 34//Decreasingrampwithslopemax(dVr/dt)
* 35 if(ramp>Vr)
* 36
* 37 if(ramp<Vr)
* 38 ramp=Vr;
* 39
* 30//ThevariablerampcorrespondstoVrwiththedynamiclimitation
* 31
* 32
* 33//CalculationofthePVvoltagecontrolleroutput(ir)
* 34e=Vpv-ramp;//PVvoltageerror
* 35 ir=_old+Kp*=(Kp-Ki-delta)*e_old;//DifferenceequationofaPI
* 36e_old+://Storedtoculatetir
* 37 if_old=ir;//Storedtoculatetir
* 38
* 39
* 30//Calculationoftheswitchingfunction(Psi)
* 31d_1=(Vpv/Vb);//calculationof(1-d)
* 32d_2=d_1+1;//calculationof(2-d)
* 33 Psi=(d_1*i2)+(d_2+i1)-Ipv-ir;//calculationofPsi
* 34
* 35//DeliveringPsitotheswitchingcircuitusingaDMC
* 36 uU01=Psi;

## References

* [PERSON] et al. (2022) [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON]; [PERSON] [PERSON] Reduction of the Risk of Inaccurate Prediction of Electricity Generation from PV Farms Using Machine Learning. _Energies_**2022**, _15_, 4006. [CrossRef]
* [PERSON] et al. (2021) [PERSON]; [PERSON]; [PERSON] Advancement of voltage equalizer topologies for serially connected solar modules as partial shading mitigation technique: A comprehensive review. _J. Clean. Prod._**2021**, _285_, 124824. [CrossRef]
* [PERSON] et al. (2012) [PERSON]; [PERSON]; [PERSON]; [PERSON] Design and evaluation of a modular resonant switched capacitors equalizer for PV panels. In Proceedings of the 2012 IEEE Energy Conversion Congress and Exposition (ECCE), Raleigh, NC, USA, 15-20 September 2012; pp. 4129-4136. [CrossRef]
* [PERSON] et al. (2012) [PERSON]; [PERSON] [PERSON]; [PERSON]; [PERSON] Synthesis and Analysis of a Switched-Capacitor-Based Battery Equalizer Using Level-Shift Circuits. _Int. J. Intell. Eng. Syst._**2012**, \(5\), 1-9. [CrossRef]
* [PERSON] et al.