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

mTouchCVD_Comm.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *  © 2011 Microchip Technology Inc.                                       
00003  *  
00004  *  Project Name:    mTouch CVD Framework v1.1
00005  *  FileName:        mTouchCVD_Comm.c
00006  *  Dependencies:    mTouchCVD.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:     mTouch CVD Framework Communication Module
00014  *                   - Implements the communication between the mTouch
00015  *                     PC GUI program and the firmware. May also be used
00016  *                     to output directly to a PC's terminal.
00017  *************************************************************************/
00018 /**************************************************************************
00019  * MICROCHIP SOFTWARE NOTICE AND DISCLAIMER: You may use this software, and 
00020  * any derivatives created by any person or entity by or on your behalf, 
00021  * exclusively with Microchip's products in accordance with applicable
00022  * software license terms and conditions, a copy of which is provided for
00023  * your referencein accompanying documentation. Microchip and its licensors 
00024  * retain all ownership and intellectual property rights in the 
00025  * accompanying software and in all derivatives hereto. 
00026  * 
00027  * This software and any accompanying information is for suggestion only. 
00028  * It does not modify Microchip's standard warranty for its products. You 
00029  * agree that you are solely responsible for testing the software and 
00030  * determining its suitability. Microchip has no obligation to modify, 
00031  * test, certify, or support the software. 
00032  * 
00033  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 
00034  * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED 
00035  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 
00036  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE, ITS INTERACTION WITH 
00037  * MICROCHIP'S PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY 
00038  * APPLICATION. 
00039  * 
00040  * IN NO EVENT, WILL MICROCHIP BE LIABLE, WHETHER IN CONTRACT, WARRANTY, 
00041  * TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), STRICT 
00042  * LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, 
00043  * SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, 
00044  * FOR COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, 
00045  * HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY 
00046  * OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW, 
00047  * MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS 
00048  * SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID 
00049  * DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. 
00050  * 
00051  * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF 
00052  * THESE TERMS. 
00053  *************************************************************************/
00057 //*****************************************************************************
00058 // Include and Header files
00059 //*****************************************************************************
00060 #include <stdlib.h>
00061 #include "includes/mTouchCVD.h"
00062 
00063 #if defined(CVD_DEBUG) && (CVD_DEBUG == 1)
00064 //*****************************************************************************
00065 // Local Function Prototypes
00066 //*****************************************************************************
00068     /* System Support Functions */
00070 void           mTouchCVD_Comm_Init(void);
00071 
00073     /* Communication Functions */
00075 void           send_Data(void);
00076 
00078     /* Communication Support Functions */
00080 void           put_Char     (unsigned char);
00081 void           str_Int      (unsigned int);
00082 void           str_Char     (unsigned char);
00083 
00084 
00086 //*****************************************************************************
00087 // SUPPORT FUNCTIONS
00088 //*****************************************************************************
00090 
00091 /****************************************************************
00092 * Function: void Init(void)
00093 *
00094 * Overview: This function configures the peripherals and presets
00095 *           the system variables
00096 *
00097 * Input:    None
00098 *
00099 * Output:   None
00100 *
00101 ****************************************************************/
00105 void mTouchCVD_Comm_Init(void)
00106 {
00107     // Turn on the Hardware UART - this is defined in each HardwareProfile header file
00108     CVD_DEBUG_COMM_INIT();
00109 }
00110 
00111 
00113 //*****************************************************************************
00114 // Communications Functions
00115 //*****************************************************************************
00117 
00121 void send_Data(void)
00122 {    
00123     #if CVD_Software_Revision == 0002
00124     // If using the 0002 version of the GUI, sensor state information should be
00125     // sent before outputting the current reading values.
00126         unsigned int btnMsk = 0;
00127         unsigned int bufMsk = 1;
00128         for (unsigned char i = 0; i < CVD_NUMBER_SENSORS; i++)
00129         {
00130             if (CVD_GetButtonState(i) < CVD_PRESSED)
00131             {
00132                 btnMsk |= bufMsk;
00133             }
00134             bufMsk <<= 1;
00135         }
00136         btnMsk = ~btnMsk;            // Bitwise 'NOT' because bits are inverted 
00137         
00138         str_Int(btnMsk);             // Output sensor state data
00139     #endif
00140     
00141     // For all versions of the GUI, output the current reading values.
00142     for (unsigned char i = 0; i < CVD_NUMBER_SENSORS; i++)
00143     { 
00144         #if defined(CVD_DEBUG_OUTPUT_RAW)
00145         str_Int(CVDGetSensor(i));
00146         #endif
00147         #if defined(CVD_DEBUG_OUTPUT_AVG)
00148         str_Int(Average[i]);
00149         #endif
00150     }
00151     
00152     // Carriage return, line feed
00153     put_Char(0x0D);
00154     put_Char(0x0A);    
00155 }
00156 
00158 //*****************************************************************************
00159 // Communications Support functions
00160 //*****************************************************************************
00162 
00163 /****************************************************************
00164 * Function: void str_Int(unsigned int i)
00165 *
00166 * Overview: This function sends an INT as a space and 5 digits
00167 *
00168 * Input:    Din is the value to be converted
00169 *           base is the radix of the value
00170 *
00171 * Output:   None
00172 *
00173 ****************************************************************/
00179 void str_Int(unsigned int i)
00180 {
00181         unsigned char ctr = 0;
00182     
00183     #if defined(CVD_DEBUG_OUTPUT_HEX)
00184     
00185     // Hex Output
00186     ctr = (unsigned char)(i >> 12);
00187     if (ctr <= 9) { put_Char(ctr+0x30); } else { put_Char(ctr+0x37); }
00188     ctr = (unsigned char)(i >> 8) & 0x0F;
00189     if (ctr <= 9) { put_Char(ctr+0x30); } else { put_Char(ctr+0x37); }
00190     ctr = (unsigned char)(i >> 4) & 0x0F;
00191     if (ctr <= 9) { put_Char(ctr+0x30); } else { put_Char(ctr+0x37); }
00192     ctr = (unsigned char)(i & 0x0F);
00193     if (ctr <= 9) { put_Char(ctr+0x30); } else { put_Char(ctr+0x37); }
00194     
00195     #else
00196     
00197     // Decimal Output
00198         while (i >= 10000)  { i -= 10000; ctr++; } put_Char(ctr+0x30); ctr=0;
00199         while (i >=  1000)  { i -=  1000; ctr++; } put_Char(ctr+0x30); ctr=0;
00200         while (i >=   100)  { i -=   100; ctr++; } put_Char(ctr+0x30); ctr=0;
00201         while (i >=    10)  { i -=    10; ctr++; } put_Char(ctr+0x30); ctr=0;
00202         while (i >=     1)  { i -=     1; ctr++; } put_Char(ctr+0x30);  
00203     
00204     #endif
00205     
00206     // A semi-colon delimiter is used for the GUI. This can be changed if
00207     // you are not using the GUI to process the output.
00208     put_Char(';');
00209 }
00210 
00211 
00218 void put_Char(unsigned char Dout)
00219 {
00220     #if defined(CVD_DEBUG_UART_ENABLED)
00221         while(CVD_DEBUG_COMM_TXIF == 0);
00222         CVD_DEBUG_COMM_TXREG = Dout;
00223     #else
00224         #define RS232__RATE         CVD_DEBUG_SPEED  
00225         #define RS232__PORT         CVD_DEBUG_PIN     
00226         #if (_XTAL_FREQ == 8000000)
00227             #if (RS232__RATE == 9600)
00228                 #define RS232__DELAY            22 
00229                 #define RS232__DELAYCODE()  for (j = RS232__DELAY; j > 0; j--) { NOP(); NOP(); NOP(); NOP(); NOP(); } NOP(); NOP(); NOP(); NOP(); 
00230             #elif (RS232__RATE == 56700)
00231                 #define RS232__DELAY            3   
00232                 #define RS232__DELAYCODE()  for (j = RS232__DELAY; j > 0; j--) { NOP(); NOP(); NOP(); } NOP();
00233             #elif (RS232__RATE == 115200)
00234                 #define RS232__DELAY            1
00235                 #define RS232__DELAYCODE()  NOP(); NOP(); NOP(); NOP(); NOP(); NOP(); NOP();
00236             #else
00237                 #error The chosen RS232 baud rate is not supported. Choose either 9600, 57600, or 115200.
00238             #endif
00239         #elif (_XTAL_FREQ == 4000000)
00240             #if (RS232__RATE == 9600)
00241                 #define RS232__DELAY            10 
00242                 #define RS232__DELAYCODE()  for (j = RS232__DELAY; j > 0; j--) { NOP(); NOP(); NOP(); NOP(); NOP(); } NOP(); NOP(); NOP();
00243             #elif (RS232__RATE == 56700)
00244                 #define RS232__DELAY            1   
00245                 #define RS232__DELAYCODE()  NOP(); NOP(); NOP(); NOP(); NOP(); NOP(); NOP();
00246             #elif (RS232__RATE == 115200)
00247                 #error 115.2k baud rate is not available with a 4MHz oscillator. Choose 9600 or 57600, or move to a 8MHz oscillator.
00248             #else
00249                 #error The chosen RS232 baud rate is not supported. Choose either 9600 or 57600.
00250             #endif
00251         #else
00252             #warning Current clock frequency is not a supported option for this version of RS232 code.
00253         #endif
00254         
00255         //DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT
00257         // Software UART Documentation
00258         //                                                                 
00259         //              b0  b1  b2  b3  b4  b5  b6  b7                   
00260         // _ _ _ _     _ _                     _ _         _ _ _ _ _ _ _         
00261         //  IDLE  |_S_| 1 |_0_ _0_ _0_ _0_ _0_| 1 |_0_ _0_| P    IDLE           
00262         //  |       |                                   |   |    |                
00263         //  IDLE    START                            PARITY STOP  IDLE          
00264         //                                                                 
00265         //             b<7:0> = 0x41 = 65 = 'A'                                                                                                              
00266         //
00267         // Design Spec:
00268         //       Use 8-bit transmissions
00269         //       Parity none  {Will use "No Parity" for Parity, this bit is not sent.  (Example shows parity=0)}
00270         //   Delay tailored to 4MHz, Tbit=17.2usec,  baud=58139 @ -0.94% of 57.6k     
00272         //DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT! DO NOT EDIT
00273 
00274         //  START BIT
00275         RS232__PORT = 0;
00276         if (!(Dout & 0x00)) { NOP(); NOP(); NOP(); NOP(); NOP(); NOP(); }
00277         RS232__DELAYCODE();
00278 
00279         //  BIT 0
00280         if   (Dout & 0x01)  { RS232__PORT = 1; } else { RS232__PORT = 0; }    
00281         if (!(Dout & 0x01)) { NOP(); NOP(); NOP(); NOP(); }
00282         RS232__DELAYCODE();
00283         
00284         //  BIT 1
00285         if   (Dout & 0x02)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00286         if (!(Dout & 0x02)) { NOP(); NOP(); NOP(); NOP(); }
00287         RS232__DELAYCODE();
00288         
00289         //  BIT 2
00290         if   (Dout & 0x04)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00291         if (!(Dout & 0x04)) { NOP(); NOP(); NOP(); NOP(); }
00292         RS232__DELAYCODE();
00293         
00294         //  BIT 3
00295         if   (Dout & 0x08)  { RS232__PORT = 1; } else { RS232__PORT = 0; }   
00296         if (!(Dout & 0x08)) { NOP(); NOP(); NOP(); NOP(); }
00297         RS232__DELAYCODE();
00298         
00299         //  BIT 4
00300         if   (Dout & 0x10)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00301         if (!(Dout & 0x10)) { NOP(); NOP(); NOP(); NOP(); }
00302         RS232__DELAYCODE();
00303 
00304         //  BIT 5
00305         if   (Dout & 0x20)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00306         if (!(Dout & 0x20)) { NOP(); NOP(); NOP(); NOP(); }
00307         RS232__DELAYCODE();
00308         
00309         //  BIT 6
00310         if   (Dout & 0x40)  { RS232__PORT = 1; } else { RS232__PORT = 0; }      
00311         if (!(Dout & 0x40)) { NOP(); NOP(); NOP(); NOP(); }
00312         RS232__DELAYCODE();
00313         
00314         //  BIT 7
00315         if   (Dout & 0x80)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00316         if (!(Dout & 0x80)) { NOP(); NOP(); NOP(); NOP(); }
00317         RS232__DELAYCODE();
00318         
00319         //  NO PARITY BIT
00320         
00321         //  STOP BIT
00322         NOP(); NOP();
00323         if   (Dout | 0xFF)  { RS232__PORT = 1; } else { RS232__PORT = 0; } 
00324         if (!(Dout | 0xFF)) { NOP(); NOP(); NOP(); NOP(); } 
00325         RS232__DELAYCODE();
00326 
00327         //  IDLE STATE
00328         RS232__PORT = 1;
00329         
00330     #endif
00331 }
00332 
00333 #endif

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