When designing software for a smaller embedded system with the 8051, it is very commonplace to develop the entire product using assembly code. With many projects, commonplace to develop the entire product using assembly code. With many projects, this is a feasible approach since the amount of code that must be generated is typically this is a feasible approach since the amount of code that must be generated is typically less than 8 kilobytes and is relatively simple in nature. If a hardware engineer is tasked less than 8 kilobytes and is relatively simple in nature. If a hardware engineer is tasked with designing both the hardware and the software, he or she will frequently be tempted with designing both the hardware and the software, he or she will frequently be tempted to write the software in assembly language.
to write the software in assembly language.
The trouble with projects done with assembly code
The trouble with projects done with assembly code can is that they can be can is that they can be difficultdifficult to read and maintain, especially if they are not well commented. Additionally, the amount to read and maintain, especially if they are not well commented. Additionally, the amount of code reusable from a typical assembly language project is usually very low. Use of a of code reusable from a typical assembly language project is usually very low. Use of a higher-level language like C can directly address these issues. A program written in C is higher-level language like C can directly address these issues. A program written in C is easier to read than an assembly program.
easier to read than an assembly program.
Since a C program possesses greater structure, it is easier to understand and Since a C program possesses greater structure, it is easier to understand and maintain. Because of its modularity, a C program can better lend itself to reuse of code maintain. Because of its modularity, a C program can better lend itself to reuse of code from project to project. The division of code into functions will force better structure of from project to project. The division of code into functions will force better structure of the software and lead to functions that can be taken from one project and used in another, the software and lead to functions that can be taken from one project and used in another, thus reducing overall development time. A high order language such as C allows a thus reducing overall development time. A high order language such as C allows a developer to write code, which resembles a human's thought process more closely than developer to write code, which resembles a human's thought process more closely than does the equivalent assembly code. The developer can focus more time on designing the does the equivalent assembly code. The developer can focus more time on designing the al
algogoririththms ms of of ththe e sysyststem em rarathther er ththan an hahaviving ng to to coconcencentntrarate te on on ththeieir r inindidivividuaduall implementation. This will greatly reduce development time and lower debugging time implementation. This will greatly reduce development time and lower debugging time since the code is more understandable.
since the code is more understandable.
By using a language like C, the programmer does not have to be intimately By using a language like C, the programmer does not have to be intimately familiar with the architecture of the processor. This means that someone new to a given familiar with the architecture of the processor. This means that someone new to a given processor can get a project up and running quicker, since the internals and organization of processor can get a project up and running quicker, since the internals and organization of the target processor do not have to be learned. Additionally, code developed in C will be the target processor do not have to be learned. Additionally, code developed in C will be more portable to other systems than code developed in assembly. Many target processors more portable to other systems than code developed in assembly. Many target processors have C compilers available, which
have C compilers available, which support ANSI C.support ANSI C.
All of this is not to say that assembly language does not have its place. In fact, All of this is not to say that assembly language does not have its place. In fact, many embedded systems (particularly real time systems) have a combination of C and many embedded systems (particularly real time systems) have a combination of C and assembly code. For time critical operations, assembly code is frequently the only way to assembly code. For time critical operations, assembly code is frequently the only way to go. One of the
go. One of the great things about the C language great things about the C language is that it allows you to perform low-levelis that it allows you to perform low-level manipulations of the hardware if need be, yet provides you with the functionality and manipulations of the hardware if need be, yet provides you with the functionality and abstraction of a higher order language.
abstraction of a higher order language.
KEIL C [EMBEDDED C] VS ANSI C:
KEIL C [EMBEDDED C] VS ANSI C:
The Keil compiler provides the user with a superset of ANSI C with a few key The Keil compiler provides the user with a superset of ANSI C with a few key differences. For the most part these differences allow the user to take advantage of the differences. For the most part these differences allow the user to take advantage of the architecture of the 8051. Add
architecture of the 8051. Additional differences are due to limitations of the 8051.itional differences are due to limitations of the 8051.
Data Types & SFR’S following table shows the standard data types and the number of bytes they take on the following table shows the standard data types and the number of bytes they take on the 8051. It should be noted that integers and longs are stored with the most significant byte 8051. It should be noted that integers and longs are stored with the most significant byte in the lower address (MSB first).
in the lower address (MSB first).
In addition to these standard data types the compiler supports a bit data type. A In addition to these standard data types the compiler supports a bit data type. A variable of type 'bit' is allocated from the bit addressable segment of internal RAM and variable of type 'bit' is allocated from the bit addressable segment of internal RAM and can have a value of either one or zero. Bit scalars can be operated on in a manner similar can have a value of either one or zero. Bit scalars can be operated on in a manner similar to other data types. Their type is promoted for operations with higher data types such as to other data types. Their type is promoted for operations with higher data types such as char or int. Arrays of bits and pointers to
char or int. Arrays of bits and pointers to bit variables are not allowed.bit variables are not allowed.
The special function registers of the 8051 are declared using the type specifier The special function registers of the 8051 are declared using the type specifier 'sfr' for an eight-bit register or 'sfr16' for a 16-bit register such as DPTR. In these 'sfr' for an eight-bit register or 'sfr16' for a 16-bit register such as DPTR. In these declarations, the name and the address of the SFR are provided in the code. The address declarations, the name and the address of the SFR are provided in the code. The address must be greater than 80
must be greater than 80 hex. Bits of the bit addressable SFRs can be hex. Bits of the bit addressable SFRs can be declared by using thedeclared by using the 'sbit' type. This type cannot be applied
'sbit' type. This type cannot be applied to any SFR, which is not normally bit addressable.to any SFR, which is not normally bit addressable.
D
DaattaaTTyyppee SSiizzee
C
Chhaar r / / uunnssiiggnneed d cchhaarr 8 8 bbiittss
IInnt t / / uunnssiiggnneed d iinntt 116 6 bbiittss
L
Loonng g / / uunnssiiggnneed d lloonngg 332 2 bbiittss
F
Fllooaat t / / ddoouubbllee 332 2 bbiittss
G
Geenneerriic c ppooiinntteerr 224 4 bbiittss
Table 4.1. Data types Table 4.1. Data types
For most of the 8051 family members, Keil provides a header file, which defines For most of the 8051 family members, Keil provides a header file, which defines all the SFRs and their bits. Using one of the existing header files as a model can easily all the SFRs and their bits. Using one of the existing header files as a model can easily create headers for new derivatives.
create headers for new derivatives.
E.g.: sfr SCON = 0x98; // declare SCON E.g.: sfr SCON = 0x98; // declare SCON Memory Types
Memory Types
Keil C allows the user to specify the memory area that will be used to hold Keil C allows the user to specify the memory area that will be used to hold program variables. This allows for user control over the utilization of certain memory program variables. This allows for user control over the utilization of certain memory
areas. The compiler recognizes the following memory areas.
areas. The compiler recognizes the following memory areas.
DATA SEGMENT: provides the most efficient access to user variables and for DATA SEGMENT: provides the most efficient access to user variables and for this reason should be used to hold frequently accessed data.
this reason should be used to hold frequently accessed data.
BDATA SEGMENT: allows you declare a variable that will be placed in the bit BDATA SEGMENT: allows you declare a variable that will be placed in the bit addressable segment of DATA memory.
addressable segment of DATA memory.
IDATA SEGMENT: allows indirect access of locations.
IDATA SEGMENT: allows indirect access of locations.
PDATA and XDATA SEGMENTS: Declarations of variables in either of these PDATA and XDATA SEGMENTS: Declarations of variables in either of these two segments follows the same syntax as the other
two segments follows the same syntax as the other memory segments did.memory segments did.
CODE SEGMENT: The CODE segment should only be used for data, which will CODE SEGMENT: The CODE segment should only be used for data, which will not change, since the 8051 does not have the capability to write to the CODE segment.
not change, since the 8051 does not have the capability to write to the CODE segment.
M
Meemmoorry y AArreeaa DDeessccrriippttiioonn
D
DAATTAA TThhe e lloowweer r 11228 8 bbyyttees s oof f iinntteerrnnaall R
RAMAM. . AlAll l llococatatiionons s cacan n be be acaccecesssseded directly and within one processor cycle.
directly and within one processor cycle.
B
BDDAATTAA TThhe e 116 6 bbyyttees s oof f bbiit t aaddddrreessssaabbllee locations in the DATA segment.
locations in the DATA segment.
IIDDAATTAA TThhe e uuppppeer r 11228 8 bbyyttees s oof f iinntteerrnnaall RA
RAM M avaavaililablable e on on dedeviviceces s susuch ch as as ththee 8052. All locations in this segment must be 8052. All locations in this segment must be accessed indirectly.
accessed indirectly.
P
PDDAATTAA TThhe e 22556 6 bbyyttees s oof f eexxtteerrnnaal l mmeemmoorryy that are accessed by an address placed on that are accessed by an address placed on P0. Any access to this segment takes two P0. Any access to this segment takes two cycles and is done via a
cycles and is done via a MOVX @RnMOVX @Rn Command.
Command.
X
XDDAATTAA EExxtteerrnnaal l mmeemmoorryy, , wwhhiicch h mmuusst t bbee accessed via the DPTR.
accessed via the DPTR.
C
COODDEE PPrrooggrraam m mmeemmoorryy, , wwhhiicch h mmuusst t bbee accessed via the DPTR.
accessed via the DPTR.
Table 4.2 Memory areas Table 4.2 Memory areas THINGS TO DO AND THINGS TO AVOID WITH KEIL C THINGS TO DO AND THINGS TO AVOID WITH KEIL C
Downsizing Your Variables Downsizing Your Variables
On an eight-bit machine like the 8051, wide use of data types whose size is On an eight-bit machine like the 8051, wide use of data types whose size is greater than eight bits will be a large waste of processing power and memory. Obviously, greater than eight bits will be a large waste of processing power and memory. Obviously, the most preferred type for variables will be unsigned
the most preferred type for variables will be unsigned char since it only uses one byte.char since it only uses one byte.
Use Unsigned Types Use Unsigned Types
The reasoning behind this is that the 8051 does not support signed arithmetic, and The reasoning behind this is that the 8051 does not support signed arithmetic, and the extra code required by a signed value as opposed to an unsigned value will take away the extra code required by a signed value as opposed to an unsigned value will take away from your overall processor resources.
from your overall processor resources.
Stay Away from Floating Point Stay Away from Floating Point
Floating-point applications take a lot of time to execute. One is better off to avoid Floating-point applications take a lot of time to execute. One is better off to avoid performing floating-point calculations.
performing floating-point calculations.
Make Use of bit Variables Make Use of bit Variables
When you are using flags, which will only contain a one or a zero, use the bit type When you are using flags, which will only contain a one or a zero, use the bit type instead of an unsigned char. This will help make your memory reserves go farther, since instead of an unsigned char. This will help make your memory reserves go farther, since you will not be
you will not be wasting seven bits. Additionally, bit variables are always in wasting seven bits. Additionally, bit variables are always in internal RAMinternal RAM and therefore will be accessed in one cycle.
and therefore will be accessed in one cycle.
Use Locals instead of Globals Use Locals instead of Globals
Variables, which are declared to be global data, will be less efficient than use of Variables, which are declared to be global data, will be less efficient than use of local variables. The reason for this is that the compiler will attempt to assign any local local variables. The reason for this is that the compiler will attempt to assign any local variables to internal registers. In comparison, global data may or may not be in internal variables to internal registers. In comparison, global data may or may not be in internal data depending on your declaration. In cases where the global are assigned to XDATA by data depending on your declaration. In cases where the global are assigned to XDATA by default (such as large memory model programs) you have
default (such as large memory model programs) you have given up speedy access.given up speedy access.
Use Internal Memory for Variables Use Internal Memory for Variables
Globals and locals can be forced into any memory area. We can optimize the Globals and locals can be forced into any memory area. We can optimize the speed of the program by placing the most frequently accessed variables in internal RAM.
speed of the program by placing the most frequently accessed variables in internal RAM.
Additionally, code will be smaller since it takes less instructions and setup to access Additionally, code will be smaller since it takes less instructions and setup to access variables in internal RAM than it does to access variables in external RAM.
variables in internal RAM than it does to access variables in external RAM.
Use Macros Instead of Functions Use Macros Instead of Functions
In addition to using intrinsics, you can also make your code more readable by In addition to using intrinsics, you can also make your code more readable by implementing small operations such as a read from a latch or code to enable a certain implementing small operations such as a read from a latch or code to enable a certain circui
circuit as t as macromacros. Instead of duplicatins. Instead of duplicating g one or one or two lines of code two lines of code all over the all over the place, youplace, you can isolate duplicate code into a macro, which will then look like a function call. The can isolate duplicate code into a macro, which will then look like a function call. The compiler will place the code inline when it emits object code and will not generate a call.
compiler will place the code inline when it emits object code and will not generate a call.
The code will be much easier to read and maintain since the macro can be given a name, The code will be much easier to read and maintain since the macro can be given a name, which describes its operation
which describes its operation
4.1.
4.1. EXPRE EXPRESS SS PCB PCB
Express PCB is a very easy to use Windows application for laying out printed Express PCB is a very easy to use Windows application for laying out printed circuit boards. This software is used for PCB designing.
circuit boards. This software is used for PCB designing.
4.3.1 Beginning a New Layout 4.3.1 Beginning a New Layout
1. Begin a new layout by running Express PCB. Open a new file. If you are 1. Begin a new layout by running Express PCB. Open a new file. If you are designing a four-layer board, select Board properties from the Layout menu and check designing a four-layer board, select Board properties from the Layout menu and check the 4-Layer option.
the 4-Layer option.
2. In the main window, the yellow rectangle defines the perimeter of the PC 2. In the main window, the yellow rectangle defines the perimeter of the PC board.
board. Set The size of your boaSet The size of your board by moving thrrd by moving three of its four corneee of its four corners (the upper lefrs (the upper leftt corner i
corner is fixes fixed d at (0, 0)at (0, 0). . Move thMove the cornere corners by dras by dragging thgging them with em with the mousthe mouse, or bye, or by
double-double-clickclicking them and entering coording them and entering coordinateinates. s. AdditAdditional cornerional corners can be s can be added to theadded to the perimeter to change its shape.
perimeter to change its shape.
3. Save the file.
3. Save the file.