Вы находитесь на странице: 1из 27

Advanced Instrumentation

Chapter 7: Microcontroller/Embedded System Instrumentation


Embedded System is any device that includes a computer but is not itself a general-purpose
computer. General Purpose Computer means Desktop, Mainframe Computer or Laptop. But devices
like Camera, Mobile, PDA, Washing machine etc. are embedded devices which perform specific
functions. It is a part of large system which has software as well as hardware. It responds to the
external events (e.g. someone pushes an elevator button).
Examples of Embedded System
NASAs Mars Sojourner Rover used Intel 8-bit 8085 microprocessor, 512 MB of RAM, 176 Kb of flash
memory, Vxworks Operating System, 3 Navigation Cameras. This Mars Lander gave 2.3 billion bits of
information including 16,500 pictures and made 8.5 million measurements of the atmospheric
pressure, temperature and wind speed.

Sony Aibo ERS-110 Robotic Dog which used 64-bit of MIPS RISC. Artificial Intelligence Robot (Aibo)
was marketed to use as Entertainment Robot. It is widely adopted by universities for educational
purposes. What they have in common is that they sense the environment i.e., decide on (compute)
their actions (responses) in real time.
General Characteristics of Embedded Systems
Single functioned: An embedded system usually executes a specific program repeatedly. For
example, a washing machine is always a washing machine. Whereas Desktop Computer executes
variety of programs like spreadsheets, word processors, and video games which not an Embedded
System.
Tightly Constrained: While constructing or implementing an embedded system certain design metric
should be considered like cost, size, performance and power. An Embedded system has some
constraints which are especially tight. It should be cheap, fit into a single chip, must perform fast to
process real time data and also consume less power to extend battery life.
Real Time: An embedded system must compute results in real-time without delay. In Hard Real time,
any delay in deadline could result in a catastrophe. For example, Air-Bag system, Landing of Airplane,
etc. Whereas in Soft Real time, any delay in deadline could degrade the systems quality of service.
For
example,
on-line
reservation
system,
Live
Video
Streaming,
etc.
Reactive: Most of the embedded systems must continually react to changes in the systems
environment and must compute certain results in real time without delay. For example, a cars
cruise controller continually monitors and reacts to speed and brake sensors. It must compute
acceleration or deceleration amounts repeatedly within a limited time, a delayed computation could
result in failure to maintain the control of the car.

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Essential Components of Embedded System
Embeds hardware like Processors, Timers, Interrupt Controller, I/O devices, Memories, Ports
etc.
Embeds main application software generally into flash or ROM and the
application software performs concurrently the number of tasks.
Embeds a real time operating system, which supervises the application
software tasks running on the hardware and organizes the accesses to the
system resources according to priorities and timing constraints of tasks in the
system.
Classification of Embedded System
Small Scale:
Single 8-bit or 16-bit Microcontroller.
Little Hardware and Software complexity.
They may be battery operated.
Usually C may be used to develop such system.
Programming Tools:
Editor, Assembler and Cross Assembler.
Example: Stepper motor controller for a robotics system, Washing system, Computer
Mouse, Printer, Scanner, Remote controller etc
Medium Scale:
Single or few 16 or 32-bit microcontrollers or Digital Signal Processors (DSP) or Reduced
Instruction Set Computers (RISC).
Both Hardware and Software Complexity.
Programming tools
RTOS, Source Code Engineering tool, Simulator, Debugger and Integrated Development
Environment (IDE).
Example: Banking systems like ATM and Credit card transactions, Entertainment systems like
Video and Music system etc
Sophisticated:
Enormous Hardware and Software complexity.
May need scalable processor or configurable processor and programming logic arrays.
Constrained by the processing speed available in their hardware units.
Example: Real time video processing system, Speech or Multimedia processing system.
Programming Tools
For these systems may not be readily available at reasonable cost or may not be available at
all. A compiler or retargetable compiler might have to be developed for this.
Overview of Processor in Embedded System
General Purpose Processor
Programmable device used in variety of applications known as Microprocessor.
It has program memory, general datapath with large register file and general ALU.
Benefits are Low time to market and NRE (non-recurring engineering cost) and high
flexibility. Pentium is the most popular one.
Figure 1(a) is general purpose processor.
Examples:
Microprocessor
Intel: 8085, 8086, 80186, 80188, 80286, 80386
Motorola: 6800, 6809, G3, G4, G5
2

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Microcontroller
Intel: 8032, 8051, 8052
PIC: 8-bit PIC 16, PIC18, 16-bit DSPIC33/PIC24
Motorola: 6811
Embedded Processor
AndeScore N9/10/12, ARM 7/9/11, Intel i960, AMD 29050.
Digital Signal Processor
PAC, TMS32oCXX, SHARC, Motorola 5600XX.

Application-Specific Processor:
Dedicated to specific tasks like image compression and provides faster solution.
It has program memory, optimized datapath and special functional units.
Benefits are some flexibility, good performance, size and power.
Used as an additional processing unit for running the application in place of using embedded
software.
Example: IIM7100, W3100A
Figure 1(b) is Application-Specific Processor.
Single-Purpose Processor:
Digital circuit designed to execute only a single program.
Contains only the components to execute a single program.
No program memory.
Benefits are fast, low power and small size.
For example: coprocessor, accelerator or peripheral.
Figure 1(c) is single purpose processor
Application Areas of Embedded System
Automotive Electronics
Aircraft Electronics
Trains
Telecommunication
Medical Systems
Military Applications
3

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Authentication Systems
Consumer Electronics e.g MP3 Audio, digital Camera, home electronics, etc.
Robotics
Microcontroller Vs general purpose microprocessor
General purpose microprocessor is simply termed microprocessor. It contains no RAM, no ROM,
and no I/O ports on the chip itself.
Whereas, the microcontroller has a CPU (a microprocessor) in addition to a fixed amount of RAM,
ROM, I/O ports, and a timer all on a single chip.

The prime use of a microprocessor is to read data, perform extensive calculations on that data, and
store the results in a mass storage device or display the results.
The design of the microcontroller is driven by the desire to make it as expandable and flexible as
possible.
A microcontroller product has many components on one chip and so is more compact, and saves
time and space needed to design a device.
With a microprocessor product one has to add several other chips and so it is bulky.
All the components on a microcontroller is manufactured on a single chip and so is much cheaper
than having to manufacture several chips as is the case for a microprocessor system.
Microcontroller or microprocessor for embedded systems
An embedded product uses microcontroller (or microprocessor) 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.
Some examples of embedded products using microcontrollers:
Telephones, TV, Video games, Camera, Toys, Security systems, Cellular phone, Transmission control,
Instrumentation, etc.

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Choosing a microcontroller
Each type of microcontroller has a unique instruction set and register set, that means programs
written for one will not run on the others. So, one must consider few design criteria while choosing
microcontrollers. They are as follows:
Meeting the computing need of the task at hand efficiently and cost effectively.
Speed
Packaging
Power consumption
Memory size
Number of I/O units
Flexibility to upgrade
Cost per unit
Availability of software development tools
Such as compiler, assemblers, debuggers, emulator, technical details and support, etc.
Wide availability and reliable sources of the microcontroller
Overview of 8051
In 1981, Intel Corporation introduced an 8-bit microcontroller called the 8051.
This microcontroller had following components on a single chip.
128 bytes of RAM,
4K bytes of on-chip ROM,
two timers,
one serial port, and
four ports (each 8-bits wide).

The 8051 is an 8-bit processor, meaning that the CPU can work on only 8-bits of data at a time. Data
larger than 8 bits has to be broken into 8-bit pieces to be processed by the CPU.
The AT89C51 is a popular and inexpensive chip used in many small projects. It is a 40 pin chips and
has 4K bytes of flash ROM. Notice the AT89C51-12PC, where C before 51 stands for CMOS, which
has low power consumption, 12 indicates 12 MHz, P is for plastic DIP package, C is for
commercial.
Registers
Registers are used to store information temporarily. The most widely used registers of the 8051 are
A (accumulator), B, R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), and PC (program counter). All
5

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
of these registers are 8-bits except DPTR and the PC. The accumulator is used for all arithmetic and
logical instructions.
MOV instruction
MOV A, #55H
MOV R0, A
MOV R3, #95H
MOV A, R3

; load value 55H into reg. A


; copy contents of A into R0 (now A=R0=55H)
; load value 93H into R3
; copy contents of R3 into A (now A=R3=95H)

A value to be loaded into a register must be preceded with a pound sign (#). Otherwise, it means to
load from a memory location.
When 8051 is powered on, from where it starts? The 8051 family wakes up at memory address 0000
when it is powered on. By powering up we mean applying Vcc to the RESET pin. This means that it
expects the first opcode to be stored at ROM address 0000H. We achieve this by the ORG statement
(origin) in the source program. Thus, in AT89C51 the Rom address range is from 0000 to 0FFF.

Memory in 8051
On-chip ROM: to save your program
Program is burn in ROM.
Program is fixed and changeless.
On-chip RAM: to save some temporary data generated in execution time
Data can be changed.
Data is lost when the 8051 powers down.
Register: to store information temporarily
Some registers are used for internal operations of the 8051.
Some registers are located in RAM. Some have their special locations.
Machine Language
The binary form of the program is referred to as machine language because it is the form required by
the machine. An instruction is a binary pattern entered through an input device in memory to
command the processor to perform that specific function.
Example: 0011 1100
INR A (3CH)
Assembly Language
Assembly languages were developed that provided mnemonics for the machine code instructions,
plus other features that made programming faster and less prone to error. Machine and assembly
6

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
language are both processor specific and are referred to as low-level language because it deals
directly with the internal structure of the CPU. To program in assembly language, the programmer
must know the architecture of the CPU and the size of each, as well as other details.
Assembly language programs must be translated into machine code by a program called Assembler,
so that it can be loaded into memory and run. It is a program that translates a symbolic version of an
instruction into the machine code. The assembler provides a friendlier representation than a
computers 0 and 1, which simplifies writing and reading programs.
Example: 3CH INR A
In assembly language, each statement corresponds to one machine instruction.
High-level Language
Programming language such as BASIC, C, C++, Java, etc., that are intended to be machine
independent are called high-level language. It enables a programmer to write programs that are
more or less independent of a particular type of processors. A Compiler or Interpreter is used to
translate this language to machine code which can be loaded into memory and executed.
8051 Assembly language programming
An assembly language programming consists of a series of lines of assembly language instruction. It
consists of a mnemonic, optionally followed by one or two operands.
An assembly language instruction consists of four fields:
[label:] mnemonic [operands] [;comment]
Example:
START: MOV A, #25H

; load 25H into A

Running an 8051 program


An editor such as Windows Notepad or Keil vision
is used to write the main program.
"asm" file is the source file and for this reason some
assemblers require that this file have the a51"
extension.
The Keil vision assembler converts the assembly
language instructions into machine language and
provides the obj file and the Ist file.
Taking the object file as input the linker program
results the absolute abs object file.
The object-hex converter creates HEX files from
absolute object modules that have been created by
the linker.
HEX files are ASCII files containing a
hexadecimal representation of your application
program that is ready to burn into ROM, EPROM or
flash memory.
Recent Windows-based assembler
assemble, linker and OH program.

Steps to create a program

combine

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
The lst file is useful to the programmer because it lists all the opcodes and addresses as well as
errors that the assembler detected. But, still it is optional.

Data types and directives


DB (define byte)
It is used to define the 8-bit data. When DB is used to define data, the numbers can be in decimal,
binary, hex or ASCII formats.
DB 28
DB 39 H
DB 0110101B
DB ABCD1234

ORG (origin)
The ORG directive is used to indicate the beginning of the address. The number that comes after
ORG can be either in hex or in decimal.
ORG 500H
ORG 0F2H

EQU (equate)
This is used to define a constant without occupying a memory location. The EQU directive does not
set aside storage for a data item but associates a constant value with a data label.
COUNT EQU 25
..
MOV R3, #COUNT

END directive
This indicates to the assembler that the end of the source file; and other parts after this is ignored.
PSW (program status word) register
It is an 8-bit register. Although the PSW register is 8-bits wide, only 6 bits of it are used by the 8051.
The two unused bits are user-definable flags. Other four flags are called conditional flags, meaning
that they indicate some conditions that result after an instruction is executed.
CY
AC
F0
RS1 RS0 OV
-P
CY
PSW.7 Carry flag
AC
PSW.6 Auxiliary carry flag
F0
PSW.5 Available to the user for general purpose
RS1
PSW.4 Register Bank selector bit1
RS0
PSW.3 Register Bank selector bit 0
OV
PSW.2 Overflow flag
8

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
-P

PSW.1 User-definable bit


PSW.0 Parity flag

Number Representation for Different Bases


The following is an example showing the decimal number 46 represented in different number bases:
46d ; 46 decimal
2Eh ; 2Eh is 46 decimal represented as a hex number
56o ; 56o is 46 decimal represented as an octal number
101110b ; 101110b is 46 decimal represented as a binary number.

Note a number digit must be used in the first character of a hexadecimal number. For example the
hexadecimal number A5h is illegally represented and should be represented as 0A5h.
Addressing modes
The data could be in a register, or in memory, or be provided as an immediate value. There are a
number of addressing modes available to the 8051 instruction set, as follows:
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Indexed Addressing
Immediate Addressing
Immediate addressing simply means that the operand (which immediately follows the instruction
opcode) is the data value to be used. For example the instruction:
MOV A, #99d

Moves the value 99 into the accumulator (note this is 99 decimal since we used 99d). The # symbol
tells the assembler that the immediate addressing mode is to be used.
MOV R4, #62
MOV B, #40H

Register Addressing
One of the eight general-registers, R0 to R7, can be specified as the instruction operand. The
assembly language documentation refers to a register generically as Rn. An example instruction
using register addressing is :
ADD A, R5

; Adds register R5 to A (accumulator)

Here the content of R5 is added to the accumulator. One advantage of register addressing is that the
instructions tend to be short, single byte instructions.
MOV A, R0
MOV R2, A

Direct Addressing
Direct addressing means that the data value is obtained directly from the memory location specified
in the operand. For example consider the instruction:
MOV A, 47h

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

The instruction reads the data from Internal RAM address 47h and stores this in the accumulator.
Direct addressing can be used to access Internal RAM, including the SFR registers.
MOV 47h, A
MOV R4, 7Fh

; save content of A in RAM location 47h


; move contents of RAM location 7Fh to R4

MOV A, 4
MOV 7,2

; is same as MOV A, R4 which means copy R4 into A


; copy R2 to R7

Indirect Addressing
Indirect addressing provides a powerful addressing capability, which needs to be appreciated. An
example instruction, which uses indirect addressing, is as follows:
MOV A, @R0

; move contents of RAM location whose address is held by R0 into A

Note the @ symbol indicated that the indirect addressing mode is used. R0 contains a value, for
example 54h, which is to be used as the address of the internal RAM location, which contains the
operand data. Indirect addressing refers to Internal RAM only and cannot be used to refer to SFR
registers. Note, only R0 or R1 can be used as register data pointers for indirect addressing, but R2-R7
cannot be used. The 8052 (as opposed to the 8051) has an additional 128 bytes of internal RAM.
These 128 bytes of RAM can be accessed only using indirect addressing.
MOV @R1, B

; move contents of B into RAM location whose address is held by R1

Indexed Addressing
With indexed addressing a separate register, either the program counter, PC, or the data pointer
DTPR, is used as a base address and the accumulator is used as an offset address. The effective
address is formed by adding the value from the base address to the value from the offset address.
Indexed addressing in the 8051 is used with the JMP or MOVC instructions. Look up tables are easy
to be implemented with the help of index addressing. Consider the example instruction:
MOVC A, @A+DPTR
; the contents of A is added to the 16-bit register DPTR to form the 16-bit
; address of the needed data

MOVC is a move instruction, which moves data from the external code memory space. The C
means code. The address operand in this example is formed by adding the content of the DPTR
register to the accumulator value.
Here the DPTR value is referred to as the base address and the accumulator value us referred to as
the index address. An example program using the indexed addressing mode will be shown later.
ORG 0000H
CLR A
MOV DPTR, #400H
MOVC A, @A+DPTR
ORG 400H
DB SAM
END

; program starts at location 0000H


; A=0
; DPTR = 400H (points to first source location)
; get S from location 400H
; data is burned into location starting from 400H

10

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Types of Instructions
The assembly level instructions include: data transfer instructions, arithmetic instructions, logical
instructions, program control instructions, and some special instructions such as the rotate
instructions.
Data Transfer
Many computer operations are concerned with moving data from one location to another. The 8051
uses five different types of instruction to move data:
MOV MOVX MOVC PUSH and POP XCH
Arithmetic
Some key flags within the PSW, i.e. C, AC, OV, P, are utilised in many of the arithmetic instructions.
The arithmetic instructions can be grouped as follows:
Addition
Subtraction
Increment/decrement
Multiply/divide
Decimal adjust
Logical
Boolean Operations
Most control applications implement control logic using Boolean operators to act on the data. Most
microcomputers provide a set of Boolean instructions that act on byte level data. However, the 8051
(somewhat uniquely) additionally provides Boolean instruction which can operate on bit level data.
The following Boolean operations can operate on byte level or bit level data:
ANL Logical AND
ORL Logical OR
CPL Complement (logical NOT)
XRL Logical XOR (exclusive OR)
Logical operations at the BIT level
The C (carry) flag is the destination of most bit level logical operations. The carry flag can easily be
tested using a branch (jump) instruction to quickly establish program flow control decisions
following a bit level logical operation. The following SFR registers only are addressable in bit level
operations:
PSW IE IP TCON SCON
Program Control Instructions
The 8051 supports three kind of jump instructions:
LJMP SJMP AJMP
Refer: Chapter 3: Jump, Loop and Call Instructions
Subroutines and program flow control
A subroutine is called using the LCALL or the ACALL instruction.
Refer: Chapter 4: I/O Port Programming

I/O programming
There are a total of four ports for I/O operations. Of the 40 pins, a total of 32 pins are set aside for
the four ports P0, P1, P2, and P3, where each port takes 8 pins.
11

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
All the ports upon RESET are configured as inputs, ready to be used as input ports. When the first 0 is
written to a port, it becomes an output. To reconfigure it as an input, a 1 must be send to the port.
Port 0
Port 0 occupies a total of 8 pins (pin 32-39). It can be used for input or output. To use the pins of
port 0 as both input and output ports, each pin must be connected externally to a 10K-ohm pull-up
resistor. This is due to the fact that P0 is an open drain, unlike P1, P2 and P3. This port can also be
used for address as well (AD0-AD7).
MOV A, #0FFH ; A=FF hex
MOV P0, A
; make P0 as input port by writing all 1s to it

M. A. Mazidi Book (page 77)


Port 1
Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. This port does
not require any pull-up resistors since it already has pull-up resistor internally.
Port 2
Port 2 occupies a total of 8 pins (pins 21 through 28). It can be used as input or output. Just like P1
this port does not require any pull-up resistors since it already has pull-up resistor internally.
Port 3
Port 3 occupies a total of 8 pins (pins 10 through 17). It can be used as input or output. P3 port does
not require any pull-up resistors since it already has pull-up resistor internally.
P3 has the additional function of providing some extremely important signals such as interrupts.
M. A. Mazidi Book (page 79)
A powerful feature of 8051 I/O ports is their capability to access individual bits of the port without
altering the rest of the bits in that port. When accessing a port in single-bit manner, we use the
syntax SETB X.Y where X is the port number 0, 1, 2, or 3, and Y is the desired bit number from 0 to
7 for data bits D0 to D7.
Example:
P1.2 represents the third bit of P1
SETB P1.2

; change only P1.2=high

Single-bit instructions: M. A. Mazidi Book (page 82)


Program the 8051 in C
Compiler produces hex file that is downloaded into the ROM of the microcontroller.
But, assembly language produces a hex file that is much smaller than C.
Major reasons for writing in C instead of assembly:
It is easier and less time consuming to write in C than assembly.
C is easier to modify and update.
It is possible to use code available in function libraries.
C code is portable to other microcontrollers with little or no modification.
Data types for 8051
Unsigned char: It is an 8-bit data type that takes a value in the range of 0-255 (00-FFH).
Signed char
Unsigned int
Signed int
Sbit: It is used to access single-bit addressable registers. Example: sbit MYBIT = P1^0;

12

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Minimum connection requirement for 8051

Example1: Wave generation


Create a square wave of 50% duty cycle on any port.
Solution:
U1
19

18

29
30
31

1
2
3
4
5
6
7
8

XTAL1

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7

XTAL2

RST

P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15

PSEN
ALE
EA

P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7

P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28

A
B
C
D

10
11
12
13
14
15
16
17

AT89C51

Assembly program:
Program1:
HERE: SETB P2.0
ACALL DELAY
CLR P2.0
ACALL DELAY
SJMP HERE

; set to high bit 0 of port 2


; call the delay subroutine
; P2.0=0
;keep doing it

13

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

;SUBROUTINE FOR DELAY


DELAY: MOV R4,#05
WAIT1: MOV R3,#05
WAIT2: MOV R2,#05
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END

Program2:
HERE: CPL P2.0
; complement bit 0 of port 2
ACALL DELAY
SJMP HERE

C program:
#include<reg51.h>
void delay(unsigned int);
//Delay function
sbit PORT2=P2^0;
//Set the bit in P2^0
void main()
{
while(1)
//Loop forever
{
PORT2 = 1;
//Set the bit 0 in port 2 to high
delay(50);
//Delay time
PORT2 = 0;
//Set the bit 0 in port 2 to low
delay(50);
//Delay time
}
}
void delay(unsigned int time)
{
unsigned int m,n;
for(m=0;m<time;m++)
for(n=0;n<1275;n++);
}

Example2: I/O interfacing


A touch sensor is connected to the P2.5 pin, and a LED is connected to P1.5. Write a program to
monitor the touch sensor, and when it opens, light the LED as indicator.
Solution:
Touch sensor connected to P2.5 as input and LED connected to P1.5 as output.
Assembly program:
CLR P1.5
START: JB P2.5, START
SETB P1.5
NEXT: JNB P2.5, NEXT
CLR P1.5
SJMP START
END

; initially the LED is OFF


; when switch open (stay here as long as P2.5=1)
;LED ON when P2.5=0
; when switch closed (stay here as long as P2.5=0)
;LED OFF when P2.5=1

C program:
#include <reg51.h>
sbit Sensor = P2^5;
sbit LED = P1^5;
void main (void)
{

14

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Sensor = 1;
LED = 0;
while (Sensor == 0)
{
LED = 1;
}

//make P2.5 an input


//make P1.5 as output for LED to light

U1
19

18

29
30
31

D1

1
2
3
4
5
6
7
8

LED-BLUE

XTAL1

XTAL2

RST

PSEN
ALE
EA

P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28

R1
10k

10
11
12
13
14
15
16
17

AT89C51

Example3: Interfacing 7 segment display


This system displays the digits from 0 to 9 continuously with a predefined delay. Seven segment
displays internally consist of 8 LEDs. In these LEDs, 7 LEDs are used to indicate the digits 0 to 9 and
single LED is used for indicating decimal point. Generally seven segments are two types, one is
common cathode and the other is common anode.
In common cathode, all the cathodes of LEDs are tied together and labeled as com, and the anode
are left alone. In common anode, seven segment display all the anodes are tied together and
cathodes are left freely.
Here, common cathode seven segment is used to display the digits. In this circuit, pins a to h of the 7
segment are connected to the PORT 2 of the microcontroller and com pin is connected to the ground
through the 330 ohm resistor. This resistor is used to drop the voltage. Since we are using common
cathode seven segment we need to send logic 1 to the segments to glow.
The operating voltage of this LEDs is 2 to 3V but from controller we will get 5V so to drop the
remaining voltage we have to connect a to g pins to the controller through the resistor.
Digit Drive Pattern:
To display the digits on 7 segment, we need to glow different logic combinations of segments. For
example, if you want to display the digit 3 on seven segment then you need to glow the segments a,
b, c, d and g. The below table show you the Hex decimal values what we need to send from PORT2
to Display the digits from 0 to 9.
15

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

U1
19

18

29
30
31

1
2
3
4
5
6
7
8

XTAL1

XTAL2

RST

PSEN
ALE
EA

P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28

R1
330

10
11
12
13
14
15
16
17

AT89C51

Interfacing 7 Segment Display to 8051 Circuit Applications:


Seven segments are widely used in digital clocks to display the time.
These are used in electronic meters for displaying the numerical information.
Used in Instrument panels
Used in digital readout displays.
Limitations of the Circuit:
The complexity is increased to display large information.
It is not possible to display the symbols on seven segment.
The 11.0592 MHz crystal oscillator is used to make the 8051-based system compatible with the serial
port of the IBM PC. In the original 8051, one machine cycle lasts 12 oscillator periods. Therefore, to
calculate the machine cycle for the 8051, we take 1/12 of the crystal frequency, and then take its
inverse.
For 11.0592 MHz,
11.0592 MHz/12 = 921.6 kHz
Machine cycle is 1/921.6 kHz = 1.085 microsecond

16

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Subroutine for delay:
Instruction
DELAY: MOV R4,#05
WAIT1: MOV R3,#05
WAIT2: MOV R2,#05
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET

Machine cycles
1
1
1
2
2
2
2

Time to execute
1*1.085
1*1.085
1*1.085
2*5*1.085
2*5*1.085
2*5*1.085
2*1.085

Total time = (1+1+1+2*5+2*5+2*5+2)*1.085 = 37.975 microsecond.


Assembly program:
Program1:
ORG 0000H
AGAIN: MOV A,#3FH
ACALL PRT
ACALL DELAY
MOV A,#06H
ACALL PRT
ACALL DELAY
MOV A,#5BH
ACALL PRT
ACALL DELAY
MOV A,#4FH
ACALL PRT
ACALL DELAY
MOV A,#66H
ACALL PRT
ACALL DELAY
MOV A,#6DH
ACALL PRT
ACALL DELAY
MOV A,#7DH
ACALL PRT
ACALL DELAY
MOV A,#07H
ACALL PRT
ACALL DELAY
MOV A,#7FH
ACALL PRT
ACALL DELAY
MOV A,#6FH
ACALL PRT
ACALL DELAY
SJMP AGAIN
PRT:

MOV P2,A
RET

; port 2 used as output for display

;SUBROUTINE FOR DELAY


DELAY: MOV R4,#05
WAIT1: MOV R3,#05
WAIT2: MOV R2,#05
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2

17

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
DJNZ R4,WAIT1
RET
END

Program2:
ORG 0000H
START: MOV DPTR,#0300H
MOV R0,#0AH
AGAIN: CLR A
MOVC A,@A+DPTR
MOV P2,A
INC DPTR
ACALL DELAY
DEC R0
MOV A,R0
JZ START
SJMP AGAIN
END

; at first points to address 0+0300H


; port 2 used as output for display

ORG 0300H
MYDATA: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
END

C program:
#include <reg51.h>
unsigned char arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
void delay(unsigned int);
void main(void)
{
unsigned int i;
for(i=0;i<10;i++)
{
P2=arr[i];
delay(100);
}
}
void delay(unsigned int time)
{
unsigned int m,n;
for(m=0;m<time;m++)
for(n=0;n<1275;n++);
}

Example4: Stepper motor control


Stepper motors are DC motors that move in discrete steps. They commonly have a permanent
magnet rotor (also called shaft) surrounded by a stator windings. The direction of the rotation is
dictated by the stator poles. The stator poles are determined by the current sent through the wire
coils.

18

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

As the sequence of power is applied to each stator winding, the rotor will rotate.
With a computer controlled stepping you can achieve very precise positioning and/or speed control.
For this reason, stepper motors are the motor of choice for many precision motion control
applications.

Step1

Step2

Step4

Step3

Refer to other documents for detail about stepper motor.

Step#
1
2
3
4

A
1
0
0
1

B
1
1
0
0

A
0
1
1
0

B
0
0
1
1

CounterClockwise

Clockwise

Normal 4-step sequence

19

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Since the 8051 lacks sufficient current to drive the stepper motor windings, a ULN2003 (a motor
driver) helps to energize the stator. It has an internal diode to take care of back EMF thus protecting
the microcontroller.
U1
39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

XTAL1

XTAL2

RST

PSEN
ALE
EA

19

18

29
30
31

U2
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7

1
2
3
4
5
6
7
8

1
2
3
4
5
6
7

1B
2B
3B
4B
5B
6B
7B

COM
1C
2C
3C
4C
5C
6C
7C

9
16
15
14
13
12
11
10

+270

ULN2003A

AT89C51

Program to rotate the stepper motor continuously:


Assembly program:
MOV A, #66H
BACK: MOV P1, A
RR A
ACALL DELAY
SJMP BACK

;load step sequence


;issue sequence to motor
;rotate right clockwise
;wait
;keep going

DELAY: MOV R4,#99


WAIT1: MOV R3,#99
WAIT2: MOV R2,#99
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET

C program:
#include <reg51.h>
void main()
{
P2 = 0x00 ;
while(1)
{
P1 = 0x66 ;
MSDelay(100) ;
P1 = 0xCC ;
MSDelay(100) ;
P1 = 0x99 ;
MSDelay(100) ;
P1 = 0x33 ;
MSDelay(100) ;

20

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
}
}
void MSDelay (unsigned int value)
{
unsigned int x, y ;
for (x=0 ; x<1275 ; x++)
for (y=0 ; y<value ; y++) ;
}

Example5: DC motor speed control


A direct current (DC) motor is another widely used device that translates electrical pulses into
mechanical movement. In the DC motor we have only + and leads. Connecting them to a DC
voltage source moves the motor in one direction continuously. By reversing the polarity, the DC
motor will move in the opposite direction. The maximum speed of a DC motor is indicated in rpm.
The rpm is reduced when moving a load and it decreases as the load is increased.

With the basic concept of H-bridge, the direction of the DC motor rotation can be controlled. Hbridge control can be created using relays, transistors, or a single IC solution such as the L293.

A program to monitor the status of SW and perform the following:


(a) If SW=0, the DC motor moves clockwise.
(b) If SW=1, the DC motor moves counterclockwise.
Assembly program:
MAIN:

MONITOR:

ORG 0H
CLR P1.0
CLR P1.1
CLR P1.2
SETB P2.7
SETB P1.0
; enable the chip
JNB P2.7, CLOCKWISE
CLR P1.1
; turn the motor counterclockwise
SETB P1.2

21

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

CLOCKWISE:

SJMP MONITOR
SETB P1.1
CLR P1.2
SJMP MONITOR
END

; turn motor clockwise

U1
39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD
AT89C51

XTAL1

XTAL2

RST

PSEN
ALE
EA

19

18

29
30
31

16
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7

1
2
3
4
5
6
7
8

2
7
1

9
10
15

IN1
IN2
EN1

EN2
IN3
IN4

VSS

GND

8
VS OUT1
OUT2

OUT3
GND OUT4

U2
3
6

11
14

L293D

C Program:
#include <reg51.h>
sbit SW = P2^7;
sbit ENABLE = P1^0;
sbit MTR_1 = P1^1;
sbit MTR_2 = P1^2;
void main()
{
SW = 1;
ENABLE = 0;
MTR_1 = 0
MTR_2 = 0
while(1)
{
ENABLE = 1;
if(SW == 1)
{
MTR_1 = 1;
MTR_2 = 0;
}
else
{
MTR_1 = 0;
MTR_2 = 1;
}
}
}

22

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Example6: Keyboard Interfacing
Keyboards are widely used input device of the 8051. Keyboards are organized in a matrix of rows
and column. The CPU accesses both rows and columns through ports; therefore, with two 8-bit
ports, an 8x8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and
a column make a contact; otherwise, there is no connection between rows and columns.
If a 4x4 matrix connected to two ports, the rows are connected to an output port and the columns
are connected to an input port. If no key has been pressed, reading the input port will yield 1s for all
columns since they are all connected to high (Vcc). If all the rows are grounded and a key is pressed,
one of the columns will have 0 since the key pressed provides the path to ground.

The program sends the ASCII code for pressed key to P0.1, P1.0-P1.3 connected to rows P2.0-P2.3
connected to columns.
Assembly Program:
K1:

K2

OVER:

OVER1:

MOV P2, #0FFH


MOV P1, #0
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, K1
ACALL DELAY
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, OVER
SJMP K2
ACALL DELAY
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, OVER1
SJMP K2
MOV P1, #11111110B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW0
MOV P1, #11111101B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW1
MOV P1, #11111011B

;make P2 an input port


;ground all ROWS at once
;read all COL. ensure all keys open
;masked unused bits
;check till all keys released
;call 20 ms delay
;see if any key is pressed
;mask unused bits
;key pressed, await closure
;check if key pressed
;wait 20 ms debounce time
;check key closure
;mask unused bits
;key pressed, find ROW
;if none, keep polling
;ground ROW0
;read all columns
;mask unused bits
;key ROW0, find the COL.
;ground ROW1
;read all columns
;mask unused bits
;key ROW1, find the COL.
;ground ROW2

23

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

ROW0:
ROW1:
ROW2:
ROW3:
FIND:

MATCH:

MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW2
MOV P1, #11110111B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW3
LJMP K2

;read all columns


;mask unused bits
;key ROW2, find the COL.
;ground ROW3
;read all columns
;mask unused bits
;key ROW3, find the COL.
;if none, false input, repeat

MOV DPTR, #KCODE0


SJMP FIND
MOV DPTR, #KCODE1
SJMP FIND
MOV DPTR, #KCODE2
SJMP FIND
MOV DPTR, #KCODE3

;set DPTR=start of ROW0


;find COL. key belongs to
;set DPTR=start of ROW1
;find COL. key belongs to
;set DPTR=start of ROW2
;find COL. key belongs to
;set DPTR=start of ROW3

RRC A
JNC MATCH
INC DPTR
SJMP FIND
CLR A
MOVC A,@A+DPTR
MOV P0, A
LJMP K1

;set if any CY bit is low


;if zero, get the ASCII code
;point to next COL. address
;keep searching
;set A=0 (match is found)
;get ASCII code from table
;display pressed key

;ASCII LOOK-UP TABLE FOR EACH ROW


ORG 300H
KCODE0:
DB
0 , 1 , 2 , 3
KCODE1:
DB
4 , 5 , 6 , 7
KCODE2:
DB
8 , 9 , A , B
KCODE3:
DB
C , D , E , F
END

1
2
3
4
5
6
7
8

PSEN
ALE
EA

P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
AT89C51

P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17

ON

+
4

29
30
31

RST

39
38
37
36
35
34
33
32

XTAL2

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7

C
1

18

XTAL1

U1
19

RXD
TXD
RTS
CTS

24

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation

C Program:
Read the keypad and send the result to the first serial port.
P1.0-P1.3 connected to rows
P2.0-P2.3 connected to columns
Configure the serial port for 9600 baud, 8-bit, and 1 stop bit.
Note: Use the clock frequency of 11.0592 in8051 microcontroller.
#include <reg51.h>
#define COL P2
//define ports for easier reading
#define ROW P1
void MSDelay (unsigned int value) ;
void SerTX (unsigned char) ;
unsigned char keypad[4][4] =

{7 , 8 , 9 , / ,
4 , 5 , 6 , X ,
1 , 2 , 3 , - ,
C , 0 , = , +} ;

void main()
{
unsigned char colloc, rowloc ;
TMOD = 0x20 ;
//timer 1, mode 2
TH1 = -3 ;
//9600 baud
SCON = 0x50 ;
//8-bit, 1 stop bit
TR1 = 1 ;
//start timer 1
//keyboard routine. This sends the ASCII code for pressed key to the serial port
COL = 0xFF ;
//make P2 an input port
while(1)
//repeat forever
{
do
{
ROW = 0x00;
//ground all rows at once
colloc = COL;
//read the columns
colloc &= 0x0F ;
//mask used bits
}while (colloc != 0x0F) ;
//check until all keys released
do
{
do
{
MSDelay(20) ;
//call delay
colloc = COL;
//see if any key is pressed
colloc &= 0x0F ;
//mask unused bits
} while (colloc == 0x0F) ;
//keep checking for keypress
MSDelay(20);
colloc = COL ;
colloc &= 0x0F;
} while (colloc == 0x0F) ;

//call delay for debounce


//read columns
//mask unused bits
//wait for keypress

while(1)
{
ROW = 0xFE ;
colloc = COL ;

//ground row 0
//read columns

25

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
colloc &= 0x0F ;
//mask unused bits
if (colloc != 0x0F)
//column detected
{
rowloc = 0 ;
//save new location
break ;
//exit while loop
}
ROW = 0xFD ;
//ground row 1
colloc = COL ;
//read columns
colloc &= 0x0F ;
//mask unused bits
if (colloc != 0x0F)
//column detected
{
rowloc = 1 ;
//save new location
break ;
//exit while loop
}
ROW = 0xFB ;
//ground row 2
colloc = COL ;
//read columns
colloc &= 0x0F ;
//mask unused bits
if (colloc != 0x0F)
//column detected
{
rowloc = 2 ;
//save new location
break ;
//exit while loop
}
ROW = 0xF7 ;
colloc = COL ;
colloc &= 0x0F ;
rowloc = 3 ;
break ;

//ground row 3
//read columns
//mask unused bits
//save row location
//exit while loop

}
//check column and send result to the serial port
if(colloc == 0x0E)
SerTX (keypad[rowloc][0]) ;
else if(colloc == 0x0D)
SerTX (keypad[rowloc][1]) ;
else if(colloc == 0x0B)
SerTX (keypad[rowloc][2]) ;
else
SerTX (keypad[rowloc][3]) ;
}
}
void SerTX(unsigned char x)
{
SBUF = x ;
while (TI == 0) ;
TI = 0 ;
}
void MSDelay (unsigned int value)
{
unsigned int x, y ;
for (x=0 ; x<1275 ; x++)
for (y=0 ; y<value ; y++) ;
}

//place value in buffer


//wait until transmitted
//clear flag

26

Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Procedure for working in Keil uvision
Uvision v5.11.2.0
New uvision Project
-filename.uvproj
-select Atmel (AT89C51)
-OK
-NO
New (Create empty doc)
-Save (filename.c)
RightClick Target1
-Options for Target Target1
-Output (tick HEX file)
-OK
RightClick Service Group1
-Add file to Service Group1
-Select filename.c file
-Add
-Close
Build
-0 error, 0 warnings
-Hex file will be created
Document log:
Version Number
Version 1.0

Date
31/06/2015

Changes
--

27

Вам также может понравиться