Home   |  Schematics |  Products |  Tutorials  |  Datasheets  |  Robotics   |   Download    |   Link Exchange


Direct Current
Alternating Current
Digital Electronics
PC Architecture
Electronics Dictionary
Resources

Experiment
Calculator/Converters
Radio
Newsletter
Associations and Societies
Component Manufacturers

 

Electronics Symentics


Microcontroller Advanced Kit - Using 8051 RAM (The Game of Simon)

All 8051s have Random Access Memory (RAM) which is used to hold data while the program is running. Almost every program you write uses some type of memory. The 2051 has 128 bytes of RAM. Each byte is given an address starting at 0 and going to 127. In the previous examples we have used Registers R0 - R7. Each register is a byte of memory located in the RAM. These 8 registers are easier to access and use than the majority of the RAM space. R0 - R7 use the memory locations (addresses) 0 - 7. In this example we will use locations 32 to 127 to store the sequence of colors that must be repeated in the game. This means the sequence could be as long as 96 before we run out of memory. That should be plenty. I usually blow it when the sequence gets to about 15.

The Game

The object of the game is to repeat a sequence of colors that the game gives you. If you get it right, one random color is added to the sequence and you have to repeat it. If you get it wrong you have to start over with a new sequence.

One important part of the program is generating a random color to be the next color in the sequence. This is done by having a counter (we will use register R3) that is always counting and then when a button is pushed the next color in the sequence is determined by the number in R3.

The program starts by waiting for the player to hit the button for the red LED. Then the first color in the sequence (as determined by the value in R3) is stored in memory location 32 (20 Hex) and the LED for that color is flashed. Then the program calls the USERS_TURN routine and compares the user input to the value in memory to see if the user pressed the right button. R3 keeps counting while the program waits for the user to press a button. If the user presses the right button then the program uses the value in R3 to get the next color in the sequence and stores that in memory and shows the user the current sequence of colors. This continues until the user screws up and the program starts back over at one color in the sequence.

Memory Access

RAM can be accessed using either direct or indirect addressing. In the previous tutorials we have used direct addressing with the R0 - R7 registers. For example, "MOV A, R0" reads the value from the R0 memory location and stores it in the A location. That is easy to see. Indirect addressing is a little more complicated. With it you use either R0 or R1 to hold the address that you want to access. Then you use the command "MOV A, @R0". This tells the processor that you want to get the data from the memory location specified by R0. Got that? It sounds complicated but once you master it, it is really useful.

For example, suppose you want to read from memory location 50H using indirect addressing. You can use:

MOV R0, #50H                (moves the number 50H into R0)
MOV A, @R0                  (reads the value from memory address 50H and stores it in A)

It seems silly for reading one byte from memory but suppose you want to read all the data in memory. You can do it with direct addressing but you need 128 seperate MOV commands  (one for each memory location). (Like "MOV A, 30H"). With indirect addressing you can read all of memory with a simple loop.

MOV R0, #00H
LOOP:
   MOV A, @R0
   INC R0
   AJMP LOOP

Each time through the loop the value in R0 is incremented by 1 and the MOV instruction transfers the value from the next memory location into A.

The Hardware

At this point you should be familiar with LEDs and switches. The hardware for this project is simple. Connect a blue LED from 5 volts to P1.2 (pin 14) on the 2051. 
Connect a red LED from 5 volts to P1.3 (pin 15) on the 2051.
Connect a green LED from 5 volts to P1.4 (pin 16) on the 2051.
Connect a yellow LED from 5 volts to P1.5 (pin 17) on the 2051.
Connect a button for the blue LED from ground to P3.2 (pin 6) on the 2051.
Connect a button for the red LED from ground to P3.3 (pin 7) on the 2051.
Connect a button for the green LED from ground to P3.4 (pin 8) on the 2051.
Connect a button for the yellow LED from ground to P3.5 (pin 9) on the 2051.





Home  Products  Tutorials   Schematics   Robotics   Resources   Radio Stuff    Career    Download   Link Exchange

HTML Sitemap   XML Sitemap


Terms & Conditions  Privacy Policy and Disclaimer