For our final project, we have decided to build a minesweeper game with a microcontroller and a PS/2 mouse to be displayed on the TV. Minesweeper is a famous game that comes with the MS-Windows operating system, and the objective of the game is to determine the location of mines on the board.
Minesweeper is originally a PC game that comes along with the Windows bundle. For our design, there are 15 x 10 cells on the game board. At first, all 150 cells are covered, and the player can left-click the PS/2 mouse on a destined cell to uncover it, or right-click to mark the cell with a flag a reminder to the player that the cell is possibly taken by a mine.
The objective of the game is to locate all the mines as quickly as possible without uncovering any of them. The default number of mines in our game is 25. If you uncover any of the mines, you lose the game. If a number appears on a square that you have uncovered, it indicates how many mines are in the eight squares that surround it.
When you have successfully uncovered all cells without mines, you will then have won the game, and a You Won message would be shown on the TV screen. Else, if any cell with a mine is uncovered, the game is over and a GAME OVER message would be shown. In both cases, right click the mouse to restart the game.
There is also a timer in the lower right hand corner of the screen, showing how much time you are taking to play the game. Once you win or lose the game, the timer would stop.
Another feature is the remaining mine count. When you have marked a cell with a flag, the count shown in the lower left hand corner of the screen would decrease.
Why do we pick this project?
There are several reasons why we wanted to implement this Minesweeper game with TV display and PS/2 mouse control. First of all, the complexity of hardware in this project is of appropriate level but the cost required is cheap. All we needed to buy is the PS/2 receptacle for the mouse, which is just $0.99. The mouse that we use in this project is a used mouse and it is free. Moreover, the parts that we require can be easily replaced. Soldering would be needed to link the signaling wires to the mouse through the receptacle, but this can be done easily.
Secondly, we are already pretty familiar with the interfacing between the microcontroller and the TV after doing labs 3 & 4, so we can divert our attention to the mouse/microcontroller interfacing and game programming.
Thirdly, we feel that writing this game would be fun and yet challenging enough. Writing the game itself needs systematic and logical thinking: we must make sure that the big parts work so that the game can be played, but we must also pay attention to details, so that the game can run smoothly. Besides the game programming, the mouse interfacing would be another challenge to us, but we proceed with this idea, since the mouse is nevertheless the best controller to be used to play a game like Minesweeper and we had some good reference to back us up. A good challenge is what that makes this project meaningful.
What we did?
We used two Atmel MEGA 32 microcontrollers for this project: One is used to produce signals to the TV display, while the other microcontroller is used to get data from the mouse. The necessary game computation is also done in the second microcontroller and deliver to the first microcontroller through UART connection. Details are shown in the subsequent sections.
High Level Design
As mentioned in the introduction that we got the idea of this minesweeper project from the popular Minesweeper games that come with the Windows operating system. Our original plan for the system is a simple scheme as shown in the figure below.
The microcontroller will be running the video generation code that we used in the lab . There are 60 lines of free time in each frame, and we will fit the mouse interfacing code and games code in these lines.
We thought this scheme is feasible because some of the more complicated computation is required only in the initialization stage. We should be able to optimize the remaining codes to be fast enough for the video generation code. However, we found out later that it is not feasible to interface with the mouse from the video generation code that we used in the lab .
The mouses clock speed is too slow to be compatible with the video generation code . Although, it appears as there are 60 lines of free time in each frame in the video generation code, the sync pulse generation interrupt event still happen at the end of each lines. The code in this 60 lines interval will be interrupted for 60 times. This is unacceptable for the mouse because each byte of data from the mouse cannot be completely transmitted in one line. This turn out that there is no way to read a complete byte of data from the mouse without interruption.
Reason for using two microcontrollers
The mouses clock speed is in the range of 10-16.7KHz .
� Time to transmit one bit is about 1/16.7Khz 60ms.
� There are a total of 11 bits to transmit one byte of data , and this will require 60ms 11=660ms.
� Clock is generated by the mouse, and the microcontroller has to read a complete 11 bits of data whenever the mouse send it. There is free time while waiting for each bit, but the microcontroller must read each bit at the fixed interval according to the clock. The best way to make sure that the microcontroller read each bit synchronized with the clock, is to use an interrupt to detect the edge of the clock.
� The interrupt used to generate the sync pulse in the TV code triggered at a period of about 63us. If we use another interrupt that is triggered by the clock’s edge (rising or falling edge depends on read or send data to mouse), the interrupt will be triggered at a period of about 60ms. Both interrupt cannot be disabled at any time, and it is impossible to coordinate these 2 interrupts from interfering each other.
� Even if we read the mouse data during the 60 lines of free time, the sync pulse generation interrupt will still continue to trigger at the fixed interval and it will interrupt the transmission from the mouse.
� Therefore, the TV generation code has no free time of 660ms for reading a byte from the mouse without any interruption.
Using two microcontrollers
We decided to use two microcontrollers for the project. One microcontroller handles the TV screen drawing and the other controller handle the mouse interfacing and games computation.
Microcontroller 1 (mc1): Generate video signal and display it on TV.
Microcontroller 2 (mc2): Read mouses data and handle the games computation.
mc1 and mc2 are communicating to each other through UART. mc1 send information about location of screen that need to be updated and the image to be displayed at that location. The schematic diagram of the system is shown below.
Microcontroller 1 (mc1): TV Controller
The code that we used in this microcontroller is started from the video generation code that we used in the lab . Analog voltage signal is generated at the 2 bit DAC (Digital to Analog Converter) to draw image on the TV screen. We added routines to draw the minesweeper board, update cells on the board, and communicate with mc2. The TV generation code is so busy that there is little time left for doing anything complex. This also posed a constraint on how fast it can read data from mc2.
Microcontroller 2 (mc2): Games and Mouse Controller
The code in this microcontroller takes care of the low level interfacing with the mouse as well as the games’ computation. It sends instructions to mc1 through the serial communication port (UART). Cursor’s movement emulation, update on the board image, and games status (start/stop/win) are computed in this microcontroller and sent to mc1 to instruct mc1 on what is needed to be drawn on the TV screen.
Hardware and Software Tradeoff
The use of two microcontrollers actually simplified the programming task. We do not need to worry too much about speed and memory usage when writing the programs. The cost of using two microcontrollers is not a big disadvantage because microcontroller is relatively cheap.
|Parts’ name and quantity||Source and price.|
|One PS/2 mouse||Used mouse got from the ECE workshop. (free)|
|One PS/2 port connector||$0.99|
|One TV and cable.||Provided in the lab|
|Two Atmel Mega32 microcontrollers||Provided in the lab|
|One STK-500 development board||Provided in the lab|
|Some resistors, capacitors, LED, switches and wires.||Provided in the lab|
|Breadboard||Provided in the lab|
Total cost is $0.99
For more detail: Cornell University ECE476 Final Project – TV Minesweeper