Professional Documents
Culture Documents
Mahesh G S
E&E-Dept,
1
REVA ITM, Bangalore
Application
2
HyperTerminal based Home Automation
3
4
5
6
COMPUTER
7
8
MICROCONTROLLER
9
10
Introduction to microcontrollers
• Microcontrollers
• CPU + I/O + Timer(s) [+ ROM] [+ RAM]
• Low to moderate performance only
• Limited RAM space, ROM space and I/O pins
• EPROM version available
• Low chip-count to implement a small system
• Low-cost at large quantities
• Development tools readily available at reasonable cost
11
Summary of features of the standard 8051
13
VON NEUMANN ARCHITECTURE AND
HARVARD ARCHITECTURE
14
VON NEUMANN ARCHITECTURE
15
HARVARD ARCHITECTURE
16
RISC AND CISC MACHINES
17
COMPARISON OF RISC AND CISC
18
Block Diagram of Microcontroller 8051
19
8051 Port bit latches
20
Port 0 and Port 1 Port 2 and Port 3
Pin diagram of 8051
21
Memory Space
22
Bit Addressable RAM
23
24
25
Register Banks
26
8051 ADDRESSING MODES AND INSTRUCTION SET
INSTRUCTION SYNTAX
•Label :The label is the symbolic address for the instruction. As the program is
assembled, the label will be given the value of the address in which the instruction is
stored.
•This facilitates referencing of the instruction at any point in the given program.
Of course, not all instructions will have labels.
•It is not necessary to define a symbol for the address of an instruction, unless that
address is needed by a branch statement elsewhere in the program.
•A label can be any combination of upto8 letters (A–Z), numbers (0–9) and period
(.).
27
INSTRUCTION SYNTAX
•Opcode: The opcode field contains a symbolic representation of the operation.
The operation tells the assembler what action the statement has to perform. The
8051 assembler converts the opcode into a unique machine language (binary
code) that can be acted on by the 8051 internal circuitry.
•Operand: The opcode specifies what action to perform, whereas the operand
indicates where to perform the action. The operand field contains the address of
the operand or the operand.
28
8051 DATA TYPES
29
SUBROUTINES
SUBROUTINES•Good program design is based on the concept of modularity,the
partitioning of a large program into subroutines.
•The CALL instruction is of the form CALL address. The address refers to the
address of the subroutine. When CALL instruction is executed, the contents of the
program counter are saved in the stack and the program counter is loaded with the
address, which is a part of CALL instruction.
•The RET instruction is usually the last instruction in the subroutine. When this
instruction is executed, the return address previously saved in the stack is retrieved
and is loaded into the program counter. Thus, the control is transferred to the
calling program.
30
SUBROUTINE PROCESSING
31
ADDRESSING MODES
A microcontroller provides, for the convenience of the programmer, various
methods for accessing data needed in the execution of an instruction.
The various methods of accessing data are called addressing modes. The 8051
addressing modes can be classified into the following categories
32
IMMEDIATE ADDRESSING
• Immediate addressing means that the data is provided as part of the
instruction (which immediately follows the instruction opcode).
33
REGISTER ADDRESSING
•Register addressing mode involves the use of registers to hold the data to be
manipulated. The lowest 32 bytes of the 8051 internal RAM are organized as four
banks of eight registers. Only one bank is active at a time. Using names, R0 to R7
can access any active register. 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.
35
INDIRECT ADDRESSING
Indirect addressing provides a powerful addressing capability, which needs to be
appreciated. The indirect addressing mode uses a register to hold the actual
address that will be used in data movement. Registers R0, R1, and DPTR are the
only registers that can be used as data pointers. Indirect addressing cannot be used
to refer to SFR registers. Both R0 and R1 can hold 8 bit address and DPTR can
hold 16 bit address
36
INDEXED ADDRESSING
•In indexed addressing, a separate register—either the program counter (PC), or the
data pointer (DTPR) is used to hold the base address, and the A is used to hold the
offset address.
•Adding the value of the base address to the value of the offset address forms the
effective address.
•Indexed addressing is used with JMP or MOVC instructions. Look up tables are easily
implemented with the help of index addressing.
37
RELATIVE ADDRESSING
•Relative addressing is used only with conditional jump instructions. The relative
address, often referred to as an offset, is an 8 bit signed number, which is
automatically added to the PC to make the address of the next instruction.
•The 8 bit signed offset value gives an address range of +127 to –128 locations. The
jump destination is usually specified using a label and the assembler calculates the
jump offset accordingly.
38
ABSOLUTE ADDRESSING
•Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL
(Absolute Call) instructions.
•These are 2 bytes instructions. The absolute addressing mode specifies the lowest
11 bit of the memory address as part of the instruction.
•The upper 5 bit of the destination address are the upper 5 bit of the current program
counter.
•Hence, absolute addressing allows branching only within the current 2 K byte page
of the program memory.
39
LONG ADDRESSING AND BIT INHERENT ADDRESSING
•The long addressing mode within the 8051 is used with the instructions LJMP
and LCALL. These are 3 byte instructions.
•The address specifies a full 16 bit destination address so that a jump or a call
can be made to a location within a 64 K byte code memory space.
•Bit Inherent: In this addressing, the address of the flag which contains the
operand, is implied in the opcode of the instruction
40
BIT DIRECT ADDRESSING
•The RAM space 20H to 2FH and most of the special function registers are bit
addressable. Bit address values are between 00H to 7FH.
41
ASSEMBLER DIRECTIVES
The following are the widely used 8051 assembler directives.
ORG (origin)
EQU
DB (define byte)
END
42
Stack Pointer
• Stack pointer (SP) is an 8-bit register at address 81H
• It contains the address of the data item currently on top of the stack.
• Stack operations include pushing data on the stack and popping data
off the stack
• Popping from the stack reads the data and decrements the SP
• 8051 stack is kept in the internal RAM
• Depending on the initial value of the SP, stack can have different sizes
43
Stack and Data Pointers
• The default value of SP (after system reset) is 07H.
• This result in the first stack write operation to store data in location 08H
which means that register bank 1 (and possible 2 and 3) are not available
• DPTR is a 16 bit register at addresses 82H (low byte) and 83H (high byte)
• Example: the following instructions write 55H into external
MOV A,#55H
MOV DPTR,#1000H
MOVX @DPTR,A
44
Look-Up Tables
• MOVC loads the accumulator with a byte from code (program) memory
• The address of the byte fetched is the sum of the original unsigned 8-bit
accumulator contents and the content of a 16-bit register (either the data
pointer or PC). In the latter case, the PC is incremented to the address of
the following instruction before being added to the accumulator
MOVC A, @A+DPTR
MOVC A,@A+PC
45
Boolean Instructions
• 8051 contains a complete Boolean processor for single-bit operations.
• Bit instructions that refer to carry bit as C are assembled as carry specific
Instructions
• Example:
CLR C
CLR CY
Both do the same. First one is 1 byte and the second one is 2-bytes
46
Conditional Jump
• The 8051 offers a variety of conditional jump instructions
47
• CJNE (compare and jump if not equal) is another conditional jump
instruction
• CJNE: two bytes in the operand field are taken as unsigned integers. If the
first one is less than the second one, the carry is set
48
Program to exchange the lower nibble of data present in external memory
(a)ADD A, 45H (b) SUBB A, #55H (c) MOV DPTR, #2000H (d)
MUL AB
50
Instruction Execution time in machine cycle Execution Time
51
Example 2 :Calculate the delay provided by the given 8051 assembly language
program with a crystal frequency of 11.0592MHz.
52
Example Project
53
Keil C51 Cross Compiler
•ANSI C Compiler
–Generates fast compact code for the 8051 and it’s derivatives
54
Basic microcontroller in C Programming
C is a high level programming language that is portable across many hardware
architectures. This means that architecture specific features such as register
definitions, initialization and start up code must be made available to your program
via the use of libraries and include files.
For the 8051 chip you need to include the file reg51.h
#include <reg51.h>
55
Basic C program structure
//-------------------------------------------------
// Includes
//-------------------------------------------------
#include <reg51.h>
void main (void)
{
// variable declaration and initialization
// code
}
56
//program to add 2 8-bit variables
void main( )
{
unsigned char data num1, num2, result;
num1 = 10;
num2 = 25;
result = num1 + num2;
}
57
C51 Data Types
Sbit 1 0,1
58
C51 Memory Types
•code
–Program memory.
–unsigned char code const1 = 0x55;//define a constant
–char code string1[ ] = “hello”;//define a string
•data
–Lower 128 bytes of internal data memory
–unsigned int data x;//16-bit variable x
•idata
–All 256 bytes if internal data memory
•bdata
–Bit addressable area of internal data memory
•xdata
–External data memory (512 bytes of on-chip auxiliary data RAM)
59
//program to flash an LED connected to Port 2 pin 0 every second
#include <reg51.h>
sbitLED = P2^0;
void delay();
void main()
{
while (1)
{
LED = 0;//LED off
delay();
LED = 1;//LED on
delay();
}
}
//Delay function
void delay()
{
……. 60
}
Relational Operators
61
Logical Operator
62
Bitwise Logical Operator
63
Timers/Counters
The 8051 comes equipped with two timers, both of which may be controlled, set,
read, and configured individually. The 8051 timers have three general functions:
64
Timer SFRs
the 8051 has two timers which each function essentially the same way. One
timer is TIMER0 and the other is TIMER1. The two timers share two SFRs
(TMOD and TCON) which control the imers, and each timer also has two
SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).
65
The TMOD SFR
The TMOD SFR is used to control the mode of operation of both timers.
Each bit of the SFR gives the microcontroller specific information concerning
how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1
whereas the low four bits (bits 0 through 3) perform the exact same
functions, but for timer 0.
The individual bits of TMOD have the following functions:
66
Four bits (two for each timer) are used to specify a mode of operation. The
modes of operation are:
67
13-bit Time Mode (mode 0)
Timer mode "0" is a 13-bit timer. When the timer is in 13-bit mode, TLx will
count from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and
increment THx. Thus, effectively, only 13 bits of the two timer bytes are being
used: bits 0-4 of TLx and bits 0-7 of THx. This also means, in essence, the
timer can only contain 8192 values. If you set a 13-bit timer to 0, it will
overflow back to zero 8192 machine cycles later.
68
16-bit Time Mode (mode 1)
Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It
functions just like 13-bit mode except that all 16 bits are used. TLx is
incremented from 0 to 255. When TLx is incremented from 255, it resets to 0
and causes THx to be incremented by 1. Since this is a full 16- bit timer, the
timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it
will overflow back to 0 after 65,536 machine cycles.
69
8-bit Time Mode (mode 2)
Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask?
Simple. When a timer is in mode 2, THx holds the "reload value" and TLx is the
timer itself. Thus, TLx starts counting up. When TLx reaches 255 and is
subsequently incremented, instead of resetting to 0 (as in the case of modes 0
and 1), it will be reset to the value stored in THx. For example, let’s say TH0
holds the value FDh and TL0 holds the value FEh.
70
Split Timer Mode (mode 3)
Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it
essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0
and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All
the bits that are related to Timer 1 will now be tied to TH0.
While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put
into modes 0, 1 or 2 normally--however, you may not start or stop the real timer
1 since the bits that do that are now linked to TH0. The real timer 1, in this
case, will be incremented every machine cycle no matter what.
71
INTERRUPTS
1. Interrupt is an input to a processor, whereby an external device or a
peripheral can inform the processor that it is ready for
communication.
3. This program is written by the user for performing tasks that the
interrupting device wants the processor.
72
INTERRUPT SERVICE SUBROUTINE
•When peripheral devices interrupt the processor, branching takes place to
interrupt service subroutine. Before branching, the actions taken by the
processor are as follows:
73
INTERRUPT SERVICE SUBROUTINE PROCESSING
74
Differences between RET and RETI instructions
## Both return the control to the calling program main Program) by popping off
the top two bytes of the stack into the program counter. RETI has the extra job of
clearing the interrupt flags (TF0/TF1 for timers and IE0/IE1 for edge triggered
interrupts)
75
Classification of Interrupts
1. Vectored Interrupts
In this method, the starting address of the interrupt service routine is
predefined when the microcontroller is designed. These types of interrupts are
called vectored interrupts.
•After receiving the INTA signal, external hardware sends the interrupt vector
to the microcontroller. These types of interrupts are called non-vectored
interrupts.
76
77
IE (INTERRUPT ENABLE) REGISTER
78
INTERRUPT PRIORITY REGISTER
79
8051 SERIAL COMMUNICATION
80
SIMPLEX, HALF DUPLEX AND
FULL DUPLEX DATA TRANSFER
81
TYPES OF SERIAL DATA COMMUNICATION
•In this mode, data is transmitted with start and stop bits.
•Transmission begins with start bit, followed by data and then stop bit.
•The transmission of 10 bit in the asynchronous format: one startbit, eight data bit and
one stop bit. For error checking purpose, parity bit is included just prior to stop bit.
82
SYNCHRONOUS TRANSMISSION FORMAT
83
ASYNCHRONOUS TRANSMISSION FORMAT
84
BAUD RATE
•The rate at which the bits are transmitted (bits/second) is called baud
or transfer rate.
•The baud rate is the reciprocal of the time to send 1 bit. In
asynchronous transmission, baud rate is not equal to number of bits
per second.
85
8051 SERIAL COMMUNICATION
•The 8051 supports a full duplex serial port. Full duplex means that it can
transmit and receive a byte simultaneously.
•The 8051 has TXD (pin 11 or P3.1) and RXD (pin 10 or P3.0) pins for
transmission and reception of serial data respectively. These pins are TTL
compatible.
•The 8051 transfers and receives data serially with different baud rates.
Three special function registers support serial communication, namely,
SBUF Register, SCON Register and PCON Register.
86
SPECIAL FUNCTION REGISTERS
•SBUF RegisterSBUF is an 8 bit register. It has separate SBUF registers for data
transmission and for data reception. These two registers are accessed by the
same name, SBUF.
•One of the registers is write only and used to hold data to be transmitted via
TXD pin. The other is read only and holds the received data from external
source via RXD pin.
•For a byte of data to be transferred via the TXD line, it must be placed in the
SBUF register. Similarly, SBUF holds the 8 bit data received by the RXD pin.
•SCON RegisterThis register contains mode selection bits, serial port interrupt
bit (TI and RI) and also the ninth data bit for transmission and reception (TB8
and RB8).
87
SERIAL CONTROL REGISTER (SCON)
88
INTERFACE OF TWO MICROCONTROLLERS WITH MINIMUM SIGNALS
89
RS232
•RS232 is the most widely used serial I/O interfacing standard. The RS232
standard was published by the Electronic Industry Association (EIA) in 1960.
•The COM1 and COM2 ports in IBMPC are RS232 compatible ports. In RS232, 1 is
represented by –3 to –25 V and 0 is represented by +3 to +25 V.
•In a microcontroller, serial TXD and RXD lines are TTL compatible i.e. 1 and 0 are
represented by +5 V and 0 V.
•MAX 232 IC is commonly used to convert the TTL logic levels to the RS232
voltage levels.
•In RS232, ground line is common to the transmitter and receiver, and they are
usable up to one meter without any shield. 90
Stepper Motors
91
INTERFACING STEPPER MOTOR
92
EXAMPLE
•The step angle and the stepping code can be obtained from the stepper
motor manufacturer, and the rotation speed depends on the delay program
between the stepping codes.
•The circuit to interface stepper motor is as shown in Fig. 6.15. The stepper
motor can be driven directly by the transistors. Transistors are used to
supply higher current to the motor.
•The microcontroller outputs the drive pattern to make the motor rotate. The
diodes in Fig. 6.15 are called fly back diodes and are used to protect the
transistors from reverse biases.
93
HALF STEP OPERATION OF STEPPER MOTOR
94
DRIVER CIRCUIT AND PORT INTERFACE FOR STEPPER
95
INTERFACING DC MOTOR
96
CIRCUIT OPERATION
•DC motor speed and direction of rotation is controlled using port pins—P2.4
and P2.5. The circuit utilizes complementary pair NPN/PNP, transistors T4/T5
and T2/T3.
•Motor is on, if a PWM signal is applied to P2.4; and if P2.5 is held at logic 0,
when the PWM is at logic 1, T6 collector will be low; so T4 is OFF and T5 is
ON.
•The motor rotates in reverse direction, if P2.4 is held at logic0; and if a PWM
signal is applied to P2.5, then T4 will be ON and T5 will be OFF.
•The diodes in the figure are used to protect the transistors from reverse 97
biases.
98