The following document contains information on Cypress products. Colophon The products described in this document are designed, developed and manufactured as contemplated for general use, including without limitation, ordinary industrial use, general office use, personal use, and household use, but are not designed, developed and manufactured as contemplated (1) for any use that includes fatal risks or dangers that, unless extremely high safety is secured, could have a serious effect to the public, and could lead directly to death, personal injury, severe physical damage or other loss (i.e., nuclear reaction control in nuclear facility, aircraft flight control, air traffic control, mass transport control, medical life support system, missile launch control in weapon system), or (2) for any use where chance of failure is intolerable (i.e., submersible repeater and artificial satellite). Please note that Spansion will not be liable to you and/or any third party for any claims or damages arising in connection with above-mentioned uses of the products. Any semiconductor devices have an inherent chance of failure. You must protect against injury, damage or loss from such failures by incorporating safety design measures into your facility and equipment such as redundancy, fire protection, and prevention of over-current levels and other abnormal operating conditions. If any products described in this document represent goods or technologies subject to certain restrictions on export under the Foreign Exchange and Foreign Trade Law of Japan, the US Export Administration Regulations or the applicable laws of any other country, the prior authorization by the respective government entity will be required for export of those products. Trademarks and Notice The contents of this document are subject to change without notice. This document may contain information on a Spansion product under development by Spansion. Spansion reserves the right to change or discontinue work on any product without notice. The information in this document is provided as is without warranty or guarantee of any kind as to its accuracy, completeness, operability, fitness for particular purpose, merchantability, non-infringement of third-party rights, or any other warranty, express, implied, or statutory. Spansion assumes no liability for any damages of any kind arising out of the use of the information in this document. ® ® ® TM Copyright © 2013 Spansion Inc. All rights reserved. Spansion , the Spansion logo, MirrorBit , MirrorBit Eclipse , TM ORNAND and combinations thereof, are trademarks and registered trademarks of Spansion LLC in the United States and other countries. Other names used are for informational purposes only and may be trademarks of their respective owners. Fujitsu Semiconductor Design (Chengdu) Co., Ltd. Application Note MCU-AN-500123-E-11 F²MC-8FX FAMILY 8-BIT MICROCONTROLLER MB95F390H SERIES CAPACITANCE TOUCH SENSOR USE TSC2_DEMO_398.LIB APPLICATION NOTE USE TSC2_Demo_398.LIB V1.1.0 Revision History Revision History Version Date Updated by Approved by Modifications 1.0.0 2011-06-03 Ivan Xiao Jacky Zhou First Draft 1.1.0 2011-07-29 Lee Song Jacky Zhou 1 Update template to the latest version 2 Modify API name and function description based on new TSC.lib 3 Add new sample code, include UART and Key service functions This manual contains 36 pages. Specifications are subject to change without notice. For further information please contact each office. All Rights Reserved. The contents of this document are subject to change without notice. Customers are advised to consult with sales representatives before ordering. The information, such as descriptions of function and application circuit examples, in this document are presented solely for the purpose of reference to show examples of operations and uses of FUJITSU SEMICONDUCTOR device; FUJITSU SEMICONDUCTOR does not warrant proper operation of the device with respect to use based on such information. When you develop equipment incorporating the device based on such information, you must assume any responsibility arising out of such use of the information. FUJITSU SEMICONDUCTOR assumes no liability for any damages whatsoever arising out of the use of the information. Any information in this document, including descriptions of function and schematic diagrams, shall not be construed as license of the use or exercise of any intellectual property right, such as patent right or copyright, or any other right of FUJITSU SEMICONDUCTOR or any third party or does FUJITSU SEMICONDUCTOR warrant non-infringement of any third-party's intellectual property right or other right by using such information. FUJITSU SEMICONDUCTOR assumes no liability for any infringement of the intellectual property rights or other rights of third parties which would result from the use of information contained herein. The products described in this document are designed, developed and manufactured as contemplated for general use, including without limitation, ordinary industrial use, general office use, personal use, and household use, but are not designed, developed and manufactured as contemplated (1) for use accompanying fatal risks or dangers that, unless extremely high safety is secured, could have a serious effect to the public, and could lead directly to death, personal injury, severe physical damage or other loss (i.e., nuclear reaction control in nuclear facility, aircraft flight control, air traffic control, mass transport control, medical life support system, missile launch control in weapon system), or (2) for use requiring extremely high reliability (i.e., submersible repeater and artificial satellite). Please note that FUJITSU SEMICONDUCTOR will not be liable against you and/or any third party for any claims or damages arising in connection with above-mentioned uses of the products. Any semiconductor devices have an inherent chance of failure. You must protect against injury, damage or loss from such failures by incorporating safety design measures into your facility and equipment such as redundancy, fire protection, and prevention of over-current levels and other abnormal operating conditions. Exportation/release of any products described in this document may require necessary procedures in accordance with the regulations of the Foreign Exchange and Foreign Trade Control Law of Japan and/or US export control laws. The company names and brand names herein are the trademarks or registered trademarks of their respective owners. Copyright © 2011 Fujitsu Semiconductor Design (Chengdu) Co., Ltd. All rights reserved. MCU-AN-500123-E-11 – Page 2 USE TSC2_Demo_398.LIB V1.1.0 Contents Contents REVISION HISTORY ............................................................................................................ 2 CONTENTS .......................................................................................................................... 3 1 INTRODUCTION .............................................................................................................. 5 1.1 Purpose ................................................................................................................... 5 1.2 Definitions, Acronyms and Abbreviations ................................................................ 5 1.3 Document Overview ................................................................................................ 5 2 SOLUTIONS .................................................................................................................... 6 2.1 GPIO Solution ......................................................................................................... 6 2.2 AD Charge Solution................................................................................................. 8 3 LIBRARY ....................................................................................................................... 10 3.1 Library Overview ................................................................................................... 10 3.2 Parameters Setup ................................................................................................. 11 3.3 3.2.1 Structure type define................................................................................ 11 3.2.2 Port and Pin define .................................................................................. 11 3.2.3 Sample parameters setup ........................................................................ 11 3.2.4 Example of Parameters Setup ................................................................. 12 Application Interface .............................................................................................. 13 3.3.1 Initialization Function ............................................................................... 13 3.3.2 Check Sensor Function............................................................................ 13 3.3.3 Get Value Function .................................................................................. 14 3.3.4 Update Threshold Function ...................................................................... 14 3.4 FUJITSU TSC Performance .................................................................................. 15 3.5 How to Add Fujitsu TSC.lib .................................................................................... 16 3.6 3.5.1 Add Fujitsu TSC.lib to User’s Project ....................................................... 16 3.5.2 Include Header File.................................................................................. 18 How to use Fujitsu TSC.lib .................................................................................... 19 4 LIB USAGE NOTICE ..................................................................................................... 21 5 ADDITIONAL INFORMATION ....................................................................................... 23 6 APPENDIX ..................................................................................................................... 24 6.1 List of Figures and Tables ..................................................................................... 24 6.2 Sample code ......................................................................................................... 25 6.2.1 Main Function .......................................................................................... 25 6.2.2 Uart Function ........................................................................................... 29 MCU-AN-500123-E-11 – Page 3 USE TSC2_Demo_398.LIB V1.1.0 Contents 6.2.3 Key_Service Function .............................................................................. 35 MCU-AN-500123-E-11 – Page 4 USE TSC2_Demo_398.LIB V1.1.0 Chapter 1 Introduction 1 Introduction 1.1 Purpose This application note describes FUJITSU TSC solution, and describes how to use TSC library and TSC GUI. 1.2 Definitions, Acronyms and Abbreviations 1.3 Document Overview The rest of document is organized as the following: Chapter 2 explains the working principles of GPIO solution. Chapter 3 explains how to use TSC library. Chapter 4 explains LIB usage notice. MCU-AN-500123-E-11 – Page 5 USE TSC2_Demo_398.LIB V1.1.0 Chapter 2 Solutions 2 Solutions This chapter introduces working principles of TSC solutions used in TSC2_EVB_418.LIB. 2.1 GPIO Solution The theory of capacitance touch sensor is to check capacitance increment. As follows, when finger not touch the C = Cp, when touching the C = Cp +Cғ, and the increment is ΔC = Cғ. This ΔC will affect the electrode charging-up time. The GPIO method detects the touch action by compare the time difference. Let t1 is time of electrode charge filled in finger not touch situation. When finger touching the pad, this will be delayed to t2 (because of ΔC). MCU can detect the touch action by compare this time delay. VCC MCU GPIO-pin Figure 2-1: GPIO method Physical Diagram V Δt Finger not touch Finger touching t1 t2 t Figure 2-2: GPIO method Curve Graph MCU-AN-500123-E-11 – Page 6 USE TSC2_Demo_398.LIB V1.1.0 Chapter 2 Solutions The GPIO method includes the following steps: 1. Set pin to output ‘L’ to discharge the pad 2. Set pin to input (high impedance) to charge the pad 3. Begin counter accumulation 4. Wait until the pad charge filled 5. Save counter value 6. Start next sample The flowchart shown as below Start Discharge the pad Charge the pad Wait timer interrupt Y Save ADC value Figure 2-3: GPIO method Check Flowchart MCU-AN-500123-E-11 – Page 7 USE TSC2_Demo_398.LIB V1.1.0 Chapter 2 Solutions 2.2 AD Charge Solution The theory of capacitance touch sensor is to check capacitance increment. As follows, when finger not touch the C = Cp, when touching the C = Cp +Cғ, and the increment is ΔC = Cғ. This ΔC will affect the electrode charging-up time. The AD charge method detects the touch action by compare the voltage difference at the same time Let V1 is voltage of electrode at time t in finger not touch situation. When finger touching, speed of electrode charging will be delayed (because of ΔC). At the same time t, the voltage is V2 which less than V1. MCU can detect the touch action by compare this voltage change. MCU ADC-pin Figure 2-4: AD charge method physical diagram V V1 ΔV Finger not touch V2 Finger touching t t Figure 2-5: AD charge method curve graph MCU-AN-500123-E-11 – Page 8 USE TSC2_Demo_398.LIB V1.1.0 Chapter 2 Solutions The AD charge method includes the following steps: 1. Set charge pin to output ‘L’ to discharge the pad 2. Set charge pin to output ‘H’ to charge the pad 3. Get the ADC value periodically 4. Save ADC value 5. Start next sample The flowchart shown as below Start Discharge the pad Charge the pad Wait timer interrupt Y Save ADC value Figure 2-6: AD charge method Check Flowchart MCU-AN-500123-E-11 – Page 9 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3 Library This chapter introduces how to use TSC library. 3.1 Library Overview There are parameters which user need to setup, and 4 functions as API for user’s situations. All the parameters and functions are introduced as follow. Table 3-1: Parameters List Name Description Remark Key_Const type TSC Key data structure type define N/A TSC_GPIOKEY TSC GPIO data structure array N/A TSC_ADKEY TSC AD data structure array N/A TSC_GPIOKeyNum TSC GPIO Key Number N/A TSC_ADKeyNum TSC AD Key Number N/A TSCKEY_GPIOPORT TSC GPIO Key IO port N/A TSCKEY_ADPORT TSC AD Key IO port N/A TSCKEY_ChargePORT TSC AD Key Charge port N/A TSCKEY_O_WH TSC Key IO bit N/A TSCKEY_GPIOSampleNumConst TSC GPIO Key sample number N/A TSCKEY_ADSampleNumConst TSC AD Key sample number N/A TSC_GPIOThreshold TSC GPIO Key Threshold data array N/A TSC_ADThreshold TSC AD Key Threshold data array N/A TSC_TimerData TSC Key Timer Dare register N/A TSC_TimerEn TSC Key Timer Start/Stop control bit N/A Table 3-2: Functions List Prototype Function Description Remark TSCKey_ADInit This function initializes the timer used, the data structures of the library and calculates the initial capacitance of the electrodes. N/A TSCKey_ADCheck This function is used to Check if the charge process are completed and save the value. N/A TSCKey_GetADValue This function is used to detect the key touched status, and generate a specific key word. N/A TSCKey_GPIOInit This function initializes the timer used, the data structures of the library and calculates the initial capacitance of the electrodes. N/A TSCKey_GPIOCheck This function is used to Check if the charge process are completed and save the value. N/A TSCKey_GetGPIOValue This function is used to detect the key touched status, and generate a specific key word. N/A TSCKey_UpdateThr This function is used to update the threshold set by the GUI and refresh the detect difference N/A MCU-AN-500123-E-11 – Page 10 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.2 Parameters Setup 3.2.1 Structure type define Every TSC Key have 4 attributes include: initial status, count value, threshold value and difference threshold value. User can define new key directly using the following structure. typedef struct { unsigned char InitFlag; //**< Contains the initial status unsigned int Value; //**< Contains the count value unsigned int Threshold; //**< Contains the threshold value now unsigned int DifThreshold; //**< Contains the threshold temp value } Key_Const; All the keys which have been defined can make up a array which length will defined by the parameter TSC_GPIOKeyNum and TSC_ADKeyNum 3.2.2 Port and Pin define Parameter TSCKEY_GPIOPORT, TSCKEY_ADPORT, TSCKEY_ChargePORT and TSCKEY_O_WH are used to configure the TSC pin. The TSCKEY_GPIOPORT, TSCKEY_ADPORT, TSCKEY_ChargePORT directly use the register value on the data sheet, and the TSCKEY_O_WH configured by the first pin (use the continuous pin in same port are recommend, because that will be good at the sample speed). 3.2.3 Sample parameters setup The parametes: #define TSCKEY_GPIOSampleNumConst 100 //Sample number #define TSCKEY_ADSampleNumConst 100 //Sample number #define TSC_TimerData T00DR //Timer Data #define TSC_TimerEn T00CR1_STA //Timer Enable extern unsigned int TSC_GPIOThreshold[TSC_KeyNum]; extern unsigned int TSC_ADThreshold[TSC_KeyNum]; Used to configure the sample number, threshold, and timer initial value. MCU-AN-500123-E-11 – Page 11 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.2.4 Example of Parameters Setup #define TSCKEY_ADSampleNumConst 100 //Sample number #define TSCKEY_GPIOSampleNumConst 100 //Sample number #define TSCKEY_ADDifThrOffset 35 //Threshold Difference Offset #define TSCKEY_GPIODifThrOffset 35 //Threshold Difference Offset #define TSC_GPIOKeyNum 8 //TSCKey number #define TSC_ADKeyNum 4 //TSCKey number #define TSCKEY_GPIOPORT 0x0014 //TSCKey port register >> P5 #define TSCKEY_ADPORT 0x0000 //TSCKey port register >> P0 #define TSCKEY_ChargePORT 0x0002 //TSCKey port register >> P1 #define TSCKEY_O_WH 0x01 //TSCKey pin dir & data #define TSC_TimerData T00DR //Timer Data #define TSC_TimerEn T00CR1_STA //Timer Enable typedef struct { unsigned char InitFlag; //**< Contains the initial status unsigned int Value; //**< Contains the count value unsigned int Threshold; //**< Contains the threshold value unsigned int DifThreshold; //**< Contains the difference threshold } Key_Const; extern unsigned int TSC_GPIOThreshold[TSC_KeyNum]; extern unsigned int TSC_ADThreshold[TSC_KeyNum]; extern Key_Const TSC_GPIOKEY[TSC_GPIOKeyNum]; extern Key_Const TSC_ADKEY[TSC_ADKeyNum]; MCU-AN-500123-E-11 – Page 12 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.3 Application Interface All the functions supplied by the TSC.lib will be introduced below, include the function prototype, input parameter(s), return value(s), and the function description. 3.3.1 Initialization Function Table 3-3: Initialization Function Table (GPIO Method) Prototype void TSCKey_GPIOInit (Key_Const *Key); Parameter: Key_Const * Return void Description This function initializes the timer used, the data structures of the library and calculates the initial capacitance of the electrodes. Remark N/A Key Pointer to the TSC Key array Table 3-4: Initialization Function Table (AD Method) Prototype void TSCKey_ADInit (Key_Const *Key); Parameter: Key_Const * Return void Description This function initializes the timer used, the data structures of the library and calculates the initial capacitance of the electrodes. Remark N/A Key Pointer to the TSC Key array 3.3.2 Check Sensor Function Table 3-5: Check Sensor Function Table (GPIO Method) Prototype unsigned int TSCKey_GPIOCheck(unsigned char TSC_Port,unsigned char TSC_Pin,unsigned char TSCKEY_SampleNum) Parameter: unsigned char* TSC_Port Port to be checked unsigned char TSC_Pin Pin to be checked unsigned char TSCKEY_SampleNum Sample number Return unsigned int Check_count The counter value Description This function is used to Check if the charge process are completed and save the value. Remark N/A Table 3-6: Check Sensor Function Table (AD Method) Prototype unsigned int TSCKey_ADCheck(unsigned char TSC_Port,unsigned char TSC_Pin,unsigned char TSCKEY_SampleNum) Parameter: unsigned char* TSC_Port Port to be checked unsigned char TSC_Pin Pin to be checked unsigned char TSCKEY_SampleNum Sample number Return unsigned int Check_count The counter value Description This function is used to Check if the charge process are completed and save the value. Remark N/A MCU-AN-500123-E-11 – Page 13 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.3.3 Get Value Function Table 3-7: Get Value Function Table (GPIO Method) Prototype unsigned char TSCKey_GetGPIOValue(Key_Const *Key) Parameter: Key_Const * Key Return Pointer to the TSC Key array The Key word for the service function unsigned char TSCKeyValue Single touch mode: Only the latest detected key will be set with ‘0’ Multi touch mode: All touched keys be set with ‘0’ Description This function is used to detect the key touched status, and generate a specific key word. Remark N/A Table 3-8: Get Value Function Table (AD Method) Prototype unsigned char TSCKey_GetADValue(Key_Const *Key) Parameter: Key_Const * Key Return Pointer to the TSC Key array The Key word for the service function unsigned char TSCKeyValue Single touch mode: Only the latest detected key will be set with ‘0’ Multi touch mode: All touched keys be set with ‘0’ Description This function is used to detect the key touched status, and generate a specific key word. Remark N/A 3.3.4 Update Threshold Function Table 3-9: Update Threshold Function Table Prototype void TSCKey_UpdateThr(Key_Const *Key,unsigned char Key_Num,unsigned int *Key_Thr) Parameter: Key_Const * Key Pointer to the TSC Key array unsigned char Key_Num Number of TSC Keys unsigned int * TSCKEY_SampleNum Pointer to the TSC Threshold array Return void Description This function is used to update the threshold set by the GUI and refresh the detect difference Remark N/A MCU-AN-500123-E-11 – Page 14 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.4 FUJITSU TSC Performance Table 3-10: Performance Resource Amount ROM 1272 Byte Description ROM space: If just need support 1 sensor, ROM of lib can be reduced to: 300 Byte If just need support 2 sensors, ROM of lib can be reduced to: 960 Byte If just need support 3 sensors, ROM of lib can be reduced to: 1120 Byte If need support 4 sensors, ROM of Lib is:1272 Byte RAM 36 Byte RAM space: If just need support 1 sensor, RAM of lib can be reduced to: 11 Byte If just need support 2 sensors, RAM of lib can be reduced to: 26 Byte If just need support 3 sensors, RAM of lib can be reduced to: 31 Byte If need support 4 sensors, RAM of Lib is:36 Byte IO Port 1 P0 IO Pin 4 P04,P05,P06,P07 Timer 2 Timebase Timer Serial Port 1 Serial Port(P04,P05),only use in check threshold mode Machine Clock >=8M Scan 1 Key 2 ms MCU-AN-500123-E-11 – Page 15 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.5 How to Add Fujitsu TSC.lib 3.5.1 Add Fujitsu TSC.lib to User’s Project 1. In Softune, Right click on folder Include Files select Add member to folder from the menu select File. Figure 3-1: Add member to folder 2. Because the default option of file type filters is *.c and * .asm, you can’t found TSC.lib in dialog box of Add Member. Figure 3-2: Popup Add Member dialog box MCU-AN-500123-E-11 – Page 16 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3. In Add Member dialog box, select ‘ALL Files’ from ‘Files of Type’, and then you will find the TSC.lib Figure 3-3: Found the lib file 4. Double click TSC.lib, and then you can see it has been added in the folder Include Files Figure 3-4: Add TSC.lib MCU-AN-500123-E-11 – Page 17 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.5.2 Include Header File 1. Add "#include"TSC.h "" in C file, such as in "main.c ". Figure 3-5: Add include statement in C file 2. Compile the whole project, "TSC.h" will link TSC.lib to c file, so that user program can use API functions in TSC.lib. Figure 3-6: Include header file successfully MCU-AN-500123-E-11 – Page 18 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library 3.6 How to use Fujitsu TSC.lib After complete above works, we can start use the TSC library now. The operation categorised by if connect to GUI. Both of two operations need initialize TSC, initialize Timer, initialize GPIO and initialize interrupt. If connect to GUI, the UART module should also be initialized. Start Disable comparer Disable system interrupt Set Oscillator Set interrupt level Enable system interrupt Initialize the GPIO Initialize the UART Initialize the timer End Figure 3-7: Initialize Flow chart After all used modules have been initialized, program go into a endless loop, to do below step circularly: 1. Wait the time interval interrupt is generate, clear the interrupt flag 2. Check the electrodes and save the counter value 3. Judgement if the new value bigger or lower than the threshold, if meet the condition, generate the key word 4. According to the key word jump to corresponding operation (such as drive LED) 5. Go to another new loop If connect GUI, in above process, there also have the UART interrupt, MCU need send out counter value or get new threshold from GUI. The direct of transmission is determined by the user-defined protocol. MCU-AN-500123-E-11 – Page 19 USE TSC2_Demo_398.LIB V1.1.0 Chapter 3 Library Start Wait time interrupt Check sensor Save counter value N UART Communication Meet detect condition? Y Generate key word Key service Figure 3-8: Main loop Flowchart MCU-AN-500123-E-11 – Page 20 GUI USE TSC2_Demo_398.LIB V1.1.0 Chapter 4 LIB Usage Notice 4 LIB Usage Notice This chapter introduces LIB usage notice. Machine clock The machine clock should be set to 8M or above. If the machine clock is less than 8M, the sensor response is slow. Interrupt This solution needs to use Timebase Timer interrupt to check sensor, and the interval time is 512us. It is unnecessary to use UART if the threshold is not gotten or tested with TSC GUI, and the interrupt setting in “vector.c” as following: #include " mb95390.h" /****************************************************************************** InitIrqLevels() This function pre-sets all interrupt control registers. It can be used to set all interrupt priorities in static applications. If this file contains assignments to dedicated resources, verify that the appropriate controller is used. ******************************************************************************/ void InitIrqLevels(void) { #ifdef enableUART ILR2 = 0xFE; // IRQ8: LIN-UART (transmission) // IRQ8: LIN-UART (transmission) // IRQ19: Timebase timer #else ILR2 = 0xFF; #endif ILR4 = 0x3F; } /****************************************************************************** Prototypes Add your own prototypes here. Each vector definition needs is prototype. Either do it here or include a header file containing them. ******************************************************************************/ __interrupt void DefaultIRQHandler (void); #ifdef enableUART __interrupt void UART_ISR (void); #endif __interrupt void TBT_ISR (void); MCU-AN-500123-E-11 – Page 21 USE TSC2_Demo_398.LIB V1.1.0 Chapter 4 LIB Usage Notice /****************************************************************************** Vector definition Use following statements to define vectors. All resource related vectors are predefined. Remaining software interrupts can be added hereas well. ******************************************************************************/ #ifdef enableUART #pragma intvect UART_T 8 // IRQ8: LIN-UART (transmission) 8 // IRQ8: LIN-UART (transmission) 19 // IRQ19: Time base timer #else #pragma intvect DefaultIRQHandler #endif #pragma intvect TBT_ISR MCU-AN-500123-E-11 – Page 22 USE TSC2_Demo_398.LIB V1.1.0 Chapter 5 Additional Information 5 Additional Information For more Information on FUJITSU semiconductor products, visit the following websites: English version address: http://www.fujitsu.com/cn/fsp/services/mcu/mb95/application_notes.html Chinese version address: http://www.fujitsu.com/cn/fss/services/mcu/mb95/application_notes.html MCU-AN-500123-E-11 – Page 23 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix 6 Appendix 6.1 List of Figures and Tables Table 3-1: Parameters List ................................................................................................... 10 Table 3-2: Functions List ...................................................................................................... 10 Table 3-3: Initialization Function Table (GPIO Method) ........................................................ 13 Table 3-4: Initialization Function Table (AD Method) ............................................................ 13 Table 3-5: Check Sensor Function Table (GPIO Method) .................................................... 13 Table 3-6: Check Sensor Function Table (AD Method) ........................................................ 13 Table 3-7: Get Value Function Table (GPIO Method) ........................................................... 14 Table 3-8: Get Value Function Table (AD Method) ............................................................... 14 Table 3-9: Update Threshold Function Table ....................................................................... 14 Table 3-10: Performance ..................................................................................................... 15 Figure 2-1: GPIO method Physical Diagram .......................................................................... 6 Figure 2-2: GPIO method Curve Graph .................................................................................. 6 Figure 2-3: GPIO method Check Flowchart............................................................................ 7 Figure 2-4: AD charge method physical diagram.................................................................... 8 Figure 2-5: AD charge method curve graph ........................................................................... 8 Figure 2-6: AD charge method Check Flowchart .................................................................... 9 Figure 3-1: Add member to folder......................................................................................... 16 Figure 3-2: Popup Add Member dialog box .......................................................................... 16 Figure 3-3: Found the lib file................................................................................................. 17 Figure 3-4: Add TSC.lib........................................................................................................ 17 Figure 3-5: Add include statement in C file ........................................................................... 18 Figure 3-6: Include header file successfully.......................................................................... 18 Figure 3-7: Initialize Flow chart ............................................................................................ 19 Figure 3-8: Main loop Flowchart ........................................................................................... 20 MCU-AN-500123-E-11 – Page 24 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix 6.2 Sample code 6.2.1 Main Function File Name: main.c Function: Initialize and configure /******************************************************************************************** -File: main.c -Author: Lee Song -Date: 20110510 -Function:Initialize and configure *********************************************************************************************/ #include "Lcd_StateGrid.h" #include "main.h" #include "PortDef.h" #include "HardKey.h" #include "TSC.h" extern UCHAR disUpdateFlage; extern UCHAR HideDataBuff[8]; unsigned char TSCUpdateFlag; extern void TBT_Init(void); volatile unsigned int Div_Counter = 0; //===================================================================== // Initial GPIO peripheral //===================================================================== void GPIO_Init(void) { LCDCE1 = 0; LCDCE1_PICTL = 1; //Set for use GPIO //Set for use GPIO LED1_Off; LED2_Off; LEDCtrl_DirOut; HKEYInput_DirIn; BeepCtrl_Off; BeepCtrl_DirOut; //BEEP_Dir } MCU-AN-500123-E-11 – Page 25 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix //================================================================== // Beeper control function //================================================================== void Beep(unsigned char num,unsigned char delay) { unsigned int i,j,k; for(k=0;k<num;k++) { BeepCtrl_On; for(i=0;i<100;i++) { for(j=0;j<delay;j++); } BeepCtrl_Off; for(i=0;i<100;i++) { for(j=0;j<delay;j++); } } } //================================================================== // FunctionName: main // Description: main function // Input: None // Return: None //================================================================== void main(void) { UCHAR dataBuff[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; unsigned char i = 0; unsigned char j = 1; CMR0_VCID = 1; //Disable CMP input __DI(); Osc_Setup(); //Setup MCU main oscillator/FLL InitIrqLevels(); //Initialise Interrupt level register and IRQ vector table __EI(); //Enable system interrupt now GPIO_Init(); TBT_Init(); MCU-AN-500123-E-11 – Page 26 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix IIC_Init(); #ifdef enableUART UART_Init(); #endif do { LED1_On; LED2_On; if(TSCUpdateFlag == 1) { Div_Counter++; TSCUpdateFlag = 0; } } while(Div_Counter < 30); Div_Counter = 0; LED1_Off; LED2_Off; Beep(2,500); TSCKey_Init(TSCKEY); while(1) { CallUpDateKey(); TSCKey_Service(); if(TSCUpdateFlag == 1) { TSCUpdateFlag = 0; if((Div_Counter % 9) == 0) { TSCKey_UpdateThr(TSCKEY,TSC_KeyNum,TSC_Threshold); } if(Div_Counter == 360) { Div_Counter = 0; } Div_Counter++; } } } MCU-AN-500123-E-11 – Page 27 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix //================================================================== // Setup MCU main oscillator/FLL // Internal clock 243KHz. Set FLL for 11MHz Bus frequency //================================================================== void Osc_Setup(void) { SYSC &=0xbf; //Set PF0/PF1 as main CR pin,set PG1/PG2 as GPIO SYCC =0xf0; WATR =0xf3; SYCC2 =0xf4; while(STBC_MRDY == 0); RESET_WATCHDOG(); } //================================================================== // Reset the watch dog timer //================================================================== void RESET_WATCHDOG(void) { WDTC = 0x35; } MCU-AN-500123-E-11 – Page 28 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix 6.2.2 Uart Function File Name: Uart.c Function: Initialize and configure UART module /******************************************************************************************** -File: Uart.c -Author: Lee Song -Date: 20110610 -Function: Initialize and configure UART module *********************************************************************************************/ #include "mb95390.h" #include "TSC.h" #include "Uart.h" unsigned char UART_Start = 0; unsigned char UART_Sent = 0; unsigned char UART_Get = 0; unsigned char Threshold_N = 0; unsigned char UART_Setfinish = 0; unsigned char *pTSC_Threshold = (unsigned char *)TSC_Threshold; unsigned char UART_TSCValue[UART_DBUFF_LEN] = {0}; unsigned char UART_TSCStatus[UART_SBUFF_LEN] = {0}; unsigned char UART_DBUFF_PTR = 0; unsigned char UART_SBUFF_PTR = 0; void UART_Init(void) { UART_Dir_IN; UART_Dir_OUT; SMC10 = 0x0C; // UART/SIO Serial Mode Control Register 1 (SMC10) // |||||||+------Operating mode select bit (Clock asynchronous mode (UART)) // |||||++------- Clock select bit(Dedicated baud rate generator) // ||||+--------- Character bit length control bits(8 bits) // |||+---------- Stop bit length control bit(2-bit length) // ||+----------- Parity control bit(No parity) // |+------------ Parity polarity bit(Even parity) // +-----------sequentially) Serial data direction control bit(Transmit/receive data from MSB side MCU-AN-500123-E-11 – Page 29 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix SMC20 = 0x5E;// UART/SIO Serial Mode Control Register 2 (SMC20) // |||||||+------- Transmission data register empty interrupt enable bit(Disables // transmission data register empty interrupts) // ||||||+------- Transmission completion interrupt enable bit(Enables transmission // completion interrupts) // |||||+-------- Reception interrupt enable bit(Enables reception interrupts) // ||||+--------- Transmission operation enable bit(Enables transmission operation) // |||+---------- Reception operation enable bit(Enables reception operation) // ||+----------- Reception error flag clear bit(Clears the error flags in the SSR0 register) // |+------------ Serial data output enable bit(Enables serial data output) // +------------ Serial data output enable bit(Disables serial clock output (usable as // a general-purpose port)) PSSR0 = 0x04; // UART/SIO Dedicated Baud Rate Generator Prescaler Select Register (PSSR0) // ||||||++------- Prescaler select bits(1/1) // |||||+---------- Baud rate clock output enable bit(Enables transmission completion interrupts) // +++++--------Undefined bit BRSR0 = 26; // UART/SIO Dedicated Baud Rate Generator Baud Rate Setting // Register (BRSR0) // 2 ~ 255 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// Sample Asynchronous Transfer Rates by Baud Rate Generator /////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Total division ratio // (PSS × BRS × 4) Baud rate ///////////////////////////////////// (Machine Clock/Total division ratio)//////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SSR0 = 0x00; // UART/SIO Serial Status Register (SSR0) // |||||||+------ Transmit data register empty flag // (0:Transmit data present / 1:Transmit data absent) // ||||||+------- Transmission completion flag // |+------------ Parity polarity bit(Even parity) // +-----------sequentially) Serial data direction control bit(Transmit/receive data from MSB side } MCU-AN-500123-E-11 – Page 30 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix void UART_Transmit(void) { if(UART_Sent == 1) { if(UART_DBUFF_PTR < UART_DBUFF_LEN) //Sent the data { TDR0 = UART_TSCValue[UART_DBUFF_PTR++]; } else if(UART_DBUFF_PTR < (UART_DBUFF_LEN << 1)) //Sent the threshold { TDR0 = pTSC_Threshold[UART_DBUFF_PTR - UART_DBUFF_LEN]; UART_DBUFF_PTR++; } else if(UART_DBUFF_PTR == (UART_DBUFF_LEN << 1)) { UART_Sent = 0; UART_DBUFF_PTR = 0; } } else if(UART_Sent == 2) //Sent the board status { if(UART_SBUFF_PTR < UART_SBUFF_LEN) { TDR0 = UART_TSCStatus[UART_SBUFF_PTR++]; } else { UART_Sent = 0; UART_SBUFF_PTR = 0; } } else { SSR0_TCPL = 0; } } MCU-AN-500123-E-11 – Page 31 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix __interrupt void UART_ISR (void) { unsigned char UART_Temp; SMC20_RERC = 0; if(SSR0_RDRF == 1) //Receive Data { SSR0_RDRF = 0; if(UART_Get == 0) { UART_Temp = RDR0; switch(UART_Temp) { case UART_Connect: { TDR0 = UART_Check_OK; UART_Start = 1; break; } case UART_Getdata: { UART_Sent = 1; SSR0_TCPL = 1; TDR0 = UART_TSCValue[UART_DBUFF_PTR++]; break; } case UART_Getstatus: { UART_Sent = 2; SSR0_TCPL = 1; TDR0 = UART_TSCStatus[UART_SBUFF_PTR++]; break; } case UART_Setdata: { TDR0 = UART_SetBack; Threshold_N = 0; UART_Get = 1; break; } MCU-AN-500123-E-11 – Page 32 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix case UART_Disconnect: { UART_Start = 0; break; } default: { TDR0 = UART_Check_Erro; break; } } } else //Set threshold by GUI { if(Threshold_N < UART_DBUFF_LEN) { pTSC_Threshold[Threshold_N] = RDR0; TDR0 = UART_SetBack; Threshold_N++; } else if(Threshold_N == UART_DBUFF_LEN) { UART_Get = 0; Threshold_N = 0; UART_Setfinish = 1; TSCKey_UpdateThr(TSCKEY,TSC_KeyNum);//,TSC_Threshold); } } } else if(SSR0_TDRE == 1) //Sent Data { SSR0_TDRE = 0; if(UART_Get == 0) UART_Transmit(); } } MCU-AN-500123-E-11 – Page 33 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix File Name: Uart.h Function: Header file of UART module /******************************************************************************************** -File: Uart.h -Function: Header file of UART module /*********************************************************************************************/ #include "TSC.h" #define UART_DBUFF_LEN (TSC_KeyNum<<1) #define UART_SBUFF_LEN 10 #define UART_Dir_IN DDR1_P10 = 0 #define UART_Dir_OUT DDR1_P11 = 1 #define UART_Connect #define UART_Getdata 0x0F 0x5F #define UART_Getstatus 0x6F #define UART_Setdata 0xAF #define UART_SetBack 0xCF #define UART_Disconnect 0xBF #define UART_Check_OK 0xFF #define UART_Check_Erro 0xEF extern unsigned char UART_TSCValue[UART_DBUFF_LEN]; extern unsigned char UART_TSCStatus[UART_SBUFF_LEN]; void UART_Init(void); void UART_Transmit(void); MCU-AN-500123-E-11 – Page 34 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix 6.2.3 Key_Service Function File Name: Key_Service.c Function: Key press service function /******************************************************************************************** -File: Key_Service.c -Author: Lee Song -Date: 20110610 -Function: Key press service function *********************************************************************************************/ #include "mb95390.h" #include "PortDef.h" #include "TSC.h" unsigned char TSCKeyCode = 0xFF; unsigned char mTSCKeyCode = 0xFF; unsigned int Key_Task_Time=0; extern unsigned char TSCKeyCode; extern volatile unsigned int Now_Time; void CallUpDateKey(void) { unsigned char TmpCode = 0xff; unsigned char TSCTmpCode = 0xff; if(Now_Time-Key_Task_Time >= 1) { Key_Task_Time = Now_Time; TSCTmpCode = TSCKey_GetValue(TSCKEY); if (TSCTmpCode == TSCOldCode) TSCKeyCode = TSCTmpCode; else TSCOldCode = TSCTmpCode; } } MCU-AN-500123-E-11 – Page 35 USE TSC2_Demo_398.LIB V1.1.0 Chapter 6 Appendix void TSCKey_Service(void) { unsigned char i; if(mTSCKeyCode != TSCKeyCode) { if(TSCKeyCode != 0xFF) { for(i=0;i<4;i++) { if((TSCKeyCode | (1 << i)) == 0xFF)break; } Beep(1,500); } mTSCKeyCode = TSCKeyCode; } } MCU-AN-500123-E-11 – Page 36