With the increasing popularity of smartphones and tablet computers, touchscreen has become one of the most common user interfaces encountered today. The idea of this project came from some apps on the smart phone. It is very interesting to play a virtual Chinese chess on the mobile phone. People can take them everywhere and play it whenever they want. So we decided to build the electronic Chinese Chess game in our project. Meanwhile, LCD touch screen is a very popular thing used in electronic products, and doing the project will give us experience in utilizing a set of devices that is popular in modern devices. So in our final project, we designed a Chinese chess game application on touchscreen. Users can play Chinese chess on it. In addition, they can set the background as they like. Meanwhile, this project will also provide ECE4760 course a library for initializing and developing LCD touch screens of the same configuration for reference.

High Level Design
Rationale
It is quite meaningful for us to do this project. On one hand, with the increasing popularity of touch-screen mobile phones and tablet PCs, touch screen becomes more and more important in our lives. It is much more convenient and flexible than the traditional keyboard. On the other hand, Chinese Chess is a traditional Chinese intellectual game which can be dating back to Warring States Period in China. This old game can help people to improve their consciousness ability, thinking ability and add much fun to our life.
Logical structure
At the high level, our project mainly consists of two parts: the ATmega1284p microcontroller, the LCD touch screen. Atmega1284p is the main control part. When user presses on the screen, the signal will be sent to the MCU and some movements would be made. Also the MCU will control what is shown on the LCD screen and the state mode, based on the screen interrupt.
The connection between MCU and touch screen needs a LCD driver and an ADC chip. They are already on the touch screen board.
And we use putty to show that it’s which player’s turn to make a movement.
Hardware/Software tradeoffs
The hardware part of our project is not very complex. We soldered our Atmega1284p MCU and the LCD touch screen together. The LCD touch screen consists of two chips, SSD1289 LCD control chip and ADS7843 touch panel controller chip.
For the software part, the tricky part is to drive the LCD touch screen to work. Based on the driver code of similar lcd touch screen but of different configurations as ours, we read the datasheet of SSD1289 LCD control chip carefully, and changed the driver code so that it can be used to drive our LCD, and made it work. The other main part is to implement Chinese chess game rules, we defined a structure to store the chessmen’s information, so it is not that difficult. Another fussy part is to debounce each touch and adjust coordinates of touched points, because of the size limitation of the touch screen, we requires a high resolution, so the coordinates adjustment part is important. We figured a way out to debounce each touch with the help of a timer and adjust the coordinates by Putty.
Standards and Patents
Our design uses the SPI(serial peripheral interface) to realize the communication of touchpanel and AVR. Devices communicate in master/slave mode where the master device initiates the data frame.
Hardware Design and Implementation
Hardware Overview
From the high-level discussed above, our hardware consists of two parts, which include the LCD touch screen and the Atmega1284 prototype board. Below we will discuss each part separately.
LCD touch screen
The TFT 3.2′ LCD touch screen we use contains three parts, the touch panel, the TFT LCD screen and SD card. This touch screen has 40 pins. The TFT LCD use the control chip, SSD1289, while the touch panel uses the Analog Device ADS7843 touch screen controller. With our project, we can provide ECE4760 class the drive code for the LCD touch screen of such configuration.
Pin 4 to pin 17 ,pin 21 to pin 28 are for the TFT LCD screen display, pin 29 to pin 34 are for the touch panel, and pin 35 to pin 40 are for the sd card. Still there are some very important pins to be introduced. For the reason that microcontroller is always getting two hot if the VCC and GND of this touch screen is connected to the VCC and GND of the microcontroller, so we use additional voltage source for the touch screen. The VCC pin of the touch screen is connected to 5V. And the most important pin is the one for LCD back light. The LED-A pin for back light should be connected to 5V through a resistor of 75 ohm. This is a very important protection for the microcontroller and TFT LCD touch screen, or else both of them would be very hot.
SSD1289 TFT Driver is an all in one driver that integrated the RAM, power circuits, gate driver and source driver into single chip. It can drive a 262k color a-TFT panel with resolution of 240 RGB x 320. It also integrated the controller function and consists of up to 172,800 bytes (240 x 320 x 18 / 8) Graphic Display Data RAM (GDDRAM) such that it interfaced with common MCU through 8/9/16/18-bits 6800-series /8080-series compatible Parallel Interface or Serial Interface and stored the data in the GDDRAM. Auxiliary 18-bits video interface (VSYNC, HSYNC, DOTCLK, ENABLE) are integrated into SSD1289 for displays animated image.
The ADS7843 is a 12-bit sampling Analog-to-Digital Converter (ADC) with a synchronous serial interface and low on resistance switches for driving touch screens. By using this LCD, we can avoid refreshing the screen all the time compared with using a TV. The existence of GRAM in ADS7843 helps us to save the time in refreshing the screen. When we need to change what will be shown on screen, we just need to write the GRAM which replace the dots on the LCD.

Connections
The connections between the LCD touch screen and the Atmega1284p is like below:

Software Design and Implementation
Overview
The most important part for the software design is the touch screen control and the rule implementations.
LCD drawing
The LCD we use is a 3.2″ TFT screen. It has a resolution of 320 × 240, as the following figure shows. There are 240 pixels each line and 320 pixels each column. We can use (x, y) coordinate to represent each pixel on screen.
Key control signals for SSD1289 TFT Driver are listed: lcd_reset, lcd_cs(chip select), lcd_rs(0 for register address, 1 for value), lcd_wr(0 for write), lcd_rd(set 0 all the time in this project).

The set up for writing data to a specified register can be figured out from the image shown above.
We have three related functions to fulfill the communication between the Atmega1284P and the LCD.
void LCD_Write_Com( unsigned int reg )//specify the register you are going to write
void LCD_Write_Data( unsigned int value )//write the register you specified
void LCD_Write_Com_Data( unsigned int reg, unsigned int value )// write a specified register
Initialize of LCD screen
We use 16-bit data for the TFT LCD touch screen. The SSD1289 TFT Driver has more than 40 registers. Data are wrote to these registers to initialize the TFT LCD touch screen. Related code is in the “LCD_Init()” function.
Useful registers for drawing on the touch screen:
R4Eh and R4Fh make initial settings for the GDDRAM X address counter and GDDRAM Y address counter(AC). After GDDRAM data are written, the address counter is automatically updated according to the settings with AM. I/D bits and settings for a new GDDRAM address is not required in the address counter. Therefore, data are written consecutively without setting an address.
R44h specify the start/end positions of the window address in the horizontal direction by and address unit. High 8 bits are for the end position of the window, while low 8 bits are for the start position of the window. Then R45h/R46h are for the start/end positions of the window address in the vertical direction. R22h transforms all the GDDRAM data into 18-bit, and writes the data. Format for transforming data into 18-bit depends on the interface used. After writing to GDDRAM, address is automatically updated according to AM bit and ID bit. Access to GDDRAM during stand-by mode is not available. So from the registers above we can draw a pixel and draw a set of area. Write the addresses for start horizontal/vertical position to R4E/R4F, then write color to R22, and then by setting the CS to 0, the function of drawing a pixel is realized. Then by set the initial position to R4E and R4F, and then set the area to be drawn with R44h, R45h and R46h. After doing this, write the related color to R22h. For example, set the horizontal start position to 0, end position to 239; set the vertical start position to 0, end position to 319; set R4Eh and R4Fh to 0; then write a for loop to write the color to R22 for 240*320 times, then the whole screen will the paint the color you wrote. With the method above we construct some basic functions necessary for drawing chessboard and the menu.
void Paint(color) // Paint the total screen to one color
void draw_line(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned int color)
void draw_rectangle(unsigned int x1, unsigned int y1,unsigned int x2,unsigned int y2, unsigned color)
void video_chessmate(int x, int y, char c,unsigned color)//draw a single chessman
LCD touch detecting
The Touch Panel is of the same size as the LCD Screen and is transparent. We have to link the position of the place we touch with the display’s position.
The ADS7843 is a resistive touch screen controller that incorporates a 12-bit 125 kHz sampling SAR typeA/D converter. It can detect the pressed screen location by performing two A/D conversions.

The control signals for touch panel are DOUT, BUSY, DIN, CS and DCLK. Timing characteristics are as below:

We have two functions for communication with the touch panel: WriteSPI() for writing commands to the touch panel, write 0xD0 for the X-position and 0x90 for the Y-position, ReadSPI() to receive the X-position/Y-position according to the command we gave. (0xD0 and 0x90 mentioned above are for 12 bits data)
This X/Y position is a 12 bits value, and can be mapped exactly to the coordinate (0,239)/(0,319). For the reason that our screen is not big enough, we would have a lot of deviation if we map the coordinate to a smaller scale. So we still use the 12bit value for verifying the position.
The PENIRQ of touch panel is connected to PIND2 of the microcontroller, which is the pin for INT0 external interrupt. Usually the touch panel is not pressed, and the output of PENIRQ stays high. Whenever the touch panel is pressed, the PENIRQ would change to low. And this falling edge is caught by INT0, X/Y positions are read using WriteSPI and ReadSPI in INT0. But a problem came out, that is more than one falling edge will be generated when the touch panel is pressed. We solved this problem by using timer0.
Debounce strategy
Whenever the touchscreen is pressed, there will be a voltage drop, and causes an external interrupt. In the external interrupt ISR we record the coordinate of the point being touched. Based on observation of the number of coordinates being recorded for each touch on Putty, we made two integer buffer of size 40 to store the X-coordinate and Y-coordinate. Whenever a touch is made, it enters the timer0 external interrupt. In this ISR, a XY coordinate is recorded, and we store the coordinates in the buffer. Before leaving this external interrupt ISR, we set the debounce timer to 500, the timer0 compare match interrupt ISR will be entered every 1ms, and in this ISR the debounce timer will be decreased by 1, when the debounce timer becomes 0, this means there is no touch for 0.5s, all the coordinates for the same touch should be all stored in the buffer. So we calculate the average value, and clear buffer for next touch.
Chinese Chess Game Implementations
We construct a structure “chessman” to help us implement the Chinese chess game.
typedef struct {
int x,y; // store the current position of the chessman
char c; // the character of the chessman, such as „k‟ for king, „m‟ for minister
int pre_x, pre_y;// the previous position of the chessman, used for play back
int blong; // „0‟ for the red part, „1‟ for black part
int id;// id for each chessman from 1 to 32
char property; // there are 7 kinds of chessman, they have different rules when make a move
} chessman;
And there are three main functions helping us implement the game.
In the function move_chessman(int id,int to_x,int to_y), there is a switch case, according to the property of selected chessman, different rules are implemented, if the move is not legal, then the move can not be made, and sign will be set to 0, means no move. 1 means a move is made successfully. The function eat_chessman(int id,int to_x,int to_y,int eaten_id) is similar to function move_chessman().
In the function task(), identify a point on the chessboard according to the touched coordinates, if there is chessman on this point, and is of the same party as the before selected chessman, or there is no selected chessman, then set this chessman as the selected chessman, if there is a chessman, but it is of the other side, then call function eat_chessman, if the move is made successfully, then change the flag, means it is other ones turn to make a move, and clear the selected chessman. If there is no chessman on this point, and there is a selected chessman, then call function move_chessman(),if the move is made successfully, then change the flag, means it is other one‟s turn to make a move, and clear the selected chessman, means there is no selected chessman for now.
Mainpage
The mainpage of our program will draw the user interface. We start drawing the mainpage by drawing two rectangles with different colors. We also shows the string in the rectangles to tell the user how to start. One figure of what our mainpage looks like is shown below. When the user touch on either of the color bar, the LCD will jump to the corresponding page.

Play Page
In the play page, two players can play the chess. One side is red, and the other is black. The red one must start first, and the player click on the chessman he/she wants to move, and then click the point or the chessman he/she wants to move to or eat. The game is over until one side been checkmate.
Chinese Chess Game rules’ description: There are 7 kinds of chessman: King (‘k’), Minister(M),Bishop(‘B’), Knight(‘K’),Rook(‘R’), Gun(‘G’), Pawn(‘P’). The red side uses red chessmen with character of lowcase letter on it. The black side uses black chessmen with uppercase letter on it.
Generals are labeled with the ‘Q’ on the black side and ‘k’ on the red side.
The general starts the game at the midpoint of the back edge, and can only move within the palace. The general may move and capture one point orthogonally.
Ministers are labeled ‘M’ for Black and ‘m’ for Red.
The minister start on either side of the general. They move and capture one point diagonally and may not leave the palace, which confines them to five points on the board. The advisor is probably derived from the mantri in chaturanga, like the queen in Western chess.
Bishops also known as elephants, are labeled ‘B’ for Black and ‘b’ for Red. They are located next to the ministers. These pieces move and capture exactly two points diagonally and may not jump over intervening pieces; the move is described as being like the character 田 Tián (“field”). If a bishop cannot move due to a diagonally adjacent piece, it is known as “blocking the elephant’s eye”. Bishop may not cross the river, and serve as defensive pieces.
Knight also known as horses are labeled ‘K’ for Black and ‘k’ Red. Horses begin the game next to the elephants, on their outside flanks. A horse moves and captures one point orthogonally and then one point diagonally away from its former position, a move which is traditionally described as being like the character 日 Rì. The horse can be blocked by a piece located one point horizontally or vertically adjacent to it. Blocking a horse is called “hobbling the horse’s leg”.
Rooks are labeled ‘R’ for Black and ‘r’ for Red. The rook moves and captures any distance orthogonally, but may not jump over intervening pieces. The rooks begin the game on the points at the corners of the board.
Guns (aka cannons) are labeled ‘G’for Black and ‘r’ for Red. Each player has two cannons, which start on the row behind the soldiers, two points in front of the horses. Cannons move like chariots, any distance orthogonally without jumping, but can only capture by jumping a single piece, friend or foe, along the path of attack. The piece over which the cannon jumps is called the “cannon platform”. Any number of unoccupied spaces, including none, may exist between the cannon, screen, and the piece to be captured.
Each side has five pawns also known as soldiers, labeled ‘P’ for Black and ‘p’ for Red. Soldiers begin the game located on every other point one row back from the edge of the river. They move and capture by advancing one point. Once they have crossed the river, they may also move and capture one point horizontally. Soldiers cannot move backward, and therefore cannot retreat.
There is one way to go back to main page, manually tap the left bottom of the screen, where there shows”Tap_to_main_menu”

Settings Page
In the Settings mode, users can set the background color. The picture below shows what our setting page looks like. Users can click on the color bar, and the background color of the play page will be set to that corresponding color.
The figure below shows the setting page

Results
Accuracy
The accuracy of the touch screen is good. The identification of coordinates (x,y) of the touched point is precise. Because of the limitation of touch screen size, the chessboard is “mini”, so we have to have a precise identification of the coordinates to distinguish different points on the chessboard, so that the game can be played smoothly. We spent a lot time on identify each point’s coordinate on the chessboard, and we got a good accuracy. So though the chessman and chessboard is mini, player can still select the chessman and move it to one legal point on the chessboard exactly.
The only disappointment with the hardware is its instability. Sometimes the touch screen will restart itself, and we have no idea how this happens. But this phenomenon is not frequent. We are still trying to figure this out.
Usability
The touch screen is designed to be easily usable by people. Because our project is implemented on the touch screen, and we have soldered the microcontroller and the touch screen together neatly, so it is convenient to carry.
Conclusions
Achievements and improvements
During our one month’s project, we made some adjustments to our initial proposal. At first, we decided to display the “playpage” on the TV screen. The problem is that there is very limited time for us to implement related manipulation. So later we just fulfill all the functions just on a single touch screen. After this revision, we basically realize a Chinese chess game.
And then I want to emphasize the resistor connected between the backlight and 5V voltage source. Without this resistor, both the micorcontroller and the LCD touch screen would get very hot very soon, allso this would affect the touch panel greatly. Before we used a resistor we always had no falling edge when we pressed the touch panel, we found this by using oscilloscope and just couldn’t figure out the reason. Now I know that, with the high variance of temperature, the resistor is no longer working on its rated temperature.
We found the problem of “several falling edge for a single touch” using putty which we didn’t think of, and then use timer0 solved this problem.
Because of the limitation of time, this app is not very mature, we just give indication for “now it’s which player’s turn to make a movement” using putty. And I think if sounds are added to this app, this would be much better. For example, it will produce different sounds if the player makes a wrong movement, or a chess is eaten. And “Undo” function should be added, this would not be difficult for us because we already have a structure which includes the previous position of every chess. Another improvement should be speed up the execution time of our code for it took a long time to program it into the chip. And we used 16 bits data for communication between the touch screen and Atmega1284P which means that we have not much pins left for other functions. So we can change this 16 bits communication to 8 bits. Futhermore this touch screen is a good platform for playing, so we can explore more games to be played on this platform.
For we use a different touch screen from the other people used before, first we meet a lot of difficulties. But after successfully make the touch screen work, I found that their rational are basically the same. With the right initialization and figuring out the registers useful for drawing, you can easily make any touch screen of this kind work.
Standards
We have already mentioned the standards we used–SPI, and we conformed to these standards in our design.
Intellectual Property Considerations
In our code, two functions are borrowed from one LCD touchsreen project of last year[1]. They are the draw_line and show_color_bar functions.
Apart from the above, there is no other intellectual property issue.
Ethical Considerations
During our design process, our behaviors were consistent with the IEEE code of Ethics.
Our decisions are consistent with the health, and welfare of the public, and to disclose promptly factors that might endanger the public or the environment. We never think of or do anything that will be harmful to the environment and people.( IEEE code of Ethics, Code 1)
We are honest with our reference and code. We cite and mark all the part from other’s help or online sources. We do most of the work ourselves in labs and we are sure these work are not the same to other sources( IEEE code of Ethics, code 3)
Our design aims at providing children the platform to have a understanding of electrical devices and the basic concepts of music. We also ( IEEE code of Ethics, code 5)
Out design is safe enough to avoid doing harm to any other people, their property, reputation, or employment. ( IEEE code of Ethics, code 9)
We do not receive bribery in all its forms( IEEE code of Ethics, code 4)
In doing this project, we treat fairly all persons regardless of such factors as race, religion, gender, disability, age, or national origin. ( IEEE code of Ethics, code 8)
Read more: A Touchscreen Chinese Chess App