0% found this document useful (0 votes)
25 views55 pages

Uc 8051 - Programming 8051 - SRK

8051 microcontroller

Uploaded by

smitamanephysics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views55 pages

Uc 8051 - Programming 8051 - SRK

8051 microcontroller

Uploaded by

smitamanephysics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Microcontroller

&
Peripheral Interfacing
FACULTY: SAGAR KHEDKAR
ELECTRONICS DEPARTMENT
WALCHAND COLLEGE OF ENGINEERING SANGLI
PROGRAMMING
8051
WHAT IS PROGRAMMING ?

 Programming is the process of creating a set of instructions that


tell a computer how to perform a task.

 Programming can be done using a variety of computer


programming languages, such as C, JavaScript, Python, C++ …

 What is Programming Language?


A programming language is a notation designed to connect
instructions to a machine or a computer.
TYPES OF PROGRAMMING
LANGUAGES
1. Procedural Programming Language
The procedural programming language is used to execute a
sequence of statements which lead to a result.

2. Functional Programming Language


Functional programming language typically uses stored data,
frequently avoiding loops in favour of recursive functions.
3. Object-oriented Programming Language
This programming language views the world as a group of
objects that have internal data and external accessing parts of
that data.

4. Scripting Programming Language


These programming languages are often procedural and
may comprise object-oriented language elements, but they fall
into their own category as they are normally not full-fledged
programming languages with support for development of large
systems.
5. Logic Programming Language
These types of languages let programmers make declarative
statements and then allow the machine to reason about the
consequences of those statements.
8051 PROGRAMMING
1. ASSEMBLY LANGUAGE PROGRAMMING
Assembly language is a low-level programming language for
a computer / controller or other programmable device
specific to a particular computer Architecture.

Each family of processors has its own set of instructions for


handling various operations such as getting input from
keyboard, displaying information on screen and performing
various other jobs. These set of instructions are called
'machine language instructions'.
A processor understands only machine language
instructions, which are strings of 1's and 0's. However,
machine language is too obscure and complex for using
in software development.

So, the low-level assembly language is designed for a


specific family of processors that represents various
instructions in symbolic code and a more understandable
form.
 The fundamental unit of computer storage is a bit; it could be ON
(1) or OFF (0) and a group of 8 related bits makes a byte on most
of the modern computers.
Word: a 2-byte data item
Double word: a 4-byte (32 bit) data item
Quad word: an 8-byte (64 bit) data item
Paragraph: a 16-byte (128 bit) area
Kilobyte: 1024 bytes
Megabyte: 1,048,576 bytes
FLOW
INSTRUCTION SET
OF 8051
 An instruction is an order or command given to a processor by a
computer program.

All commands are known as instruction set and set of instructions


is known as program

 8051 have in total 111 instructions, i.e. 111 different words


available for program writing
Format

It consist of OP-CODE and OPERAND

Where first part describes WHAT should be done, while other


explains HOW to do it.

The latter part can be a data (binary number) or the address at


which the data is stored.
Ex.
OP-CODE

The first part of the Instruction is the Op-code, which is short for
Operation Code, specifies the operation to be performed by the
Microcontroller.

Op-codes in Assembly Language are called as Mnemonics. Op-


codes are in binary format (used in Machine Language)
OPERAND

The second part of the instruction is called the Operand(s) and it


represents the Data on which the operation is performed.

There are two types of Operands: the Source Operand and the
Destination Operand.

The Source Operand is the Input of the operation and the


Destination Operand is where the result is stored.
The Elements of an Assembly
Language Programming
Assembler Directives

Instruction Set

Addressing Modes
Assembler Directives

They give the directions to the CPU

e.g.
ORG(origin): This directive indicates the start of the program. This is
used to set the register address during assembly. For example; ORG
0000h tells the compiler all subsequent code starting at address
0000h.

Syntax: ORG 0000h


DB(define byte): The define byte is used to allow a string of bytes.

EQU (equivalent): The equivalent directive is used to equate


address of the variable.

END: The END directive is used to indicate the end of the program.
1st Assembly Program
ORG 0000H
MOV A, #05H
MOV R, #09H
ADD A, R1 ; addition instruction
END
Rules of Assembly Language

The assembly code must be written in upper case letters


The labels must be followed by a colon (label:)
All symbols and labels must begin with a letter
All comments are typed in lower case
The last line of the program must be the END directive
Types

Depending upon the number of bytes required to represent 1


instruction completely, Instructions are divided into 3 types

One/single byte instruction

Two/double byte instruction

Three/triple byte instruction


If operand is not given in the instruction or there is no digits present
with instruction, the instructions can be completely represented in
one byte opcode
OPCODE 8 bit
If 8 bit number is given as operand in the instruction, then such
instruction can be completely represented in two bytes
First byte OPCODE
Second byte 8 bit data or I/O port
If 16 bit number is given as operand in the instruction then such
instruction can be completely represented in three bytes

16 bit number specified may be data or address

First byte OPCODE.


Second byte 8 LSB’s of data/address.
Third byte 8 MSB’S of data/address.
OPERATION BASED TYPES
1. Data transfer instructions.

2. Arithmetic instructions.

3. Logical instructions.

4. Logical instructions with bits.

5. Branch instructions.
Data Transfer Instructions

 These instructions move the data / content of one register to


another one or transfer data from one location to other.

XCH
XCHD
Ex.
MOV
The MOV instruction moves data bytes between the two specified
operands. The byte specified by the second operand is copied to
the location specified by the first operand. The source data byte is
not affected.
Arithmetic Instructions
These instructions perform several basic operations. After
execution, the result is stored in the first operand.

Arithmetic
ADDC
The ADDC instruction adds a byte value and the value of the carry
flag to the accumulator. The results of the addition are stored
back in the accumulator. Several of the flag registers are affected.
Logical Instructions

These instructions perform logical operations between two register


contents on bit by bit basis.

Logical
ANL
The ANL instruction performs a bitwise logical AND operation
between the specified byte or bit operands and stores the result in
the destination operand.
Logical Instructions On Bits

Similar to logical instructions, these instructions also perform logical


operations.
The difference is that these operations are performed on single
bits.
CLR C CLR bit
SETB C SETB bit
CPL C
ANL C, bit
Program Flow Control Instructions
In this group, instructions are related to the flow of the program,
these are used to control the operation like, JUMP and CALL
instructions.
Some instructions are used to introduce delay in the program, to
the halt program.
ACALL adr11 LCALL adr16
RET LJMP addr16
SJMP JNZ , rel
CJNE A, Rx, rel DJNZ Rn , rel
NOP
JMP JZ LCALL NOP

LJMP JNZ ACALL

AJMP CJNE RET

SJMP DJNZ RETI


8051 Addressing Modes

Addressing modes specifies where the data (operand) is, i.e. they
specify the source or destination of data (operand) in several
different ways, depending upon the situation.

Addressing modes are used to know where the operand is


located.
8051 Addressing Modes

There are 5 types of addressing modes:

1. Register addressing.
2. Direct addressing.
3. Register indirect addressing.
4. Immediate addressing.
5. Index addressing.
The source and/or destination is a register
In this case; data is placed in any of the 8 registers(R0-R7); in
instructions it is specified with letter Rn (where n indicates 0 to 7).

 For example
ADD A, Rn (This is general instruction).
ADD A, R5 (This instruction will add the contents of register R5 with
the accumulator contents).
Direct Addressing Mode

In Direct Addressing Mode, the address of the data is specified as


the Operand in the instruction.
Using Direct Addressing Mode, we can access any register or on-
chip variable. This includes general purpose RAM, SFRs, I/O Ports,
Control registers.

Example: MOV A, 47H


Here, the data in the RAM location 47H is moved to the
Accumulator.
Register Indirect Addressing Mode

In the Register Indirect Addressing Mode, the address of the


Operand is specified as the content of a Register.
Example: MOV A, @R1
The @ symbol indicates that the addressing mode is indirect. If the
contents of R1 is 56H, for example, then the operand is in the
internal RAM location 56H. If the contents of the RAM location 56H
is 24H, then 24H is moved into accumulator.
Only R0 and R1 are allowed in Indirect Addressing Mode. These
register in the indirect addressing mode are called as Pointer
registers.
Immediate Addressing Mode

In Immediate Addressing mode, the operand, which follows the


Opcode, is a constant data of either 8 or 16 bits.
The name Immediate Addressing came from the fact that the
constant data to be stored in the memory immediately follows the
Opcode.
The constant value to be stored is specified in the instruction itself
rather than taking from a register.
The destination register to which the constant data must be
copied should be the same size as the operand mentioned in the
instruction.
Example: MOV A, #030H
Here, the Accumulator is loaded with 30 (hexadecimal). The # in
the operand indicates that it is a data and not the address of a
Register.
Immediate Addressing is very fast as the data to be loaded is
given in the instruction itself.
Index Addressing Mode

With Indexed Addressing Mode, the effective address of the


Operand is the sum of a base register and an offset register.
The Base Register can be either Data Pointer (DPTR) or Program
Counter (PC) while the Offset register is the Accumulator (A).
In Indexed Addressing Mode, only MOVC and JMP instructions
can be used. Indexed Addressing Mode is useful when retrieving
data from look-up tables.
Example: MOVC A, @A+DPTR
Here, the address for the operand is the sum of contents of DPTR
and Accumulator.
OPERANDS AND THEIR MEANINGS

A - accumulator
Rn - is one of working registers (R0-R7) in the currently active RAM
memory bank
Direct - is any 8-bit address register of RAM. It can be any general-
purpose register or a SFR (I/O port, control register etc.)
@Ri - is indirect internal or external RAM location addressed by
register R0 or R1
#data - is an 8-bit constant included in instruction (0-255)
#data16 - is a 16-bit constant included as bytes 2 and 3 in
instruction (0-65535)
addr16 - is a 16-bit address. May be anywhere within 64KB of
program memory
addr11 - is an 11-bit address. May be within the same 2KB page of
program memory as the first
bit - is any bit-addressable I/O pin, control or status bit
C - is carry flag of the status register (register PSW)
PROGRAMMING
8051 IN C
Why C?

It is easier and less time consuming to write in C than Assembly

C is easier to modify and update

You can use code available in function libraries

C code is portable to other microcontroller with little or no


modification
C data types

Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
Bit-wise operators

AND (&) OR (|) EX-OR (^) Inverter (~)


Shift Right (>>) Shift Left (<<)
1st C Program for 8051
#include <REGX51.H> Software Delay
sbit LED = P0^0; Generation – 1
void main(void)
{ unsigned char i;
while(1)
{ LED = ~LED;
for(i=0;i<100;i++);

}
}
#include <REGX51.H> Software Delay
sbit LED = P0^0; Generation – 2

void delay(void)
{ unsigned char i;
for(i=0;i<100;i++);
}
void main(void)
{ while(1) // for( ; ; )
{ LED = 0;
delay( );
LED = 1;
delay( );
}
}
#include <REGX51.H> Software Delay
sbit LED = P0^0; Generation – 3

void delay(unsigned char j)


{
unsigned char i;
for(i=0;i<100;i++);
}
void main(void)
{ while(1) // for(;;)
{
LED = 0;
delay(20);
LED = 1;
delay(10);
}
}

You might also like