For the optimal parameters calculation the compiler optimization must be set to the required level. If the version of the compiler or optimization level is changed then the optimization process must be repeated again. The Debugger Watch Window can be used to see the result of the optimization. When the compiler optimization is on some variables can be optimized out and can be not available for the debugger. All variables displayed in the Watch Window must be global and declared as volatile. The code below calculates optimal parameters:
// Header file for mTouch library API
#include "mTouch.h"
//////////////////////////////////////////////////////////
// GLOBAL VARIABLES
//////////////////////////////////////////////////////////
// This structure will contain the optimal CTMU current.
volatile DEBUGCURRENT* pOptimalCurrent;
// This structure will contain the optimal CTMU charge delay.
volatile DEBUGDELAY* pOptimalDelay;
// This variable will contain the optimal threshold.
volatile UINT16 optimalThreshold;
void main(void)
{
// STEP 1
// mTouch library initialization.
MTouchInit();
// STEP 2
// Sensors initialization. All sensors must be initialized
// see MTOUCH_SENSORS_NUMBER in mTouchConfig.h).
// Set default parameters.
// Sensor #0 is connected to RB0/AN0 pin
MTouchSetSensor(0, // sensor number
&TRISB, // port B
&LATB,
0, // IO bit number
0, // analog channel number
-1, // press detection threshold by default
// (see MTOUCH_DEFAULT_THRESHOLD in
// mTouchConfig.h)
-1, // oversampling by default
//(see MTOUCH_DEFAULT_OVERSAMPLING in
// mTouchConfig.h)
-1 ); // CTMU charge delay by default
//(see MTOUCH_DEFAULT_CHARGE_DELAY in
// mTouchConfig.h,
// not used for CVD acquisition)
// STEP 3
// MTouchDebugCurrent(sensorNumber) function calculates the optimal CTMU
// current value (optimal CTMU charge delay will be about 8).
// This will be a final value for MTOUCH_CTMU_CURRENT parameter in
// mTouchConfig.h.
// Before measurement set MTOUCH_CTMU_CURRENT to 0x01.
// Sensor #0 is tested.
pOptimalCurrent = MTouchDebugCurrent(0);
// Set adjusted CTMU current value.
MTouchSetCTMUCurrent(pOptimalCurrent->current);
// STEP 4
// MTouchDebugDelay(sensorNumber) function calculates the optimal
// CTMU charge delay value to provide charging of sensor to
// about 75% of AVdd.
// Optimal delay for sensor #0.
pOptimalDelay = MTouchDebugDelay(0);
// Set adjusted CTMU charge delay value for the sensor # 0
MTouchSetChargeDelay(0, pOptimalDelay->delay);
// STEP 5
// MTouchDebugThreshold(sensorNumber) function calculates the optimal
// press detection threshold value. It waits for the sensor press event
// from user to measure maximum signal amplitude (delta).
// Optimal threshold for sensor #0.
optimalThreshold = MTouchDebugThreshold(0);
// Set adjusted threshold value for the sensor # 0
MTouchSetThreshold(0, optimalThreshold);
// STEP 6
// Put break point here. Use Watch Window to see
// pOptimalCurrent->current, pOptimalDelay->delay and optimalThreshold
// values.
while(1);
}
After the code execution the result in the Debugger Watch Window can be:
pOptimalCurrent->error field shows an offset of the CTMU charge delay from nominal value (8). pOptimalDelay->error fiels shows an offset of the sample for the adjusted charge delay from the nominal value (should be less than 128 for 10-bit ADC and less than 512 for 12-bit ADC).