Uc 8051 - Programming 8051 - SRK
Uc 8051 - Programming 8051 - SRK
&
Peripheral Interfacing
FACULTY: SAGAR KHEDKAR
ELECTRONICS DEPARTMENT
WALCHAND COLLEGE OF ENGINEERING SANGLI
PROGRAMMING
8051
WHAT IS PROGRAMMING ?
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.
There are two types of Operands: the Source Operand and the
Destination Operand.
Instruction Set
Addressing Modes
Assembler Directives
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.
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
2. Arithmetic instructions.
3. Logical instructions.
5. Branch instructions.
Data Transfer Instructions
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
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
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.
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
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?
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit and sfr
Bit-wise operators
}
}
#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