AN205227 - F²MC-8FX Family, MB95F410H/470H Series TSC2_Demo_418.Lib.pdf

THIS SPEC IS OBSOLETE
Spec No:
002-05227
Spec Title:
AN205227 - F2MC-8FX Family,
MB95F410H/470H Series TSC2_DEMO_418.Lib
Replaced by: None
AN205227
F²MC-8FX Family, MB95F410H/470H Series TSC2_Demo_418.Lib
This application note describes Cypress TSC solution, and describes how to use TSC library and TSC GUI.
Contents
1
Introduction ...............................................................1
1.1
Purpose ...........................................................1
1.2
Definitions, Acronyms and Abbreviations ........1
1.3
Document Overview ........................................1
2
Solutions ...................................................................1
2.1
GPIO Solution ..................................................1
2.2
AD Charge Solution .........................................4
3
Library ......................................................................6
3.1
Library Overview ..............................................6
3.2
Parameters Setup ............................................7
1
Introduction
1.1
Purpose
3.3
Application Interface ........................................ 9
3.4
Cypress TSC Performance ............................ 11
3.5
How to Add Cypress TSC.lib ......................... 12
3.6
How to use Cypress TSC.lib .......................... 15
4
LIB Usage Notice ................................................... 16
5
Additional Information ............................................. 17
5.1
Sample code.................................................. 18
6
Document History ................................................... 30
This application note describes Cypress 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.
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.
www.cypress.com
Document No. 002-05227 Rev.*A
1
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
Figure 1. GPIO method Physical Diagram
VCC
MCU
GPIO-pin
Figure 2. GPIO method Curve Graph
V
Δt
Finger not touch
Finger touching
t1
www.cypress.com
t2
t
Document No. 002-05227 Rev.*A
2
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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
Figure 3. GPIO method Check Flowchart
Start
Discharge the pad
Charge the pad
Wait charge filled
Y
Save counter value
www.cypress.com
Document No. 002-05227 Rev.*A
3
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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.
Figure 4. AD charge method physical diagram
MCU
ADC-pin
Figure 5. AD charge method curve graph
V
V1
ΔV
Finger not touch
V2
Finger touching
t
t
www.cypress.com
Document No. 002-05227 Rev.*A
4
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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
Figure 6. AD charge method Check Flowchart
Start
Discharge the pad
Charge the pad
Wait timer
interrupt
Y
Save ADC value
www.cypress.com
Document No. 002-05227 Rev.*A
5
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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 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 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
www.cypress.com
Document No. 002-05227 Rev.*A
6
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.2
Parameters Setup
3.2.1
S t r u c t u r e t yp e d e f i n e
Every TSC Key has 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
unsigned
unsigned
unsigned
} Key_Const;
char InitFlag;
int Value;
int Threshold;
int DifThreshold;
//**<
//**<
//**<
//**<
Contains
Contains
Contains
Contains
the
the
the
the
initial status
count value
threshold value now
threshold temp value
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
#define TSCKEY_ADSampleNumConst
100
//Sample number
//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.
www.cypress.com
Document No. 002-05227 Rev.*A
7
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.2.4
Example of Parameters Setup
#define
#define
#define
#define
TSCKEY_ADSampleNumConst
TSCKEY_GPIOSampleNumConst
TSCKEY_ADDifThrOffset
TSCKEY_GPIODifThrOffset
100
100
35
35
//Sample number
//Sample number
//Threshold Difference Offset
//Threshold Difference Offset
#define
#define
#define
#define
#define
#define
#define
#define
TSC_GPIOKeyNum
TSC_ADKeyNum
TSCKEY_GPIOPORT
TSCKEY_ADPORT
TSCKEY_ChargePORT
TSCKEY_O_WH
TSC_TimerData
TSC_TimerEn
1
8
0x0014
0x0000
0x0002
0x01
T00DR
T00CR1_STA
//TSCKey number
//TSCKey number
//TSCKey port register
//TSCKey port register
//TSCKey port register
//TSCKey pin dir & data
//Timer Data
//Timer Enable
typedef struct
{
unsigned
unsigned
unsigned
unsigned
char InitFlag;
int Value;
int Threshold;
int DifThreshold;
//**<
//**<
//**<
//**<
Contains
Contains
Contains
Contains
the
the
the
the
>> P5
>> P0
>> P1
initial status
count value
threshold value
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];
www.cypress.com
Document No. 002-05227 Rev.*A
8
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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. Initialization Function Table (GPIO Method)
Prototype
void TSCKey_GPIOInit (Key_Const *Key);
Parameter:
Key_Const *
Return
Description
Remark
Key
Pointer to the TSC Key array
void
This function initializes the timer used, the data structures of the library and calculates the initial
capacitance of the electrodes.
N/A
Table 4. Initialization Function Table (AD Method)
Prototype
void TSCKey_ADInit (Key_Const *Key);
Parameter:
Key_Const *
Return
Description
Remark
3.3.2
Key
Pointer to the TSC Key array
void
This function initializes the timer used, the data structures of the library and calculates the initial
capacitance of the electrodes.
N/A
Check Sensor Function
Table 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
unsigned int
Check_count
The counter value
Return
Description
Remark
This function is used to Check if the charge process are completed and save the value.
N/A
Table 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
unsigned int
Check_count
The counter value
Return
Description
Remark
www.cypress.com
This function is used to Check if the charge process are completed and save the value.
N/A
Document No. 002-05227 Rev.*A
9
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.3.3
Get Value Function
Table 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
Remark
This function is used to detect the key touched status, and generate a specific key word.
N/A
Table 8. Get Value Function Table (AD Method)
Prototype
unsigned char TSCKey_GetADValue(Key_Const *Key)
Parameter:
Key_Const *
Key
Pointer to the TSC Key array
The Key word for the service function
Return
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‟
3.3.4
Description
This function is used to detect the key touched status, and generate a specific key word.
Remark
N/A
Update Threshold Function
Table 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
www.cypress.com
Document No. 002-05227 Rev.*A
10
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.4
Cypress TSC Performance
Table 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
www.cypress.com
Document No. 002-05227 Rev.*A
11
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.5
How to Add Cypress TSC.lib
3.5.1
A d d C yp r e s s T S C . l i b t o U s e r ’ s P r o j e c t
1. In Softune, Right click on folder Include Files  select Add member to folder from the menu select File.
Figure 7. 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 8. Popup Add Member dialog box
www.cypress.com
Document No. 002-05227 Rev.*A
12
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.
In Add Member dialog box, select „ALL Files‟ from „Files of Type‟, and then you will find the TSC.lib
Figure 9. Found the lib file
4.
Double click TSC.lib, and then you can see it has been added in the folder Include Files
Figure 10. Add TSC.lib
www.cypress.com
Document No. 002-05227 Rev.*A
13
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.5.2
Include Header File
1. Add "#include"TSC.h "" in C file, such as in "main.c ".
Figure 11. 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 12. Include header file successfully
www.cypress.com
Document No. 002-05227 Rev.*A
14
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
3.6
How to use Cypress TSC.lib
After complete above works, we can start use the TSC library now. The operation categorized by if connects 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.
Figure 13. Initialize Flow chart
Start
Disable comparer
Disable system interrupt
Set Oscillator
Set interrupt level
Enable system interrupt
Initialize the GPIO
Initialize the UART
Initialize the timer
End
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.
www.cypress.com
Document No. 002-05227 Rev.*A
15
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
Figure 14. Main loop Flowchart
Start
Wait time interrupt
Check sensor
Save counter value
N
UART
Communication
GUI
Meet
detect condition?
Y
Generate key word
Key service
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:
www.cypress.com
Document No. 002-05227 Rev.*A
16
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
#include "mb95410.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)
#else
ILR2 = 0xFF;
// IRQ8: LIN-UART (transmission)
#endif
ILR4 = 0x3F;
// IRQ19: Timebase timer
}
/******************************************************************************
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);
/******************************************************************************
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)
#else
#pragma intvect DefaultIRQHandler
8
// IRQ8: LIN-UART (transmission)
#endif
#pragma intvect TBT_ISR
19 // IRQ19: Time base timer
5
Additional Information
Please contact your local support team for any technical questions.
www.cypress.com
Document No. 002-05227 Rev.*A
17
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
5.1
Sample code
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
}
www.cypress.com
Document No. 002-05227 Rev.*A
18
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
//==================================================================
// 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
www.cypress.com
Document No. 002-05227 Rev.*A
19
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
GPIO_Init();
TBT_Init();
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;
}
www.cypress.com
Document No. 002-05227 Rev.*A
20
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
Div_Counter++;
}
}
}
//==================================================================
// 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;
}
www.cypress.com
Document No. 002-05227 Rev.*A
21
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
5.1.1
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 "mb95410.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)
www.cypress.com
Document No. 002-05227 Rev.*A
22
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
// +------------
Serial data direction control bit(Transmit/receive data from MSB side sequentially)
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
}
www.cypress.com
Document No. 002-05227 Rev.*A
23
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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;
}
}
www.cypress.com
Document No. 002-05227 Rev.*A
24
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
__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;
www.cypress.com
Document No. 002-05227 Rev.*A
25
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
break;
}
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();
}
}
www.cypress.com
Document No. 002-05227 Rev.*A
26
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
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);
www.cypress.com
Document No. 002-05227 Rev.*A
27
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
5.1.2
K e y_ S e r vi c e F u n c t i o n
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 "mb95410.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;
}
}
void TSCKey_Service(void)
{
unsigned char i;
www.cypress.com
Document No. 002-05227 Rev.*A
28
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
if(mTSCKeyCode != TSCKeyCode)
{
if(TSCKeyCode != 0xFF)
{
for(i=0;i<4;i++)
{
if((TSCKeyCode | (1 << i)) == 0xFF)break;
}
Beep(1,500);
}
mTSCKeyCode = TSCKeyCode;
}
}
www.cypress.com
Document No. 002-05227 Rev.*A
29
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
6
Document History
Document Title: AN205227 - F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
Document Number: 002-05227
Revision
**
ECN
-
Orig. of
Change
HUAL
Submission
Date
Description of Change
06/03/2011
Initial release
07/26/2011
1) Updated template to the latest version
2 ) Modified API name and function description
based on new TSC.lib
3 ) Added new sample code, include UART and Key
service functions
*A
5260593
HUAL
06/22/2016
Migrated Spansion Application Note MCU-AN-500125-E-11 to Cypress
format
This app note is primarily about a particular library and how to use it,
that LIB file must be attached to this app note.
But there is no .lib file on spansion.com, So this AN is for obsolete.
www.cypress.com
Document No. 002-05227 Rev.*A
30
F²MC-8FX Family, MB95F410H/470H Series TSC2_DEMO_418.Lib
Worldwide Sales and Design Support
Cypress maintains a worldwide network of offices, solution centers, manufacturer‟s representatives, and distributors. To find
the office closest to you, visit us at Cypress Locations.
PSoC® Solutions
Products
®
®
ARM Cortex Microcontrollers
cypress.com/arm
cypress.com/psoc
Automotive
cypress.com/automotive
PSoC 1 | PSoC 3 | PSoC 4 | PSoC 5LP
Clocks & Buffers
cypress.com/clocks
Cypress Developer Community
Interface
cypress.com/interface
Lighting & Power Control
cypress.com/powerpsoc
Memory
cypress.com/memory
PSoC
cypress.com/psoc
Touch Sensing
cypress.com/touch
USB Controllers
cypress.com/usb
Wireless/RF
cypress.com/wireless
Community | Forums |Blogs | Video |Training
Technical Support
cypress.com/support
PSoC is a registered trademark and PSoC Creator is a trademark of Cypress Semiconductor Corporation. All other trademarks or registered trademarks
referenced herein are the property of their respective owners.
Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone
Fax
Website
: 408-943-2600
: 408-943-4730
: www.cypress.com
© Cypress Semiconductor Corporation, 2011-2016. This document is the property of Cypress Semiconductor Corporation and its subsidiaries,
including Spansion LLC (“Cypress”). This document, including any software or firmware included or referenced in this document (“Software”), is owned
by Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such
laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other
intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with
Cypress governing the use of the Software, then Cypress hereby grants you a personal, non-exclusive, nontransferable license (without the right to
sublicense) (1) under its copyright rights in the Software (a) for Software provided in source code form, to modify and reproduce the Software solely for
use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end
users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of
Cypress‟s patents that are infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for
use with Cypress hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD
TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. To the extent permitted by applicable law, Cypress reserves the right to
make changes to this document without further notice. Cypress does not assume any liability arising out of the application or use of any product or
circuit described in this document. Any information provided in this document, including any sample design information or programming code, is
provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality and
safety of any application made of this information and any resulting product. Cypress products are not designed, intended, or authorized for use as
critical components in systems designed or intended for the operation of weapons, weapons systems, nuclear installations, life-support devices or
systems, other medical devices or systems (including resuscitation equipment and surgical implants), pollution control or hazardous substances
management, or other uses where the failure of the device or system could cause personal injury, death, or property damage (“Unintended Uses”). A
critical component is any component of a device or system whose failure to perform can be reasonably expected to cause the failure of the device or
system, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim,
damage, or other liability arising from or related to all Unintended Uses of Cypress products. You shall indemnify and hold Cypress harmless from and
against all claims, costs, damages, and other liabilities, including claims for personal injury or death, arising from or related to any Unintended Uses of
Cypress products.
Cypress, the Cypress logo, Spansion, the Spansion logo, and combinations thereof, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are trademarks or
registered trademarks of Cypress in the United States and other countries. For a more complete list of Cypress trademarks, visit cypress.com. Other
names and brands may be claimed as property of their respective owners.
www.cypress.com
Document No. 002-05227 Rev.*A
31