Often the ADC must be used for many different tasks. To share the ADC between mTouch Library and these tasks the state machine can be used. The code example below shows possible implementation for the system where ADC is shared between Touch Screen, mTouch Buttons and Battery Level Measurement. Battery_ADCInit() and TouchScreen_ADCInit() functions configure ADC and Vref as needed for these modules.
// This variable holds the state machine current state.
volatile int current_state = STATE_TOUCH_SCREEN;
// The state machine is run by the timer interrupt.
void __attribute__((interrupt, shadow, auto_psv)) _T4Interrupt(void)
{
switch(current_state) // The state machine main switch start.
{
case STATE_TOUCH_SCREEN:
// If touch screen scan is finished then switch to mTouch Buttons task.
// The TouchScreenDetectPosition() function runs the touch screen state machine.
// A few calls of TouchScreenDetectPosition() are required to detect a touch on the touch screen.
// When the position is detected this function returns non-zero.
if(TouchScreenDetectPosition()!= 0)
{
// Initialize ACD for mTouch Buttons.
MTouchInit();
current_state = STATE_MTOUCH_BUTTONS;
}
break;
case STATE_MTOUCH_BUTTONS:
// Get data from capacitive buttons.
MTouchAcquisition();
// Initialize ACD for Battery Level Measurement.
Battery_ADCInit();
current_state = STATE_BATTERY_LEVEL;
break;
case STATE_BATTERY_LEVEL:
// BatteryLevelDetect() measures the battery level.
BatteryLevelDetect();
// Initialize ACD for touch screen.
TouchScreen_ADCInit();
current_state = STATE_TOUCH_SCREEN;
break;
} // The state machine main switch end.
// Clear timer interrupt flag.
TMR4 =0; IFS1bits.T4IF = 0;
} // End of timer interrupt.