• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

main.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *  © 2012 Microchip Technology Inc.                                       
00003  *  
00004  *  Project Name:    mTouch Framework v2.1
00005  *  FileName:        main.c
00006  *  Dependencies:    mTouch.h
00007  *  Processor:       See documentation for supported PIC® microcontrollers 
00008  *  Compiler:        HI-TECH Ver. 9.81 or later
00009  *  IDE:             MPLAB® IDE v8.50 (or later) or MPLAB® X                        
00010  *  Hardware:         
00011  *  Company:         
00012  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00013  *  Description:     Main routine
00014  *                   - Example implementation of the framework's API calls
00015  *                   - All application code should be implemented in this
00016  *                     or some other user-created file. This will allow
00017  *                     for better customer support and easier upgrades to
00018  *                     later firmware versions.
00019  *                   - See the documentation located in the docs/ folder
00020  *                     for a detailed guide on getting started with your
00021  *                     application and the mTouch framework.
00022  *************************************************************************/
00023 /*************************************************************************
00024  * MICROCHIP SOFTWARE NOTICE AND DISCLAIMER: You may use this software, and
00025  * any derivatives created by any person or entity by or on your behalf,
00026  * exclusively with Microchip's products in accordance with applicable
00027  * software license terms and conditions, a copy of which is provided for
00028  * your referencein accompanying documentation. Microchip and its licensors
00029  * retain all ownership and intellectual property rights in the
00030  * accompanying software and in all derivatives hereto.
00031  *
00032  * This software and any accompanying information is for suggestion only.
00033  * It does not modify Microchip's standard warranty for its products. You
00034  * agree that you are solely responsible for testing the software and
00035  * determining its suitability. Microchip has no obligation to modify,
00036  * test, certify, or support the software.
00037  *
00038  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
00039  * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
00040  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
00041  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE, ITS INTERACTION WITH
00042  * MICROCHIP'S PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY
00043  * APPLICATION.
00044  *
00045  * IN NO EVENT, WILL MICROCHIP BE LIABLE, WHETHER IN CONTRACT, WARRANTY,
00046  * TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), STRICT
00047  * LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT,
00048  * SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE,
00049  * FOR COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE,
00050  * HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY
00051  * OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW,
00052  * MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS
00053  * SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID
00054  * DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
00055  *
00056  * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF
00057  * THESE TERMS.
00058  *************************************************************************/
00062 
00063 
00064 #include "mTouch.h"                            // Required Include
00065 
00066 // CONFIGURATION SETTINGS
00067 #include "generic_processorConfigBits.h"        // Provided for ease-of-development. 
00068                                                 // Should not be used in an actual 
00069                                                 // application.
00070 
00071     // TIP: Valid configuration bit labels can be found in Hi-Tech's folder.
00072     //      C:\Program Files\HI-TECH Software<COMPILER NAME><VERSION NUMBER>\include
00073     //      Open the 'pic' header file that corresponds to your microcontroller.
00074     //      Ex: 'pic16f1937.h'  --  NOT 'as16f1937.h' or 'cas16f1937.h'
00075     
00076     //uint8_t     matrixOutput[2];
00077     
00078 // PROTOTYPES
00079     void            Example_System_Init (void);
00080     void interrupt  ISR                 (void);
00081 
00082 // START PROGRAM
00083 void main(void)
00084 {
00085     Example_System_Init();          // Your System Initialization Function
00086 
00087     
00088     //================================================================================================
00089     //  ____                  _              _           _____                _          _    ____ ___ 
00090     // |  _ \ ___  __ _ _   _(_)_ __ ___  __| |  _ __ __|_   _|__  _   _  ___| |__      / \  |  _ \_ _|
00091     // | |_) / _ \/ _` | | | | | '__/ _ \/ _` | | '_ ` _ \| |/ _ \| | | |/ __| '_ \    / _ \ | |_) | | 
00092     // |  _ <  __/ (_| | |_| | | | |  __/ (_| | | | | | | | | (_) | |_| | (__| | | |  / ___ \|  __/| | 
00093     // |_| \_\___|\__, |\__,_|_|_|  \___|\__,_| |_| |_| |_|_|\___/ \__,_|\___|_| |_| /_/   \_\_|  |___|
00094     //               |_|                    
00095     //================================================================================================                                                           
00096     
00097     mTouch_Init();                  // mTouch Initialization (Required)
00098     
00099     #if defined(MCOMM_ENABLED)
00100     mComm_Init();
00101     #endif
00102       
00103     INTCONbits.GIE = 1;             // Initialization complete. Begin servicing interrupts.
00104     
00105     while(1)
00106     {    
00107     
00108         if (mTouch_isDataReady())   // Is new information ready?
00109         {
00110             mTouch_Service();       // Decode the newly captured data and transmit new data updates.
00111 
00112     //------------------------------------------------------------------------------------------------
00113     //                              REQUIRED MTOUCH API ENDS HERE
00114     //
00115     // NOTE: The below API examples show some possible ways to integrate your application with
00116     //       the mTouch framework. These are not required, but may be helpful.
00117     //------------------------------------------------------------------------------------------------
00118     
00119     
00120             //  ____                              ____  _        _            _    ____ ___ 
00121             // / ___|  ___ _ __  ___  ___  _ __  / ___|| |_ __ _| |_ ___     / \  |  _ \_ _|
00122             // \___ \ / _ \ '_ \/ __|/ _ \| '__| \___ \| __/ _` | __/ _ \   / _ \ | |_) | | 
00123             //  ___) |  __/ | | \__ \ (_) | |     ___) | || (_| | ||  __/  / ___ \|  __/| | 
00124             // |____/ \___|_| |_|___/\___/|_|    |____/ \__\__,_|\__\___| /_/   \_\_|  |___| 
00125             
00126             // The mTouch framework does not have an automated, board-level output system. 
00127             // All system responses to sensor state changes must be programmed by you, as shown below.
00128 
00129             #define LED_ON  0
00130             #define LED_OFF 1
00131             
00132             #define LED0  LATC2
00133             #define LED1  LATC1
00134             #define LED2  LATC0
00135             #define LED3  LATA7
00136             #define LED4  LATA6
00137             #define LED5  LATA4
00138             #define LED6  LATD7
00139             #define LED7  LATD6
00140             #define LED8  LATD5
00141             #define LED9  LATD4
00142             #define LED10 LATD3
00143             #define LED11 LATD2
00144             #define LED12 LATD1
00145             #define LED13 LATD0
00146 
00147             
00148             // #define LED0 RA2
00149             // #define LED1 RA3            
00150             
00151             // #define LED0 GP1
00152             // #define LED1 GP2
00153             
00154             // Example of a sensor's output being directed to an I/O pin
00155             //
00156             // Ex:  (User presses)  LED turns on.
00157             //      (User releases) LED turns off.
00158             //      etc.
00159             
00160             
00161             // if (mTouch_GetButtonState(0) < MTOUCH_PRESSED) { LED0 = LED_OFF; } else { LED0 = LED_ON; } // Sensor0
00162             // if (mTouch_GetButtonState(1) < MTOUCH_PRESSED) { LED1 = LED_OFF; } else { LED1 = LED_ON; } // Sensor1
00163             // if (mTouch_GetButtonState(2) < MTOUCH_PRESSED) { LED2 = LED_OFF; } else { LED2 = LED_ON; } // Sensor2
00164             // if (mTouch_GetButtonState(3) < MTOUCH_PRESSED) { LED3 = LED_OFF; } else { LED3 = LED_ON; } // Sensor3
00165             // if (mTouch_GetButtonState(4) < MTOUCH_PRESSED) { LED4 = LED_OFF; } else { LED4 = LED_ON; } // Sensor4
00166             // if (mTouch_GetButtonState(5) < MTOUCH_PRESSED) { LED5 = LED_OFF; } else { LED5 = LED_ON; } // Sensor5
00167             // if (mTouch_GetButtonState(6) < MTOUCH_PRESSED) { LED6 = LED_OFF; } else { LED6 = LED_ON; } // Sensor6
00168             // if (mTouch_GetButtonState(7) < MTOUCH_PRESSED) { LED7 = LED_OFF; } else { LED7 = LED_ON; } // Sensor7
00169                 
00170 
00171             //  _____                 _           _    ____ ___ 
00172             // |_   _|__   __ _  __ _| | ___     / \  |  _ \_ _|
00173             //   | |/ _ \ / _` |/ _` | |/ _ \   / _ \ | |_) | | 
00174             //   | | (_) | (_| | (_| | |  __/  / ___ \|  __/| | 
00175             //   |_|\___/ \__, |\__, |_|\___| /_/   \_\_|  |___|
00176             //            |___/ |___/                       
00177                         
00178             // Example of a sensor using the toggle latch. This is a memory-efficient toggle implementation
00179             // that will alternate between on and off each time the sensor is pressed.
00180             //
00181             // Ex:  (User presses)  LED turns on. 
00182             //      (User releases) LED remains on. 
00183             //      (User presses)  LED turns off.
00184             //      (User releases) LED remains off.
00185             //      (User presses)  LED turns on.
00186             //      etc
00187             
00188             #if defined(MTOUCH_TOGGLE_ENABLED)
00189             if (mTouch_GetToggleState(0) == MTOUCH_TOGGLE_OFF) { LED0 = LED_OFF; } else { LED0 = LED_ON; }
00190             #endif
00191             
00192             
00193             //  ____  _ _     _                _    ____ ___ 
00194             // / ___|| (_) __| | ___ _ __     / \  |  _ \_ _|
00195             // \___ \| | |/ _` |/ _ \ '__|   / _ \ | |_) | | 
00196             //  ___) | | | (_| |  __/ |     / ___ \|  __/| | 
00197             // |____/|_|_|\__,_|\___|_|    /_/   \_\_|  |___|
00198              
00199             // Example slider output code
00200             #if defined(MTOUCH_SLIDERS_ENABLED)
00201             #if defined(MTOUCH_SLIDER_SCALING_ENABLED)
00202                 
00203                 // Scaling Enabled: mTouch_GetSlider(i) returns a value between 0 and 255.
00204                 #define NUMBER_OF_SLIDER_LED_OUTPUT     8
00205                 
00206                 #if     (NUMBER_OF_SLIDER_LED_OUTPUT ==  8)     // 8-LED Slider Output Example :: 9 distinct slider output values
00207                     if (mTouch_GetSlider(0) >= 28)  { LED0 = LED_ON; } else { LED0 = LED_OFF; }
00208                     if (mTouch_GetSlider(0) >= 57)  { LED1 = LED_ON; } else { LED1 = LED_OFF; }
00209                     if (mTouch_GetSlider(0) >= 85)  { LED2 = LED_ON; } else { LED2 = LED_OFF; }
00210                     if (mTouch_GetSlider(0) >= 114) { LED3 = LED_ON; } else { LED3 = LED_OFF; }
00211                     if (mTouch_GetSlider(0) >= 142) { LED4 = LED_ON; } else { LED4 = LED_OFF; }
00212                     if (mTouch_GetSlider(0) >= 170) { LED5 = LED_ON; } else { LED5 = LED_OFF; }
00213                     if (mTouch_GetSlider(0) >= 199) { LED6 = LED_ON; } else { LED6 = LED_OFF; }
00214                     if (mTouch_GetSlider(0) >= 227) { LED7 = LED_ON; } else { LED7 = LED_OFF; }
00215                 #elif   (NUMBER_OF_SLIDER_LED_OUTPUT ==  4)     // 4-LED Slider Output Example :: 5 distinct slider output values
00216                     if (mTouch_GetSlider(0) >= 51)   { LED0 = LED_ON; } else { LED0 = LED_OFF; }
00217                     if (mTouch_GetSlider(0) >= 102)  { LED1 = LED_ON; } else { LED1 = LED_OFF; }
00218                     if (mTouch_GetSlider(0) >= 153)  { LED2 = LED_ON; } else { LED2 = LED_OFF; }
00219                     if (mTouch_GetSlider(0) >= 204)  { LED3 = LED_ON; } else { LED3 = LED_OFF; }
00220                 #endif
00221                 
00222             #else
00223             
00224                 // Scaling Disabled: mTouch_GetSlider(0) returns a value between 0 and MTOUCH_SLIDER0_MAX_VALUE.
00225                 
00226                     #define MTOUCH_SLIDER0_MAX_VALUE     ((MTOUCH_NUMBER_SLIDER0_SENSORS - 1) * 256)
00227                     if (mTouch_GetSlider(0) >= (uint16_t)( 1/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED0 = LED_ON; } else { LED0 = LED_OFF; }
00228                     if (mTouch_GetSlider(0) >= (uint16_t)( 2/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED1 = LED_ON; } else { LED1 = LED_OFF; }
00229                     if (mTouch_GetSlider(0) >= (uint16_t)( 3/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED2 = LED_ON; } else { LED2 = LED_OFF; }
00230                     if (mTouch_GetSlider(0) >= (uint16_t)( 4/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED3 = LED_ON; } else { LED3 = LED_OFF; }
00231                     if (mTouch_GetSlider(0) >= (uint16_t)( 5/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED4 = LED_ON; } else { LED4 = LED_OFF; }
00232                     if (mTouch_GetSlider(0) >= (uint16_t)( 6/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED5 = LED_ON; } else { LED5 = LED_OFF; }
00233                     if (mTouch_GetSlider(0) >= (uint16_t)( 7/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED6 = LED_ON; } else { LED6 = LED_OFF; }
00234                     if (mTouch_GetSlider(0) >= (uint16_t)( 8/9 * MTOUCH_SLIDER0_MAX_VALUE)) { LED7 = LED_ON; } else { LED7 = LED_OFF; }
00235                     // NOTE: This is an example of a slider using 8 LEDs to display its current value.
00236                     //       By defining it like this, the number of sensors in slider 0 can be adjusted 
00237                     //       without changing how the slider's output is displayed.
00238             #endif
00239             #endif
00240             
00241             
00242             //  __  __       _        _           _    ____ ___ 
00243             // |  \/  | __ _| |_ _ __(_)_  __    / \  |  _ \_ _|
00244             // | |\/| |/ _` | __| '__| \ \/ /   / _ \ | |_) | | 
00245             // | |  | | (_| | |_| |  | |>  <   / ___ \|  __/| | 
00246             // |_|  |_|\__,_|\__|_|  |_/_/\_\ /_/   \_\_|  |___|
00247             // Example matrix output code for a 3x4 Matrix
00248 
00249             #if defined(MTOUCH_MATRIX_ENABLED)  
00250                 if (mTouch_Matrix_hasChanged())         // If the matrix value has changed since last time
00251                 {
00252                     mTouch_Matrix_latchNewValue();      // Save the new value of the matrix
00253                     
00254                     LED1  = LED_OFF;                    // Reset all matrix outputs
00255                     LED2  = LED_OFF;
00256                     LED3  = LED_OFF;
00257                     LED4  = LED_OFF;
00258                     LED5  = LED_OFF;
00259                     LED6  = LED_OFF;
00260                     LED7  = LED_OFF;
00261                     LED8  = LED_OFF;
00262                     LED9  = LED_OFF;
00263                     LED10 = LED_OFF;
00264                     LED11 = LED_OFF;
00265                     LED12 = LED_OFF;
00266                 }
00267 
00268                 if (mTouch_Matrix_isPressed())          // If the matrix is being pressed
00269                 {
00270                     switch(mTouch_Matrix_getRow())      // Find the row/column being pressed and output accordingly.
00271                     {
00272                         case 0:                         
00273                             switch(mTouch_Matrix_getColumn())
00274                             {
00275                                 case 0: LED1  = LED_ON; break;  // (0, 0)
00276                                 case 1: LED2  = LED_ON; break;  // (0, 1)
00277                                 case 2: LED3  = LED_ON; break;  // (0, 2)
00278                                 case 3: LED4  = LED_ON; break;  // (0, 3)
00279                             }
00280                             break;
00281                         case 1:
00282                             switch(mTouch_Matrix_getColumn())
00283                             {
00284                                 case 0: LED5  = LED_ON; break;  // (1, 0)
00285                                 case 1: LED6  = LED_ON; break;  // (1, 1)
00286                                 case 2: LED7  = LED_ON; break;  // (1, 2)
00287                                 case 3: LED8  = LED_ON; break;  // (1, 3)
00288                             }
00289                             break;
00290                         case 2:
00291                             switch(mTouch_Matrix_getColumn())
00292                             {
00293                                 case 0: LED9  = LED_ON; break;  // (2, 0)
00294                                 case 1: LED10 = LED_ON; break;  // (2, 1)
00295                                 case 2: LED11 = LED_ON; break;  // (2, 2)
00296                                 case 3: LED12 = LED_ON; break;  // (2, 3)
00297                             }
00298                             break;
00299                     }
00300                 }
00301             #endif
00302             
00303             //  __  __           _           _    ____ ___ 
00304             // |  \/  | ___   __| | ___     / \  |  _ \_ _|
00305             // | |\/| |/ _ \ / _` |/ _ \   / _ \ | |_) | | 
00306             // | |  | | (_) | (_| |  __/  / ___ \|  __/| | 
00307             // |_|  |_|\___/ \__,_|\___| /_/   \_\_|  |___|
00308             // 
00309             // Modes allow for different sensors to be read in different application states.
00310             //
00311             // IMPORTANT NOTE:
00312             //  The logic, below, assumes debouncing is set to 0. If debouncing is higher
00313             //  than this, the state of the sensors will be checked before their state has
00314             //  had an opportunity to update itself. 
00315             //
00316             // This is just one way in which the mode switching may be implemented.
00317             
00318             
00319             #if MTOUCH_NUM_MODES > 1
00320         
00321             mTouch_DisableScanning();   // Temporarily disable scanning while this logic completes
00322             
00323             // Are all of the current sensors initialized? If not, stay in the current mode and keep scanning until 
00324             // they are. mTouch_state.areInitialized is a single-bit reserved for making temporary, local checks 
00325             // such as this.
00326             mTouch_state.areInitialized = 1;
00327             
00328             if (mTouch_GetButtonState(0) == MTOUCH_INITIALIZING) {   mTouch_state.areInitialized = 0;    }
00329             #if (MTOUCH_NUMBER_SENSORS > 1)        
00330             if (mTouch_GetButtonState(1) == MTOUCH_INITIALIZING) {   mTouch_state.areInitialized = 0;    }
00331             #endif
00332             #if (MTOUCH_NUMBER_SENSORS > 2)        
00333             if (mTouch_GetButtonState(2) == MTOUCH_INITIALIZING) {   mTouch_state.areInitialized = 0;    }
00334             #endif
00335             
00336             if (mTouch_state.areInitialized && !mTouch_state.skippedDecode)
00337             {
00338                 if (mTouch_modeIndex == 0)
00339                 {
00340                     if (mTouch_GetButtonState(1) == MTOUCH_PRESSED)
00341                     {
00342                         mTouch_ChangeMode(1);
00343                     }
00344                 }
00345                 else if (mTouch_modeIndex == 1)
00346                 {
00347                     #if defined(MCOMM_ENABLED) && defined(MCOMM_UART_HARDWARE_USED) 
00348                                                         // If we are outputting data using a hardware UART...
00349                         while(MCOMM_UART_TXIF == 0);    // Finish all communications before entering sleep
00350                     #endif
00351                     
00352                     //PIC_SWDTEN_ON();                  // If using a software-enabled WDT, enable it now.
00353                     //SLEEP();                          // Sleep, if you want to.
00354                     //NOP();                            // One more instruction is executed before sleeping.
00355                     
00356                     if (mTouch_GetButtonState(0) == MTOUCH_PRESSED)
00357                     {
00358                         mTouch_ChangeMode(0);
00359                     }
00360                     
00361                     //PIC_SWDTEN_OFF();
00362                 }
00363             }
00364             mTouch_EnableScanning();    // Re-enable scanning
00365             #endif
00366             
00367         } // end - mTouch_isDataReady() check
00368         
00369         
00370         
00371         //  _____                       ____       _            _   _                  _    ____ ___ 
00372         // | ____|_ __ _ __ ___  _ __  |  _ \  ___| |_ ___  ___| |_(_) ___  _ __      / \  |  _ \_ _|
00373         // |  _| | '__| '__/ _ \| '__| | | | |/ _ \ __/ _ \/ __| __| |/ _ \| '_ \    / _ \ | |_) | | 
00374         // | |___| |  | | | (_) | |    | |_| |  __/ ||  __/ (__| |_| | (_) | | | |  / ___ \|  __/| | 
00375         // |_____|_|  |_|  \___/|_|    |____/ \___|\__\___|\___|\__|_|\___/|_| |_| /_/   \_\_|  |___|
00376         
00377         // Checks whether any sensors are shorted to VDD or VSS
00378         #if defined(MTOUCH_ERROR_DETECTION_ENABLED)
00379         mTouch_ErrorDetect();
00380         if (mTouch_state.error)
00381         {
00382             // Perform action to notify the user of error.
00383         }
00384         #endif
00385         
00386 
00387         
00388     } // end - while(1) main loop
00389 } // end - main() function
00390 
00391 
00392 //================================================================================================
00393 //  _____                           _        ____            _                   ___       _ _   
00394 // | ____|_  ____ _ _ __ ___  _ __ | | ___  / ___| _   _ ___| |_ ___ _ __ ___   |_ _|_ __ (_) |_ 
00395 // |  _| \ \/ / _` | '_ ` _ \| '_ \| |/ _ \ \___ \| | | / __| __/ _ \ '_ ` _ \   | || '_ \| | __|
00396 // | |___ >  < (_| | | | | | | |_) | |  __/  ___) | |_| \__ \ ||  __/ | | | | |  | || | | | | |_ 
00397 // |_____/_/\_\__,_|_| |_| |_| .__/|_|\___| |____/ \__, |___/\__\___|_| |_| |_| |___|_| |_|_|\__|
00398 //                           |_|                   |___/                          
00399 //================================================================================================               
00400 
00401 void Example_System_Init() 
00402 {
00403     // The mTouch framework controls these modules:
00404     // *  TMR0  - YOU MUST INIT THE OPTION REGISTER / TMR0 PRESCALER
00405     //            Do not choose 1:1 prescaling.
00406     //
00407     // *  ADC   - automatic initialization
00408     // *  UART  - automatic initialization (if enabled and available)
00409     //
00410     // mTouch performs better as Fosc increases.    
00411     
00412     // NOTE: Update the configuration file if Fosc is changed!
00413     #if defined(_PIC14E)
00414         #if     _XTAL_FREQ == 32000000
00415         OSCCON  = 0b01110000;       // 32 MHz Fosc w/ PLLEN_ON (config bit)  
00416         #elif   _XTAL_FREQ == 16000000
00417         OSCCON  = 0b01111000;       // 16 MHz (PLLEN config bit doesn't matter)
00418         #endif
00419     #elif defined(_PIC18)
00420         OSCCONbits.IRCF = 7;        // 8 MHz
00421     #endif
00422     
00423     
00424     // EXAMPLE PORT INITIALIZATION
00425     //
00426     // mTouch sensor pins should be initialized as digital, output low.
00427     #if   defined(_16F1937)
00428     ANSELA      = 0b00000000;       
00429     ANSELB      = 0b00000000;       
00430     ANSELD      = 0b00000000;
00431     ANSELE      = 0b00000000;
00432     TRISA       = 0b00000000;
00433     TRISB       = 0b00000000;
00434     TRISC       = 0b00000000;
00435     TRISD       = 0b00000000;
00436     TRISE       = 0b00000000;
00437     PORTA       = 0b11011110;    
00438     PORTB       = 0b00000000;    
00439     PORTC       = 0b10100111;    
00440     PORTD       = 0b11111111;    
00441     PORTE       = 0b00000111; 
00442     #endif
00443     
00444     // EXAMPLE TIMER INITIALIZATIONS   
00445     //
00446     // Only an 8-bit timer may be used as the mTouch framework timer. 
00447     // TMR1/3/5 are not currently able to be used for this purpose.
00448     #if defined(MTOUCH_ISR_TIMER)
00449     #if (MTOUCH_ISR_TIMER == 0)
00450         #if defined(_PIC18)
00451         T0CON       = 0b11000000;   // TMR0 is 8-bit. Prescaler = 1:2
00452         #else
00453         OPTION_REG  = 0b10000000;   // TMR0 Prescaler  = 1:2
00454         #endif
00455     #elif (MTOUCH_ISR_TIMER == 2)
00456         T2CON       = 0b00001100;   // TMR2 Postscaler = 1:2
00457         PR2         = 0xFF;
00458     #elif (MTOUCH_ISR_TIMER == 4)
00459         T4CON       = 0b00001100;   // TMR4 Postscaler = 1:2
00460         PR4         = 0xFF;
00461     #elif (MTOUCH_ISR_TIMER == 6)
00462         T6CON       = 0b00001100;   // TMR6 Postscaler = 1:2
00463         PR6         = 0xFF;
00464     #endif
00465     #endif
00466 }
00467 
00468 
00469 //================================================================================================
00470 //  _____                           _        ___ ____  ____  
00471 // | ____|_  ____ _ _ __ ___  _ __ | | ___  |_ _/ ___||  _ \ 
00472 // |  _| \ \/ / _` | '_ ` _ \| '_ \| |/ _ \  | |\___ \| |_) |
00473 // | |___ >  < (_| | | | | | | |_) | |  __/  | | ___) |  _ < 
00474 // |_____/_/\_\__,_|_| |_| |_| .__/|_|\___| |___|____/|_| \_\
00475 //                           |_|                                                                                     
00476 //================================================================================================
00477 
00478 #if (MTOUCH_INTEGRATION_TYPE != MTOUCH_CONTROLS_ISR)
00479 void interrupt ISR(void)
00480 {
00481     // EXAMPLE INTERRUPT SERVICE ROUTINE
00482     //
00483     // If MTOUCH_INTEGRATION_TYPE is defined as MTOUCH_CONTROLS_ISR, the framework will implement 
00484     // a dedicated ISR for the mTouch scans' use. If it is not defined, the application may implement 
00485     // its own ISR. 
00486     //
00487     // A few rules must be followed by custom ISR functions:
00488     //
00489     // 1. If MTOUCH_INTEGRATION_TYPE is defined as MTOUCH_CALLED_FROM_MAINLOOP, you must set 
00490     //    mTouch_state.isrServiced each time you enter the ISR. This tells the framework that the scan 
00491     //    was interrupted and needs to be repeated.
00492     //
00493     // 2. If MTOUCH_INTEGRATION_TYPE is defined as MTOUCH_CALLED_FROM_ISR, the example API usage below 
00494     //    is required to service mTouch scanning.
00495 
00496     SAVE_STATE();                       // mTouch Framework-supplied general ISR save state macro. 
00497                                         // Not required, but convenient. 
00498 
00499     #if defined(MCOMM_ENABLED) && defined(MCOMM_TWO_WAY_ENABLED)  
00500         // If the V2 comms have been enabled, we need to receive incoming requests.
00501         #if     (MCOMM_TYPE == MCOMM_UART_TWO_WAY)
00502         if (MCOMM_UART_RCIE && MCOMM_UART_RCIF)     // Check UART for new data
00503         #elif   (MCOMM_TYPE == MCOMM_I2C_TWO_WAY)
00504         if (MCOMM_I2C_SSPIE && MCOMM_I2C_SSPIF)      // Check I2C for new data
00505         #elif   (MCOMM_TYPE == MCOMM_SPI_TWO_WAY)
00506         if (MCOMM_SPI_SSPIE && MCOMM_SPI_SSPIF)      // Check SPI for new data
00507         #endif
00508         {
00509             mComm_Receive();            // Two-way Communication Receive Service Function
00510         }
00511         
00512     #endif  
00513     
00514     
00515     #if (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_ISR)
00516         if (mTouch_checkInterrupt())    // Checks if the TMRxIE and TMRxIF flags are both equal to 1.
00517         {
00518             mTouch_Scan();              // Required if running as ISR slave. The mTouch timer interrupt 
00519                                         // flag is cleared inside the mTouch_Scan() function.
00520         }
00521     #elif (MTOUCH_INTEGRATION_TYPE == MTOUCH_CALLED_FROM_MAINLOOP)
00522         mTouch_state.isrServiced = 1;   // Alerts the mTouch scanning routine that an interrupt may 
00523                                         // have disrupted a scan. This is cleared at the start of a
00524                                         // new scan and is checked at the end of the scan.
00525                                         // Bad data can affect the readings if this flag is not set.
00526     #endif
00527     
00528     
00529     RESTORE_STATE();                    // mTouch Framework-supplied general ISR restore state macro. 
00530                                         // Not required, but convienent.
00531 }
00532 #endif
00533 
00535 
00536 

mTouch Framework v2.1 documentation by  Click here to visit our website at www.microchip.com