Professional Documents
Culture Documents
Design Document
James Beukers
Jordan Jacobson
ECE 3710; Project 2
May 3, 2013
15 Puzzle
Table of Contents
List of Figures 2
List of Tables 2
1. Introduction 3
2. Scope 3
3. Design Overview 3
3.1 Requirements 3
3.2 Dependencies 3
3.3 Theory of Operation 4
3.4 Design Alternatives 5
4. Design Details 6
4.1 Hardware Design 6
4.2 Software Design 7
4.2.1 Register Configuration 10
5. Testing 11
5.1 Test 1-Hardware 11
5.2 Test 2-Menu 11
5.3 Test 3-Scramble 12
5.4 Test 4-User touches and game actions 12
5.5 Test 5-Save high scores 12
5.6 Test 6-Solve-it function 12
6. Conclusion 12
Appendix A-Hardware schematic 13
Appendix B-Software 14
List of Figures
Figure 1: The main menu 4
Figure 2a: Completed puzzle 4
Figure 2b: Scrambled puzzle 4
Figure 3: Winning state 4
Figure 4: High score menu 5
Figure 5: Second level 5
Figure 6: Hardware Setup 6
Figure 7: Software Flow Chart 7
List of Tables
Table 1: LCD Register Configuration 10
Table 2: Touch Panel Register Configuration 10
Table 3: Timers and DAC Register Configuration 10
Table 4: Real Time Clock Register Configuration 11
2
15 Puzzle
1. Introduction
The 15 Puzzle Game was invented by Noyes Palmer Chapman around 1874. The classic version of the
game consists of a square board that contains 15 numbered squares. Because there are only 15 num-
bered squares, there is a blank square on the board. The game is played by moving the numbered
squares on the board until the numbers are in order from 1 to 15, starting with the 1 square in the top
left corner. These puzzles are also very often made with a picture broken into 16 squares, with the
bottom right square being removed. Now-a-days the 15 puzzle can be played on a computer or a hand
held electronic device. The game is greatly enhanced on an electronic device because the device has the
ability to keep track of a high score, output sound, and introduce multiple images to have as the basis of
the puzzle. In this project a 15 Puzzle game is implemented using the STM32F103 ARM Microcontrol-
ler and a 3.2" TFT LCD Display Module.
2. Scope
In this document an overview is given of the 15 Puzzle game design which is based upon requirements
outlined in the project proposal. Included in this document are the explanations of the methods and
techniques in which the project was designed, implemented and tested in order to meet the require-
ments that are outlined in the project proposal. Also included are digital images to help the reader more
fully understand the description of the design. There are two appendices, one of which contains a
hardware schematic, and the other which contains the full code of the program excluding the very large
and bulky hexadecimal arrays that store the data for the images that are used. Because the hexadecimal
arrays that store the images are so large only the declarations of the arrays have been included without
all of the hexadecimal values.
3. Design Overview
3.1. Requirements.
1. It shall display a picture on the LCD screen.
2. The game shall have a menu in which the user can make a selection (play puzzle, check high
score)
3. It shall scramble the picture into different blocks.
4. It shall recognize user touches on the touch screen.
5. The game shall make corresponding action based on user touch location. If user touches a
button the program shall access the button, if the user touches a block, it shall move the block
accordingly.
6. It shall output sound.
7. The fastest time and smallest number of moves shall be saved to nonvolatile memory.
8. It shall also have a solve-it function that shows the steps to solve the puzzle (optional)
3.2. Dependencies.
5 volt power source
8 MHz Oscillator
3.2" TFT LCD Display Module /Touchscreen
Analog test board
STM32F103 ARM Microcontroller
3
15 Puzzle
a) b)
Figure 2: The completed puzzle is shown for a moment (a)
and then is scrambled and a square is deleted (b).
The user is then required to press the screen over a block ad-
jacent to the empty square. Nothing occurs if the user presses
on a block not adjacent to the empty square.
After the user makes a move the timer is started and the num-
ber of moves is displayed and incremented after each move.
Included in the game play screen are two buttons displayed at
the bottom of the screen.
One is the ‘menu’ button,
to return to the main menu
Figure 1: The main menu and the other is a ‘solve’
button (this is also seen in
Figure 2b).
If the high score menu is chosen the high scores are dis-
played as shown in Figure 4 as well as a menu button to re-
turn to the main menu.
3.4. Design Alternatives
As more puzzles and sounds were created it was found that the
microcontroller did not have sufficient memory to fit the code.
Options including using the SD card to provide more memory
were looked into; however, there were difficulties with the
SDIO module sharing pins with other modules on the board and
time did not permit for these issues to be solved.
Another alternative method that was considered was to have the pic-
tures used for the puzzle formatted differently. Lower quality pictures
were implemented but the results were very undesirable, causing dis-
coloration and difficulty in distinguishing each square.
A method using the same pixel table to draw the blocks and then the
numbers on top of the blocks separately was also considered. This
method would save memory because only one pixel table instead of
fifteen would be needed; however, this required lots of new code and
would reduce the quality of the graphics.
Figure 5: Shows another
These methods were not chosen due to the poor graphic quality and level to the puzzle game
the amount of time it would take to alter the code to allow these new adding more difficulty as well
methods. as variety and color.
A method to save the high score in nonvolatile memory in the LCD as a pixel was considered, however,
the better option of saving to Flash memory was chosen. Flash memory was chosen because saving the
high score to a pixel would create an odd pixel on the screen and would require code changes to ensure
that it never got erased.
Instead of using the ASCII text functions provided in lab 6 for the menu screen and high score screen, it
was considered to create buttons similar to those used in the current game play screen (see Figure 2b).
This would create a much better looking interface as well as provide for a greater uniformity between
the different game screens.
5
15 Puzzle
4. Design Details
4.1 Hardware Design
15 Puzzle is implemented on an STM32F103 ARM Cortex-M3 Microcontroller board. The game
hardware consists of a 3.2" TFT LCD Display Module/Touchscreen connected to the LCD Port on
the board as seen in Appendix A and an analog test board connected to the 5V, GND, and Port A5
pins of the board as also seen in Appendix A. The game is also dependent upon the external 8MHz
oscillator and a reset button included with the board, as well as an external 5V USB power supply
(see Appendix A).
The game is displayed through the LCD module and receives user input through the touchscreen.
The touchscreen is connected to the microcontroller through SPI and shares Port A5 with the DAC
output for the speaker. The speaker on the analog test board is used to output the game sound. Be-
cause Port A5 is shared by both SPI and the DAC it is necessary to disable the DAC when using SPI
and visa-versa so that each functions properly when needed. Remapping the pins or using another
SPI module is not an option as the remapped pins and other modules share pins with the LCD mod-
ule on Port B.
6
15 Puzzle
Menu Screen
Start Time
Count Moves
Output Time
Shuffle Button
Output Sound
Figure 7: Flow Chart of Software Design
Save High Score
7
15 Puzzle
Code Outline
The code for the project consists of three C files: main.c, lcd.c and touchscreen.c. The main.c
file is very short and just contains configuration function calls, and a switch statement to de-
termine which screen is displayed. Inside the cases of the switch statement there are function
calls to display the screen for the appropriate state. Case 0 of the switch statement is the
Menu Screen, Case 1 is the Game Play Screen, and Case 2 is the High Score Screen.
The C file lcd.c contains all of the functions to configure and use the LCD display. See Table
1 on page 10 for details on the configuration. The majority of this code is the same as the
code from lab 4, with some necessary changes in order for it to be compatible with this pro-
ject.
The C file touchscreen.c contains the bulk of the code for the project. The desire was to have
more C files that contained just a few related functions. However, due to issues with global
variables accessed by many different functions, a large majority of the code was placed in the
touchscreen.c file. This file also contains the configurations of the touch screen, timers, DAC
and RTC. See Tables 2-4 on pages 10-11 for details on these configurations.
For more details on the code for the project see Appendix B which contains the full code for
the project. The code has very detailed comments that should be easy to read and understand.
Once the puzzle has been scrambled the user has three options: play the game, press
the "Solve" button to have the solver algorithm solve the puzzle, or press the "Menu"
button to return to the main menu screen. The program is constantly monitoring the
touch panel and locating any touches on the screen so that the correct action may be
performed. As soon as the user starts playing the game, a timer implemented by the
Real Time Clock is started to keep track of the time it takes the user to solve the puz-
zle. The time isn't displayed until the winning screen. Also the number of moves the
user makes are kept track of and displayed on the screen as the user plays.
8
15 Puzzle
The solver function isn't perfect. It solves the puzzle some of the time, but there are
some bugs in the algorithm so it gets stuck sometimes.
Winning State
Once the user or the solver has completed the puzzle the following things always
happen in the winning state:
The missing piece of the puzzle is added so the full picture is displayed
A message saying "Congratulations!! YOU WON!!" is displayed on the
screen
The time it took to complete the puzzle is output to the screen
A winning sound is output through the speaker
If the score is a high score it is saved to flash memory
In the winning state the "Solve" button changes to a "Shuffle" button so the user has
the option to push the "Shuffle" button to re-shuffle the puzzle and start again, or to
push the "Menu" button to return to the Menu Screen.
9
15 Puzzle
10
15 Puzzle
5. Testing
Multiple tests were run to verify each requirement. These consisted of hardware and program tests to
verify proper game play.
11
15 Puzzle
6. Conclusion
The design described in this document is a version of 15 Puzzle implemented on a STM32F103 ARM
Microcontroller board with LCD and analog test modules. Six tests were run to verify all eight re-
quirements (see section 3.1 for requirements, sections 5 for tests) and all tests passed. The design func-
tion as all requirements specify, however it is not optimal and other design alternatives are available
(see section 3.4). With more time, implementations using the SD card to provide more memory can be
made to allow multiple levels at the same time and provide memory space to store higher quality
graphics and sound. There are also some errors in the solve function as it doesn’t solve every case,
however, with more time and knowledge in algorithms and programming these can be fixed.
12
15 Puzzle
Appendix A
Hardware Schematic
13
15 Puzzle
Appendix B
Main.c
14
15 Puzzle
lcd.c
15
15 Puzzle
16
15 Puzzle
17
15 Puzzle
18
15 Puzzle
19
15 Puzzle
Lcd.h
20
15 Puzzle
Touchscreen.c
21
15 Puzzle
22
15 Puzzle
23
15 Puzzle
24
15 Puzzle
25
15 Puzzle
26
15 Puzzle
27
15 Puzzle
28
15 Puzzle
29
15 Puzzle
30
15 Puzzle
31
15 Puzzle
32
15 Puzzle
33
15 Puzzle
34
15 Puzzle
35
15 Puzzle
36
15 Puzzle
37
15 Puzzle
Touchscreen.h
38
15 Puzzle
Tables.h
39
15 Puzzle
40