Showing posts with label Embedded System. Show all posts
Showing posts with label Embedded System. Show all posts

Reading Switches


• Embedded systems usually use switches as part of their user
interface.
• This general rule applies from the most basic remote-control
system for opening a garage door, right up to the most
sophisticated aircraft autopilot system.
• Whatever the system you create, you need to be able to
create a reliable switch interface.









In this seminar, we consider how you can read inputs from
mechanical switches in your embedded application.
Before considering switches themselves, we will consider the
process of reading the state of port pins.



Review: Basic techniques for reading from port pins

We can send some data to Port 1 as follows:
sfr P1 = 0x90; /* Usually in header file */
P1 = 0x0F; /* Write 00001111 to Port 1 */
In exactly the same way, we can read from Port 1 as follows:
unsigned char Port_data;
P1 = 0xFF; /* Set the port to ‘read mode’ */
Port_data = P1; /* Read from the port */


Driving a single digit


• In most cases, we require some form of buffer or driver IC
between the port and the MS LED.
• For example, we can use UDN2585A.


Each of the (8) channels in this buffer can simultaneously
source up to 120 mA of current (at up to 25V): this is
enough, for example, for even very large LED displays.







Note that this is an inverting (current source) buffer. Logic 0
on the input line will light the corresponding LED segment.



What is a multi-segment LED?


Multiple LEDs are often arranged as multi-segment displays:
combinations of eight segments and similar seven-segment displays
(without a decimal point) are particularly common.




Such displays are arranged either as ‘common cathode’ or ‘common
anode’ packages:





The required current per segment varies from about 2 mA (very
small displays) to about 60 mA (very large displays, 100mm or
more).



Using an IC Buffer



Using a TTL buffer.
It makes sense to use CMOS logic in your buffer designs wherever


possible. You should also make it clear in the design
documentation that CMOS logic is to be used.


Example: Buffering three LEDs with a 74HC04


This example shows a 74HC04 buffering three LEDs. As discussed
in Solution, we do not require pull-up resistors with the HC
(CMOS) buffers.
In this case, we assume that the LEDs are to be driven at 15 mA
each, which is within the capabilities (50 mA total) of the buffer.






Driving a low-power load without using a buffer


Use of pull-up resistors


To adapt circuits for use on pins without internal pull-up resistors is
straightforward: you simply need to add an external pull-up resistor:






The value of the pull-up resistor should be between 1K and 10K.
This requirement applies to all of the examples on this course.
NOTE:
This is usually only necessary on Port 0

NAKED LED




Connecting a single LED directly to a microcomputer port is usually
possible.



• Supply voltage, Vcc = 5V,

• LED forward voltage, Vdiode = 2V,

• Required diode current, Idiode = 15 mA (note that the data
sheet for your chosen LED will provide this information).
This gives a required R value of 200Ω.

Reset Hardware


• The process of starting any microcontroller is a non-trivial
one.

• The underlying hardware is complex and a small,
manufacturer-defined, ‘reset routine’ must be run to place


this hardware into an appropriate state before it can begin
executing the user program. Running this reset routine takes
time, and requires that the microcontroller’s oscillator is
operating.

• An RC reset circuit is usually the simplest way of controlling
the reset behaviour.


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.

Oscillator Hardware


• All digital computer systems are driven by some form of
oscillator circuit.
• This circuit is the ‘heartbeat’ of the system and is crucial to
correct operation.
For example:
• 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.

Creating “software delays”


How do you create a simple delay without using any hardware
(timer) resources?

Solution

Loop_Delay()


{
unsigned int x,y;
for (x=0; x <= 65535; x++)
{
y++;
}
}
Longer_Loop_Delay()
{
unsigned int x, y, z;
for (x=0; x<=65535; x++)
{
for (y=0; y<=65535; y++);
{
z++;
}
}
}

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:


#define LED_PORT P3


#define LED_ON 0 /* Easy to change the logic here */
#define LED_OFF 1
...
sbit Warning_led = LED_PORT^0; /* LED is connected to pin 3.0 */
...
Warning_led = LED_ON;
... /* delay */
Warning_led = LED_OFF;
... /* delay */
Warning_led = LED_ON;
... /* etc */

SFRs and ports



Control of the 8051 ports through software is carried out using what
are known as ‘special function registers’ (SFRs).

Physically, the SFR is a area of memory in internal RAM:




• P0 is at address 0x80
• P1 at address 0x90
• P2 at address 0xA0
• P3 at address 0xB0

NOTE: 0x means that the number format is HEXADECIMAL

Strengths and weaknesses 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.

The “super loop” software architecture



What is the minimum software environment you need to create an
embedded C program?

Solution


void main(void)
{
/* Prepare for task X */
X_Init();
while(1) /* 'for ever' (Super Loop) */
{
X(); /* Perform the task */
}
}

why c ?

* 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;

Embedded System Design Using 8031&8051 Microcontrollers


We are living in the Embedded World. You are surrounded with many embedded products and your
daily life largely depends on the proper functioning of these gadgets. Television, Radio, CD player of
your living room, Washing Machine or Microwave Oven in your kitchen, Card readers, Access
Controllers, Palm devices of your work space enable you to do many of your tasks very effectively.
Apart from all these, many controllers embedded in your car take care of car operations between the
bumpers and most of the times you tend to ignore all these controllers.
In recent days, you are showered with variety of information about these embedded controllers in
many places. All kinds of magazines and journals regularly dish out details about latest technologies,
new devices, fast applications which make you believe that your basic survival is controlled by these
embedded products. Now you can agree to the fact that these embedded products have successfully
invaded into our world. You must be wondering about these embedded controllers or systems. What is
this Embedded System?
The computer you use to compose your mails, or create a document or analyze the database is known
as the standard desktop computer. These desktop computers are manufactured to serve many purposes
and applications.
You need to install the relevant software to get the required processing facility. So, these desktop
computers can do many things. In contrast, embedded controllers carryout a specific work for which
they are designed. Most of the time, engineers design these embedded controllers with a specific goal
in mind. So these controllers cannot be used in any other place.
Theoretically, an embedded controller is a combination of a piece of microprocessor based hardware
and the suitable software to undertake a specific task.
These days designers have many choices in microprocessors/ microcontrollers. Especially, in 8 bit
and 32 bit, the available variety really may overwhelm even an experienced designer. Selecting a right
microprocessor may turn out as a most difficult first step and it is getting complicated as new devices
continue to pop-up very often.
In the 8 bit segment, the most popular and used architecture is 8031. Market acceptance of this
particular family has driven many semiconductor manufacturers to develop something new based on
this particular architecture. You can find this 8031 in a variety of configurations, flavour and definitely
you cannot see this kind of variations with any other architecture. Even after 25 years of existence,
semiconductor manufacturers still come out with some kind of device using this 8031 core.

 8031 Microcontrollers

8031 Architecture

The generic 8031 architecture sports a Harvard architecture, which contains two separate buses for
both program and data. So, it has two distinctive memory spaces of 64K X 8 size for both program and
data. It is based on an 8 bit central processing unit with an 8 bit Accumulator and another 8 bit B
register as main processing blocks. Other portions of the architecture include few 8 bit and 16 bit
registers and 8 bit memory locations.
Each 8031 device has some amount of data RAM built in the device for internal processing. This area
is used for stack operations and temporary storage of data.
This base architecture is supported with onchip peripheral functions like I/O ports, timers/counters,
versatile serial communication port. So it is clear that this 8031 architecture was designed to cater
many real time embedded needs.
The following list gives the features of the 8031 architecture:
 Optimized 8 bit CPU for control applications.
 Extensive Boolean processing capabilities.
  64K Program Memory address space.
  64K Data Memory address space.
  128 bytes of onchip Data Memory.
  32 Bi-directional and individually addressable I/O lines.
  Two 16 bit timer/counters.
  Full Duplex UART. 
  6-source / 5-vector interrupt structure with priority levels.
  Onchip clock oscillator.
Now you may be wondering about the non mentioning of memory space meant for the program storage,
the most important part of any embedded controller. Originally this 8031 architecture was introduced
with onchip, ‘one time programmable’ version of Program Memory of size 4K X 8. Intel delivered all
these microcontrollers (8051) with user’s program fused inside the device. The memory portion was
mapped at the lower end of the Program Memory area. But, after getting devices, customers couldn’t
change any thing in their program code, which was already made available inside during device
fabrication.






So, very soon Intel introduced the 8031 devices (8751) with re-programmable type of Program Memory
using built-in EPROM of size 4K X 8. Like a regular EPROM, this memory can be re-programmed many
times. Later on Intel started manufacturing these 8031 devices without any onchip Program Memory.
Now I go ahead giving more information on the important functional blocks of the 8031.



                                                   8031 Microcomputer Pinout Diagram
Central Processing Unit

The CPU is the brain of the microcontrollers reading user’s programs and executing the expected task
as per instructions stored there in. Its primary elements are an 8 bit Arithmetic Logic Unit (ALU),
Accumulator (Acc), few more 8 bit registers, B register, Stack Pointer (SP), Program Status Word
(PSW) and 16 bit registers, Program Counter (PC) and Data Pointer Register (DPTR).
The ALU (Acc) performs arithmetic and logic functions on 8 bit input variables. Arithmetic operations
include basic addition, subtraction, multiplication and division. Logical operations are AND, OR,
Exclusive OR as well as rotate, clear, complement and etc. Apart from all the above, ALU is responsible
in conditional branching decisions, and provides a temporary place in data transfer operations within
the device.
B register is mainly used in multiply and divide operations. During execution, B register either keeps
one of the two inputs and then retains a portion of the result. For other instructions, it can be used as
another general purpose register.
Program Status Word keeps the current status of the ALU in different bits.
Stack Pointer (SP) is an 8 bit register. This pointer keeps track of memory space where the important
register information are stored when the program flow gets into executing a subroutine. The stack
portion may be placed in any where in the onchip RAM. But normally SP is initialized to 07H after a
device reset and grows up from the location 08H. The Stack Pointer is automatically incremented or
decremented for all PUSH or POP instructions and for all subroutine calls and returns.
Program Counter (PC) is the 16 bit register giving address of next instruction to be executed during
program execution and it always points to the Program Memory space.
Data Pointer (DPTR) is another 16 bit addressing register that can be used to fetch any 8 bit data from
the data memory space. When it is not being used for this purpose, it can be used as two eight bit
registers.

Input / Output Ports

The 8031’s I/O port structure is extremely versatile and flexible. The device has 32 I/O pins configured
as four eight bit parallel ports (P0, P1, P2 and P3). Each pin can be used as an input or as an output
under the software control. These I/O pins can be accessed directly by memory instructions during
program execution to get required flexibility.
These port lines can be operated in different modes and all the pins can be made to do many different
tasks apart from their regular I/O function executions. Instructions, which access external memory,
use port P0 as a multiplexed address/data bus. At the beginning of an external memory cycle, low
order 8 bits of the address bus are output on P0. The same pins transfer data byte at the later stage
of the instruction execution.
Also, any instruction that accesses external Program Memory will output the higher order byte on P2
during read cycle. Remaining ports, P1 and P3 are available for standard I/O functions. But all the 8
lines of P3 support special functions: Two external interrupt lines, two counter inputs, serial port’s two
data lines and two timing control strobe lines are designed to use P3 port lines. When you don’t use
these special functions, you can use corresponding port lines as a standard I/O.
Even within a single port, I/O operations may be combined in many ways. Different pins can be configured
as input or outputs independent of each other or the same pin can be used as an input or as output at
different times. You can comfortably combine I/O operations and special operations for Port 3 lines.

Timers / Counters

8031 has two 16 bit Timers/Counters capable of working in different modes. Each consists of a ‘High’
byte and a ‘Low’ byte which can be accessed under software. There is a mode control register and a
control register to configure these timers/counters in number of ways.
These timers can be used to measure time intervals, determine pulse widths or initiate events with
one microsecond resolution upto a maximum of 65 millisecond (corresponding to 65, 536 counts). Use
software to get longer delays. Working as counter, they can accumulate occurrences of external events
(from DC to 500KHz) with 16 bit precision

Serial Port

Each 8031 microcomputer contains a high speed full duplex (means you can simultaneously use the
same port for both transmitting and receiving purposes) serial port which is software configurable in 4
basic modes: 8 bit UART; 9 bit UART; Interprocessor Communications link or as shift register I/O
expander.
For the standard serial communication facility, 8031 can be programmed for UART operations and can
be connected with regular personal computers, teletype writers, modem at data rates between 122
bauds and 31 kilobauds. Getting this facility is made very simple using simple routines with option to
select even or odd parity. You can also establish a kind of Interprocessor communication facility among
many microcomputers in a distributed environment with automatic recognition of address/data.
Apart from all above, you can also get super fast I/O lines using low cost simple TTL or CMOS shift
registers.

Memory Organization

The 8031 architecture provides both onchip memory as well as off chip memory expansion capabilities.
It supports several distinctive ‘physical’ address spaces, functionally separated at the hardware level
by different addressing mechanisms, read and write controls signals or both:
l On chip Program Memory
l On chip Data Memory
l Off chip Program Memory
l Off chip Data Memory
l On chip Special Function Registers
The Program Memory area (EPROM incase of external memory or Flash/EPROM incase of internal
one) is extremely large and never lose information when the power is removed. Program Memory is
used for information needed each time power is applied: Initialization values, Calibration data, Keyboard
lookup tables etc along with the program itself. The Program Memory has a 16 bit address and any
particular memory location is addressed using the 16 bit Program Counter and instructions which
generate a 16 bit address.
Onchip Data memory is smaller and therefore quicker than Program Memory and it goes into a random
state when power is removed. Onchip RAM is used for variables which are calculated when the program
is executed.
In contrast to the Program Memory, On chip Data Memory accesses need a single 8 bit value (may be
a constant or another variable) to specify a unique location. Since 8 bits are more than sufficient to
address 128 RAM locations, the onchip RAM address generating register is single byte wide.
Different addressing mechanisms are used to access these different memory spaces and this greatly
contributes to microcomputer’s operating efficiency.
The 64K byte Program Memory space consists of an internal and an external memory portion. If the EA
pin is held high, the 8051 executes out of internal Program Memory unless the address exceeds 0FFFH
and locations 1000H through FFFFH are then fetched from external Program Memory. If the EA pin is
held low, the 8031 fetches all instructions from the external Program Memory. In either case, the 16 bit
Program Counter is the addressing mechanism.
The Data Memory address space consists of an internal and an external memory space. External Data
Memory is accessed when a MOVX instruction is executed.
Apart from onchip Data Memory of size 128/256 bytes, total size of Data Memory can be expanded
upto 64K using external RAM devices.
Total internal Data Memory is divided into three blocks:
Lower 128 bytes.
Higher 128 bytes
Special Function Register space.
Higher 128 bytes are available only in 8032/8052 devices.
Even though the upper RAM area and SFR area share same address locations, they are accessed
through different addressing modes. Direct addresses higher than 7FH access SFR memory space
and indirect addressing above 7FH access higher 128 bytes (in 8032/8052).

Common Memory Space

The 8031’s Data Memory may not be used for program storage. So it means you can’t execute
instructions out of this Data Memory.
But, there is a way to have a single block of offchip memory acting as both Program and Data Memory.
By gating together both memory read controls (RD and PSEN) using an AND gate, a common memory
read control signal can be generated.
In this arrangement, both memory spaces are tied together and total accessible memory is reduced
from 128 Kbytes to 64 Kbytes.
The 8031 can read and write into this common memory block and it can be used as Program and Data
Memory.
You can use this arrangement during program development and debugging phase. Without taking
Microcontroller off the socket to program its internal ROM (EPROM/Flash ROM), you can use this
common memory for frequent program storage and code modifications.

Interrupts

The 8031 has five interrupt sources: one from the serial port when a transmission or reception operation
is executed; two from the timers when overflow occurs and two come from the two input pins INT0,
INT1. Each interrupt may be independently enabled or disabled to allow polling on same sources and
each may be classified as high or low priority.
A high priority source can override a low priority service routine. These options are selected by interrupt
enable and priority control registers, IE and IP.
When an interrupt is activated, then the program flow completes the execution of the current instruction
and jumps to a particular program location where it finds the interrupt service routine. After finishing
the interrupt service routine, the program flows return to back to the original place.
The Program Memory address, 0003H is allotted to the first interrupt and next seven bytes can be
used to do any task associated with that interrupt.

Interrupt Source               Service routine starting address
External  0                           0003H
Timer/Counter 0                  000BH
External 1                            0013H
Timer/counter 1                   001BH
Serial port                           0023H

Addressing Modes

8031’s assembly language instruction set consists of an operation mnemonic and zero to three operands
separated by commas. In two byte instructions the destination is specified first, and then the source.
Byte wide mnemonics like ADD or MOV use the Accumulator as a source operand and also to receive
the result.
The 8031 supports five types of addressing modes:
* Register Addressing
* Direct Addressing
* Register Indirect Addressing
* Immediate Addressing
* Index Addressing

Register Addressing

Register Addressing accesses the eight working registers (R0-R7) of the selected register bank. The
least significant three bits of the instruction opcode indicate which register is to be used for the
operation. One of the four banks of registers is to be predefined in the PSW before using register
addressing instruction. ACC, B, DPTR and CY, (the Boolean Accumulator) can also be addressed in
this mode.

Direct Addressing

Direct addressing can access any onchip variables or hardware register. To indicate the address of
the location, an additional byte is attached to the opcode. Depending on the highest order bit of the
direct address byte one of two physical memory space is selected.
When the direct address range is between 0 and 127 (00H - 7FH) one of the 128 low order onchip
RAM location is accessed. All I/O ports, special function, control registers are assigned between 128
and 255 (80H - FFH). When direct addressing indicates any location in this range, corresponding
hardware register is accessed.
This is the only method available for accessing I/O ports and special function registers.

Register Indirect Addressing

Register indirect addressing uses the contents of either R0 or R1 (in the pre selected register bank)
as a address pointer to locate in a 256 byte block (the lower 128 bytes of internal RAM in 8031 or 256
bytes in 8032) or the lower 256 bytes of external data memory. Note that the special function registers
are not accessible in this mode. Access to full 64K external data memory address space is indicated
by the 16 bit Data Pointer register, DPTR.
Execution of PUSH and POP instructions also involve indirect register addressing. The Stack Pointer
indicates the correct stack location anywhere in the internal RAM.

 Immediate Addressing

When a source operand is a constant rather than a variable, then the constant can be embedded into
the instruction itself. This kind of instructions take two bytes and first one specifies the opcode and
second byte gives the required constant.

Index Addressing

Only the Program Memory can be accessed by this mode. This mode is intended for reading lookup
tables in the Program Memory. A 16 bit base register (either DPTR or the Program Counter) points to
the base of the lookup tables and the Accumulator carries the constant indicating table entry number.
The address of the exact location of the table is formed by adding the Accumulator data to the base
pointer.

Why Atmel Devices?

In this part of the world (precisely India where I live) Atmel devices are freely available everywhere at
an affordable cost. Since Atmel has very good distribution network in many countries, I am sure you
can also find Atmel devices in your place.
I like Atmel’s flash memory very much. In early days, we used to get EPROM devices from ‘God - only
- knows’ sources and we always worried about the life of our target hardware. Atmel really relieved us
form this. Without caring about EPROM erases, we can program/reprogram the device during prototype
development and most of the time device stays cool even after many hours of running. Also we can go
upto 24MHz with these devices using 3V to 6V supply.
Also Whatever we are going to discuss can be applied to many other 8031 devices without any problem.

Real Life Projects

Having introduced 8031 architecture in your knowledge base, it is time for me to make you understand
what is actually going on in the Embedded World.
Now I am going to discuss about some simple yet useful embedded applications to give you more idea. All
of these projects are modeled from the commercial products. There are many companies manufacturing
these products for the general market. Then I proceed with telling you about various hardware and
software development tools you may need during project implementation.
This flow of discussion should make you comfortable in facing more complex and elaborate designs.

www.electronicsready4u.blogspot.com