-
The 128 bytes of RAM space with address range 00 – 7FH. (In the 8052, it is
256 bytes.) We can read (from) or write (into) this RAM space directly or indi
rectly using the RO and Rl registers as we saw in Chapter 5.
The 64K bytes of code (program) space with addresses of 0000 – FFFFH. This
64K bytes of on-chip ROM space is used for storing programs (opcodes) and
therefore is directly under the control of the program counter (PC). We can use
the “MOVC A, @A+DPTR” Assembly language instruction to access it for
data (see Chapter 5). There are two problems with using this code space for
data. First, since it is ROM memory, we can burn our predefined data and
tables into it. But we cannot write into it during the execution of the program.
The second problem is that the more of this code space we use for data, the less
is left for our program code. For example, if we have an 8051 chip such as
DS89C420 with only 16K bytes of on-chip ROM, and we use 4K bytes of it to
store some look-up table, only 12K bytes is left for the code program. For
some applications this can be a problem. For this reason Intel created another
memory space called external memory especially for data. This is discussed
next very briefly and we postpone the full discussion to Chapter 14.
-
Bank 0 addresses 0-7
-
Individual variables addresses 08 and beyond
-
Array elements addresses right after variables
-
Stack addresses right after array elements
-
You can verify the above order by running Example 7-30 on your 8051 C simulator and examining the contents of the data RAM space. Remember that array elements need contiguous RAM locations and that limits the size of the array due to the fact that we have only 128 bytes of RAM for everything. In the case of Example 7-31 the array elements are limited to around 100. Run Example 7-31 on your 8051 C simulator and examine the RAM space allocation. Keep changing the size of the array and monitor the RAM space to see what happens.
Figure 7-1. RAM Allocation in the 8051
Example 7-30
Compile and single-step the following program on your 8051 simulator. Examine the contents of the 128-byte RAM space to locate the ASCII values.
Run the above program on your 8051 simulator and examine the RAM space to locate values 41H, 42H, 43H, 44H, etc., the hex values for ASCII letters ‘A’, ‘B’, ‘C’, and so on.
Example 7-31
Write, compile, and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the values.
Run the above program on your 8051 simulator and examine the data RAM space to locate values FFH, FEH, FDH, and so on in RAM.
The 8052 RAM data space
Intel added some new features to the 8051 microcontroller and called it the 8052. One of the new features was an extra 128 bytes of RAM space. That means that the 8052 has 256 bytes of RAM space instead of 128 bytes. Remember that the 8052 is code-compatible with the 8051. This means that any program written for the 8051 will run on the 8052, but not the other way around since some fea-
tures of the 8052 do not exist in the 8051. The extra 128 bytes of RAM helps the 8051/52 C compiler to manage its registers and resources much more effectively. Since the vast majority of the new versions of the 8051 such as DS89C4xO are really based on 8052 architecture, you should compile your C programs for the 8052 microcontroller. We do that by (1) using the reg52.h header file, and (2) choosing the 8052 option when compiling the program.
Accessing code data space in 8051 C
In all our 8051 C examples so far, byte-size variables were stored in the 128 bytes of RAM. To make the C compiler use the code space instead of the RAM space, we need to put the keyword code in front of the variable declaration. The following are some examples:
Example 7-32 shows how to use code space for data in 8051 C.
Example 7-32
Compile and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the ASCII values.
Solution:
Run the above program on your 8051 simulator and examine the code space to locate values 41H, 42H, 43H, 44H, etc., the hex values for ASCII characters of’A', ‘B’, ‘C’, and so on.
Compiler variations
Look at Example 7-33. It shows three different versions of a program that sends me string “HELLO” to the PI port. Compile each program with the 8051 C compiler of your choice and compare the hex file size. Then compile each program on a different 8051 C compiler, and examine the hex file size to see the effectiveness of your C compiler. See www.MicroDigitalEd.com for 8051 C compilers.
Example 7-33
Compare and contrast the following programs and discuss the advantages and disadvantages of each one.
Solution:
All the programs send out “HELLO” to PI, one character at a time, but they do it in different ways. The first one is short and simple, but the individual characters are embedded into the program. If we change the characters, the whole program changes. It also mixes the code and data together. The second one uses the RAM data space to store array elements, therefore the size of the array is limited. The third one uses a separate area of the code space for data. This allows the size of the array to be as long as you want if you have the on-chip ROM. However, the more code space you use for data, the less space is left for your program code. Both programs (b) and (c) are easily upgradable if we want to change the string itself or make it longer. That is not the case for program (a).
See the following Web sites for 8051 C compilers:
www.MicroDigitalEd.com
www.8052.com