Categories
8051-8052 Assembly Language

Digital Dice using 8051 microcontroller Assembly Language

Let’s create a digital dice using 8051 microcontrollers in assembly language. This is very basic problem and will help you to learn very core concepts of 8051 microcontroller assembly language programming. By creating a digital dice using 8051 microcontroller you will get hands on experience on how to make use of most widely used instructions of 8051 assembly language. We will create this digital dice using 8051 microcontrollers with the same concept which is implemented in the 1 digit 7 segment based up counter in 8051 microcontrollers.

Dice Pattern LEDs Interfacing and Global Variables

To create a digital Dice we need to put the 5 LEDs to the 8051 microcontrollers in a dice manner. We will display the different dice patterns onto the LEDs later. So We are going to use the P1 of the 8051 microcontroller. We will use the pins of P1 and use the LEDs attached to the P1 to display the dice 🎲 pattern. In the code we will make a reference to the global variables like this

SEG_DISP EQU P1
DIGIT1 EQU 30H
SW1 BIT P3.0

TEMP_CNT EQU  32H

Code language: ARM Assembly (armasm)

The other global variables which we are using is one Switch called SW1 in above code interfaced at the pin P3.0 of the 8051 microcontroller. One DIGIT1 variable is used to store the random pattern which we will generate by incremental counter. One TEMP_CNT which is the label to the 32H memory location in the RAM in case if we need it later.

Saving Bytes in Code Memory in 8051 Assembly

We are creating dice patterns and saving it in code memory of the 8051 microcontrollers by using the DB instruction available in 8051 assembly language. We can use this instruction to let compiler know that the value would be stored as it is in the code memory. This is a similar approach made in 1 digit 7 segment up counter with 8051 microcontrollers. So, we can simply save the dice patterns from 1 to 6 in the code memory like following.

;================================================

SEG_CODES: 
 DB  11111111B  ;0
 DB  11110111B  ;1
 DB  11011101B  ;2
 DB  11010101B  ;3
 DB  10011100B  ;4
 DB  10010100B  ;5
 DB  10001000B  ;6 


END

These are patterns used in such a manner that it would mimic the combination of LED’s attached in dice pattern.

Generating a Random Number in 8051 Assembly

There are lot of ways to implement random pattern, from simplest to most complex. We are going to implement the digital Dice Random Pattern with very simple incremental Counter. the Incremental Counter method would simply count from starting point to the max value we need to generate a random number. We stop it at random position and once the incremental counter is stopped, whatever value it would be at that time, that is our random value from min value to max value to be generated as a random number.

This method is one of the easiest to implement in 8051 assembly language and could serve the purpose of the random number generations. There are some limitations to it which could be overcome by extending the algorithm. But in our case where we only need to generate the random number from 1 to 6 in 8051 assembly language this is one of the most suited methods.

One thing we should keep in mind that we should make the counter so fast that it would count at max speed so when user release the button user cannot guess what number it would generate. If you make the counting slow down by some delay, user would be able to guess the number it would stop at. This will make it work as a digital Dice with a good game play.

;================================================

UPCNT1: 
 MOV  A,#7
 INC  DIGIT1
 CJNE  A,DIGIT1,UPCNT1_EXIT
 MOV  DIGIT1,#1
UPCNT1_EXIT:
 RET
Code language: PHP (php)

Adding a Touch of Game Play

We can add a touch of game play by letting user think he can generate the number of whatever he can. This is similar to rolling the dice. When user rolls the dice he shake it well. So we can add a similar effect by letting user think that he can generate the desired number. We are mimicing this by letting the counter keep counting until the user presses the button. So if the user keep pressing the counter will keep counting. Once the user release the button 🔳, it will stop the counter and displays the output to the LED port.

 MOV DIGIT1,#0 ;NUMBER TO BE DISPLAYED
 ACALL DISPLAY
 

MAIN:
 JNB  SW1,ACTION1
 ACALL DELAY1
 INC  TEMP_CNT
 ACALL UPCNT1
 SJMP  MAIN 
ACTION1:
 MOV  R0,#TEMP_CNT

A1L1:
 MOV  P1,#255
 MOV  R7,#255
A1L2:
  
 DJNZ  R7,A1L2
 ACALL UPCNT1 
 ACALL DISPLAY
 ACALL DELAY
 DJNZ  R0,A1L1
 
 JNB  SW1,ACTION1
 SJMP  MAIN
;================================================

Code language: ARM Assembly (armasm)

Complete Digital Dice 8051 Assembly Code

Here is the complete code in 8051 assembly language to roll a digital dice 🎲

;================================================
;
;         DIGITAL DISE PROGRAME
;
;
;================================================

SEG_DISP EQU P1
DIGIT1 EQU 30H
SW1 BIT P3.0

TEMP_CNT EQU  32H
;================================================

 MOV DIGIT1,#0 ;NUMBER TO BE DISPLAYED
 ACALL DISPLAY
 

MAIN:
 JNB  SW1,ACTION1
 ACALL DELAY1
 INC  TEMP_CNT
 ACALL UPCNT1
 SJMP  MAIN 


ACTION1:
 MOV  R0,#TEMP_CNT

A1L1:
 MOV  P1,#255
 MOV  R7,#255
A1L2:
  
 DJNZ  R7,A1L2
 ACALL UPCNT1 
 ACALL DISPLAY
 ACALL DELAY
 DJNZ  R0,A1L1
 
 JNB  SW1,ACTION1
 SJMP  MAIN
;================================================

DISPLAY: MOV DPTR,#SEG_CODES
 MOV A,DIGIT1
 MOVC A,@A+DPTR
 MOV SEG_DISP,A
 RET
  
;================================================

UPCNT1: 
 MOV  A,#7
 INC  DIGIT1
 CJNE  A,DIGIT1,UPCNT1_EXIT
 MOV  DIGIT1,#1
UPCNT1_EXIT:
 RET
 
;================================================

DELAY1:
 MOV  R7,#255
 DJNZ  R7,$
 RET 
DELAY: 
 MOV R5,#89
 MOV R6,#59
 MOV R7,#1
DLOOP: 
 DJNZ R5,$
 DJNZ R6,DLOOP
 DJNZ R7,DLOOP 
 RET
;================================================

SEG_CODES: 
 DB  11111111B  ;0
 DB  11110111B  ;1
 DB  11011101B  ;2
 DB  11010101B  ;3
 DB  10011100B  ;4
 DB  10010100B  ;5
 DB  10001000B  ;6 


END
 Code language: AVR Assembler (avrasm)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.