8051 Microcontroller Tutorial(basics)
HEX ADDITION AND SUBTRACTION,
8051 Timers
Before actually going through this tutorial let me tell you
something about number systems used in Computer Systems. As
you know human know the decimal number system 1,2,3---9, but
how will computer understand our language hence we use
binary system which uses 0 & 1. Computers understand the
language of 0 & 1. We also have a hexadecimal system which
is nothing but a way of representing a binary number.
Similarly we have a ASCII System for information sharing
between computers.
Memory inside computer system: There are Basically
two types of memories RAM & ROM. RAM as you know is Random
Access Memory and data stored in it is temporary whereas ROM
is read only memory and data stored in it is permanent. CPU
(Central Processing Unit is combination of ALU Arithmetic
Logic Unit & Control Unit. The
A.L.U. (Arithmetic and Logic Unit) performs all the
calculations.
Main features of 8051 Microcontroller are :
RAM-128 bytes, ROM-4K bytes, Timer-2, I/O pins-32, Serial
port, Interrupt sources.
Below on the left you can see 8051 pin diagram and on the
right is the Internal RAM of 8051.Internal RAM locations
20-2FH are both byte-addressable and bit addressable. Out of
128 bytes of Internal RAM only 16 bytes of it are bit
addressable.
 
8051’s programming registers
& SFR's
A Accumulator
-Used to accumulate resultants of big
instructions.
B register-Similar To ACC except it holds 1byte
SP Stack Pointer register-Used for Push and Pop.
This SFR indicates where the next value to be taken from the
stack will be read from in Internal RAM
DPTR Data Pointer register(16bit)
-Points to Data to be executed.The SFRs DPL and DPH act
together to represent a 16-bit value called the Data
Pointer.
R0-R7
general registers-8 bit registers
PSW (Program Status Word (Flagregister): PSW SFR
contains the carry flag, the auxiliary carry flag, the
overflow flag, and the parity flag.
P1 Port -Input/Output Port
P2 Port-Input/Output Port
P3 Port-Input/Output Port
TH0 TL0 16-bit timer register
TH1 TL1 16-bit timer register
SCON Serial Control register -controls the baud rate of the
serial port
SBUF Serial Buffer register-Act as an Input/Output Port
Addressing Modes in 8051:
MOV A,#30H ;immediate mode
MOV A,30H ; direct mode
MOV A,R0 ; register mode
MOV R0,#30H ; immediate mode
MOV A,@R0 ; indirect mode
MOV DPTR,#9000H ;immediate mode
MOVX A,@DPTR ; indirect mode
8051 Timer Basics
Timer SFRs-TMOD SFR and TCON SFR
|
SFR
Type |
Description of SFR |
SFR Address |
|
TH0 |
High Byte OF Timer 0 |
8Ch |
|
TL0 |
Low Byte OF Timer 0 |
8Ah |
|
TH1 |
High Byte OF Timer 1 |
8Dh |
|
TL1 |
Low Byte OF Timer 1 |
8Bh |
|
TCON |
Timer Control |
88h |
|
TMOD |
Timer Mode |
89h |
TMOD
SFR -Timers
By Using this SFR we can modify timer to be a 16-bit
timer, an 8-bit autoreload timer, a 13-bit timer, or 2 separate timers
|
Bit Number |
Type |
Working |
Timer Type |
|
7 |
GATE1 |
Gating Control when set |
1 |
|
6 |
C/T1 |
To decide whether timer is used as a
delay generator or as an event counter |
1 |
|
5 |
M1 |
Timer mode bit |
1 |
|
4 |
M0 |
Timer mode bit |
1 |
|
3 |
GATE0 |
Gating Control Purpose |
0 |
|
2 |
C/T0 |
To decide whether timer is used as a
delay generator or as an event counter |
0 |
|
1 |
M1 |
Timer mode bit |
0 |
|
0 |
M0 |
Timer mode bit
|
0 |
|
M1 |
M0 |
Timer Mode |
Description of Mode |
|
0 |
0 |
0 |
13-bit Timer. |
|
0 |
1 |
1 |
16-bit Timer |
|
1 |
0 |
2 |
8-bit auto-reload |
|
1 |
1 |
3 |
Split timer mode |
TCON SFR-Timers
The Timer Control SFR is used to modify the way in which the 8051's two
timers T0 & T1 are operating
|
Bit |
Type |
Working |
|
7 |
TF1 |
Timer 1 Overflow
flag
|
|
6 |
TR1 |
Timer 1 Run
control bit |
|
5 |
TF0 |
Timer 0 Overflow
flag
|
|
4 |
TR0 |
Timer 0 Run
control bit |
|
3 |
IE1 |
External Interrupt 1 |
|
2 |
IT1 |
Interrupt 1 type |
|
1 |
IE0 |
External Interrupt 0 |
|
0 |
IT0 |
Interrupt 0 Type |
8051Microntroller vs 8052 Microcontroller
|
|
8051 Subroutines
BCD TO BYTE
bcd_to_byte:
XCH A,R0
SUBB A,#30h
JNB ACC.4,bcd_to_byte_2
SUBB A,#07h
bcd_to_byte_2:
XCH A,R0
SUBB A,#30h
JNB ACC.4,bcd_to_byte_3
SUBB A,#07h
bcd_to_byte_3:
SWAP A
ORL A,R0
RET
BYTE TO BCD
byte_to_bcd:
MOV R0,A
ANL A,#0Fh
ADD A,#0F6h
JNC byte_to_bcd_2
ADD A,#07h
byte_to_bcd_2:
ADD A,#3Ah
XCH A,R0
SWAP A
ANL A,#0Fh
ADD A,#0F6h
JNC byte_to_bcd_3
ADD A,#07h
byte_to_bcd_3:
ADD A,#3Ah
RET
Decrement Data Pointer
DEC_DPTR:
XCH A,DPL ;Exchange A for DPL
DEC A ;Decrement A (which is DPL)
CJNE A,#0FFh,_dec_dptr2 ;If A (DPL) is not #0FFh, continue
normally
DEC DPH ;If A=FFh, we need to decrement DPH
_dec_dptr2:
XCH A,DPL ;Exchange A for DPL (thus saving DPL and restoring
A)
RET
Binary To ASCII
BINTOASC :
anl a,#00fh ; Keep Only Low Bits
add a,#090h ; Add 144
da a ; Decimal Adjust
addc a,#040h ; Add 64
da a ; Decimal Adjust
ret ; Return To Call
end
|
BYTE TO ASCII
Byte2ascii:
mov b, a
swap a
acall Nibble2ascii
xch a, b
Nibble2ascii:
anl a, #0x0f
inc a
movc a, @a+pc
ret
.ascii "0123456789ABCDEF"
HEX TO ASCII
Convert a hexadecimal nibble to its ASCII character
equivalent.
HexAsc: ANL A,#0Fh ; Make sure we're working with only
; one nibble.
CJNE A,#0Ah,HA1 ; Test value range.
HA1: JC HAVal09 ; Value is 0 to 9.
ADD A,#7 ; Value is A to F, extra adjustment.
HAVal09: ADD A,#'0' ; Adjust value to ASCII hex.
RET
Delays Subroutines
Delay For 500 Microseconds
delay500ms:
call delay100ms
call delay100ms
call delay100ms
call delay100ms
call delay100ms
ret
Delay for 100 Microseconds
delay100ms:
mov r7,#100
x4: call delay1ms
djnz r7,x4
ret
Delay for 1 Seconds
delay1s:
mov r6,#10
x5: call delay100ms
djnz r6,x5
ret
Delay for 1ms
delay1ms:
mov r2,#10
x3: mov r1,#49
x2: djnz r1,x2
djnz r2,x3
ret
|
Microprocessor vs Microcontroller

Microprocessor is a microcomputer on a single
chip. A microprocessor are of various types such as 20 pin microprocessor or a
40 pin microprocessor. A microprocessor is a programmable digital electronic
component that incorporates the functions of a central processing unit (CPU) on
a single semi conducting integrated circuit (IC). Advanced Microprocessors can
be said as Microcontrollers like we have
8051 and 8052.
In a
Microcontroller we have an inbuilt RAM, ROM, I/O Ports that is they exist on the
chip itself as shown in the above diagram.
Embedded Products using Microcontrollers
INTERCOM
TELEPHONE
FAX MACHINES TVs
CABLE TV
TUNER VCR
CAMCORDER
MICROWAVE LASER PRINTER
ENGINE CONTROL SEWING MACHINES
REMOTE CONTROLS VIDEO GAMES
CELLULAR PHONES TELEPHONES |