Skip to main content

8051 tutorial part:3 - Memory, Data Type and Directive

In this tutorial we will talk about Types of Memory, RAM, bit addressable RAM and how to use it. then we will cover  8051 data type and assembler directive, need of directives and their effect on program. And  how to use them in program with the help of some short program.

This is the second part of  8051 Tutorial Series. you can also check  first part and second part of series

Types of Memory:

 As you know generally memory categorised as RAM and ROM  But it can be also categories as internal and external. For 8051 we generally talk about three type of memory is 
  • On chip memory (RAM and ROM)
  • External Code memory (ROM memory) and
  • External RAM memory.

External RAM and ROM:

some time internal ROM or RAM are not sufficient to project then instead of using two or  more microcontroller we use external ROM to hold your program and external RAM to run program. but generally ROM is not needed because 4k or 8k ROM is enough to hold your program but  RAM (only 128btye or 256 byte on chip ) many times required.

On Chip Memory:

 8051 have on chip RAM and ROM , but in general we only talk about on chip RAM. The on chip RAM is also two types 
  • Internal RAM ( specified by manufactuar )
  • Special Function Register (SFR) memory
Internal RAM: This is the memory we use to declare variable and hold data. All the register R0, R1, R3..... . .R7 and register bank (we discuss in last tutorial ) are also part of this RAM. It can be divided in three parts Register Bank, Bit addressable RAM, and scratch pad RAM. 
Register bank: all four register bank cover 32byte of RAM and they can be used as R0,R1...R7 by setting Register bank or  directly by their address of RAM. it is addressed from 00h to 1Fh
Example 1
++++++++++++++++++++Register Bank+++++++++++++++++++++
SETB PSW.3   ;Selecting register bank 1
MOV R1, #23H ;mov 23h on register R! or 1st bank
----or----
MOV 09H,#23H ;09h is addr of R1 of reg.bank 1
++++++++++++++++++++++++++++++++++++++++++++++++++++++

Bit Addressable RAM: This part of RAM is bit addressable, that means you can excess individual bit of this RAM by bit operation like SETB, CLR, CPL, JB etc and bit address.
        Each bit has a address and addressed from 00-127 in decimal and 00-7Fh in hex. Advantage of this RAM is that you can define bool variable in this RAM. In RAM map it is addressed from 20H to 2FH
Example 2
++++++++++++++Bit Addressable RAM++++++++++++++++++++++
SETB 00H   ;set bit 00h 
CLR  70H   ;clear bit 70h
CPL  12H  ;complement bit 12h
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Scratch Pad RAM: This is the remaining RAM after register bank and bit addressable RAM and size of it depend on type of controller. If your code has more variable or need to store data then this ram is used.

8051 Data Type and Directives:

There is only one data type for 8051 which is 8 bit data type.  All variable you define is 8bit wide and can hold value 0-255 or -128 to 127.
Let you define a variable (counter) then you need to take of overflow of counter by writing code, because after reaching counter to 255 it not go to 256 instead it reset to 00. while in other languages like c, you don't need to checking overflow of variable.

DB (Define Byte): DB is used to define 8 bit data in Internal ROM memory. It can be used as
Example 3
+++++++++++++++++++++DB (define byte)+++++++++++++++++++
DATA 1: DB 45             ;define a decimal data 
DATA 2: DB 01001011B      ;define a binary data
DATA 3: DB 37H            ;define a hex data
DATA 4: DB "1223"         ;ASCII number
DATA 5: DB "It is Tentuts.com"  ; define a string
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
H after hex value and B after binary value necessary but you can drop D from decimal values. ASCII and string automatically converted in hex by assembler
Some directives are:
ORG (Origin) :  It show the beginning address of the program. When you define a ORG for a segment of program then that code if burned in ROM memory at that memory address.
Example 4
++++++++++++++++++++++++ORG (Origin)++++++++++++++++++++
ORG 00H          ;code burned after 00h memory 
MOV R0,30H 
ADD A,R0
                 --and--
ORG 300H         ;code burned after 300h memory
DB: 34h,32h      ;300h contain 34h and 301h contain 32h
                 ;because we define two byte data
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EQU (equate): It define a constant without occupying memory.  This makes task very simple by giving a name   to memory location or constant value
Example 5
 
++++++++++++++++++++++++EQU (Equate)++++++++++++++++++++
COUNT EQU  25
MOV R0, #COUNT  ;mov 25 to r0 
                ;it is value because of #sign
--similar to--
MOV R0, #25     
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
END: It tells to assembler that this is the end of assembly program. so a program may look like this after use of all directive
++++++++++++++++++++++++SAMPLE PROGRAM++++++++++++++++++
;perform addition of three numbers
         ORG 00H
CONST    EQU 30H           ;define a constant
STORE    EQU 46H           ;define memory address to
                           ;store data after addition
         ADD A,#DATA1      ;add data to reg A.
         ADD A,#DATA2      ;add data
         ADD A,#CONST      ;add const "# sign is used"
         MOV STORE,A       ;STORE as address because no #sing
         
         ORG 330H
DATA1:   DB 23H     ;define 23h at 330h
DATA2:   DB 34H     ;define 34h at 331h
         END 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Directive has no effect on program and no hex is generated for them. they are used for assembler and programmer to make program less complex and readable
Hope you enjoy the tutorial. In next part we cover  JUMP, LOOPs and CALL instructions and use them.
 if you like this tutorial please share to others and follow us.

Comments

Popular posts from this blog

LCD Interfacing with 8051 in 4 bit mode : assembly tutorial

Hello friends, in this tutorial we are going to interface LCD with 8051 microcontroller but in 4 bit mode. we already interfaced LCD with 8051 in 8 bit and also done scrolling of text in previous tutorials.

Tutorial for Scroll text on 16x2 LCD with 8051 Microcontroller in c

In our last tutorial we interface 2x16 LCD with 8051 in assembly, now lets display some scrolling text on LCD. it is very useful because generally we have a long text to display and a 2x16 LCD display only 16 character at a time.

How To Use keil uVision 4 IDE - Project Setup, Debug And Simulation

In this tutorial i am going to show you how to setup your project  in Keil for 8051 (AT89s51) to simulate and debug your code. This is one of the most important tutorial for beginner who just start learning 8051. Because Keil is the best IDE to compile,debug and simulate your program.  And most of 8051 programmer you this tool so you get lot of help about this on internet. In this tutorial i use Keil v4.0 but it is same for all version or you can download lates trail version from here