BSL 00000102

MP TypeADC
OS TypeOS : Multi-Thread Free-OS
HAL : Arduino
Calculation accuracyfloating-point calculation
Characteristic Conversionlinear conversion
Filter Exponential Moving Average ( EMA )
Diagnostics SP Max/Min , CP Max/Min , MP AnalogIn fault

Software Specifications

File ( Arduino )

main.inoapplication source code file
pcmp.cppADC Component source code file
pcmp.hADC Component header file
oss-ec.h OSS-EC header file

Source Code

  • Constants for components   Constants to match the component
  • Constants for MPU        Constants to match the microcontroller of the product
  • Constants for circuits      Constants to match the circuit of the product

// BCL No.00000102

// Filter definitions
#define iK_esf                  0.75F                   // Exponential Smoothing Factor
                                                         // CAUTION: k is in the range of 0 to 1.0

// MPU definitions
#define iADC_bit                10U                     // MPU ADC bit

#define iPIN_adc                A0                      // Arduino board : ADC pin
#define iVref_cnf               DEFAULT                 // Configures the reference voltage

// P-CMP ( Sysyem Parts , Circuit , IC ) definitions
// Model type System Parts definitions
#define iX_offset               0.0F                    // X offset [V]
#define iY_offset               0.0F                    // Y offset [unit]
#define iK_gain                 1.0F                    // Gain [V/unit]
#define iVout_max               3.3F                    // Vout Max [V]
#define iVout_min               0.0F                    // Vout Min [V]
#define iPhysical_max           100.0F                  // Physical Max [unit]
#define iPhysical_min           0.0F                    // Physical Min [unit]

// Circuit definitions
#define iADC_vdd                3.3F                    // MPU Vdd [V]
#define iK_tc                   1.0F                    // Circuit Transformation ratio


sp2ap_t Phy_Read( uint16 cmd )
{
         return( pcmp( cmd ) );
}

static sp2ap_t pcmp( uint16 cmd )
{
         mp2cp_t ai    = mpw( cmd );     // A/D value read
         cp2sp_t vi    = cp( ai );       // A/D value to Voltage value conversion
         sp2ap_t phy   = sp( vi );       // Voltage value to Physical value conversion
        
         return( phy );
}

static sp2ap_t sp( cp2sp_t in )
{
         sp2ap_t out = { iMP_read , iNormal ,iNormal , iNormal , 0.0F };
         cp2sp_t wk;
        
         // Input buffering
         wk = in;
        
         if( ( wk.cp_stt == iMP_read ) &&
             ( wk.cp_dia == iNormal )  &&
             ( wk.mp_dia == iNormal ) )
         {
                 // Voltage value to Physical value conversion
                 out.phy = ( wk.vi – iX_offset ) / iK_gain + iY_offset;
                
                 // Physical value Exponential Moving Averages calculation
                 out.phy = lib_f32_EMA( out.phy , &Phy_EMA );
                
                 // Physical value Max or Min Diagnostics
                 out.sp_dia = lib_f32_MaxMin( &out.phy , iPhysical_max , iPhysical_min );
         }
         else
         {
                 // Nothing to do
         }
        
         out.sp_stt = wk.cp_stt;
         out.cp_dia = wk.cp_dia;
         out.mp_dia = wk.mp_dia;
        
         return( out );
}

static cp2sp_t cp( mp2cp_t in )
{
         cp2sp_t out = { iMP_read , iNormal , iNormal , 0.0F };
         mp2cp_t wk;
        
         // Input buffering
         wk = in;
        
         if( ( wk.mp_stt == iMP_read ) &&
             ( wk.mp_dia == iNormal ) )
         {
                 // A/D value to Voltage value conversion
                 out.vi = (float32)wk.ai * iADC_vdd / ( 1<<iADC_bit ) * iK_tc;
                
                 // Voltage value Max or Min Diagnostics
                 out.cp_dia = lib_f32_MaxMin( &out.vi , iVout_max , iVout_min );
         }
         else
         {
                 // Nothing to do
         }
        
         out.cp_stt = wk.mp_stt;
         out.mp_dia = wk.mp_dia;
        
         return( out );
}

static mp2cp_t mpw( uint16 cmd )
{
         mp2cp_t out = { iMP_read , iNormal , 0U };
        
         if( cmd == iMP_read )
         {
                 out = mp();
         }
         else
         {
                 // Non Command Error
                 out.mp_dia = iMPL_NG;
                 out.mp_stt = iError;
         }
        
         return( out );
}

static mp2cp_t mp( void )
{
         mp2cp_t out = { iMP_read , iNormal , 0U };
         uint16 wk;
         uint8 sts;
        
         // Read A/D convension value
         sts = adc_read_u16_wrapper( an , &wk );
        
         // M-CAL judgment
         if( sts != iNg )                // M-CAL normal
         {
                 out.ai = wk;
                 out.mp_dia = iNormal;
                 out.mp_stt = iMP_read;
         }
         else                            // M-CAL abnormal
         {
                 out.mp_dia = iMPL_NG;
                 out.mp_stt = iError;
         }
        
         return( out );
}

OSS-EC Site