Chinese Chess App – ECE 4760

From the high-level discussed above, our hardware consists of two parts, which include the LCD touch screen and the Mega1284 prototype board. Below we will discuss each part separately.

The TFT 3.2′ LCD touch screen we use contains there 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 ads1289 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.

The connections between the LCD touch screen and the maga1284p 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 2.8″ 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.

Control signals for SSD1289 TFT Driver: lcd_reset, lcd_cs(chip select), lcd_rs(0 for register address, 1 for value), lcd_wr(0 for write, 1 for read), lcd_rd(set 0 all the time in this project).

Timing characteristic of 16-bit interface

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) // Pant 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_circle(unsigned int x, unsigned int y, unsigned int r,unsigned int color)

void draw_rectangle(unsigned int x1, unsigned int y1,unsigned int x2,unsigned int y2, unsigned color)

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 for that place.

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.

 

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”

Playpage

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

Settings 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.

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 have a structure which includes the previous position of every chess. Another improvement should be speed up the execution time of our code it take a long time to program it into the chip. And we use 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. And 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)

Legal Considerations

Our design is carried out under legal power supply. What we have done is based on ensuring safety, stability and other’s ware fare.

Appendices

A. Code Listing

  • main.c
  • character.h

 

B. Parts List and Costs

Part
Unit Cost
Vendor
Quantity
Total Cost

Atmega 1284p
$5
Lab Stock
1
$5

small solder board
$1
Lab Stock
2
$2

Power Supply
$5
Lab Stock
1
$5

Resistors
$0
Lab stock
6
$0

Wire
$0
Lab Stock
30
$0

LCD touch screen
$15
Amazon
1
$15

Total Cost

$27

C. Distribution of Work

Zijing Yao

  • High-level Design and Hardware Design
  • Touch Screen initialization
  • Prototype board soldering
  • Report and Webpage

Qian Chen

  • High-level Design and Software Design
  • Touch Screen Position Adjustment
  • Chinese chess rules realization
  • Report and Webpage

References

Below are links to external documents, code, and websites referenced and used throughout the duration of this project.

Datasheets

  • Atmega1284P
  • SSD1289
  • ADS7843

References

  • draw_line function
  • Virtuoso: A Touchscreen Music App
  • character.h

Acknowledgements

We’d like to thank Professor Bruce Land for helping us to solve the problems we met during our project. We’d also like to thank all the TAs for their help.