You are on page 1of 16

Components

Microcontroller
MICRO CNTROLLRER VRESUS GENERAL PURPOUS MICROPROCESSOR

What is the difference between a microprocessor and microcontroller ? By microprocessor is


meant the general-purpous microprocessors such as Intel’s family (8086,80286,80386,80486 and
the Pentium) or Motorola’s 680x0 family(68000,68010,68020,68030,68040,etc.). These
microprocessors contain no RAM, no ROM, and no I/O ports on the chip itself. For this reason,
they are commonly referred to as general-purpose microprocessors.

A system designer using a general –purpose microprocessor such as the Pentium or the 68040
must add RAM, ROM, I /O ports, and timers externally to make them functional. Although the
addition of external RAM, ROM, I /O ports make these systems bulkier and much more expensive ,
they have the advantage of versality such that the designer can decide on the amount of RAM,
ROM, I /O ports needed to fit the task at the hand. This is not the case with microcontrollers. A
microcontroller has a CPU (a microprocessor ) in addition to fixed amount of RAM, ROM, I /O
ports , and a timer all on a single chip . In other words, the processor, RAM, ROM, I /O ports, and
timer are all embedded together on one chip ; therefore, the designer cannot add any external
memory, I /o, or timer to it . The fixed amount of on chip ROM, RAM, and number of I/ O ports in
a microcontrollers make them ideal for many applications in which cost and space are critical.

In many applications, for example a TV remote control, there is no need for the computing
power of a 48/6 or even an 8086 microprocessor. In many applications, the space it takes ,the
power it consumes, and the price per unit are much more critical considerations than the
computing poer. These applications most often require some I /O operations to read and turn on
and off certain bits. For this reason some are call these processors IBP, “ itty –bitty processors “
(see “ Good Things in small Packages are Generating Big Product Opportunities’’)

It is interesting to note that some microcontroller manufacturers have gone as far as


integrating an ADC (analog-to-digital converter) and other peripherpherals into the
microcontroller.

MICROCONTROLLERS FOR EMBEDDED SYSTEMS

In the literature discussing microprocessors, we often see term embedded system.


Microprocessors and microcontrollers are widely used in embedded system products. A n
embedded product uses a microprocessors (or microcontroller) to do one task and one task only.

A printer is an example of embedded system since the processor inside it performs only one
task; namely, getting the data and printing it.

Contrast this with a Pentium-based PC (or any x86 IBM- compatible PC). A PC can be used
for any number of applications such as word processor, print server, bank teller terminal, video
game player, network srever , or internet terminal. Software for a variety of applications can be
loaded and run. Of course the reason a PC can perform mybrid tasks is that it has RAM memory
and an operating systm that loads application software into RAM and lets the CPU run it.
In an embedded system, there is only one application software that is typically burned into
ROM. A n x86 PC contains or is connected to various embedded products such as the keyboard,
printer, modem, disk controller, sound card, CD-ROM driver, mouse and so on. Each one of these
peripherals has a microcontroller inside it that performs only one task.

For example, inside every mouse there is a microcontroller that performs the task of finding
the mouse position and sending it to the PC.

X86 PC EMBEDDED APPLICATIONS

Although microcontrollers are the preferred choice for many embedded systems, there are
times that a microcontroller is inadequate for the task. For this reason, in recent years many
manufacturers of general-purpous microprocessors such as Intel Freescale Semiconductor Inc.
(formerly Motorola), AMD
(Advanced Micro Devices,Inc.), Cyrix (now a division of National

Semicondutor,Inc.) have targeted their microprocessor for the high end of the embedded market .
While Intel and AMD push their x86 processors for both the embedded and desktop PC markets,
Freescale is determined to keep the 68000 family alive by targeting it mainly for the high end of
embedded systems now that Applle no longer uses the 680x0 in their Macintosh. In the early 1990s
Apple computer began using Power PC microprocessors (604, 603, 620, etc.) in place of the IBM
and Freescale, and is targeted for the high end of the embedded market as well as the PC market. It
must be noted that when a compony targets a general purpose microprocessor for the embedded
market it optimizes the processor used for embedded systems. For this reason these processors are
often called high-end embedded processors. Very often the terms embedded processor and
microcontroller are used interchangeably.
One of the most critical needs of an embedded system is to decrease power consumption and
space. This can be achieved by integrating more functions into the CPU chip. All the embedded
processors based on the x86 and 680x0 have low power consumption in addition to some forms of I /O,
COM port and ROM all on a single chip. In high-performance embedded processors, the trend is to
integrate more and more functions on the CPU chip and let the designer decide which features he/she
wants to use. This ternd is invading PC sustem design as well. Normally, in designing the PC
motherboard we need a CPU plus a chip-set containing I/O, a cache controller, a flash ROM containing
BIOS, and finally a secondary cache memory. New design are emerging in industry.
For example, Cyrix has announced that it is working on a chip that contains the entire PC, except
for DRAM. In other words, we are about to see an entire computer on a chip.
Currently, because of MS-DOS and Windows standardization many embedded systems are using x86
PCs. In many cases using x86 PCs for the high end embedded applications not only saves money but also
shortens development time since there is a vast library of software already written for the DOS and
Windows platforms. The fact that Windows is a widely used and well understood platform means that
developing a Windows-based embedded product reduces the cost and shortens t he development time
considerably.

CHOOSING A MICROCONTROLLER
There are four major 8-bit microcontrollers. They are: Freescale’s 6811, Intel’s 8051, Zilog’s Z8, and
PIC 16X from Microchip Technology. Each of these microcontrollers has a unique instruction set and
register; therefore, they are not compatible with other. Programs written for one will not run on the others.
There are also 16-bit and 32-bit microcontrollers made by various chip makers. With all these different
microcontrollers, what criteria do designers consider in choosing one?

Three criteria in choosing microcontrollers are as follows:

1>Meeting the computing needs of the task at and efficiently and cost effectively,
2>Availability of software development tools such as compilers, assemblers, and
debuggers.
3>Wide availability and reliable sources of the microcontroller.

Features
®
• Compatible with MCS -51Products
• 2K Bytes of Reprogrammable Flash Memory
– Endurance: 1,000 Write/Erase Cycles
• 2.7V to 6V Operating Range
• Fully Static Operation: 0 Hz to 24 MHz
• Two-level Program Memory Lock
• 128 x 8-bit Internal RAM
• 15 Programmable I/O Lines
• Two 16-bit Timer/Counters
• Six Interrupt Sources
• Programmable Serial UART Channel
• Direct LED Drive Outputs
• On-chip Analog Comparator
• Low-power Idle and Power-down Modes
• Green (Pb/Halide-free) Packaging Option

1.Description
The AT89C2051 is a low-voltage, high-performance CMOS 8-bit microcomputer with
2K bytes of Flash programmable and erasable read-only memory (PEROM). The
device is manufactured using Atmel’s high-density nonvolatile memory technology
and is compatible with the industry-standard MCS-51 instruction set. By combining a
versatile 8-bit CPU with Flash on a monolithic chip, the Atmel AT89C2051 is a power-
ful microcomputer which provides a highly-flexible and cost-effective solution to many
embedded control applications.

The AT89C2051 provides the following standard features: 2K bytes of Flash, 128
bytes of RAM, 15 I/O lines, two 16-bit timer/counters, a five vector two-level interrupt
architecture, a full duplex serial port, a precision analog comparator, on-chip oscillator
and clock circuitry. In addition, the AT89C2051 is designed with static logic for opera-
tion down to zero frequency and supports two software selectable power saving
modes. The Idle Mode stops the CPU while allowing the RAM, timer/counters, serial
port and interrupt system to continue functioning. The power-down mode saves the
RAM contents but freezes the oscillator disabling all other chip functions until the next
hardware reset.
2.Pin Configuration

2.120-lead PDIP/SOIC

3.Block Diagram
4. Pin Description 4.1 VCC Supply voltage. 4.2 GND Ground. 4.3 Port 1 The Port 1 is an 8-
bit bi-directional I/O port. Port pins P1.2 to P1.7 provide internal pull-ups. P1.0 and P1.1
require external pull-ups. P1.0 and P1.1 also serve as the positive input (AIN0) and the
negative input (AIN1), respectively, of the on-chip precision analog comparator. The Port 1
out-put buffers can sink 20 mA and can drive LED displays directly. When 1s are written to
Port 1 pins, they can be used as inputs. When pins P1.2 to P1.7 are used as inputs and are
externally pulled low, they will source current (IIL) because of the internal pull-ups. Port 1
also receives code data during Flash programming and verification. 4.4 Port 3 Port 3 pins
P3.0 to P3.5, P3.7 are seven bi-directional I/O pins with internal pull-ups. P3.6 is hard-wired
as an input to the output of the on-chip comparator and is not accessible as a gen-eral-purpose
I/O pin. The Port 3 output buffers can sink 20 mA. When 1s are written to Port 3 pins they
are pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 3 pins that
are externally being pulled low will source current (IIL) because of the pull-ups. Port 3 also
serves the functions of various special features of the AT89C2051 as listed below:

4.5 RST Reset input. All I/O pins are reset to 1s as soon as RST goes high. Holding the RST
pin high for two machine cycles while the oscillator is running resets the device. Each
machine cycle takes 12 oscillator or clock cycles.
4.6 XTAL1 Input to the inverting oscillator amplifier and input to the internal clock
operating circuit.
4.7 XTAL2 Output from the inverting oscillator amplifier.
5. Oscillator Characteristics The XTAL1 and XTAL2 are the input and output, respectively,
of an inverting amplifier which can be configured for use as an on-chip oscillator. Either a
quartz crystal or ceramic resonator may be used. To drive the device from an external clock
source, XTAL2 should be left unconnected while XTAL1 is driven There are no require-
ments on the duty cycle of the external clock signal, since the input to the internal clocking
circuitry is through a divide-by-two flip-flop, but minimum and maximum voltage high and
low time specifications must be observed.
6. Special Function Registers A map of the on-chip memory area called the Special Function
Register (SFR) space is shown in the table below. Note that not all of the addresses are
occupied, and unoccupied addresses may not be imple-mented on the chip. Read accesses to
these addresses will in general return random data, and write accesses will have an
indeterminate effect. User software should not write 1s to these unlisted locations, since they
may be used in future products to invoke new features. In that case, the reset or inactive
values of the new bits will always be 0.

7. Restrictions on Certain Instructions The AT89C2051 and is an economical and cost-


effective member of Atmel’s growing family of microcontrollers. It contains 2K bytes of
Flash program memory. It is fully compatible with the MCS-51 architecture, and can be
programmed using the MCS-51 instruction set. However, there are a few considerations one
must keep in mind when utilizing certain instructions to pro-gram this device. All the
instructions related to jumping or branching should be restricted such that the destination
address falls within the physical program memory space of the device, which is 2K for the
AT89C2051. This should be the responsibility of the software programmer. For example,
LJMP 7E0H would be a valid instruction for the AT89C2051 (with 2K of memory), whereas
LJMP 900H would not.
7.1 Branching Instructions LCALL, LJMP, ACALL, AJMP, SJMP, JMP @A+DPTR –
These unconditional branching instructions will execute correctly as long as the programmer
keeps in mind that the destination branching address must fall within the physical boundaries
of the program memory size (loca-tions 00H to 7FFH for the 89C2051). Violating the
physical space limits may cause unknown program behavior. CJNE [...], DJNZ [...], JB, JNB,
JC, JNC, JBC, JZ, JNZ – With these conditional branching instructions the same rule above
applies. Again, violating the memory boundaries may cause erratic execution. For
applications involving interrupts the normal interrupt service routine address locations of the
80C51 family architecture have been preserved.
7.2 MOVX-related Instructions, Data Memory The AT89C2051 contains 128 bytes of
internal data memory. Thus, in the AT89C2051 the stack depth is limited to 128 bytes, the
amount of available RAM. External DATA memory access is not supported in this device,
nor is external PROGRAM memory execution. Therefore, no MOVX [...] instructions should
be included in the program. A typical 80C51 assembler will still assemble instructions, even
if they are written in violation of the restrictions mentioned above. It is the responsibility of
the controller user to know the physi-cal features and limitations of the device being used and
adjust the instructions used correspondingly.
8. Program Memory Lock Bits On the chip are two lock bits which can be left
unprogrammed (U) or can be programmed (P) to obtain the additional features listed in the
Table 8-1.

9. Idle Mode In idle mode, the CPU puts itself to sleep while all the on-chip peripherals
remain active. The mode is invoked by software. The content of the on-chip RAM and all the
special functions regis-ters remain unchanged during this mode. The idle mode can be
terminated by any enabled interrupt or by a hardware reset. The P1.0 and P1.1 should be set
to “0” if no external pull-ups are used, or set to “1” if external pull-ups are used. It should be
noted that when idle is terminated by a hardware reset, the device normally resumes program
execution, from where it left off, up to two machine cycles before the internal reset algorithm
takes control. On-chip hardware inhibits access to internal RAM in this event, but access to
the port pins is not inhibited. To eliminate the possibility of an unexpected write to a port pin
when Idle is terminated by reset, the instruction following the one that invokes Idle should
not be one that writes to a port pin or to external memory.
10. Power-down Mode In the power-down mode the oscillator is stopped, and the instruction
that invokes power-down is the last instruction executed. The on-chip RAM and Special
Function Registers retain their values until the power-down mode is terminated. The only exit
from power-down is a hardware reset. Reset redefines the SFRs but does not change the on-
chip RAM. The reset should not be activated before VCC is restored to its normal operating
level and must be held active long enough to allow the oscillator to restart and stabilize. The
P1.0 and P1.1 should be set to “0” if no external pull-ups are used, or set to “1” if external
pull-ups are used.
11. Programming The Flash The AT89C2051 is shipped with the 2K bytes of on-chip
PEROM code memory array in the erased state (i.e., contents = FFH) and ready to be
programmed. The code memory array is pro-grammed one byte at a time. Once the array is
programmed, to re-program any non-blank byte, the entire memory array needs to be erased
electrically. Internal Address Counter: The AT89C2051 contains an internal PEROM address
counter which is always reset to 000H on the rising edge of RST and is advanced by applying
a positive going pulse to pin XTAL1. Programming Algorithm: To program the AT89C2051,
the following sequence is recommended. 1. Power-up sequence: Apply power between VCC
and GND pins Set RST and XTAL1 to GND 2. Set pin RST to “H” Set pin P3.2 to “H” 3.
Apply the appropriate combination of “H” or “L” logic levels to pins P3.3, P3.4, P3.5, P3.7
to select one of the programming operations shown in the PEROM Programming Modes
table.
Comparator
The best way to learn about comparators is to use one in a circuit.
The first thing you notice in Fig is that the comparator looks
much like an operational amplifier (op-amp). This is true; comparators
are specialized op-amps. The comparator used in our first
example is the LM339 quad comparator. This integrated circuit
contains four comparators in a 14-pin dip package. Like op-amps
the comparator has an inverting and noninverting input. In this
particular circuit the reference voltage is placed on the inverting
input (_).

Voltage divider
The voltage divider is a simple but important concept. When using it,
you will be able to connect most resistance-type sensors to a
comparator.
The reference voltage is derived from a voltage divider made
up of two 10K-ohm resistors (see Fig. 5.2A). The Vref in this case will
be half of the supply voltage (Vcc) of 5 V, or 2.5 V (see Table 5.1). We
can make Vref any voltage we require between ground and Vcc by
adjusting the two resistance values of the voltage divider.
light-emitting diode (LED), which we can use as an indicator. In
addition,
the output may be used as a simple single-pole, singlethrow
(SPST) switch to ground. This feature will be useful when
we later need to trigger a 555 timer.
With the circuit wired, let’s see what happens. When the input
voltage (Vin) is less than the reference voltage (Vref), the output is
0 V (ground) and the LED is forward-biased and lit. If we adjust the
potentiometer so that the voltage is greater than Vref, the output
of the comparator goes high, turning off the LED. You can verify
the operation of the comparator by using a voltmeter to measure
the voltages at points A (Vref) and B (Vin).
Many people (myself included) feel this circuit is counterintuitive.
I would like the LED to be lit when the sensor voltage is
higher than the reference voltage. This can be accomplished by
reversing the input leads and connecting the inverting input (_)
Vin and the noninverting input to Vref. The output function reverses
also.
When one doesn’t need too many comparators, you may consider
using a complementary metal oxide semiconductor (CMOS) op-amp
configured as a comparator. The reason I like to use an op-amp is
that it can source (supply) sufficient current to drive an LED or circuit
directly (see Fig.)

coding & programming


8051PROGRAMMING IN C

WHY PROGRAM THE 8051 IN C?

Compilers produce hex files that we download into the ROM of the
microcontroller. The size of the hex file produced by the compiler of
microcontroller programmers, for two reasons:

1> Microcontrollers have limited on-chip ROM.


2> The code space for the 8051 is limited to 64KB.

How does the choice of programming language affect the compiled


program size? While Assembly language is tedious and time consuming. C
programming, on the other hand, is less time consuming and easier to write,
but the hex file size produced is much larger than if we used Assembly
language.
The following are some of the major reasons for writing programs in C
instead of Assembly:

1>It is easier and less time consuming to wriete in C than Assembly.


2>C is easier to modify and update.
3>C code is portable to other microcontrollers with littli or no
modification.

C DATA TYPES FOR THE 8051

Since one of the goals of 8051 programmers is to create smaller hex files,
it is worth while to re-examine C data types for 8051 C. In other words, a
good understanding of C data types for the 8051 can help programmers to
create smaller hex files.
Some of the most data types are:

Unsigned char

Since the 8051 is an 8-bit microcontroller, the character data type is the
most natural choice for many applications. The unsigned char is an 8-bit data
type that takes a value for the 8051. In many situations, such as setting a
counter value, where there is no need for signed data we should use unsigned
char instead of the signed char. Remember that C compilers use the signed
char as the default if we do not put the keyword unsigned in front of the char.
We can also use the unsigned char data type for a string of ASCII
characters, including extended ASCII characters.
In declaring variables, we must pay carefull attention to the size of the
data and try use unsigned char instead of int if possible. Because the 8051 has
a limited number of registers and data RAM locations, using the int in place
of char data type can lead to a larger size hex file.

Signed char

The signed char is an 8-bit data type that uses the most significant bit (D7
of D7-D0) to represent the – or + value. As a result, we have only 7 bits for the
magnitude of the unsigned number, giving us values from -128 to +127. In
situations where + and – are needed to represent a given quantity such as
temperature, the use of the signed char data type is a must.
Again notice that if we do not use the keyword unsigned. The default is
the signed value. For that reason we should stick with the unsigned char
unless the data needs to be represent as signed numbers.

Unsigned int

The unsigned int is a 16-bit data type that takes a value in the range of 0
to 65535 ( 0000-FFFH ). In the 8051, unsigned int is used to define 16-bit
variables such as memory addresses. It is also used to set counter values of
more than 256.

Since the 8051 is an 8-bit microcontroller and the int data type takes two
bytes of RAM, we must not use the int data type unless we have to.Since
registers and memory accesses are in 8-bit chunks, the misuse of int variables
will result in a larger hex file. Such misuse is not a big deal in PCs with 256
MB of memory, 32-bit Pentium registers and a bus speed of 133MHz.
However ,for 8051 programming do not use unsigned int in places where
unsigned char will do the job. Of course the complier will not generate an
error for this misuse, but the overhead in hex file size is noticeable. Also in
situations where there is no need for signed data ( such as setting counter
values), we should use unsigned int instead of signed int. This gives a much
wider range for data declaration. Again, remember that the C compiler uses
signed int as the default if we do not use the keyword unsigned.

Signed int

Signed int is a 16-bit data type that type uses the most significant bit (D15
of D15-D0) to represent the – or + value. As a result, we have only 15 bits for
the magnitude of the number, or values from -32768 to + 32767.

Sbit ( single bit)

The sbit keyword is a widely used 8051 C data type designed specifically
to access single-bit addressable registers. It allows access to single bits of the
SFR registers.
The bit data type allows access to single bits of bit- addressable memory
spaces 20-2FH. Notice that while the sbit data type is used for bit-addressable
SFRs, the bit data type is used for the bit-addressable section of RAM space
20-2FH. To access the byte- size SFR registers, we use the sfr data type.

Some Widely Used Data Types For 8051 C

Data Type Size in Bits DATA Range/Usage


Unsigned char 8-bit 0 to 255
(signed) char 8-bit -128 to +127
unsigned int 16-bit 0 to 65535
(signed) int 16-bit -32768 to +32767
sbit 1-bit SFR bit-addressable
only
bit 1-bit RAM bit-addressable
only
Sfr 8-bit RAM addressable 80-
FFHonly

TIME DELAY

There are two ways to create a time delay in 8051 C:

1>Using a simple for loop


2>Using the 8051 timers
In either case, when we write a time delay we must use the oscilloscope to
measure the duration of our time delay. Next we use the for loop to create
delays.

In creating a time delay using a for loop, we must be mindful of three


factors that can affect the accuracy of the delay.

1> The 8051 design. Since the original 8051 was designed in 1980,
both the fields of IC technology and microprocessor architectural design
have seen great advancements. The number of machine cycles and the
number of clock periods per machine cycle vary among different versions
of the 8051/52 microcontroller. While the original 8051/52 design used 12
clock periods per machine cycle.
2> The crystal frequency connected to the X1-X2 input pins. The
duration of clock period for the machine cycle is a function of this crystal
frequency.
3> Compiler choice. The third factor that affects the time delay is
the compiler used to compile the C program. When we program in
Assembly language, we can control the exact instructions and their
sequences used in the delay subroutine.
In the case C programs, it is the compiler that
converts then C statements and functions to Assembly
language instructions. As aresult, different compilers produce
different code. In other words, if we compile a given 8051 C
programs with different compilers, each compiler produces
different hex code.

For the above reasons, when we write time delays for C, we must use
oscilloscope to measure the exact duration.

I/O PROGRAMMING IN 8051 C

Byte size I/O

Ports P0-P3 are byte-accessible. We use the P0-P3 labels as defined in


the 8051/52 C header file.
Bit-addressable I/O programming

The I/O ports of P0-P3 are bit addressable. We can access a single bit
without distributing the rest of the port. We use the sbit data type to access a
single bit of P0-P3. One way to do that is to use the Px^y format where x is the
port 0, 1,2,or 3 , and y is the bit 0-7 of that port.
For example,P1^7 indicates P1.7. When using this method, you need to
include the reg51.h file.

LOGIC OPERATIONS IN 8051 C

One of the most important and powerful features of the C language is its
ability to perform bit manipulation.

Bit-wise operators in C

While every C programmer is familiar with the logical operators AND


(&&),OR( || ),and NOT (!), many C programmers are less familiar with the
bit-wise operators AND (&), OR ( | ), EX-OR ( ^ ), Inverter ( - ), Shift Right
( >>), and Shift left (<<). These bit-wise operators are widely used in software
engineering for embedded systems and control; consequently, understanding
and mastery of them are critical in microprocessor-based system design and
interfacing.

Bit-wise Logic Operators for C

AND OR EX-OR Inverter


A B A&B A|B A^B Y=~B
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1
1 1 1 1 0
Bit-wise operation in C

There are two bit-wise shift operators in C :

• (1) shift right ( >>)


• (2) shift left ( <<)

Their format in C is as follows :

data >>number of bits to be shifted right


data <<number of bits to be shifted left

You might also like