You are on page 1of 17

B.

Eng Electronic Engineering Yr 2 / Qualifiers


“Embedded Hardware” Module
Lecture 14

Presented by
Mr. Fergal Henry
I.T, Sligo
Monday 24th January 2011
8051 Microcontroller Data Memory Map
8051 Microcontroller Instruction Set - 1
8051 Microcontroller Instruction Set - 2
First 8051 Assembly Program
• Let’s write an assembly program to add two numbers
together & generate a result (sum).
• The first number is stored at general-purpose RAM
address 30h.
• The second number is stored at general-purpose RAM
address 31h.
• The result is to be stored at address 32h.
• The program is as follows:
org 0
mov a,30h
add a,31h
mov 32h,a
end
Assembly Program Explained
• We will use the EdSim51 Simulator (developed by Jim
Rogers) to assemble this program.
• The assembler converts each instruction into an opcode.
• The assembler directive org 0 places the first opcode in the
program at address 0000h in code memory.
• The first instruction mov a,30h copies (reads) the number
from RAM address 30h & pastes (writes) it into the
accumulator.
• The second instruction add a,31h adds the number at
address 31h to the number in the accumulator & places the
result (sum) in the accumulator.
• The third & final instruction mov 32h,a copies the sum
from the accumulator & pastes it into RAM address 32h.
• Assembly programs terminate with end.
Code Memory in EdSim51
• A screenshot of code memory is illustrated below.
• Questions:
(i) What are the opcodes for each of the three
instructions?
(ii) How many bytes of code memory are added to each
opcode for addresses?
(iii) What are the respective addresses for each opcode?
Data Memory in EdSim51
• A screenshot of data memory is
illustrated below.
• In this example, I have manually
modified memory addresses 30h
& 31h to contain the data 01h &
02h respectively.
• By default, code memory
addresses contain 00h.
• Address 32h is updated with the
result of the addition (01h + 02h =
03h) after the program is stepped
through.
• The final value in the accumulator
can be viewed in binary & hex
formats opposite.
Echo Switches to LEDs
• Let’s write a program to read the status of the switches &
echo them to the LEDs.
• Q.1 What port are the switches SW0 – SW7 wired to?
• Q.2 What logic level is on a P2.0 when a switch SW0 is
open?
• Q.3 What port are the cathodes of the LEDs LED0 –
LED7 wired to?
• Q.4 What logic level on P1.0 turns LED0 on?
org 0
start:
mov p1,p2
jmp start
end
echo.asm Discussed
• There are 3 types of jump instruction (AJMP, LJMP & SJMP)
where the range for jumping code memory addresses is
medium, long & short respectively.
• We will use jmp & let the assembler decide which is the
appropriate one to use.
• The jump instruction in this program creates an infinite loop,
which allows us to constantly read the switches to see if any
change has occurred.
• We use a data transfer instruction where P2 is the source & P1
is the destination.
• P1 & P2 are the special function registers which keep track of
the logic levels on the port pins.
• The binary pattern is first read from P2 & is then written to P1.
• Hence the status of the switches is echoed to the LEDs.
DC Motor Control
• The DC motor is interfaced to the 8051 via a bridge driver.
• The inputs to the driver, A & B, are connected to P3.1 &
P3.0 respectively.
• The motor runs clockwise / anti-clockwise if the voltage
on these 2 pins are different.
• If the voltage is the same on both pins, the motor stops.
org 0
start:
mov c,p2.0
mov p3.0,c
mov c,p2.1
mov p3.1,c
jmp start
end
motor.asm Discussed
• We will control the motor using switches SW0 & SW1.
• The instructions used here are in the Boolean Variable
Manipulation category as we need to access bits rather
than bytes.
• All reading & writing of bits is done through the carry
flag, which is bit 7 (MSB) in the Program Status Word
special function register.
• The status of SW0 is read into the carry flag & then
written from the carry flag to the B input of the driver.
• The status of SW1 is read into the carry flag & then
written from the carry flag to the A input of the driver.
• An infinite loop is used again here as the switches will
have to be monitored in case they change.
Testing the Motor Program
• Set the Update Freq to 1.
• This runs the simulator at its slowest speed.
• Adjust the slider for the motor to MAX.
• Ensure that the motor is enabled.
• Run the program rather than stepping it.
• Switch off the key/switch bounce i.e. leave it un-checked.
• When neither switch 0 or 1 is pressed, the motor is off.
• When switch 0 is pressed only, the motor runs anti-clockwise.
• When switch 1 is pressed only, the motor runs clockwise.
• When both switches are pressed, the motor stops.
Controlling the Seven Segment Displays (SSDs)
• Only one of the SSDs is turned on at any given time.
• This is controlled using the 2-to-4 line decoder.
• A1 & A0 are the inputs of the decoder & are connected to
P3.4 & P3.3 respectively.
• The decoder works as follows:

A1 A0 Q3 Q2 Q1 Q0 Display Activated
0 0 1 1 1 0 DISP 0
0 1 1 1 0 1 DISP 1
1 0 1 0 1 1 DISP 2
1 1 0 1 1 1 DISP 3
Displaying Digits on the SSDs
• There are 8 LEDs on each SSD.
• The are labelled a, b, c, d, e, f, g & dp.
• The displays are common anode.
• This means that all 8 anodes are tied together.
• When a display is activated, 5V is applied to the common
anode of a display.
• The 8 cathodes are connected to P1 as shown below.
• A LOW (0) on a cathode will light a particular segment
while a HIGH (1) will turn it off.
• Question: What binary number is written to P1 to display
the number 5 with decimal point turned off?
SSD.asm
• We will write a program to display the number 3 on DISP 2
with the decimal point off.
• We will use the Boolean Variable Manipulation instructions
for setting & clearing bits.
• # differentiates a number from an address.
• b specifies a binary number as opposed to h for hexadecimal.
• A semicolon precedes a comment.
• Ensure that the SSD is enabled rather than the LCD module.
org 0
start:
setb p3.4
clr p3.3
mov p1,#10110000b; SSD code for 3.
end

You might also like