⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ab_t1.hpp

📁 C学习课件
💻 HPP
📖 第 1 页 / 共 4 页
字号:
float           AB_T1C1A::V        ( )
 {
   float V;
   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1C1A::AD_Path( 0x2 ); delay(1);
      V = AB_T1C1A::AD( );
      AB_T1C1A::AD_Path( 0x1 );
      return( V );
    }
   else
      return( 0.0 );
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
int             AB_T1C1A::ERR_NO   ( void )
 {
   return( AB_T1C1A::Err[AB_T1::ID] | AB_T1::Err[AB_T1::ID] );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1C1A::DA_12bit ( unsigned int D )
 {
   union { unsigned int                DATA;
           struct { unsigned D0 : 4;
                    unsigned D1 : 4;
                    unsigned D2 : 4;
                    unsigned D3 : 4; } PART; } DA_BUF;

   if( !AB_T1::Err[AB_T1::ID] )
    {
      DA_BUF.DATA = D;

      // Select
      AB_T1::S( 0x30 );

      // Write Data
      AB_T1::W( 0x7, DA_BUF.PART.D2 );
      AB_T1::W( 0x6, DA_BUF.PART.D1 );
      AB_T1::W( 0x5, DA_BUF.PART.D0 );

      // Update
      AB_T1::W( 0x4, 0x0 );
    }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
unsigned int    AB_T1C1A::AD_12bit ( )
 {
   union { unsigned int                DATA;
           struct { unsigned D0 : 4;
                    unsigned D1 : 4;
                    unsigned D2 : 4;
                    unsigned D3 : 4; } PART; } AD;
   int Times  = 0;
   int Ready  = 0;

   AD.DATA = 0;
   if( !AB_T1::Err[AB_T1::ID] )
    {
      // Select
      AB_T1::S( 0x30 );

      // Start convert
      AB_T1::R( 0x4 );

      // Wait
      for( Times=0 ; !Ready && (Times<10000); Times++ )
       {
          delay(1);
          Ready = AB_T1::R( 0x3 );
       }
      if( !Ready ) AB_T1::Err[AB_T1::ID] = 0x40;

      // Read Data
      AD.PART.D3 = 0x0;
      AD.PART.D2 = AB_T1::R( 0x7 );
      AD.PART.D1 = AB_T1::R( 0x6 );
      AD.PART.D0 = AB_T1::R( 0x5 );

    }
   return AD.DATA;
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void	        AB_T1C1A::AD_Path  ( unsigned PATH )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1::S( 0x30 );
      AB_T1::W( 0x2, PATH );
    }
 }

//=< AB_T1P2A >==============================================================//
//                                                                           //
//                                                                           //
//                                                                           //
//                                                                           //
//===========================================================================//

class AB_T1P2A :public AB_T1C1A {
   public:
                           AB_T1P2A  ( unsigned int P, unsigned PIN );
      void                 INIT      ( );

      int                  VR_Adj    ( int I );
      void                 DA_Cal    ( );
      void                 DA        ( float    V );

      void                 SETUP     ( unsigned N, unsigned R );
      void                 N         ( unsigned N );
      void                 R         ( unsigned R );
      float                V         ( );
      float                I         ( );

      int                  ERR_NO    ( );

   private:
      void                 DA_12bit  ( unsigned int D );
      void                 AD_Path   ( unsigned N );

      unsigned             PIN;
      unsigned             N_Save;
      unsigned             R_Save;
      float                RV        [4];
      float                DH        [4];
      float                DL        [4];
      float                AH        [4];
      float                AL        [4];

      int                  Err;

 };

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
                AB_T1P2A::AB_T1P2A ( unsigned int P, unsigned PIN )
                         :AB_T1C1A ( P )
 {
   PIN = ( (1 > PIN) || (PIN > 16) ) ? 1 : PIN - 1 ;
   AB_T1P2A::PIN = 0x10 + PIN;
   AB_T1P2A::INIT( );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::INIT     ( )
 {
   AB_T1P2A::N_Save =        0;
   AB_T1P2A::R_Save =        0;
   AB_T1P2A::RV[0]  =      0.0;
   AB_T1P2A::RV[1]  =      0.1;
   AB_T1P2A::RV[2]  =      1.0;
   AB_T1P2A::RV[3]  =     10.0;
   AB_T1P2A::DH[0]  =   3072.0;
   AB_T1P2A::DH[1]  =   3072.0;
   AB_T1P2A::DH[2]  =   3072.0;
   AB_T1P2A::DH[3]  =   3072.0;
   AB_T1P2A::DL[0]  =   1024.0;
   AB_T1P2A::DL[1]  =   1024.0;
   AB_T1P2A::DL[2]  =   1024.0;
   AB_T1P2A::DL[3]  =   1024.0;
   AB_T1P2A::AH[0]  =     10.0;
   AB_T1P2A::AH[1]  =     10.0;
   AB_T1P2A::AH[2]  =     10.0;
   AB_T1P2A::AH[3]  =     10.0;
   AB_T1P2A::AL[0]  =      0.0;
   AB_T1P2A::AL[1]  =      0.0;
   AB_T1P2A::AL[2]  =      0.0;
   AB_T1P2A::AL[3]  =      0.0;

   AB_T1P2A::Err    =        0;
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
int             AB_T1P2A::VR_Adj   ( int I )
 {
   int Diff = 0;

   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1::RST( );
      switch( I )
       {
         case 2:   AB_T1P2A::AD_Path( 0x8 );
                   AB_T1P2A::DA_12bit( 0x0C00 ); delay(1);
                   Diff  = (int) AB_T1C1A::AD_12bit( );
                   AB_T1P2A::DA_12bit( 0x0400 ); delay(1);
                   Diff -= (int) AB_T1C1A::AD_12bit( );
                   Diff -= (int) 0x0800;
	           break;
         case 3:   AB_T1P2A::AD_Path( 0x8 );
                   AB_T1P2A::DA_12bit( 0x0400 ); delay(1);
                   Diff  = (int) AB_T1C1A::AD_12bit( );
                   Diff -= (int) 0x0400;
	           break;
         default : Diff = 0;

       }
      AB_T1::RST(  );
    }
   return ( Diff );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::DA_Cal   ( )
 {
   float H, L;

   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1::RST( );
      AB_T1P2A::AD_Path( 0x8 );

      AB_T1P2A::SETUP( 0x8, 0x0 );
      AB_T1P2A::DA_12bit( 0x0C00 ); delay(10); AB_T1P2A::AH[0] = AB_T1C1A::AD();
      AB_T1P2A::DA_12bit( 0x0400 ); delay(10); AB_T1P2A::AL[0] = AB_T1C1A::AD();

//    AB_T1P2A::SETUP( 0x8, 0x1 );
//    AB_T1P2A::DA_12bit( 0x0C00 ); delay(10); AB_T1P2A::AH[0] = AB_T1C1A::AD();
//    AB_T1P2A::DA_12bit( 0x0400 ); delay(10); AB_T1P2A::AL[0] = AB_T1C1A::AD();

//    AB_T1P2A::SETUP( 0x8, 0x2 );
//    AB_T1P2A::DA_12bit( 0x0C00 ); delay(10); AB_T1P2A::AH[1] = AB_T1C1A::AD();
//    AB_T1P2A::DA_12bit( 0x0400 ); delay(10); AB_T1P2A::AL[1] = AB_T1C1A::AD();

//    AB_T1P2A::SETUP( 0x8, 0x4 );
//    AB_T1P2A::DA_12bit( 0x0C00 ); delay(10); AB_T1P2A::AH[2] = AB_T1C1A::AD();
//    AB_T1P2A::DA_12bit( 0x0400 ); delay(10); AB_T1P2A::AL[2] = AB_T1C1A::AD();

//    AB_T1P2A::SETUP( 0x8, 0x8 );
//    AB_T1P2A::DA_12bit( 0x0C00 ); delay(10); AB_T1P2A::AH[3] = AB_T1C1A::AD();
//    AB_T1P2A::DA_12bit( 0x0400 ); delay(10); AB_T1P2A::AL[3] = AB_T1C1A::AD();

      AB_T1::RST( );
    }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::DA       ( float V )
 {
   float DH, DL, AH, AL;
   unsigned int DA;

   if( !AB_T1::Err[AB_T1::ID] )
    {
//    if( N_Save == 0x4 ) {
//       switch( AB_T1P2A::R_Save )
//        {
//          case 2:   DH = AB_T1P2A::DH[1];
//                    DL = AB_T1P2A::DL[1];
//                    AH = AB_T1P2A::AH[1];
//                    AL = AB_T1P2A::AL[1];
//                    break;
//          case 4:   DH = AB_T1P2A::DH[2];
//                    DL = AB_T1P2A::DL[2];
//                    AH = AB_T1P2A::AH[2];
//                    AL = AB_T1P2A::AL[2];
//                    break;
//          case 8:   DH = AB_T1P2A::DH[3];
//                    DL = AB_T1P2A::DL[3];
//                    AH = AB_T1P2A::AH[3];
//                    AL = AB_T1P2A::AL[3];
//                    break;
//          default : DH = AB_T1P2A::DH[0];
//                    DL = AB_T1P2A::DL[0];
//                    AH = AB_T1P2A::AH[0];
//                    AL = AB_T1P2A::AL[0];
//        }
//     } else {
         DH = AB_T1P2A::DH[0];
         DL = AB_T1P2A::DL[0];
         AH = AB_T1P2A::AH[0];
         AL = AB_T1P2A::AL[0];
////       }

      if( AH == AL ) AB_T1P2A::Err |= 0x100;
      if( !AB_T1P2A::Err ) {
         DA = (unsigned int) ( (((V-AL)*(DH-DL))/(AH-AL))+DL+0.5 );
         AB_T1P2A::DA_12bit( DA );
       }
    }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::SETUP    ( unsigned N, unsigned R )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
	  AB_T1P2A::R ( 0x0 );
	  AB_T1P2A::N ( 0x8 );
	  AB_T1P2A::DA( 0.0 );
	  AB_T1P2A::R (  R  );
	  AB_T1P2A::N (  N  );
   }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::N        ( unsigned N )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
      N = (N>=8) ? 8 : ( (N>=4) ? 4 : 0 );
      if( N != AB_T1P2A::N_Save )
       {
         AB_T1::S( AB_T1P2A::PIN );
         if( N != 0 )
            AB_T1::W( 0x1, 0xC );
         AB_T1::W( 0x1, N );
         AB_T1P2A::N_Save = N;
       }
    }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::R        ( unsigned R )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
      if( R != AB_T1P2A::R_Save )
       {
         AB_T1::S( AB_T1P2A::PIN );
         if( R != 0 )
            AB_T1::W( 0x3, (AB_T1P2A::R_Save | R) );
         AB_T1::W( 0x3, R );
         AB_T1P2A::R_Save = R;
       }
    }
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
float           AB_T1P2A::V        ( )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1P2A::AD_Path( 0x4 ); delay(1);
      return( AB_T1C1A::AD( ) );
    }
   else
      return( 0.0 );
  }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
float           AB_T1P2A::I        ( )
 { float R = -1.0;

   float V1, V0;
   if( !AB_T1::Err[AB_T1::ID] )
    {
      AB_T1P2A::AD_Path( 0x8 ); delay(1); V1 = AB_T1C1A::AD( );
      AB_T1P2A::AD_Path( 0x4 ); delay(1); V0 = AB_T1C1A::AD( );

      if( AB_T1P2A::R_Save & 0x1 ) R = RV[0];
      if( AB_T1P2A::R_Save & 0x2 ) R = (R>=0) ? (R*RV[1])/(R+RV[1]) : RV[1];
      if( AB_T1P2A::R_Save & 0x4 ) R = (R>=0) ? (R*RV[2])/(R+RV[2]) : RV[2];
      if( AB_T1P2A::R_Save & 0x8 ) R = (R>=0) ? (R*RV[3])/(R+RV[3]) : RV[3];

      return( (R>0.0) ? ((V1-V0)/R)-((N_Save==0x4) ? V0/20.0 : 0.0 ) : 0.0 );
    }
   else
      return( 0 );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
int             AB_T1P2A::ERR_NO   ( void )
 {
   return( AB_T1P2A::Err | AB_T1C1A::Err[AB_T1::ID] | AB_T1::Err[AB_T1::ID] );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::DA_12bit ( unsigned int D )
 {
   union { unsigned int                DATA;
           struct { unsigned D0 : 4;
                    unsigned D1 : 4;
                    unsigned D2 : 4;
                    unsigned D3 : 4; } PART; } DA_BUF;

   if( !AB_T1::Err[AB_T1::ID] )
    {
      DA_BUF.DATA = D;

      // Select
      AB_T1::S( AB_T1P2A::PIN );

      // Write Data
      AB_T1::W( 0x7, DA_BUF.PART.D2 );
      AB_T1::W( 0x6, DA_BUF.PART.D1 );
      AB_T1::W( 0x5, DA_BUF.PART.D0 );

      // Update
      AB_T1::W( 0x4, 0x0 );
    }
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
void            AB_T1P2A::AD_Path  ( unsigned N )
 {
   if( !AB_T1::Err[AB_T1::ID] )
    {
      N  = (N>=8) ? 8 : ( (N>=4) ? 4 : 0 );

      // Select
      AB_T1::S( AB_T1P2A::PIN );

      // Write Data
      AB_T1::W( 0x2, N );
    }
 }

//=< AB_T1O1B >==============================================================//
//                                                                           //
//                                                                           //
//                                                                           //
//                                                                           //
//===========================================================================//

class AB_T1O1B :public AB_T1C1A {
   public:
                           AB_T1O1B  ( unsigned int P );
                           AB_T1O1B  ( unsigned int P, unsigned EXT );
      unsigned             LOC       ( );
      unsigned             LOC       ( unsigned EXT );
      void                 INIT      ( );
      int                  WHERE     ( );

      void                 CTL       ( unsigned MODE, unsigned int PIN );
      void                 CTL       ( unsigned MODE );
      void                 CTL_SET   ( unsigned int PIN );
      void                 CTL_SET   ( );
      void                 CTL_ON    ( unsigned int PIN );
      void                 CTL_ON    ( );
      void                 CTL_OFF   ( unsigned int PIN );
      void                 CTL_OFF   ( );
      void                 CTL_REL   ( );
      unsigned int         STU       ( );

      int                  ERR_NO    ( );

   private:
      static int           USED[4];
      unsigned             EXT;

      int                  Err;

 };

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
int             AB_T1O1B::USED[4] = { 0, 0, 0, 0 };
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
                AB_T1O1B::AB_T1O1B ( unsigned int P )
                         :AB_T1C1A ( P )
 {
   int      error=1;
   unsigned LOC_TMP=0x2F;

   for( unsigned S=0x20; !AB_T1::Err[AB_T1::ID] && (S<=0x23); S++ )
      if( AB_T1::C_ID(S) == 0x0D )
         if( LOC_TMP == 0x2F ) {
            LOC_TMP = S;
            error = 0;
         } else
            error = 1;

   AB_T1O1B::LOC( (error)?0:(LOC_TMP-0x1F) );
 }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
                AB_T1O1B::AB_T1O1B ( unsigned int P, unsigned EXT )
                         :AB_T1C1A ( P )
 {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -