Emd C Module1 22 April 24
Emd C Module1 22 April 24
1
OUTCOME OF MODULE 1
2
Module - 1
Programming Embedded Systems in C:
Introduction ,
What is an embedded system,
Which processor should you use,
Which programming language should you use,
Which operating system should you use,
How do you develop embedded software,
Conclusions.
3
Introduction
Consider some important decisions that must be made at the start of any embedded project:
The choice of processor.
The choice of programming language.
The choice of operating system.
What is an embedded system?
An embedded system is an application that contains at least one programmable computer (typically in the form
of a microcontroller, a microprocessor or digital signal processor chip) and which is used by individuals who are,
in the main, unaware that the system is computer-based.
Typical examples of embedded applications:
Mobile phone systems (including both customer handsets and base stations).
Automotive applications (including braking systems, traction control, airbag release systems, engine-
management units, steer-by-wire systems and cruisecontrol applications).
Domestic appliances (including dishwashers, televisions, washing machines, microwave ovens, video
recorders, security systems, garage door controllers).
Aerospace applications (including flight control systems, engine controllers, autopilots and passenger in-flight
entertainment systems).
Medical equipment (including anaesthesia monitoring systems, ECG monitors, drug delivery systems and MRI
scanners).
Defence systems (including radar systems, fighter aircraft flight control systems, radio systems and missile
guidance systems).
4
Which processor should you use?
Typical desktop processors cost more than US $100.00 a piece (often much more).
In addition, a desktop processor requires numerous external support chips in order to function: this further
increases the cost.
The additional components also increase the physical size of the system, and the power consumption: both of
these factors are major problems for battery-powered embedded devices.
Overall, the state-of-the art technology used in desktop processors matches the needs of the PC user very well:
however, their key features – an ability to execute industry-standard code at a rate of more than 1000 million
instructions per second – come with a heavy price tag and are simply not required in most embedded systems.
The 8051 device is very different. It is a well-tested design, introduced in its original form by Intel in 1980
5
Which processor should you use? Continued....
Performance of around 1 million instructions per second, and 256 bytes of on-chip RAM and also 32 port pins
and a serial interface.
The 8051’s profile (price, performance, available memory, serial interface) match the needs of many embedded
systems very well. As a result, it is now produced in more than 400 different forms by a diverse range of companies
including Philips, Infineon, Atmel and Dallas.
Versions of the 8051 are currently used in a long list of embedded products, from children’s toys to automotive
systems.
8051 is an excellent device for building many embedded systems. One important factor is that the 8051 requires
a minimum number of external components in order to operate.
For example, Figure 1.2 shows the circuit diagram for a complete 8051- based application.
6
Which processor should you use? Continued....
Small 8051 based devices typically have only some 15 I/O pins, and do not support external memory. These
devices are finding their way into applications that would have involved a small number of discrete components
(transistors, diodes, resistors, capacitors).
An example of a ‘Small 8051’ in use. Small 8051s are produced by Atmel and Philips.
They have two ports (or less), and around 20 pins. In this case, an Atmel AT89C2051 is
used, in a ‘cupboard light’ application.
The circuit here is intended for use in a cupboard (closet), and will be battery powered.
When the light is switched on (by pressing the switch), it will operate for 20 seconds. If, in
this time, the user does not press the switch again (to turn off the light), the power will be
removed automatically. This is a typical example of a very simple product that may now
be economically produced using a microcontroller.
Both the Standard and Small 8051s are aimed, largely, at low-performance application areas, where limited
memory is required, and one of the most important considerations is product cost.
To develop applications requiring additional hardware or larger amounts of memory, we can opt to switch to a 16-
bit (or 32-bit) microcontroller environment – or even consider using a desktop microprocessor.
7
Which processor should you use? Continued....
One important application area for Extended 8051s has been the automotive sector. Recent economic, legislative
and technological developments in this sector mean that an increasing number of road vehicles contain embedded
systems.
Linking these systems together in many recent vehicles is a low-cost, two-wire Controller Area Network (CAN)
computer bus. The CAN bus eliminates the expensive (and heavy) multi-wire looms, saving more from production
costs: which is a significant saving.
An example of an Extended 8051 device. Extended 8051s have
additional on-chip facilities, and additional port pins.
In the case of the Infineon C515C (shown here), the additional
facilities include support for the ‘Controller Area Network’
(CAN) bus: this bus is widely used in the automotive sector and
in industrial environments.
8
Which processor should you use? Continued....
In order to connect to the CAN bus, the various devices – from door mirrors to braking systems – each require an
embedded processor. As a consequence, a modern passenger car may typically have 50 processors on board.
To use 50 desktop chips in these circumstances, we would need to spend around US $5000. This cost greatly
outweighs any saving achieved through the use of the CAN bus in the first place: in addition, the desktop
processor would not have on-chip support for CAN, so additional hardware would be needed in order to provide
this, further increasing our costs and design complexity.
As an alternative, various Extended 8051s have on-chip hardware support for CAN (see Figure). The use of 50
such chips in a car design would not generally cost more than US $200. Using these processors, the switch to CAN
may result in overall savings of around US $400 per vehicle.
Overall, the low cost, huge range, easy availability and widespread use of the 8051 architecture makes it an
excellent platform for developing embedded systems: these same factors also make it an ideal platform for
learning about embedded systems. Whether you will subsequently use 8-, 16- or 32-bit embedded processors,
learning to work within the performance and memory limits of devices such as the 8051 is a crucial requirement
in the cost-conscious embedded market.
9
Which programming language should you use?
Having decided to use an 8051 processor as the basis of embedded system, the next key decision that needs to be
made is the choice of programming language.
In order to identify a suitable language for embedded systems, we might begin by making the following
observations:
Computers (such as microcontroller, microprocessor or DSP chips) only accept instructions in ‘machine code’
(‘object code’). Machine code is, by definition, in the language of the computer, rather than that of the
programmer. Interpretation of the code by the programmer is difficult and error prone.
All software, whether in assembly, C, C++, Java or Ada must ultimately be translated into machine code in
order to be executed by the computer.
There is no point in creating ‘perfect’ source code, if we then make use of a poor translator program (such as
an assembler or compiler) and there by generate executable code that does not operate as we intended.
Embedded processors – like the 8051 – have limited processor power and very limited memory available:
the language used must be efficient.
To program embedded systems, we need low-level access to the hardware: this means, at least, being able to
read from and write to particular memory locations.
10
Which programming language should you use? Continued...
Of course, not all of the issues involved in language selection are purely technical: (Non Technical Issues)
No software company remains in business for very long if it generates new code, from scratch, for every
project.
The language used must support the creation of flexible libraries, making it easy to re-use (well-tested) code
components in a range of projects. It must also be possible to adapt complete code systems to work with a
new or updated processor with minimal difficulty.
Good code must be easy to understand now, and in five years’ time (and not just by those who first wrote it).
The language chosen should be in common use. This will ensure that you can continue to recruit experienced
developers who have knowledge of the language. It will also mean that your existing developers will have
access to sources of information (such as books, training courses, WWW sites) which give examples of good
design and programming practice.
The real code needs to be maintained and re-used in new projects, possibly on different hardware. All we can
really say is that we require a language that is efficient, high-level, gives low-level access to hardware, and is
well defined.
In addition – of course – the language must be available for the platforms we wish to use. Against all of these
points, C scores well.
11
Which programming language should you use? Continued...
We can summarize C’s features as follows:
It is ‘mid-level’, with ‘high-level’ features (such as support for functions and modules), and ‘low-level’ features
(such as good access to hardware via pointers).
It is very efficient.
It is popular and well understood.
Even desktop developers who have used only Java or C++ can soon understand C syntax.
Good, well-proven compilers are available for every embedded processor (8-bit to 32-bit or more).
Experienced staff are available.
Books, training courses, code samples and WWW sites discussing the use of the language are all widely
available.
Overall, C’s strengths for embedded system development greatly outweigh its weaknesses. It may not be an ideal
language for developing embedded systems, but it is unlikely that a ‘perfect’ language will ever be created.
12
Which operating system should you use? Continued...
Let us consider some of the differences between software development for desktop and embedded
systems.
In the desktop environment, the program the user requires (such as a word processor program) is usually loaded
from disk on demand, along with any required data (such as a word processor file). \
Figure 1.5 shows a typical operating environment for such a word processor. Here the system is well insulated
from the underlying hardware.
13
Which operating system should you use? Continued...
For example, when the user wishes to save his or her latest novel on disk, the word processor delegates most of
the necessary work to the operating system, which in turn may delegate many of the hardware-specific commands
to the BIOS (basic input/output system).
The desktop PC does not require an operating system (or BIOS). However, for most users, the main advantage of a
personal computer is its flexibility: that is, that the same piece of equipment has the potential to run many
thousands of different programs.
If the PC had no operating system, each of these programs would need to be able to carry out all the low-level
functions for itself. This would be very inefficient and would tend to make systems more expensive.
A desktop PC is used to run multiple programs, and the operating system provides the ‘common code’ (for
printing, file storage, graphics, and so forth) that is required by this set of programs.
There are two fundamental differences between the embedded systems and desktop computer systems:
1. The vast majority of embedded systems are required to run only one program: this program will start
running when the microcontroller is powered up, and will stop running when the power is removed.
2. Many of the facilities provided by the modern desktop OS – such as the ability to display high-resolution
graphics, printing facilities and efficient disk access – are of little value in embedded systems, where sophisticated
graphics screens, printers and disks are generally unavailable.
14
Which operating system should you use? Continued...
As a consequence, the simplest architecture in an embedded system is typically a form of ‘Super Loop’
It is important to appreciate that there is no operating system in use here. When power is applied to the system,
the function main() will be called: having performed the initializations, the function X() will be called, repeatedly,
until the system is disconnected from the power supply (or a serious error occurs).
15
Which operating system should you use? Continued...
For example, suppose we wish to develop a microcontroller-based control system to be used as part of the central-
heating system in a building.
The simplest version of this system might consist of a gas-fired boiler (which we wish to control), a sensor
(measuring room temperature), a temperature dial (through which the desired temperature is specified) and the
controller (Figure ).
A Super Loop framework similar to that shown in Listing
/*Main.C Framework for a central heating system using a Super Loop. */
#include "Cen_Heat.h“
void main(void)
{
C_HEAT_Init(); // Init the system
while(1) // 'for ever' (Super Loop)
{
// Find out what temperature the user requires
// (via the user interface)
C_HEAT_Get_Required_Temperature();
// Find out what the current room temperature is
// (via temperature sensor)
C_HEAT_Get_Actual_Temperature();
// Adjust the gas burner, as required
C_HEAT_Control_Boiler();
}
}
16
How do you develop embedded software?
In the embedded environment for example, the 8051 devices do not have sufficient memory resources to allow
them to be used for compiling programs, and they will not support a keyboard or graphics display.
As a result, the code will be ‘cross-compiled’ on a desktop PC, generating machine code that is compatible with
the 8051 family.
Having created the required executable code, we need to test it and refine it. To do this, we need to do the
following:
1. Build the hardware for the embedded system.
2. Transfer the executable code to the embedded hardware and test the system.
A typical approach used to prototype small embedded applications is the ‘breadboard’. This allows the
microcontroller and associated components to be connected together, without soldering, in order to test and
refine the hardware and software design.
The process of wiring up a breadboard to test the first simple embedded programs can be daunting, expensive
and rather prone to error. Fortunately, there is now an alternative approach that can be used by those starting out
in this area:
1. Create the executable code for the embedded system on a desktop PC using an appropriate cross-
compiler and related tools (as above).
2.Use a software simulator (running on the desktop PC) to test the code.
3. Repeat Step 1 and Step 2, as necessary, until the software operates as required.
17
Conclusions
In this introductory chapter, we have considered:
The type of embedded systems that will be discussed in this book.
The choice of programming language for embedded systems.
The choice of operating system for embedded systems.
The process of creating executable code for an embedded processor on a desktop PC.
The process of testing the embedded code.
18
Module-1 Questions
1.What is an embedded system? Explain with examples.
2. Explain Standard’ ‘8051’ microcontroller (40-pin DIP package) and Extended 8051 device (80pin) (quad
package) with pin diagrams.
3.Discuss the simple application example of 8051 microcontroller in use(circuit diagram). In this example, the
microcontroller is intended to flash an LED connected to Pin 6.
4. What are the technical and non technical observations required in order to identify a suitable language for
embedded systems development.
5.Discuss the features of C Programming Language available for the platforms we wish to use.
6. Draw the schematic representation of the BIOS/OS sandwich from a desk-bound computer system running
some word processor software.
7.Expalin two fundamental differences between the embedded systems and desktop computer systems.
8.Expalin‘Super Loop’ architecture for an embedded system.
9.With the code an figure explain a simple central-heating system.
10.Discuss the development of embedded software.