You are on page 1of 24

Q1.

1) Input(Button)
2) MCU(8051) – Controlling the Task
3) ULN2308A - Transistor Array IC
4) LEDS for Output Display

We can provide input by pressing switch to MCU either 0 or 1.


MCU will give the output 5V or zero based random number by MCU to that Transistor
available in ULN2308A so transistor get turn on or turn off. And based on Transistor
condition LED gets ground or 5V. so it will turn on turn off.
a. A momentary low (0V) on Port pin 3.2
Dice1: 00001000 b

Dice2: 01000001 b

Dice3: 01001001 b

Dice4: 01010101 b

Dice5: 01011101 b

Dice6: 01110111 b
First we will generate random number. Now In interrupt service routine we will compare this
random generated value with 1 to 6. Let first compare that random value with 1 if equal then we will
write 00001000b(08H) on port it will turn on middle led as like one on dice. If it is not equal then we
will compare with 2. If equal then we will write 01000001b (41H) on port. So it will turn on two
corner led . so two leds becomes on as like two on dice. Same for 3,4,5,6 we will compare and write
49h,55h, 5Dh, 77h on port respectively. I used this convention for giving above ans.
First we have to make a list of code for each dice value;

Dice1 : 08h

Dice2 : 41h

Dice3 : 49h

Dice4 : 55h

Dice5 : 5Dh

Dice6 : 77h

Now in assembly code we compare random generated value which is available in R6 with 1 to 6 .and
if equal we will write above corresponding dice code to port. I write this login in interrupt service
routine.

ISR: MOV P1,#00h


MOV A,R6

ONE: CJNE A,#01,TWO


MOV P1,#00001000b
SJMP OVER
TWO: CJNE A,#02,THREE
MOV P1,#01000001b
SJMP OVER
THREE: CJNE A,#03,FOUR
MOV P1,#01001001b
SJMP OVER
FOUR: CJNE A,#04,FIVE
MOV P1,#01010101b
SJMP OVER
FIVE: CJNE A,#05,SIX
MOV P1,#01011101b
SJMP OVER
SIX: CJNE A,#06,OVER
MOV P1,#01110111b

OVER: RET
Generally the Push button is used to give the either 0 or 1 input to MCU.

We can do it by two way to detect the input. The one is with interrupt and second is with interrupt.
In first case the by using push button we can change the input to the MCU zero to 1 or 1 to 0. So
based on input change we can write the logic to display the random number on LEDs.

In second way. We can use the interrupt to detect input. In this case when button is pressed the
interrupt is generated. Now when interrupt is generated the MCU automatically execute the
interrupt service routine. In interrupt service routine we can write the logic for random number
display on LEDs.
We are using level trigger input. So once level trigger input is generated ISR get executed. if we want
we can capture that pin status in one of register for future use
.

Once pressed button the interrupt is generated. So in ISR we disable the interrupt . so during the
executing of present ISR. If any one pressed button for new dice number. Then also interrupt is not
going to generate. This how we can stop the system from registering a new press.
$MOD51 ; This includes 8051 definitions for the Metalink assembler

org 0000h

SJMP MAIN

ORG 003H ; sets the starting address for the ISR

ACALL ISR ; calls the ISR subroutine when S2 is pressed

RETI ; return from interrrupt

MAIN: SETB IP.0 ; set the external interrupt priority

SETB TCON.0 ; set the level trigger interrupt

SETB IE.0 ; enable the interrupt

SETB IE.7 ; enable the interrupt

MOV P1,#00h ; initialize the Port1 as output port

MOV P3,#0FFh ; initialize the port3 as input port


END

Once button pressed . high to low signal is passed to MCU . so MCU get interrupted and ISR get
executed. I write to display two on LED when button pressed. I success fully get that.
delay:

mov R1,#002h ; initialize the R1 register with an immediate value 02

mov R0,#0FFh ; load R0 with FFh value to repeat the loop for 256 times

back: DJNZ R0, back ;internal loop repeates 256 times

DJNZ R1,back ;

RET

END
We can use this program to generate random . R6 continuously changing from 6 to 0. And we can
display that value with some delay. So it will continuous changing the output number while roll is
occurring.

LABEL: MOV R6,#06 ; load maximum value in R6 register

LOOP: MOV A,R6 ;move R6 into A

MOV P1,A ; this will write continuously random number on port

DJNZ R6,LOOP; Decrement R6 and check it become zero or not

SJMP LABEL ; jump to label


We are using Interrupt service routine to write random value on port. So we will not clear the port
until next interrupt generated .so it will keep that value until new roll is triggered.
Below program continuously generate the random number. And when button pressed interrupt
service routine executed.

LABEL: MOV R6,#06 ; load maximum value in R6 register


LOOP: MOV A,R6 ;move R6 into A
DJNZ R6,LOOP; Decrement R6 and check it becomer zero or not
SJMP LABEL ; jump to label

ISR: MOV P1,#00h ;clear the prious output


MOV A,R6 ; move Random number from R6 to A

ONE: CJNE A,#01,TWO ; check that number is one or not


MOV P1,#00001000b ; display one on LED dice
SJMP OVER ; jump to over
TWO: CJNE A,#02,THREE ; check that number is two or not
MOV P1,#01000001b ; display two on LED dice
SJMP OVER ; jump to over
THREE: CJNE A,#03,FOUR ; check that number is three or not
MOV P1,#01001001b ; display three on LED dice
SJMP OVER ; jump to over
FOUR: CJNE A,#04,FIVE ; check that number is four or not
MOV P1,#01010101b ; display four on LED dice
SJMP OVER ; jump to over
FIVE: CJNE A,#05,SIX ; check that number is five or not
MOV P1,#01011101b ; display five on LED dice
SJMP OVER ; jump to over
SIX: CJNE A,#06,OVER ; check that number is six or not
MOV P1,#01110111b ; display six on LED dice

OVER: RET ; return to main

END
As we can observe that when button pressed and released then random number is display on LED
dice which is 3.

$MOD51 ; This includes 8051 definitions for the Metalink assembler

org 0000h
SJMP MAIN
ORG 003H ; sets the starting address for the ISR
ACALL ISR ; calls the ISR subroutine when S2 is pressed
RETI ; return from interrrupt

MAIN: SETB IP.0 ; set the external interrupt priority


SETB TCON.0 ; set the level trigger interrupt
SETB IE.0 ; enable the interrupt
SETB IE.7 ; enable the interrupt
MOV P1,#00h ; initialize the Port1 as output port
MOV P3,#0FFh ; initialize the port3 as input port

; this part generates the random number

LABEL: MOV R6,#06 ; load maximum value in R6 register


LOOP: MOV A,R6 ;move R6 into A
DJNZ R6,LOOP; Decrement R6 and check it becomer zero or not
SJMP LABEL ; jump to label

; this part interrupt service routine

ISR: MOV P1,#00h ;clear the prious output


MOV A,R6 ; move Random number from R6 to A

ONE: CJNE A,#01,TWO ; check that number is one or not


MOV P1,#00001000b ; display one on LED dice
SJMP OVER ; jump to over
TWO: CJNE A,#02,THREE ; check that number is two or not
MOV P1,#01000001b ; display two on LED dice
SJMP OVER ; jump to over
THREE: CJNE A,#03,FOUR ; check that number is three or not
MOV P1,#01001001b ; display three on LED dice
SJMP OVER ; jump to over
FOUR: CJNE A,#04,FIVE ; check that number is four or not
MOV P1,#01010101b ; display four on LED dice
SJMP OVER ; jump to over
FIVE: CJNE A,#05,SIX ; check that number is five or not
MOV P1,#01011101b ; display five on LED dice
SJMP OVER ; jump to over
SIX: CJNE A,#06,OVER ; check that number is six or not
MOV P1,#01110111b ; display six on LED dice

OVER: RET ; return to main

END
Button Pressed Number on LED
Dice
1 1
2 3
3 1
4 6
5 2
6 1
7 3
8 2
9 4
10 1
As we can observe from above figure we will get random number on dice LEDS when every time
button pressed.

You might also like