Basic mTouch Sensor Configuration

The mTouch Framework scans all sensors in the same basic manner. All sensors (whether normal 'button', part of a slider or wheel, a single row in a matrix, or a proximity sensor) start by being scanned using the mTouch CVD waveform. Once all enabled sensors have a 'final' reading, the data ready flag is set and the decoding routine is called from the main loop.

Basic mTouch Sensor Configuration

  1. In the mTouch_config.h file, under 'System Setup' start by defining _XTAL_FREQ to the frequency of your processor's oscillator in Hz. This does not set the value of your oscillator - it merely tells the framework what the value is initialized to by your application.
  2. Set MTOUCH_INTEGRATION_TYPE to the behavior you desire for your application.
  3. Set MTOUCH_SCAN_FUNCTIONALITY to the behavior you desire for your application.
    • MTOUCH_SCANS_ONE_SENSOR :: means the framework only scans a single sensor per mTouch_Scan() call.
      • This is the preferred value. It provides the best noise immunity due to increased jittering.
    • MTOUCH_SCANS_ALL_SENSORS :: means the framework scans all sensors one time per mTouch_Scan() call.
      • This can be chosen to increase the sampling rate of the system. Trade-off: slightly decreased noise robustness
  4. Under 'Sensor Setup', set MTOUCH_NUMBER_SENSORS to the number of sensors to scan, in total.
    Note:
    Each sensor is usually a unique analog channel, but this is not required. To minimize the possibility of errors in the mTouch waveform, try not to repeat the same analog channel twice in a row. Also try and avoid having the first and last sensor as the same analog channel.
  5. For each sensor:
    1. Set MTOUCH_SENSORx to its corresponding analog channel.
       #define MTOUCH_SENSOR0                AN0     // Sensor0 is connected to AN4
      
    2. Set THRESHOLD_PRESS_SENSORx to the threshold value for this sensor.
       #define THRESHOLD_PRESS_SENSOR0       100     // Reading must be 100 counts > the baseline
      
      Note:
      This value is difficult to determine prior to running the system on the hardware and examining the level of sensitivity. 100-500 is a good default value but may need to be drastically adjusted based on the specific hardware design.
  6. Under 'Filtering', set MTOUCH_SAMPLES_PER_SCAN to the amount of oversampling to perform on each sensor.
  7. Under 'Decoding', set MTOUCH_BUTTON_TIMEOUT to the maximum number of consecutive 'pressed' state decisions in a row. After this counter is exceeded, the pressed sensor will be reset to unpressed. This creates a maximum press time and is an easy way to automatically recover from a stuck sensor.
  8. Set MTOUCH_DEBOUNCE_PRESS and MTOUCH_DEBOUNCE_RELEASE to the amount of debouncing to implement.

Basic mTouch Sensor API

First, we will need to set up the application to scan mTouch. To do this, we need to create a check in the main loop to service the mTouch decode function. If we are using mTouch as one of several ISRs, we also need to add the scanning call to our ISR logic.

 void main(void)
 {
     myApp_Init();                       // Your application's initialization (examples provided in main.c)

     mTouch_Init();                      // mTouch initialization

     #if defined(MCOMM_ENABLED)
     mComm_Init();                       // mComm initialization
     #endif

     INTCONbits.GIE = 1;                 // Initialization complete. Begin servicing interrupts.

     while(1)
     {
         if (mTouch_isDataReady())       // Is new information ready?
         {
             mTouch_Service();           // Decode the newly captured data and transmit new data updates.
             
             // Application sensor-state logic checks go here.
         }
     }
 }
 void interrupt ISR(void)
 {
     SAVE_STATE();                       // mTouch Framework-supplied general ISR save state macro. 
                                         // Not required, but convenient. 

     #if (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_ISR)
         if (mTouch_checkInterrupt())    // Checks if the TMRxIE and TMRxIF flags are both equal to 1.
         {
             mTouch_Scan();              // Required if running as ISR slave. The mTouch timer interrupt 
                                         // flag is cleared inside the mTouch_Scan() function.
         }
     #elif (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_MAINLOOP)
         mTouch_state.isrServiced = 1;   // Alerts the mTouch scanning routine that an interrupt may 
                                         // have disrupted a scan. This is cleared at the start of a
                                         // new scan and is checked at the end of the scan.
                                         // Bad data can affect the readings if this flag is not set.
     #endif
     
     
     RESTORE_STATE();                    // mTouch Framework-supplied general ISR restore state macro. 
                                         // Not required, but convienent.
 }

The API for accessing button states is mTouch_GetButtonState(i). The states have been enumerated with the labels: MTOUCH_INITIALIZING, MTOUCH_RELEASED, and MTOUCH_PRESSED.

 if (mTouch_GetButtonState(0) < MTOUCH_PRESSED)
 { 
     LED0 = LED_OFF;                     // Sensor0 not pressed. Turn off LED.
 } else { 
     LED0 = LED_ON;                      // Sensor0 pressed. Turn on LED.
 }