This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
I have covered several aspects of the Freescale FRDM-KL25Z Freedom board with a tutorial: LED, Accelerometer and FreeRTOS.
One peripheral on the board is missing so far: the touch slider area.
The Kinetis device has implemented a TSS (Touch Sensing Software) in the silicon. It helps a lot to implement a capacitive touch sensing application, but requires a lot of reference manual reading. Luckily there is a Processor Expert driver which simplifies things a lot. So in this tutorial I’m using that Freescale Processor Expert component.
Creating the Project
I’m not going into the details to create a Processor Expert project with CodeWarrior for MCU10.3 here. You should be able to find all the needed information in the LED, Accelerometer and FreeRTOS tutorials.
Alternatively, it is possible to add the touch support to any of the above tutorial projects.
Related Tutorials
- Tutorial: Enlightening the Freedom KL25Z Board
- Tutorial: Accelerating the KL25Z Freedom Board
- Tutorial: Tutorial: Freescale FRDM-KL25Z Freedom with FreeRTOS and Kinetis-L
Adding the TSS_Library Processor Expert Component
From the Components Library view, I add the TSS_Library component to my project, either with the context menu (Add to project), with the ‘+’ icon in that view or simply double clicking on the TSS_Library component:
Where are the Electrodes?
Looking at the board schematics, it shows that there are two electrodes, connected to PTB16/Channel9 and PTB17/Channel10:
Adding the Electrodes
This two electrodes needs to be configured. For this I select the TSS_Library component in my project. In the Component Inspector view define/add two electrodes. For this I press the ‘+’ sign to increase the number of Electrodes to two.
Hint 1: If the Component Inspector view is not visible: use the context menu ‘Inspector’ on the TSS_Library icon.
Hint 2: In order two show the ‘+’ and ‘-’ sign for the number of electrodes as shown in the screenshot below, I need to click into the field.
With this, I set it up to use two electrodes:
Setting the Controls
The really cool thing with this Processor Expert component is that it provides support for all kind of controls. So I add one slider control, and for now I go with the default settings:
Note that this is a *ASLIDER* (Analog Slider) control. I have it configured that it has a range of 0..64, and that the values will be in a structure named TSS1_cKey0. This structure name and value range is what we will use later when the TSS library detects a touch.
Adding LEDs
In my code I want to show the status of the slider on the RGB LED of my board. I do not explain the steps how to add the LEDs to my project, as this is explained in details in the LED tutorial.
Hint: I can copy-paste the LED component from my other tutorial projects. That way it only takes seconds to have it in my new project.
Generating Processor Expert Driver Code
Now I can generate my driver code based on my settings. The simplest way is to select the project and to use the ‘Generate Processor Expert Code’ button:
The generated code is placed into the ‘Generated_Code’ project folder, which now has my TSS driver code based on my settings:
Initializing the TSS Library
I have not initialized the TSS library. For this I need to call the Configure()
function. An easy way to call that function is to drag&drop that method into my source code:
TSS Library Task Function
The other thing is: I need to periodically call the TSS library with a call to TSS_Task()
. So my code in main needs to add this in a loop:
01 | int main( void ) |
02 | /*lint -restore Enable MISRA rule (6.3) checking. */ |
03 | { |
04 | /* Write your local variable definition here */ |
05 |
06 | /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ |
07 | PE_low_level_init(); |
08 | /*** End of Processor Expert internal initialization. ***/ |
09 |
10 | Configure(); /* initialize TSS library */ |
11 | for (;;) { |
12 | TSS_Task(); /* call TSS library to process touches */ |
13 | } |
14 | /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ |
15 | /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ |
16 | #ifdef PEX_RTOS_START |
17 | PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ |
18 | #endif |
19 | /*** End of RTOS startup code. ***/ |
20 | /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ |
21 | for (;;){} |
22 | /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ |
23 | } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/ |
TSS Library Callback
The function TSS_Task()
will detect if the slider has been touched, and will call a callback. This callback is named ‘fcallBack()
‘. For my first (and only) control in the system this is TSS1_fCallback0()
:
Double clicking on that callback event jumps the editor view to the user callback function of it in Events.c:
Remember the Range and Structure Name we defined in the control? Now it is time to use it here and to turn on a LED depending on the range value. This is implemented with following code:
1 | void TSS1_fCallBack0(TSS_CONTROL_ID u8ControlId) |
2 | { |
3 | ( void )u8ControlId; /* avoid warning */ |
4 | LED1_Put(TSS1_cKey0.Position<=20); |
5 | LED2_Put(TSS1_cKey0.Position>20 && TSS1_cKey0.Position<=40); |
6 | LED3_Put(TSS1_cKey0.Position>40 && TSS1_cKey0.Position<=64); |
7 | } |
Touching it
The rest is easy: compile, download and run it on the board. With this, LED1 is on if I touch it on the left, LED2 is on if I touch it in the middle, and LED3 is on if I touch it on the right .
Summary
Using capacitive touch functionality is usually not easy. But the TSS Processor Expert component delivered with the Eclipse based CodeWarrrior for MCU10.3 makes it really easy. For sure there are a lot of things which can be fine tuned (sensitivity, resolution, kind of controls), but I hope this gives an idea what can be implemented. Additionally there is a lot of documentation about the TSS library available here.
The current software was attached to this document but I would recommend to download the updated version on GitHub with all the Processor Expert components.