MP Type | ADC |
OS Type | OS : Multi-Thread Free-OS HAL : Arduino |
Calculation accuracy | floating-point calculation |
Characteristic Conversion | Non-linear conversion(Two-dimensional table method) |
Filter | Exponential Moving Average ( EMA ) |
Diagnostics | SP Max/Min , CP Max/Min , MP AnalogIn fault |
Software Specifications

File ( Arduino )
main.ino | application source code file |
pcmp.cpp | ADC Component source code file |
pcmp.h | ADC 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.00000106
// 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
// CAUTION : When Mbed is selected, 16 bit is fixed
#define iPIN_adc A0 // ADC pin
#define iVref_cnf DEFAULT // Configures the reference voltage
// P-CMP ( Sysyem Parts , Circuit , IC ) definitions
// Model type System Parts definitions
#define iData_num 5U // Number of characteristic table elements
#define iVout_max 2.0F // Vout Max [V]
#define iVout_min 0.0F // Vout Min [V]
#define iPhysical_max 100.0F // Distance Max [unit]
#define iPhysical_min 10.0F // Distance Min [unit]
const float32 Data_x[iData_num] = // X axis for table search : Voltage [V]
{
0.0F , 0.5F , 1.0F , 1.5F , 2.0F // X data
};
const float32 Data_y[iData_num] = // Y axis for table search : Physical [unit]
{
10.0F , 15.0F , 30.0F , 50.0F , 100.0F // Y data
};
// 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 = lib_f32_Tbl2D( wk.vi , &Tbl );
// Physical value Exponential Moving Average 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 );
}