PIC18F57Q43 Curiosity Nano and QT8 Touch Board Example

Last modified by Microchip on 2024/06/24 06:29

Objective

This guide shows you how to use the MPLAB® Code Configurator (MCC) mTouch® Sensing Solutions module to create a touch surface project and view the data in the 2D touch surface utility. The setup requires the PIC18F57Q43 Curiosity Nano Board, Curiosity Nano Touch Adapter, QT8 Xplained Pro Extension Kit, and a connecting cable.

PIC18F57Q43 Curiosity Nano with Q8 Touch board attached

Reference Materials

Back to Top


Procedure

Create an MPLAB X IDE Project for the PIC18F57Q43
  • Choose Project: Standalone Project
  • Select Device: PIC18F57Q43
  • Select Tool: PIC18F57Q43 Curiosity Nano
  • Select Compiler: XC8
  • Select Project Name and Folder

Screenshot of PIC_QT8 Curiosity Nano Example Project in MPLAB X IDE

If you are not familiar with MPLAB X IDE, please visit the "Learn MPLAB X IDE" page.

Back to Top


Open MCC in MPLAB X IDE

If you do not see the MCC logo, please check that you have installed the MCC plugin. More information on MCC installation can be found on the "MPLAB Code Configurator (MCC)" page.

MPLAB X toolbar showing the MCC button to open the tool

Back to Top


Configure the System Clock

Open the System Module from the Project Resources menu. Select HFINTOSC from the Oscillator Select drop-down box and select 32_MHz from the HF Internal Clock drop-down box. Choose a division factor of 1 from the Clock Divider drop-down box. This will result in a 32 MHz system clock.

Screenshot of system clock setup in MCC

To ensure the performance of the mTouch surface, the system clock is required to be at least 8 MHz. If you select a system clock slower than 8 MHz, the mTouch module will generate a warning in the notification window.

Back to Top


Load the mTouch Module

Double-click on the mTouch button in the Libraries list inside the Device Resources window.

Device Resources window

After loading the module, the mTouch button will appear in the Project Resources pane.

 Project Resources pane

Back to Top


Select the mTouch Sensors

After loading the mTouch module, all available mTouch sensor pins will be shown in the Pin Manager: Grid View pane. You will need to select the physical sensors based on the sensor pinout information. Choose the sensors in the following order:

  1. RB0
  2. RA2
  3. RA6
  4. RC4
  5. RA7
  6. RC5
  7. RD4
  8. RA0
  9. RB3
  10. RC6

Pin Manager Grid View

Back to Top


Add mTouch Surface and Link to the Sensors

Go to the Surfaces configuration view, click Create New Surface.

mTouch window

Now, based on the surface design configuration, make the changes. Choose Horizontal Key Count as 5 and Vertical Key count as 5 and click Create/Upgrade Grid.

mTouch window

Back to Top


Enable Debugging with 2D Surface Utility and Configure Universal Asynchronous Receiver Transmitter (UART) Module

Go to the Debug tab within the mTouch module. Check the Enable Debug box, select UART1 to be the UART module, and select 2D Surface Utility as the debug method. The UART module will be automatically added to the project and should appear in the Project Resources window.

mTouch window

Click on the UART1 module in the Project Resources window to check that it is configured correctly.

UART1 Easy setup tab

Check that the RX and TX pins are properly selected in the Pin Manager: Grid View. The RX pin should be RB1 and the TX pin should be RB0.

Pin Manager Grid View

Back to Top


Configure LEDS through I2C

The LEDs in the QT8 board are driven using the I2C peripheral. Choose the I2C peripheral from the Device Resources menu.

Device Resources pane

I2C peripheral will be added to the Project Resources.

Project Resources pane

Use the Pin Manager: Grid View to select the I2C pins.
SCL – RB1, SDA – RB2

Pin Manager Grid View

Back to Top


Generate Code

Click on the Generate button.

Generate button

Back to Top


Call mTouch Service and Enable Interrupts in main.c

Open the generated main.c file. Place MTOUCH_Service_Mainloop() into the while loop as shown in the code block, uncomment the functions that enable interrupts.

Add the files corresponding to the LED driver (led_driver.cled_driver.h) to the project and call the LED functions correspondingly.
The contents of main.cled_driver.cled_driver.h files are shown:

main.c:​

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "mcc_generated_files/mcc.h"
#include
"led_driver.h"
/*
Main application
*/

void main(void)
{
// Initialize the device
SYSTEM_Initialize();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
init_led_driver();
led_reset();
while (1)
{
// Add your application code
if(MTOUCH_Service_Mainloop())
{
led_decode_position();
}
}
}
/**
End of File
*/

Back to Top


led_driver.c:​

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* led_driver.c
*
*/

#include "led_driver.h"
#include
"mcc_generated_files/mtouch/mtouch_surface.h"
#include
"mcc_generated_files/examples/i2c1_master_example.h"
#if ENABLE_LED == 1u
/*
* =======================================================================
* LED driver globals
* =======================================================================
*/

static uint8_t i2c_write_buf[3];
/*
* =======================================================================
* LED driver Function Definitions
* =======================================================================
*/

void init_led_driver(void)
{
i2c_write_buf[0] = LED_HOR_DIR_ADDR; // reg direction
i2c_write_buf[1] = 0x00; // output
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_HOR_REG_ADDR; // reg address
i2c_write_buf[1] = 0x00; // all low 0X6F
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_VER_DIR_ADDR; // reg direction
i2c_write_buf[1] = 0x00; // output
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
i2c_write_buf[0] = LED_VER_REG_ADDR; // reg address
i2c_write_buf[1] = 0x00; // all low 0X1F
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
}
void led_gpio_update(uint8_t data, uint8_t ver_or_hor)
{
if (ver_or_hor == LED_HOR) {
i2c_write_buf[0] = 0x14; // reg address
i2c_write_buf[1] = data; // all low
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
} else {
i2c_write_buf[0] = 0x15; // reg address
i2c_write_buf[1] = data; // all low
I2C1_WriteNBytes(LED_DRIVER_ADDR, i2c_write_buf, 2u);
}
}
void led_decode_position(void)
{
uint8_t h_pos = 0u, v_pos = 0u;
led_reset();
if (MTOUCH_Surface_Contact_Status_Get(0u)) {
h_pos = (MTOUCH_Surface_Position_Get(HORIZONTAL,0u) / 43u);
h_pos = (1u << (5u - h_pos));
led_gpio_update(h_pos, LED_HOR);
v_pos = (MTOUCH_Surface_Position_Get(VERTICAL,0u) / 52u);
v_pos = (1u << (4u - v_pos));
led_gpio_update(v_pos, LED_VER);
}
}
void led_reset(void)
{
led_gpio_update(0u, LED_HOR);
led_gpio_update(0u, LED_VER);
}
#endif

Back to Top


led_driver.h:​

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* led_driver.h
*
*/

#ifndef LED_DRIVER_H_
#define LED_DRIVER_H_
#include
"mcc_generated_files/mcc.h"
#define ENABLE_LED 1u
#if ENABLE_LED == 1u
/*
* =======================================================================
* LED Driver constants
* =======================================================================
*/

#define LED_DRIVER_ADDR 0x20
#define LED_HOR_DIR_ADDR 0x00
#define LED_VER_DIR_ADDR 0x01
#define LED_HOR_REG_ADDR 0x14
#define LED_VER_REG_ADDR 0x15
#define LED_HOR 0u
#define LED_VER 1u
/*
* =======================================================================
* LED Driver prototypes
* =======================================================================
*/

void init_led_driver(void);
void led_gpio_update(uint8_t data, uint8_t ver_or_hor);
void led_decode_position(void);
void led_reset(void);
#endif
#endif 
/* LED_DRIVER_H_ */

Back to Top


Program the Board

After you connect the board to the PC with a USB cable, click the Program button to program the board.

Make and Program Device button

Back to Top


Connect to 2D Surface Utility

Open the 2D Surface GUI, change the Baud rate to 115200 and click Apply.

2D Touch Surface Utility window

When a successful connection is established, you will see the live touch data appear in the utility window. This data can be useful for debugging and tuning a touch project.

2D Touch Surface Utility window

Back to top