Xu ZHANG
/
DEBUG_10K
to junhao
Revision 0:1b13f03ce7eb, committed 2018-03-08
- Comitter:
- xuzhang
- Date:
- Thu Mar 08 14:38:32 2018 +0000
- Commit message:
- synthesis for 10k
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cal.cpp Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,93 @@ +#include "mbed.h" +#include "math.h" +#include "cal.h" + +//calcualte |Zx|,Cx, Rx +void cal_Zx ( + double z, double a,double f,int type, //input + double Ro,//constant + double *ZxMag, double *Cx, double *Rx//output + ) +{ //unit&description: + // z ohm, magnitude of impedance of Zx+Ro + // a degree, angle of Zx+Ro. angle is measured at 10kHz + // f Hz, the frequency when measure z and a + // type 1-series or 2-parallel + // Ro ohm, split resistor 33ohm typically + // *ZxMag ohm, magnitude of DUT + // *Cx F, cap of DUT + // *Rx ohm, resistance of DUT + double w=2*M_PI*f; + double arad=a/180*M_PI; + + double Rx2=0,Cx2=0; + double Zx=0; + + double Re=0,Im=0; + double arad2=0; + double temp; + if (type!=1&&type!=2) + printf("wrong type"); + else if(type==1){//series + Rx2=z*cos(arad)-Ro; + Cx2=1/(z*sin(arad)*w); + Zx=sqrt(Rx2*Rx2+(z*sin(arad))*(z*sin(arad))); + } + else{//if type==2 parallel + Re=z*cos(arad)-Ro;//real part of Zx parallel + Im=z*sin(arad); + Zx=sqrt(Re*Re+Im*Im); + arad2=atan2(Im,Re); + Rx2=Zx/cos(arad2); + Cx2=sin(arad2)/(Zx*w); + } + *ZxMag=Zx; + *Cx=Cx2; + *Rx=Rx2; +} + +double cal_z(double Vin,double Vo,double Rs) +{ + return Vin/Vo*Rs; + +} + + +double pwToAngle(int pw) +{ //angle,pw is measured at 10kHz + //10kHz=100us=1e-4s + //unit pw: number of counts, 50us=320counts + // angle degree + //pw id pulse width of level 0 + double angle=1; + double temp=0; + if (pw>=320||pw<=480) + { + //angle=(pw-320)/320*180; + temp=(double)pw;//+0.5 + angle=temp/16*9-180; + return angle; + } + else if (pw>=160||pw<=320) + { + //angle=(320-pw)/320*180; + temp=(double)pw;//+0.5 + angle=180-temp/16*9; + return angle; + } + else //if the pw is in the above range + {//the result angle must be in 0~90 + printf ("wrong pw"); + return 0; + } +} + +int check_angle(double angle) +{ //absolute angle must be within (0,90) + //the excat angle of RC is (-90,0) + //0 wrong angle, 1 correct + if (angle>90 || angle <0) + return 0; + else + return 1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cal.h Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,17 @@ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + + +#ifndef CAL_H_ +#define CAL_H_ +double cal_z(double Vin,double Vo,double Rs); +int check_angle(double angle); +double pwToAngle(int pw);//calcualte the angle of Zx+Ro +void cal_Zx ( + double z, double a,double f,int type, //input + double Ro,//constant + double *ZxMag, double *Cx, double *Rx//output + );//calculate magnitude of Zx+Ro + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/detectors.cpp Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,72 @@ +#include "mbed.h" +#include "detectors.h" +//count low level time PLL phase comparator output +//320 counts => 50us +DigitalIn counter_input(p8); + + +double countPulse0 (void) +{ + int counter=0; + double ave=0; + int i; + for (i=0;i<10;i=i+1){ + while (counter_input==0){;} + while (counter_input==1){;} + do { counter+=1;} + while(counter_input==0) ; + } + ave=counter/10; + return ave; + } +double countPulse0Debug (void) +{ + int counter=0; + int sum=0; + int i; + double ave; + for (i=0;i<10;i=i+1){ + counter=0; + while (counter_input==0){;} + while (counter_input==1){;} + do { counter+=1;} + while(counter_input==0) ; + sum=sum+counter; + printf("pulse0 10kHz[%d]=%d\n\r", i, counter); + } + ave=sum/10; + printf("avepulse0 10kHz=%0.3f\n\r", ave); + return ave; + } + +//peak detector output voltage +AnalogIn percentpeak(p20); + +double peakDetector (void) +{ + double sum=0,ave=0; + int i=0; + for (i=0;i<10;i=i+1){ + sum=sum+3.3* percentpeak; + } + ave=sum/10; + return ave; + } + +double peakDetectorDebug (void) +{ + double peak10k[10]={0}; + double sum=0,ave=0; + int i=0; + for (i=0;i<10;i=i+1){ + peak10k[i]=3.3* percentpeak; + printf("peak10k[i]=%0.3lf\n\r",peak10k[i]); + } + sum=0; + for (i=0;i<10;i=i+1){ + sum=peak10k[i]+sum; + } + ave=sum/10; + printf("avePeak10k=%0.3lf\n\r",ave); + return ave; + } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/detectors.h Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,10 @@ +#ifndef DETECTORS_H_ +#define DETECTORS_H_ + +double peakDetector (void); +double peakDetectorDebug (void); +double countPulse0 (void); +double countPulse0Debug (void); + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,53 @@ +#include "mbed.h" +#include "detectors.h" +#include "selection.h" +#include "cal.h" +#include <math.h> + + +int main() { + double Vin10k=3.3; + double Rs=300;//ohm + int typeSP=1;//type 1-series or 2-parallel + double Ro=33;//ohm + double f=10000;//Hz + + double pulse0=0;//measured quantity + double angle=0;//measured quantity + double Vo=0,Vo2=0,Vo3=0; + + double z=0; + + double Zx=0;//result + double Rx=0,Cx=0; + + VAddr(10); + //RAddr(2); + wait (0.5); + pulse0=countPulse0(); + Vo=peakDetector(); + + + /*wait (2); + printf("wait\n\r"); + pulse0=countPulse0Debug(); + Vo=peakDetectorDebug ();*/ + + angle=pwToAngle(pulse0); + z= cal_z(Vin10k,Vo,Rs); + printf("ave Vo=%0.3f\n\r",Vo); + printf("ave-pulse0=%0.3f\n\r",pulse0); + printf("angle_measure=%0.3f",angle); + + + cal_Zx (z,angle,f,typeSP,Ro,&Zx,&Cx,&Rx); + printf("|Zx|measured=%0.3lf ohm\n\r",Zx); + + if (typeSP==1) printf("in series\n\r"); + else if (typeSP==2) printf("in parallel\n\r"); + else printf("wrong type series/paralle?\n\r"); + + printf("Rx_measured=%0.6lf nF\n\r",Cx*1e9); + printf("Cx_measured=%0.3lf ohm\n\r",Rx); + printf("\n\r"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/aa5281ff4a02 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/selection.cpp Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,93 @@ +#include "mbed.h" +#include "selection.h" +//define the PWM output port +PwmOut PWMout(p21); + +//define the address bit of MUX for multi-Vin +//MSB B_A LSB, NB an inverter - transiver +// 01 1st 100kHz +// 10 2rd 10kHz +// 11 3rd 1kHz +DigitalOut VAddrA1(p6); +DigitalOut VAddrA0(p5); + +//define the address bit of MUX for ResistorArray +//MSB CBA LSB, NB an inverter - transiver +// 110 1st 1kohm +// 101 2rd +// 100 3rd +// 011 4th +// 010 5th +// 001 6th +// 000 7th +DigitalOut RAddrA0(p9); +DigitalOut RAddrA1(p10); +DigitalOut RAddrA2(p11); + + +void RAddr (int in) +{ + switch (in){ + case 0:RAddrA2=0; + RAddrA1=0; + RAddrA0=0; + break; + + case 1:RAddrA2=0; + RAddrA1=0; + RAddrA0=1; + break; + case 2:RAddrA2=0; + RAddrA1=1; + RAddrA0=0; + break; + case 3: RAddrA2=0; + RAddrA1=1; + RAddrA0=1; + break; + case 4: RAddrA2=1; + RAddrA1=0; + RAddrA0=0; + break; + case 5: RAddrA2=1; + RAddrA1=0; + RAddrA0=1; + break; + case 6:RAddrA2=1; + RAddrA1=1; + RAddrA0=0; + break; + case 7:RAddrA2=1; + RAddrA1=1; + RAddrA0=1; + break; + default:RAddrA2=1; + RAddrA1=1; + RAddrA0=1; + } +} +void VAddr (int in) +{ + switch (in){ + + case 100:PWMout.period(0.00001f); //100kHz_10us_0.00001s, + PWMout.write(0.50f); // 50% duty cycle, relative to period + VAddrA1=0; + VAddrA0=1; + break; + case 10:PWMout.period(0.0001f); //10kHz_100us_0.0001s, + PWMout.write(0.50f); // 50% duty cycle, relative to period + VAddrA1=1; + VAddrA0=0; + break; + case 1: PWMout.period(0.001f); //1kHz_1ms_0.001s, + PWMout.write(0.50f); // 50% duty cycle, relative to period + VAddrA1=1; + VAddrA0=1; + break; + default:PWMout.period(0.1f); + PWMout.write(0.50f); // 50% duty cycle, relative to period + VAddrA1=0; + VAddrA0=0; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/selection.h Thu Mar 08 14:38:32 2018 +0000 @@ -0,0 +1,7 @@ +#ifndef SELECTION_H_ +#define SELECTION_H_ + +void RAddr (int in); +void VAddr (int in); + +#endif \ No newline at end of file