Académique Documents
Professionnel Documents
Culture Documents
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
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
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.
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
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
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
MOV A, 4
MOV 7,2
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
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
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
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
12
Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
Minimum connection requirement for 8051
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
13
Advanced Instrumentation
Chapter 7: Microcontroller/Embedded System Instrumentation
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++);
}
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;
}
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
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
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
MOV P2,A
RET
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
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++);
}
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
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
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
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++) ;
}
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.
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
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:
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
RRC A
JNC MATCH
INC DPTR
SJMP FIND
CLR A
MOVC A,@A+DPTR
MOV P0, A
LJMP K1
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) ;
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++) ;
}
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