Ece3140 lab5 writeup


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ece3140 lab5 writeup

  1. 1. Charles Moyes (cwm55) and Shentong Wang (sw477) Lab 5 Write-UpNOTE: A YouTube video demo of our project can be viewed at 5 Write-Up: MSP430 Microcontroller Craps GameProject OverviewThe idea was to implement a playable game of Craps (casino dice rolling game) using the MSP430 mi-crocontroller board. External LEDs that were arranged in dice shaped format are used to show thevalues of the dice rolls. A basic (yet detailed) description of the game rules can be found at A more thorough explanation can be found in the How To Play Craps section. Current limitingresistors are connected across the LEDs and the digital I/O pins in series to ground. The push button isused to trigger dice rolls. The Charlie-plexing LED multiplexing scheme is used to connect lots of LED’s tothe micro-controller without running out of pins. Red and green LED’s on the breadboard indicate whetherthe “Pass” (green LED lit) or the “Don’t Pass” (red LED lit) betters win the round. The built-in greenand red LEDs on the MSP430 board indicate whether the round is in the “come out” or “point” stage. TheCraps embedded system acts as dice for each player in the game, and as such, is passed around the table toeach player who wishes to be a “shooter.”How To Play CrapsCraps is an easy-to-learn betting game played with two dice. The players bet “pass” or “don’t pass” (whetherthe dice roller will win or lose). The player who rolls the dice is designated the “shooter.” The shooter beginsrolling. If the first roll is a 2, 3 or 12, the shooter loses (“craps”) and the “don’t pass” betters win the round.On the other hand, if the first roll is a 7 or 11 (a “natural”) then the shooter wins and the “pass” betterswin the round. If the first roll is any other number (the “point”), then the shooter continually rolls the diceuntil that number is rolled again (win!) or the shooter rolls a seven (lose!). The first roll is the “come out”roll, while all others afterwards are “point” rolls. Playing the game using the electronic device we designed is very easy. Simply press the push button onthe microcontroller and the dice roll will illuminate on the LEDs. If the dice roll wins, the green status LEDlights up and a happy song is played on the piezoelectric speaker. If the dice roll loses, then the red statusLED lights up and a sad song is played. If the round continues with another dice roll, then a chirp sound isemitted, and the game waits for the player to press the push button again.Game Logic Finite State Machine (FSM)The game logic was easily represented by a finite state machine with several states, transitions, and variablesas shown in the following diagram: 1
  2. 2. Charles Moyes (cwm55) and Shentong Wang (sw477) Lab 5 Write-Up The game state variable stores 0 if the round is in the “come out” phase and 1 if the round is in the“point” phase. The state led variable stores a 0 if nobody has won or lost, 1 if the shooter has won (pass),and 2 if the shooter has lost (don’t pass). Lastly, the point variable stores the value of the point dice rollthat must be rolled again before a seven in order to win the round. Initially, state led = game state =point = 0, and if an unknown state configuration comes up during gameplay, then the FSM resets to thissafe initial state. The “come out” states (for game state == 0) are a first-roll lose state, a first-roll win state, and a pointstate that sets game state to 1 and advances onto the “point” round. The “point states” are a win state(for rolling the point again), a lose state (for rolling a seven), and an anything else state that allows theround to continue in the “point” game state. Transitions between these states are governed by the value ofthe dice roll, the point, and the game state.Pseudo-Random Number GeneratorA pseudo-random number generator will be used to determine the values of the dice rolls in software. TheLinear Congruential Generator algorithm based on modular arithmetic was selected due to its “randomness”and its ease of implementation: Xn+1 ≡ (aXn + c)(mod m)where Xn is the sequence of values generated and m, a, c, and X0 (the so-called seed) are constants. Since16-bit numbers are used, we chose values of m = 216 , a = 16, 807, and c = 33, where c and m are necessarilyrelatively prime. Because the microcontroller board lacks a built-in real-time clock, an un-initialized garbagevalue in memory is used instead to seed the random number generator (X0 ). Hardcoding a random numbergenerator seed into the code would make playing the game much less interesting since upon rebooting, allgames would have the same sequence of dice rolls and thus would not be random.Project ConstructionProject construction was accomplished using a breadboard and external electronic components that interfacewith the digital I/O pins on the microcontroller breakout board. Bruce Land assisted us in soldering low-profile headers onto the breakout board pins. From there, we wired a piezoelectric speaker across on ofthe pins to the ground pin (pin 12). From reading its data sheet, we found that no limiting resistor wasnecessary for the piezoelectric speaker. The LED’s were then wired to the board using a Charlie-plexingLED multiplexing scheme described in the next section. Limiting resistors were calculated using Ohm’s Law(180-220 Ohm resistors were used) for the specifications given in the LED data sheets. The overall allocationand usage of I/O pins is shown in the following table: Break-out Board Pin Microcontroller Pin Usage 5 P2.2 Pin 1 of Red Dice LED Charlie-plex Configuration 6 P2.3 Pin 3 of Red Dice LED Charlie-plex Configuration 7 P2.4 Pin 2 of Red Dice LED Charlie-plex Configuration 8 P4.3 Pin 1 of Yellow Dice LED Charlie-plex Configuration 9 P4.4 Pin 2 of Yellow Dice LED Charlie-plex Configuration 10 P4.5 Pin 3 of Yellow Dice LED Charlie-plex Configuration 11 P4.6 Middle LED of Red Dice Display 12 GND Ground for Piezoelectric Speaker 13 P2.6 Piezoelectric Speaker (+) Terminal 14 P2.7 Middle LED of Green Dice Display 15 Unused – 16 Unused – 17 P3.0 Green “Win/Pass” Status LED 18 P3.1 Red “Lose/Don’t Pass” Status LED Note that break-out board pins 1-4 were inaccessible because the plastic enclosure of the USB programmerdongle covered them up. 2
  3. 3. Charles Moyes (cwm55) and Shentong Wang (sw477) Lab 5 Write-UpCharlie-plexing LEDsThis project uses 16 external LEDs, but there are a limited number of I/O pins available on the microcon-troller breakout board. This technique allows n pins to drive up to 2 · n = n(n − 1) LED’s. Moreover, only 2n limiting resistors are needed. Each unique pair of I/O pins in the configuration supports two LED’s (one inthe forward direction and one in the reverse direction) in series. This pair is referred to as a “complementarypair” and either LED in the pair can be lit by setting one I/O pin to Ground and the other to +5V. Bydisconnecting all other I/O pins in the configuration, any one LED in any of the complementary pairs canbe selectively lit. Disconnecting an I/O pin can be accomplished by setting that pin to be an input pin usingthe appropriate DIR register. This setting puts the pin in a high-impedance state. A schematic illustratingthe general idea follows: In our project, two Charlie-Plexed configurations of 6 LED’s (using three I/O pins each) are used forthe two dice displays. Moreover, the seventh center LED is connected to a separate I/O pin across groundwithout any multiplexing scheme. Each I/O pin has a limiting resistor connected in series to it. Thecomplementary pairs are connected in parallel on the breadboard. In order to light multiple LED’s in onedice display at a time, cycling is used where each simultaneously-lit LED is toggled on and off one-by-one ata high refresh rate. If the refresh rate is high enough, this technique gives the illusion that multiple LED’sare being lit at the same time.Piezoelectric SpeakerA piezoelectric speaker is used to play audio feedback during the game. Musical note frequencies are deter-mined from the equal-tempered scale (tabulated at look-up table of compiler-time definitions stores these frequencies. Pulse width modulation (PWM) isused to send a 5V amplitude square wave to the piezoelectric speaker from a digital I/O pin. By varyingthe frequency of the square wave signal, different pitches can be played from the speaker. Songs are simplysequences of notes (of varying time durations) with periods of silence in between. Sequential code can bewritten to play notes at specific frequencies for certain durations and to add silences in between notes. Asan example, the code to play the “game win” song follows:// P l a y s a happy song t h a t t e l l s t h e p l a y e r t h a t t h e y wonvoid playWin ( ){ beep ( c , 3 5 0 ) ; beep ( e , 3 5 0 ) ; beep ( g , 3 5 0 ) ; beep ( cH , 5 0 0 ) ;}Code OverviewA brief overview of the code written in the C programming language for this project follows. Note that thecode is heavily documented using lots of comments. The main.c file contains an intro() function that doesa light-up demo where the “Imperial March” song is played from Star Wars on the piezoeletric speaker. Themain() function sets up the I/O pins, interrupts, and enters the game loop. The Port 1 interrupt handlercatches the push button interrupts. 3
  4. 4. Charles Moyes (cwm55) and Shentong Wang (sw477) Lab 5 Write-Up The speaker.c file contains piezoelectric speaker code. The beep function plays a tone at a specifiedfrequency on the speaker using digital PWM. The playGoAgain(), playLose(), playWin(), and play()functions all play various songs on the speaker using beep and appropriately spaced delays. Note thatsome of the code here (particularly beep and the Imperial March song code) was borrowed from The util.c file contains code to delay the MSP430 using delay cycles and random(), a LinearCongruential random number generator described in the earlier section. The leds.c file contains code to light specific LEDs in the red and yellow Charlieplex configurations(lightLED() and lightLED2() functions respectively). The lightDiceNum() function is used to show aparticular dice face pattern (for a die value of 1-6) on one of the dice displays by cycling through the LEDsin the configuration at a high refresh rate. The lightStatusLEDs() function controls the red and greenstatus LED’s on the board and on the microcontroller (for communicating game state information to theplayer). Lastly, the displayDice() is a utility function lights up the two dice displays with a given dice rollby calling lightDiceNum() with the appropriate parameters. The game.c file contains all of the game logic. The updateGameState() function implements the finitestate machine (FSM) described in an earlier section. This function encodes all of the game rules for Crapsand is heavily commented. The gameLoop() function updates the dice LED displays by cycling through themonce and then updates the status LED displays. It is continually invoked throughout program execution.Lastly, the buttonPressHandler() push button handler invokes the game state FSM to update the gamestate during a new dice roll. For more information about the code, feel free to look through it and read ourcomments.Conclusion/Photo/Demo VideoIn conclusion, a fun and playable game was implemented using a microcontroller. The project was a rich andworthwhile hands-on learning experience. We learned about Charlie-plexing, practiced soldering, assembleda circuit using a breadboard, debugged a circuit using a multimeter, calculated appropriate resistances forcomponents such as LEDs, and got to plan, design, and create a tangible electronic gadget. Many hours ofwork in the lab went into implementing and debugging this project, but the work was very fulfilling and theend result was great! 4