Tic-Tac-Toe With Mtouch: June 2010

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

PR28

Tic-Tac-Toe with mTouch

Version 1.0
June 2010
Cytron Technologies Sdn. Bhd.
Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. No representation or warranty is given and no liability is assumed by Cytron Technologies Incorporated with respect to the accuracy or use of such information or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Cytron Technologiess products as critical components in life support systems is not authorized except with express written approval by Cytron Technologies. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights. FEATURES

OVERVIEW
This document describes the development of Cytron Technologies DIY (Do It Yourself) Project PR28. This project demonstrates hobbyist on how to build a simple Microchips mTouch where no extra components are needed excluding the sensing pads on PCB. In this project, capacitive sensing module (CSM) is used. Tic Tac Toe game is one of the simple applications of mTouch. This DIY project will be using PIC 16F727 microcontroller, 74HC595 shift registers, piezo, mTouch, RGB LEDs and 2 x 16 characters LCD.

PIC16F727 - 8-bit microcontroller with 35 I/O - Operates with 5V supply - Operating speed 20MHz - mTouch Sensing Oscillator Module which is up to 16-input channels 74HC595 Shift Register - 8-bit serial input - 8-bit serial or parallel output - Shift register with direct clear Capacitive Touch Sensor (mTouch) - Made up from copper sensor pads

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

SYSTEM OVERVIEW

74 HC 595 mTouch

RGB LEDs

PIC16F727

LCD Piezo

GENERAL DESCRIPTION
PR28 is an open-sourced microcontroller Do It Yourself kit. This PIC microcontroller based project is designed to develop a Tic-Tac-Toe game using mTouch. mTouch, also known as capacitive touch sensor, can replace mechanical buttons with capacitive alternative. In addition, it helps in cost reduction. mTouch has become more prevalent and in demand for commercial applications such as mobile devices and mp3 players. Thus, this project will show users how to work with mTouch. For information, PR28 uses internal oscillator to run the program. Enhanced Timer1: a. Dedicated low-power 32 kHz oscillator b. 16-bit timer/counter with pre-scaler c. External Gate Input mode with toggle and single shot modes d. Interrupt-on-gate completion 2 capture/compare/PWM functions Synchronous Serial Port (SSP): a. SPI (Master/Slave) b. IC (Slave) with Address Mask Addressable Universal Asynchronous Receiver Transmitter (AUART). mTouch Sensing Oscillator Module which is up to 16 input channels

PIC16F727
This powerful (200 nanosecond instruction execution) yet easy-to-program (only 35 single word instructions) CMOS FLASH-based 8-bit microcontroller with nanoWatt XLP Technology packs Microchip's powerful PIC architecture into a 28- or 40- or 44-pin package and is upwards compatible with the 28- or 40- pin PIC16CXX and PIC16FXXX Microcontrollers. Features of the device: Interrupt capability 8-Level Deep Hardware Stack Direct, Indirect or Relative Addressing modes Processor Read Access to Program Memory Precision Internal Oscillator a. 16MHz or 500kHz operation b. Factory calibrated to 1%, typical c. Software tunable d. Software selectable +1, +2, +4 or +8 divider Multiplexed Master Clear with Pull-up or Input Pin 14 channels of 8-bit Analog-to-Digital (A/D) converter where conversion is also available during sleep mode

40-pin PDIP (PIC 16F727)

Figure 1 Figure 1 shows the pin diagram for PIC16F727. For more information about the PIC microcontroller, please refer to the datasheet. The datasheet can be found in microchip web site at: http://www.microchip.com

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

74HC595 Shift Register


74HC595 shift register is the cheapest and easiest way to extend I/O ports if user is running out of I/O pins. Using this 8-bit shift register, user can control 8 outputs at a time while only taking up 3 I/O ports on microcontroller. Features of this device are as follows: 8-bit serial input 8-bit serial or parallel output Storage register with 3-state output Shift register with direct clear 100MHz (typical) shift out frequency Output capability a. Parallel outputs; bus driver b. Serial outputs; standard Icc category: MSI For more information on 74HC595 shift register, please refer to the datasheet which is available at the website http://www.nxp.com/documents/data_sheet/74HC_HCT5 95.pdf

Push Button microcontroller

as

input

for

PIC

Figure 4 One push button needs one I/O pin to be used as an input for PIC microcontroller. The connection of the push button to the I/O pin is shown in Figure 3. The I/O pin should be pulled up to 5V using a resistor (with value range 1K-10K) and this configuration will result in an active-low input. When the button is being pressed, reading of I/O pin will be in logic 0, while when the button is not pressed, reading of that I/O pin will be logic 1.

Capacitive Touch Sensor (mTouch)


The construction of mTouch only consists of copper pads on PCB board. There is no extra components needed to make the mTouch functions. The detailed explanation on how to work with mTouch will be discussed in later section.

Power supply for the circuit

ICSP for microcontroller

programming

PIC

Figure 5 The basic connection for In-Circuit Serial Programmer (ICSP) is as displayed in Figure 5. There are a few types of USB in-circuit programmer can be found in Cytron, for example, UIC00B. However, programmer is not included in this DIY project package since the programmer can be reused for other projects. For the instruction of using a PIC programmer, please refer to the particular PIC programmer users manual at cytron website.

Figure 3 User can choose either AC to DC adaptor (not included in the DIY project set) or 9V-12V battery (not included in the DIY project set) to power up the circuit. Higher input voltage will produce more heat at LM7805 voltage regulator. Typical voltage is 12V. Anyhow, LM7805 will still generate some heat at 12V. There are two type of power connector for the circuit, DC plug (J1) and 251002 (Power Connector). Normally AC to DC adaptor can be plugged to J1 type connector. Referring to Figure 3, the D2 is used to protect the circuit from wrong polarity supply. C2 and C3 are used to stabilize the voltage at the input side of the LM7805 voltage regulator, while the C1 and C4 are used to stabilize the voltage at the output side of the LM7805 voltage supply. LED is a green color LED to indicate the power status of the circuit. R1 is a resistor to protect LED from excessive current that will burn the LED.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

Transistor

HARDWARE
This project will require following hardware: a. b. c. d. e. f. g. h. 1 x PIC16F727 1 x PR28 Printed Circuit Board (PCB) 1 x LCD (2x16 character) 3 x 74HC595 Shift Register 9 x RGB LEDs 1 x piezo 1 x 9V-12V power supply Related electronic components

Figure 8 The transistor is used to amplify the current for the RGB LEDs. In this project, 3 NPN transistors, 2N2222A, are used. The pin sequence is as shown in Figure 8. Beware that wrong sequence will burn the transistor.

Please refer to Appendix A for the board layout of PR28. The board layout is provided for free therefore Cytron Technologies will not be responsible for any further modification or improvement.

RGB LEDs

Interface LCD (2x 16 Character) with PIC16F727


To use the LCD display, users have to solder 16 pin header pin to the LCD display. LCD used in this project is RT1602C, for other type of LCD, please refer to its data sheet.

Figure 9 The RGB LED contains 3 LEDs encased in one shell. It looks like a single white LED except that it has four leads. It can be used to display 3 different colors which are red, green or blue. Pin 1, pin 3 and pin 4 are the anode pins for the red color LED, green color LED and blue color LED, respectively. Pin 2 which has the longest leg, is the cathode pin. In this project, pin 1, 3 and 4 are connected to the 74HC595 shift register whereas pin 2 is connected to the collector of a transistor. In this project, RGB LEDs are used to show the output for the Tic-Tac-Toe game where red color represents player 1 and green color to indicate player 2. In addition, if there is no winner in the game, blue color LEDs will blink continuously.

Figure 11 Figure 11 is a 2X16 character LCD. LCD connection pin and function of each pin is shown: Pin 1 2 3 Name VSS VCC VEE Pin function Ground Positive supply for LCD Brightness adjust Connection GND 5V Connected to a trimmer to adjust LCD brightness RE0

Piezo
The piezo used in this project works similarly as a buzzer except that it has no polarity. The difference of piezo and buzzer is that piezo can play melody with the use of PWM. Of course, before using PWM function, the piezo needs to be connected to CCP1 or CCP2 pin of PIC microcontroller. In this case, the piezo is connected to RC2 which is CCP1. To play a certain note, we need to adjust the PR2 value to get the PWM period needed by using the equation below.

RS

PWM period = [(PR2) +1] 4Tosc


To fully understand the working of the piezo, kindly refer to the sample source code provided as well as the PIC microcontroller datasheet.

5 6

R/W E

7 DB0 8 DB1 9 DB2 Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

Select register, select instruction or data register Select read or write Start data read or write Data bus pin Data bus pin Data bus pin

GND RE1 RB0 RB1 RB2

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch 10 11 12 13 14 15 DB3 DB4 DB5 DB6 DB7 LED+ Data bus pin Data bus pin Data bus pin Data bus pin Data bus pin Backlight positive input RB3 RB4 RB5 RB6 RB7 a resistor to limit excessive current GND

16

LED-

Backlight negative input

Figure 14

Figure 12

Interface 74HC595 with PIC16F727


The basic connection of 74HC595 is as demonstrated in Figure 13 below. Figure 15

Interface mTouch with PIC16F727


To utilize the capacitive sensing module in PIC16F727, all the copper pads must be connected to the capacitive channels of PIC microcontroller and set those pins to analog pins. This step is very vital before proceeding to software algorithm. In this project, the mTouch or touch pad is connected as shown in Figure16. Figure 13 As mentioned earlier, 74HC595 is a serial-in parallel- or serial-out shift register. Basically, it works as synchronous serial communication where one pin can be pulsed up and down thereby communicating a data byte to the register bit by bit. By pulsing second pin, the clock pin, bits can be delineated. To have clearer picture on this, please refer to Figure 14 and Figure 15, respectively.

Figure 16 Theoretically, to know whether the touch pad has been touched or not is by determining the change of capacitance. As demonstrated in Figure 17, before a

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch finger presses the pad, the PCB pad itself has a capacitance, Cp. However, if a finger presses the copper pad, another capacitance, Cf is introduced where it is in parallel with Cp. Thus, the equivalent of capacitance increases after finger press. Additionally, the capacitance change will result in change in frequency. To scan a pad, frequency change on copper pad is measured. To get a clear picture on this, please refer to Figure 17.

Not Pressed Figure 17 There are a few methods to perform capacitive sensing, such as comparator, Charge Time Measurement Unit (CTMU), Capacitive Voltage Divider (CVD) and Capacitive Sensing Module (CSM). In this project, the touch pad is sensed by capacitive sensing module (CSM) at fixed time interval. The benefit of this method is that it does not need external oscillator since the module has its own oscillator embedded. The frequency of each pad at rest is averaged.

Pressed

Not Pressed

Figure 18 For more detailed information on the working of mTouch, kindly refer to the application notes which can be downloaded for free in Microchip website (http://www.microchip.com)

Explanation mTouch

on

Tic-Tac-Toe

using

Frequency Measurement
Since CSM is a frequency-based method, timer method is used. Basically, Timer 2 is used as the timer resource instead of using Timer 0 due to it has greater flexibility in defining time base. On the other hand, Timer 1 gate is used as a counter. It will increment at every rising edge of capacitive sensing module output frequency. The value on the Timer 1 will be a measure for CSM oscillator frequency. The completion of Timer 1 gate event, triggered by Timer 2 overflow, will generate a Timer 1 gate interrupt. When servicing the interrupt, the value from the Timer 1 can be read to determine the oscillator frequency. In detailed, capacitance of the pad alone results in a corresponding square wave frequency. When the PR2 value is matched, the current count value in Timer 1 will be read and stored as reference. Once the finger is touched, the RC time constant of the oscillator increases and thus results in a decrease of frequency of the square wave output. Therefore, on the next interrupt, the Timer 1 value will be smaller. Using a software algorithm to compare the difference between these values, the sensor can be identified as pressed or not pressed. Below explains the frequency changes graphically.

When a pad is touched, the frequency on the CSM changes due to the extra capacitance from the finger. The change in frequency is noted and the LEDs light up to indicate which button was pressed. Since the mTouch is used for the Tic-Tac-Toe game, the LEDs will not light up immediately after the touch pad has been pressed. Each copper pad is only responsible for only one RGB LED. For example, a touch pad which is noted as X1 is responsible to the RGB LED that is also noted as X1. When that particular touch pad has been pressed and being detected by PIC microcontroller, PIC will check whether that particular pad has been touched previously or not. If yes, that particular RGB LED will not response. On the contrary, the RGB LED will light up.

PCB circuit board


2 1 8 9 14 10 3 4 5 11 15 19 17 18 16 6 12 13 7

Components: 1. 2. 3. Created by Cytron Technologies Sdn. Bhd. All Rights Reserved Slide Switch ( to ON or OFF the circuit) 2510-02 connector ( to use either 9V or 12V battery) AC-DC adaptor socket

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. IN 4007 diode ( to protect the circuit from wrong polarity) LM7805 (to supply 5V for PIC and 74HC595 Shift register. ICSP programmer connector ( to program the code into PIC) 74HC595 shift register ( to control RGB LEDs) Reset button ( to reset the PIC) LED ( power indicator) 1N4148 ( to protect LED from wrong polarity) Capacitors ( to stabilize both input and output voltage of LM7805) 2N2222 transistors ( to amplify the current for RGB LEDs) RGB LEDs Piezo ( to play melody) PIC 16F727 ( the main brain of this project) Contrast ( to control the contrast of LCD) mTouch (for Tic Tac Toe gaming) LCD ( to display the result and guidelines of the Tic Tac Toe game) 0.1uF capacitor ( to stabilize the supply voltage to the PIC)

Flow chart Measurement

for

ISR:

Frequency

ISR

Read Timer 1 value

Current Reading < (average threshold)?

No

Yes

Button not pressed

Button pressed

Software: Flow Chart for main loop

Start

Perform average

Initialize PIC, LCD And Capacitive Sensing Module

Set Next Channel

Clear all variables and set flag = 0

Restart Timer

Enable interrupt Return

Timer 1 gate interrupt? Yes No Service interrupt

Main loop

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

Flow chart for Tic-Tac-Toe Game

Main loop

Play starting melody

Check flag

Flag = 0? Yes Player 1

No

Player 2

The button pressed has been pressed previously?

Yes

No Store X or O into matrix based on flag

Toggle flag

Display RGB LEDs accordingly

Winner? No

Yes

Yes Draw? Play melody No Toggle flag Display result on LCD

Stop

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch For more information about the software for this system, please refer to the source code provided. The explanation of each instruction is provided in the source code as the comment of each line. The source code is provided free and Cytron Technologies will not be responsible for any further modification or improvement.

Guide for making 2510-02 connector


1 2

GETTING START
User can obtain the hardware set for this project (PR28) either by online purchasing (www.cytron.com.my) or by purchasing it in Cytron Technologies Shop. 1. Once user has the hardware set, soldering process can be started. Please solder the electronic components one by one according to the symbols or overlays on the Printed Circuit Board (PCB). Ensure that the component value and polarity is correctly soldered. Please refer to PCB Layout in Appendix A. 3 4

Caution: Make sure all the connectors (2510) are soldered in proper side. Those electronic components have polarity such as capacitors, diodes, PIC, 74HC595s, LM7805, transistors, RGB LEDs and LED should be soldered in right polarity or it may cause the circuit board fail to work. Warning: Before the battery (Power) is plugged in, make sure the polarity is correct to prevent explosion. Wrong polarity of capacitor also may cause explosion.

Figure 19 Guide for making 2510-02 for 9V battery connector:

Figure 20 (not included in DIY project set)

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch 2. Please download the necessary files and document from Cytron Technologies website. These included documentation, sample source code, schematic, component list and software. 7. 3. The next step is to install MPLAB IDE and HI-TECC C PRO into a computer. The MPLAB IDE and HI-TECC C PRO can be downloaded from www.cytron.com.my . Please refer MPLAB IDE installation step document to install the software. The documents can be used to any version of MPLAB IDE software. After the installation complete, open the project file provided using MPLAB IDE. Please refer MPLAB Open Project document to open the sample program. Plug in power supply for the circuit. User can choose to use battery or AD to DC adaptor. 2. 8. the power. The programmer is not included in the hardware set but it can be found at Cytron website. (User manual is provided at website). Test the functionality of the PCB board. Have fun!

TEST METHOD
1. Switch ON the power Power LED (Green) will turn ON. A short melody will be played. LCD will show Cytron Technologies Sdn Bhd and PR 28 Tic-Tac-Toe with mTouch). Then, LCD will show the indicator for RGB LEDs, which is Player 1 = red and Player 2 = green. Next, LCD will show Player 1 starts first. Press your finger on the touch pad Tic Tac Toe game has been started. RGB LED will turn ON accordingly with the position. For player 1, the RGB LED will be in red color. Whereas, player 2 is in green color. LCD will guide user who is the turn each time. Repeat this step continuously until there is a winner. If player 1 is a winner A melody will be played. LCD will display Congratulation!! **Player 1 win** If player 2 is a winner A melody will be played. LCD will display Congratulation!! **Player 2 win** If there is a draw Different melody will be played. LCD will display Congratulation!! **draw** and blink continuously. All RGB LEDs will be blinking continuously in blue color. If all steps mentioned above can be executed, your project is done successfully. Congratulations!! Please note that do not try to press more than one touch pad each time as it will cause instability of the system.

4.

5.

AC to DC adaptor:

3.

Figure 21 (Not included in DIY project set)

4.

Connection to the PCB board:


5.

12V

Polarity

6.

Figure 22 6. Build the project and load the hex file into the PIC microcontroller using the USB In-Circuit Programmer (UIC00B). When user builds the project, MPLAB IDE will generate hex file. The hex file generated from MPLAB IDE will be named according to project name, not C file name. Cytron Technologies also provide hex file for user. Do not forget to switch ON

WARRANTY
No warranty will be provided as this is DIY project. Thus, user is advised to check the polarity of each electronic component before soldering it to board.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

10

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

Demonstration
1. When player 1 press position Z1 on the touch pad, the RGB LED on position Z1 will turn ON in red color. The LCD will show that player 2 is the next turn. This step is as shown in Figure below.

2.

Next, when player 2 press position Y2 at touch pad, the RGB LED on position Y2 will turn ON in green color. The LCD will then guide users that player 1 is the next turn.

3.

These two steps will repeat till there is a winner or draw.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

11

ROBOT . HEAD to TOE PR28 Tic-Tac-Toe with mTouch

Appendix A PCB Layout:

1N4007 PWR 74HC595 ICSP Adaptor Slide Programmer Switch LM7805 2N2222 Transistors 1N4148 Reset 330 0.1uF 1K LED 1K 4K7 16V 10uF 100 PIEZO PIC 16F727 Contrast

74HC595

74HC595

RGB LEDS

0.1 uF

mTouch
LCD

* Cytron Technologies reserved the right to replace the component in the list with component of the same functionality without prior notice.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

12

ROBOT . HEAD to TOE PR28-Tic-Tac-Toe with mTouch

Prepared by Cytron Technologies Sdn. Bhd. 19, Jalan Kebudayaan 1A, Taman Universiti, 81300 Skudai, Johor, Malaysia. Tel: Fax: +607-521 3178 +607-521 1861

URL: www.cytron.com.my Email: [email protected] [email protected]

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

13

You might also like