Pages

Total Pageviews

Sunday, August 2, 2015

PIC32 Tic-Tac-Toe: Demonstration of using touch-screen, TFT and the Protothreads threading library

I had previously used the Adafruit TFT display using my
library (ported from the Adafruit Arduino library). I decided to
optimize the library to improve drawing speed. The same
display I use comes with a 4-wire resistive touch-screen as well. I
decided to write a simple library for the touch-screen and give
Protothreads a try. To incorporate all this, I thought it would be cool
if I used these to make a simple game. Tic-tac-toe came to mind as a fun
little demo.

I'm sure everyone's familiar with the game
so I won't explain the rules there. The touch-screen is simply two
resistive sheets placed on top of each other on top of the TFT screen.
When it is pressed down at a given place, the two sheets make contact
and a voltage divider is formed. Using the IO's and the ADC, this
voltage is read in the X and Y directions to register a touch.

Here is a very good pictorial depiction of the resistive touch screen (taken from the Atmel AVR341 document):

So
in order to read the touch, the X+ and X- points are applied power, and
one of Y+ or Y- is read to read the x-coordinate. Then Y+ and Y- are
applied power and one of X+ or X- is read to read the y-coordinate. X+,
X-, Y+ and Y- are connected to four GPIO pins on the PIC32 that are
configured to outputs when driving the touch-screen and analog inputs
when reading. Every time the IO pin switches state, a long delay is
provided to allow the outputs to stabilize. Alternately, the ADC is
significantly slowed down to negate effects of capacitive charging by
high source impedance. The library is written in the form of a simple
state machine cycling through its states every few milliseconds, decided
by the application calling the library functions. In my application, I
use 5 milliseconds.

There
are two main executing threads, one is the main game thread and the
other is a clock thread that keeps track of, and displays, time since
the program was started. There is a third thread used to retrieve touch
information. It is spawned by the main game thread when touch input is
required. The main Protothreads functions (macros) I've
made use of are:

BL (backlight): I left it unconnected, but you can connect it to 3.3V for backlight.
SCK: connected to RB14 on the PIC
MISO: left unconnected, since I'm not reading anything from the screen
MOSI: connected to RB11 on the PIC
CS: connected to RB1 on the PIC
SDCS: left unconnected as I'm not using the microSD card for this
RST: connected to RB2 on the PIC
D/C: connected to RB0 on the PIC
X+: connected to RA4 on the PIC
X-: connected to RB13 on the PIC
Y+: connected to RB5 on the PIC
Y-: connected to RB15 on the PIC
VIN: connected to 3.3V supply
GND: connected to gnd

Here is a demo of the game:

Besides the game itself, you can see the running clock on the bottom left right above the players' scores. To the bottom right you can see a flickering circle that is either green or red, depending on if it's player 1 or 2's turn, respectively. Once the game is over, you have the option of playing another game while score is being tracked.

Hi Tahmid , Glad to see your work here. I have a question that is related to IR 2110. I want to understand the groud connection of high side driver with ac grid as a load in case of flyback inverter configuration.Thanks

Recent Posts

Translate this blog

Search This Blog

Follow by Email

About Me

I am Syed Tahmid Mahbub, from Dhaka, Bangladesh, born on August 1, 1994.
Electronics is my passion and from class V, I have been learning electronics. I learnt and worked mostly on SMPS, power electronics, microcontrollers and integration of microcontrollers with SMPS and power electronics. I've used PIC and AVR microcontrollers - PIC 10F, 12F, 16F, 18F, 24F, dsPIC 30F, 33F, PIC32, ATmega and ATtiny, integrating them with various SMPS and power electronics circuits.
I have completed my Bachelor's degree from Cornell University (Class of 2017) in Ithaca, New York, USA, majoring in Electrical and Computer Engineering (ECE).
I am a member of the forum www.edaboard.com, where I am an "Advanced Member Level 5" (the highest level attainable) and also the forum allaboutcircuits.com, where I am a "Senior Member". I post to help solve electronics-related problems of engineers and engineering students from all over the world.
I love watching and playing cricket and football (soccer), and listening to music.
I am now a hardware engineer at Apple in Silicon Valley, California, USA.