Seminar 1: “Hello, Embedded World” 1 Using the performance analyzer to test software delays 18 Strengths and weaknesses of software-only delays 19... Seminar 2: Basic hardware foundatio
Trang 1Programming Embedded Systems I A 10-week course, using C
40 39 38 37 36 35 34
1 2 3 4 5 6 7
33 32 31 30 29 28 27 26 25 24
11 12 13 14 15 16 17 18 19 20
23 22 21
P3.0
P1.7 RST P1.6 P1.5 P1.4
P1.2 P1.3 P1.1 P1.0
VSS
XTL2 XTL1 P3.7 P3.6 P3.5
P3.3 P3.4 P3.2 P3.1
/ EA
P0.6 P0.7 P0.5 P0.4 P0.3
P0.1 P0.2 P0.0 VCC
P2.0
P2.2 P2.1 P2.3 P2.4 P2.5
P2.7 P2.6 / PSEN ALE
Trang 2Copyright © Michael J Pont, 2002-2006
This document may be freely distributed and copied, provided that copyright notice at the foot of each OHP page is clearly visible in all copies
Trang 3Seminar 1: “Hello, Embedded World” 1
Using the performance analyzer to test software delays 18
Strengths and weaknesses of software-only delays 19
Trang 4Seminar 2: Basic hardware foundations (resets, oscillators and port I/O) 21
Improving the stability of a crystal oscillator 31
Driving a low-power load without using a buffer 39
Trang 5Seminar 3: Reading Switches 45
Review: Basic techniques for reading from port pins 47
Example: Reading and writing bits (simple version) 49
Example: Reading and writing bits (generic version) 51
Trang 6Example: Re-structuring the Goat-Counting Example 104
Trang 8Seminar 6: Creating an Embedded Operating System 139
Timer-based interrupts (the core of an embedded OS) 144
Trang 9Seminar 7: Multi-State Systems and Function Sequences 177
Implementing a Multi-State (Input/Timed) system 195
Trang 10Seminar 8: Using the Serial Interface 211
Using the on-chip U(S)ART for RS-232 communications 219
RS-232 and 8051: Overall strengths and weaknesses 225
Trang 11Seminar 9: Case Study: Intruder Alarm System 241
Trang 12Seminar 10: Case Study: Controlling a Mobile Robot 263
Trang 13C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
1 2 3 4 5 6
7 A
8 9 10
13 12 11
GND
P3.4 P3.5 P3.3 P3.2 XTL1
P3.1 XTL2 P3.0 RST
P3.7
P1.1 P1.0 P1.2 P1.3 P1.4
P1.6 P1.5 P1.7 VCC
Trang 14C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Overview of this seminar
This introductory seminar will:
• Provide an overview of this course
• Introduce the 8051 microcontroller
• Present the “Super Loop” software architecture
• Describe how to use port pins
• Consider how you can generate delays (and why you might
need to)
Trang 15C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Overview of this course
This course is concerned with the implementation of software (and a small amount of hardware) for embedded systems constructed using
a single microcontroller
The processors examined in detail are from the 8051 family
(including both ‘Standard’ and ‘Small’ devices)
All programming is in the ‘C’ language
Trang 16C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
By the end of the course …
By the end of the course, you will be able to:
1 Design software for single-processor embedded applications
based on small, industry standard, microcontrollers;
2 Implement the above designs using a modern, high-level
programming language (‘C’), and
3 Begin to understand issues of reliability and safety and how
software design and programming decisions may have a
positive or negative impact in this area
Trang 17C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Main course textbook
Throughout this course, we will be making heavy use of this book:
Trang 18C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Why use C?
• It is a ‘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 may not be an perfect language for developing embedded
systems, but it is a good choice (and is unlikely that a ‘perfect’ language will ever be created)
Trang 19C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Pre-requisites!
• Throughout this course, it will be assumed that you have had
previous programming experience: this might be in - for
example - Java or C++
• For most people with such a background, “getting to grips”
with C is straightforward
Trang 20C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Typical features of a modern 8051:
• Thirty-two input / output lines
• Internal data (RAM) memory - 256 bytes
• Up to 64 kbytes of ROM memory (usually flash)
• Three 16-bit timers / counters
• Nine interrupts (two external) with two priority levels
• Low-power Idle and Power-down modes
The different members of this family are suitable for everything from automotive and aerospace systems to TV “remotes”
Trang 21C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Crucially, the ‘super loop’, or ‘endless loop’, is required because we
have no operating system to return to: our application will keep looping until the system power is removed
Trang 22C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Strengths and weaknesseses of “super loops”
☺ The main strength of Super Loop systems is their simplicity This
makes them (comparatively) easy to build, debug, test and maintain
☺ Super Loops are highly efficient: they have minimal hardware
resource implications
☺ Super Loops are highly portable
BUT:
If your application requires accurate timing (for example, you need to
acquire data precisely every 2 ms), then this framework will not
provide the accuracy or flexibility you require
The basic Super Loop operates at ‘full power’ (normal operating mode)
at all times This may not be necessary in all applications, and can have a dramatic impact on system power consumption
[As we will see in Seminar 6, a scheduler can address these
problems.]
Trang 23C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Example: Central-heating controller
Central heating controller
Boiler
Temperature sensor
Temperature dial
/* 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) */
Trang 24C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
The Standard 8051s have four 8-bit ports
All of the ports are bidirectional: that is, they may be used for both input and output
Trang 25C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
NOTE: 0x means that the number format is HEXADECIMAL
- see Embedded C, Chapter 2
Trang 26C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
unsigned char Port_data;
Port_data = 0x0F;
P1 = Port_data; /* Write 00001111 to Port 1 */
Similarly, we can read from (for example) Port 1 as follows:
unsigned char Port_data;
P1 = 0xFF; /* Set the port to ‘read mode’ */
Port_data = P1; /* Read from the port */
Note that, in order to read from a pin, we need to ensure that the last thing written to the pin was a ‘1’
Trang 27C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Creating and using sbit variables
To write to a single pin, we can make use of an sbit variable in the Keil (C51) compiler to provide a finer level of control
Here’s a clean way of doing this:
Trang 28C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Example: Reading and writing bytes
The input port
The output port
void main (void)
{
unsigned char Port1_value;
/* Must set up P1 for reading */
Trang 29C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Trang 30C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Using the performance analyzer to test software delays
Trang 31C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Strengths and weaknesses of software-only delays
☺ SOFTWARE DELAY can be used to produce very short delays
☺ SOFTWARE DELAY requires no hardware timers
☺ SOFTWARE DELAY will work on any microcontroller
BUT:
It is very difficult to produce precisely timed delays
The loops must be re-tuned if you decide to use a different processor,
change the clock frequency, or even change the compiler optimisation settings
Trang 32C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Preparation for the next seminar
In the lab session associated with this seminar, you will use a
hardware simulator to try out the techniques discussed here This will give you a chance to focus on the software aspects of embedded systems, without dealing with hardware problems
In the next seminar, we will prepare to create your first test systems
on “real hardware”
Please read Chapters 1, 2 and 3 before the next seminar
Trang 33C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Seminar 2:
Basic hardware foundations (resets, oscillators and port
I/O)
Atmel89C52
XTAL 1
DS1812
12 MHz
Trang 34C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Typical features of a modern 8051:
• Thirty-two input / output lines
• Internal data (RAM) memory - 256 bytes
• Up to 64 kbytes of ROM memory (usually flash)
• Three 16-bit timers / counters
• Nine interrupts (two external) with two priority levels
• Low-power Idle and Power-down modes
The different members of this family are suitable for everything from automotive and aerospace systems to TV “remotes”
Trang 35C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Review: Central-heating controller
Central heating controller
Boiler
Temperature sensor
Temperature dial
/* 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) */
Trang 36C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Overview of this seminar
This seminar will:
• Consider the techniques you need to construct your first
“real” embedded system (on a breadboard)
Specifically, we’ll look at:
• Oscillator circuits
• Reset circuits
• Controlling LEDs
Trang 37C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
• If the oscillator fails, the system will not function at all
• If the oscillator runs irregularly, any timing calculations
performed by the system will be inaccurate
Trang 38C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
• A variant of the Pierce oscillator is common in the 8051
family To create such an oscillator, most of the components
are included on the microcontroller itself
• The user of this device must generally only supply the crystal
and two small capacitors to complete the oscillator
implementation
Trang 39C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
XTAL
In the absence of specific information, a capacitor value of
30 pF will perform well in most circumstances
Trang 40C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Oscillator frequency and machine cycle period
• In the original members of the 8051 family, the machine
cycle takes twelve oscillator periods
• In later family members, such as the Infineon C515C, a
machine cycle takes six oscillator periods; in more recent
devices such as the Dallas 89C420, only one oscillator period
is required per machine cycle
• As a result, the later members of the family operating at the
same clock frequency execute instructions much more
rapidly
Trang 41C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Keep the clock frequency as low as possible
Many developers select an oscillator / resonator frequency that is at
or near the maximum value supported by a particular device
This can be a mistake:
• Many application do not require the levels of performance
that a modern 8051 device can provide
• The electromagnetic interference (EMI) generated by a
circuit increases with clock frequency
• In most modern (CMOS-based) 8051s, there is an almost
linear relationship between the oscillator frequency and the
power-supply current As a result, by using the lowest
frequency necessary it is possible to reduce the power
requirement: this can be useful in many applications
• When accessing low-speed peripherals (such as slow
memory, or LCD displays), programming and hardware
design can be greatly simplified - and the cost of peripheral
components, such as memory latches, can be reduced - if the
chip is operating more slowly
In general, you should operate at the lowest possible oscillator
frequency compatible with the performance needs of your application
Trang 42C OPYRIGHT © M ICHAEL J P ON T , 2001-2006 Contains material from:
Pont, M.J (2002) “Embedded C”, Addison-Wesley.
Stability issues
• A key factor in selecting an oscillator for your system is the
issue of oscillator stability In most cases, oscillator stability
is expressed in figures such as ‘±20 ppm’: ‘20 parts per
million’
• To see what this means in practice, consider that there are
approximately 32 million seconds in a year In every million
seconds, your crystal may gain (or lose) 20 seconds Over
the year, a clock based on a 20 ppm crystal may therefore
gain (or lose) about 32 x 20 seconds, or around 10 minutes
Standard quartz crystals are typically rated from ±10 to ±100 ppm, and
so may gain (or lose) from around 5 to 50 minutes per year