Project Title: Led Dice Project Description: in This Project, 7 Leds Are Connected To Portb of A Pic Microcontroller

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Project title: LED dice

Project description: In this project, 7 LEDs are connected to PORTB of a PIC microcontroller
and arranged such that they can show the faces of a dice when lit. Also a
push-button switch is connected to bit 7 of PORTB using a pull-up resistor.
When the switch is pressed the LEDs are lit randomly to show a dice
number between 1 and 6. Figure 5.35 shows the LEDs lit for a given dice
number.

Flow diagram: The flow diagram of the project is shown in Figure 5.38. At the beginning
of the program the I/O direction is specified. When the switch is pressed
a random number generation is simulated between 1 and 6 and this number
is then displayed on the LEDs which are constructed similar to the
face of a real dice.
Required number and LED to be turned on
Required number LEDs to be turned on
1 D4
2 D2, D6
3 D2, D4, D6
4 D1, D3, D5, D7
5 D1, D3, D4, D5, D7
6 D1, D2, D3, D5, D6, D7

Required number and PORTB data


Required number PORTB data (Hex)
1 $08
2 $22
3 $2A
4 $55
5 $5D
6 $77

LED DICE
========

7 LEDs are connected to PORTB of a PIC microcontroller and arranged as in the faces
of a dice. Also, a push-button switch is connected to bit 7 of PORTB.
When the switch is pressed the program generates a dice number
between 1 and 6 and turns on the appropriate LEDs to imitate the faces
of a real dice. The LEDs are turned on for 5 seconds and after this time
they are cleared and the program is ready to accept a new push-button
action.

The microcontroller is operated with internal 4MHz clock and internal


power-on-reset.

DEFINITIONS
LEDS VAR BYTE

DICE VAR BYTE

START OF MAIN PROGRAM

TRISB = $80 RB7 input, RB0-RB6 outputs

Wait until switch is pressed

AGAIN:

DICE = 1

NXT: IF PORTB.7 = 0 THEN NEWNO If pressed goto NEWNO

DICE = DICE + 1 Increment dice number

IF DICE = 7 THEN AGAIN between 1 and 6

GOTO NXT repeat

NEWNO:

Find the LEDs to be turned on. DICE is between 1 and 6. LEDS is the data

to be sent to PORTB to turn on the required LEDs

LOOKUP DICE, [0, $08, $22, $2A, $55, $5D, $77], LEDS

Turn on the LEDs

PORTB = LEDS Turn on appropriate LEDs

PAUSE 5000 Wait 5 seconds

PORTB = 0 Turn off all LEDs

GOTO AGAIN Repeat

END End of program

You might also like