to junhao

Dependencies:   mbed

cal.cpp

Committer:
xuzhang
Date:
2018-03-08
Revision:
0:1b13f03ce7eb

File content as of revision 0:1b13f03ce7eb:

#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;   
}