You are on page 1of 9

Keil II

Memory Access
1) Memory Areas
i) Program (CODE) memory: Program memory may be accessed using the
code memory type specifier in the Cx51 compiler.
code :
Program memory (64 KBytes); accessed by opcode MOVC @A+DPTR.
Example:
char code text[] = "ENTER PARAMETER:";

ii) Internal Data Memory:- Internal data can be broken down into three distinct
memory types: data, idata,and bdata.
The data memory specifier always refers to the first 128 bytes of internal data
memory. Variables stored here are accessed using direct addressing.

The idata memory specifier refers to all 256 bytes of internal data memory;
however, this memory type specifier code is generated by indirect addressing which
is slower than direct addressing.
The bdata memory specifier refers to the 16 bytes of bit-addressable memory in
the internal data area (20h to 2Fh). This memory type specifier allows you to
declare data types that can also be accessed at the bit level.
data :
Directly addressable internal data memory; fastest access to variables
(128 bytes).
idata
:
Indirectly addressable internal data memory; accessed across the full
internal address space (256 bytes).
bdata :
Bit-addressable internal data memory; supports mixed bit and byte
access (16 bytes).
Examples :
char data var1;
float idata x,y,z;
char bdata flags;

iii) External Data Memory:- The Cx51 Compiler offers two different memory
types that access external data: xdata and pdata.
The xdata memory specifier refers to any location in the 64 KByte address space
of external data memory.

The pdata memory type specifier refers to only one (1) page or 256 bytes of
external data memory.
xdata :
pdata :
@Rn.

External data memory (64 KBytes); accessed by opcode MOVX @DPTR.


Paged (256 bytes) external data memory; accessed by opcode MOVX

Examples:
unsigned long xdata array[100];
unsigned int pdata dimension;

unsigned char xdata vector[10][4][4];

2) Memory Models: (SMALL, COMPACT and LARGE)


i) Small Model:In this model, all variables, by default, reside in the internal data memory of the
8051 system. (This is the same as if they were declared explicitly using the data
memory type specifier.) In this memory model, variable access is very efficient.
However, all objects, as well as the stack must fit into the internal RAM. Stack size is
critical because the real stack size depends upon the nesting depth of the various
functions. Typically, if the linker/locator is configured to overlay variables in the
internal data memory, the small model is the best model to use.

ii) Compact Model:Using the compact model, all variables, by default, reside in one page of external
data memory. (This is as if they were explicitly declared using the pdata memory
type specifier.) This memory model can accommodate a maximum of 256 bytes of
variables. The limitation is due to the addressing scheme used, which is indirect
through registers R0 and R1 (@R0, @R1). This memory model is not as efficient as
the small model, therefore, variable access is not as fast. However, the compact
model is faster than the large model. When using the compact model, the Cx51
compiler accesses external memory with instructions that utilize the @R0 and @R1
operands. R0 and R1 are byte registers and provide only the low-order byte of the
address. If the compact model is used with more than 256 bytes of external
memory, the high-order address byte (or page) is provided by Port 2 on the 8051. In
this case, you must initialize Port 2 with the proper external memory page to use.
This can be done in the startup code. You must also specify the starting address for
PDATA to the linker.

iii) Large Model:In the large model, all variables, by default, reside in external data memory (up to
64 KBytes). (This is the same as if they were explicitly declared using the xdata
memory type specifier.) The data pointer (DPTR) is used for addressing. Memory
access through this data pointer is inefficient, especially on variables with a length
of two or more bytes. This type of data access mechanism generates more code
than the small or compact models.

Exercise:
1) Write programs to store variables in memory as specified in the following table:
Sr.
Variable/s
Memory
No.
a.
Inputs - Integers a,b,c
Internal RAM
Result integer y = a+b+c
Port1
b.
Character array message
Code memory
Integer array temperatures
External RAM (one page 00-FF h)
c.
Inputs - Bits b1,b2, b3
Bitwise RAM
Results bit P1.0 = b1 AND b2 AND Port bits
b3
bit P1.1 = b1 OR b2 OR b3

d.

bit P1.2 = b1 NAND b2


bit P1.3 = b1 NOR b3
Input character array mydata[16]
Output copy mydata[16]
to
newdata[16]

Internal RAM
External RAM

Keil I
Data Types
Data Types

Bits

bit
signed char
unsigned char
enum
+32767
signed short
unsigned short
signed int
unsigned int
signed long
unsigned long
float
sbit
sfr
sfr16

Bytes

Value Range
0 to 1

8
8
8 / 16

1
1
1 or 2

16
16

2
2

16

2
16
32
32

32

2
4
4
4

1
8

1
16

-128 to +127
0 to 255
-128 to +127 or 32768 to
-32768 to +32767
0 to 65535
-32768 to +32767
0 to 65535
-2147483648 to +2147483647
0 to 4294967295
1.175494E-38 to 3.402823E+38
0 or 1
0 to 255
0 to 65535

The bit, sbit, sfr, and sfr16 data types are not provided in ANSI C and are unique
to the Cx51 compiler.
Exercises:
1) Write a program to demonstrate use of ANSI and Cx51 data types.
2) Write a program to read, complement and output a bit to 8051 ports.
3) Write a program to demonstrate the use of sbit in accessing individual bits of
SFR.

Interfacing of Stepper Motor with 8051


1) Write 8051 programs for:
a) Generate wave driving sequence (one phase on
sequence)
b) Generate two phase on sequence
c) Generate half step sequence
Comment on advantages and limitations of above driving
sequences.
2) Considering stepping angle of 1.8o
a) Write a program to rotate a motor by one rotation
clockwise and stop.
b) Write a program to rotate a motor by one rotation
clockwise and anticlockwise continuously.
3) Write a program to rotate motor at different speeds. Use P1.0
to increase the speed and P1.1 to decrease the speed.

Keil IV
Port Programming
Write a program to
1) Configure P1 as input and P2 as an output port.
2) Read a switch connected to P1.0 and indicate switch status
using LED connected to P2.0, using i) bit-wise operation, ii)
byte-wise operation.
3) Generate scrolling LED pattern on P1.
4) Read 3x3 matrix keyboard connected to P1, and output the
number of key pressed to P2.
5) Generate square wave on P1.0.

Keil V
Interrupts
Write a program to
1) configure INT0 edge sensitive, and toggle P1.0 when the
interrupt arrives.
2) configure INT0 level sensitive, and toggle P1.0 when the
interrupt arrives.
Comment on the difference observed in edge v/s level
sensitive responses.
1) configure INT0 edge sensitive, and read P1, compliment and
write P2. Use bank-1 for ISR data, if required.
2) configure INT0 and INT1 both edge sensitive, toggle P1.0
from INT0 ISR, P1.1 from INT1 ISR. Reverse priorities of these
interrupts.
3) configure INT0 and INT1 both edge sensitive, toggle P1.0
from INT0 ISR, P1.1 from INT1 ISR. Use interrupt nesting to
serve INT0 better in real-time.

Keil V
Loop unrolling
1. Simple loop of 16 iterations, no unrolling.
*******************************************************
void main(void){
unsigned char xdata array1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
unsigned char i=0;
for( ;i<16;i++){
P1 = array1[i]+5;
}
while(1);
}
Program Size: data=9.0 xdata=16 code=319 t = 0.00102700sec
*******************************************************
2. Loop unrolled by factor of 2.
*******************************************************
void main(void){
unsigned char xdata array1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
unsigned char i=0;
for( ;i<8;i++){
P1 = array1[i]+5;
i++;
P1 = array1[i]+5;
}
while(1);
}
Program Size: data=9.0 xdata=16 code=338 t = 0.000927 sec
*******************************************************
3. Loop unrolled by factor of 4.
*******************************************************
void main(void){
unsigned char xdata array1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
unsigned char i=0;
for( ;i<4;i++){
P1 = array1[i]+5;
i++;
P1 = array1[i]+5;
i++;
P1 = array1[i]+5;
i++;
P1 = array1[i]+5;
}
while(1);
}
Program Size: data=9.0 xdata=16 code=370 t = 0.000868 sec

Exercise:

Use the above procedure with arrays of 32, 64 and 96 bytes, and
comment on i) Program Size: data and code memories used,
execution time t.

You might also like