0% found this document useful (0 votes)
99 views74 pages

U1 3691 Microcontroller

The document outlines the syllabus for the CS3691 Embedded Systems and IoT course at Anna University, detailing five units covering topics such as 8-bit embedded processors, embedded C programming, IoT and Arduino programming, IoT communication models, and applications development. Each unit includes course objectives, outcomes, and important questions, with references to textbooks. The course aims to provide students with knowledge and skills in embedded systems and IoT applications, including practical design and programming aspects.

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views74 pages

U1 3691 Microcontroller

The document outlines the syllabus for the CS3691 Embedded Systems and IoT course at Anna University, detailing five units covering topics such as 8-bit embedded processors, embedded C programming, IoT and Arduino programming, IoT communication models, and applications development. Each unit includes course objectives, outcomes, and important questions, with references to textbooks. The course aims to provide students with knowledge and skills in embedded systems and IoT applications, including practical design and programming aspects.

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

CS3691

EMBEDDED SYSTEMS AND IOT


(Anna University, Regulation 2021)

Mrs. S. Priya, M.E., (Ph.D)


Assistant Professor-
Senior Grade,
Nehru Institute of Engineering and
Technology
Accredited by NACC ‘A+’, Recognized by UGC with Section 2(f) and 12(B),
NBA Accredited UG Courses: AERO, CSE, ECE, EEE, MCT & MECH
Syllabus
UNIT I 8-BIT EMBEDDED PROCESSOR (9)
8 Bit Microcontroller – Architecture – Instruction Set and
Programming – Programming Parallel Ports – Timers and Serial
Port – Interrupt Handling.
TEXTBOOK:
Important Question: 1. Muhammed Ali Mazidi, Janice Gillispie Mazidi,
Rolin D. McKinlay, “The 8051 Microcontroller and
1. 8 Bit MC Architecture.
Embedded Systems”, Pearson Education, Second
2. Timer and Serial Port.
Edition, 2014
3. Interrupt Handling.

Course Objective1: To learn the internal architecture and


programming of an embedded processor.
Course Outcome 1: Explain the architecture of embedded
Mrs. S. Priya, AP SG- CSE, NIET 2
processors.
Syllabus
UNIT II EMBEDDED C PROGRAMMING (9)
Memory and I/O Devices Interfacing – Programming Embedded
Systems in C – Need For RTOS – Multiple Tasks and Processes –
Context Switching – Priority Based Scheduling Policies.
TEXTBOOK:
Important Question: 1. Muhammed Ali Mazidi, Janice
1. Memory and I/ O Devices Interfacing.
Gillispie Mazidi, Rolin D. McKinlay,
2. Need for RTOS. “The 8051 Microcontroller and
3. Multiple Task & Process. Embedded Systems”, Pearson
Education, Second Edition, 2014
4. Priority Based Scheduling Policies.

Course Objective 2: To introduce interfacing I/O devices to the


processor.
Mrs. S. Priya, AP SG- CSE, NIET 3
Course Outcome 2: Write embedded C programs.
Syllabus
UNIT III IOT AND ARDUINO PROGRAMMING
Introduction to the Concept of (9)
IoT Devices – IoT Devices Versus
Computers – IoT Configurations – Basic Components –
Introduction to Arduino – Types of Arduino – Arduino Toolchain –
Arduino Programming Structure – Sketches – Pins – Input/Output
From Pins Using Sketches – Introduction to Arduino Shields –
TEXTBOOK:
Integration of Sensors and Actuators with Arduino.
Important Question: 2. Robert Barton, Patrick Grossetete,
David Hanes, Jerome Henry, Gonzalo
1. IoT Devices versus Computers.
Salgueiro, “IoT Fundamentals:
2. Integration of Sensors and Actuators
Networking Technologies, Protocols, and
with Arduino. Use Cases for the Internet of Things”,
Course Objective 3: CISCO Press, 2017.
To introduce the evolution of the Internet of Things (IoT).
Course Outcome 3: DesignMrs. S. simple embedded
Priya, AP SG- CSE, NIET applications. 4
Syllabus
UNIT IV IOT COMMUNICATION AND OPEN PLATFORMS (9)
IoT Communication Models and APIs – IoT Communication
Protocols – Bluetooth – WiFi – ZigBee – GPS – GSM modules –
Open Platform (like Raspberry Pi) – Architecture – Programming
– Interfacing – Accessing GPIO Pins – Sending and Receiving
Signals Using GPIO Pins – Connecting to theTEXTBOOK:
Cloud.
2. Robert Barton, Patrick Grossetete, David
Important Question:
Hanes, Jerome Henry, Gonzalo Salgueiro, “IoT
1. Bluetooth, Wifi, ZigBee. Fundamentals: Networking Technologies,
2. GSM Modules. Protocols, and Use Cases for the Internet of
3. Open Platform Architecture. Things”, CISCO Press, 2017.
Course Objective 4: To build a small low-cost embedded and IoT
system using Arduino/Raspberry Pi/ open platform.
Course Outcome 4: Compare the communication models in IOT.
Mrs. S. Priya, AP SG- CSE, NIET 5
Syllabus
UNIT V APPLICATIONS DEVELOPMENT (9)
Complete Design of Embedded Systems – Development of IoT
Applications – Home Automation – Smart Agriculture – Smart
Cities – Smart Healthcare. TEXTBOOK:
Important Question: 2. Robert Barton, Patrick Grossetete,
1. Complete Design of David Hanes, Jerome Henry, Gonzalo
Salgueiro, “IoT Fundamentals:
Embedded Systems.
Networking Technologies, Protocols,
2. Development of IoT Applications. and Use Cases for the Internet of
3. Smart Agriculture, Smart Cities etc.,CISCO Press, 2017.
Things”,
Course Objective 5: To apply the concept of the Internet of
Things in real-world scenarios.
Course Outcome 5: Design IoT applications using
Arduino/Raspberry Pi /open platform.
Mrs. S. Priya, AP SG- CSE, NIET 6
LEARNING- Eg., Home App/ Security Cameras
• A microcontroller is a small and low-cost microcomputer.
• The Intel MCS-51 (commonly termed 8051) is a
single-chip microcontroller (MCU) series
developed by Intel in 1981 for use in embedded
systems.
• An 8-bit microcontroller. (PROGRAMMABLE)
• Built with 40 pins DIP (dual inline package), 4kb
of ROM storage and 128 bytes of RAM storage, 2
16-bit timers.
• Small system on an integrated chip that
comes with CPU, I/O ports, timers,
Mrs. S. Priya, AP- CSE,
NIET
7
LEARNING
• A microcontroller is a small and low-cost microcomputer.

Mrs. S. Priya, AP- CSE, 8


NIET
Microcontroller 8051:
• Older versions of 8051 used assembly programming language.
• Recent editions can be programmed using more advanced
languages like C, Python or JavaScript.
• ROM Memory- Non-volatile where information is sustained over
a long period/ doesn't require a constant source of power supply.
• RAM is volatile in nature stores information for a short period and
requires a constant source of power supply.
• Interrupts: Main program on hold when a specific function is
required to perform over the main function. Five interrupts
present in the architecture of 8051 known as INT0,
TF0, INT1, TF1, R1/T1
Mrs. S. Priya, AP- CSE, 1
NIET 1
Microcontroller 8051:
• Interrupts: Main program on hold when a specific function is
required to perform over the main function. Five interrupts
present in the architecture of 8051 are known as
INT0, TF0, INT1, TF1, R1/T1.
• TF0 and TF1 are timer interrupts
• INT0 and INT1 are external
interrupts
• R1/T1 is a serial port interrupt.

Mrs. S. Priya, AP- CSE, 12


NIET
Microcontroller 8051:
• Serial Port: 8051 comes with UART (Universal
Asynchronous Receiver/Transmitter.
• The serial port is duplex i.e. it can transfer or receive
data.
• The serial port comes with three pins called Tx, Rx and
ground.
• Buses. Buses are a valuable addition to the
development of the microcontrollers that are used
for data transmission. Mainly two buses are used
for communication called Data Bus and Address
Bus and are 8 bit and 16 bit respectively.
Mrs. S. Priya, AP- CSE, 13
NIET
Microcontroller 8051:
• Buses/ Address Bus 16 bit/address memory up to 2
(64k).
 ADDRESS BUS IS OF FOUR addressing modes called
a. Immediate addressing mode,
b. Direct addressing modes,
c. Register addressing mode and
d. Register indirect addressing mode.

Mrs. S. Priya, AP- CSE, 14


NIET
Microcontroller 8051:
• Buses/ Data Bus 8 bit .

 Transfer of data for a particular application within


microprocessor and memory I/O devices.
 Bi-directional and used for sending and receiving
data.

Mrs. S. Priya, AP- CSE, 15


NIET
Microcontroller 8051:
• I/O Ports. Microcontroller comes with four I/O ports called P0, P1, P2, and
P3.
•Port
Each
0. port comes with
Pin number from832
pins.
to 39 (bi- Port 1. Pin from 1 to 8 (bi-dir)
dir)
Port 2. Pin from 21 to 28(bi-dir) Port 3. pins from 10 to 17
(Special Features)
• Timers/Counters. The 8051 microcontroller comes with two 16-bit timers
and counters
 Timers are used to count the internal signal of a particular function of
the controller
 Counters are used to count the external signals of the peripherals
connected to the controller.
•Oscillator. It is added in the control
Mrs. forCSE,
S. Priya, AP- generating clock pulses. 16
NIET
Microcontroller 8051:
• I/O Ports. Microcontroller comes with four I/O ports called P0, P1, P2, and
P3.
• Each port comes with 8 pins.
Port 3. pins from 10 to
17
• RST. Pin 9 represents the reset pin.
• Pin 18 and Pin 19 (XTAL2 And XTAL1)
• GND. Pin 20 represents the ground pin.
(0 V)
• PSEN. Pin 29 is PSEN (Program Stored
Enable Pin) / External program memory.
• ALE. Pin 30 is ALE ( Address Latch Enable).
• VPP. Pin 31 is external access that enables
external program memory
Mrs. S. Priya, AP- CSE, 17
• Vcc. Pin 40 represents the power
NIET source
Mrs. S. Priya, AP- CSE, 18
NIET
Microcontroller 8051:
• An 8051 microcontroller has the following 12 major components:

1. ALU (Arithmetic and Logic Unit)


2. PC (Program Counter)
3. Registers
4. Timers and counters
5. Internal RAM and ROM
6. Four general-purpose parallel input/output ports
7. Interrupt control logic with five sources of interrupt
8. Serial data communication
9. PSW (Program Status Word)
10. Data Pointer (DPTR)
11. Stack Pointer (SP)
12. Data and Address bus.
Mrs. S. Priya, AP- CSE, 19
NIET
Microcontroller 8051:
Instruction Set and Programming
Instruction format
 Instruction format of 8051 consists of Opcode and Operand
 The Opcode represents the type of operation to be performed, and
 The Operand represents the data upon which the operation is performed.
 Example: MOV A, Rn; copies the content of the register Rn of selected register bank to A.
INSTRUCTION SETS OF 8051:
The instruction set of 8051 is broadly classified as follows: (9 Sets of instruction)
1. Data Transfer Instruction:
2. Instruction to Access External Data Memory
3. Rotate and Swap Instruction
4. Logical Instructions
i. Byte Level Logical Instruction ii. Bit Level Logical Instruction
Mrs. S. Priya, AP- CSE, 20
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
The instruction set of 8051 is broadly classified as follows:
5. Arithmetic Instruction:
i. Incrementing and Decrementing.
ii. Addition
iii.Subtraction
iv.Multiplication and Division
6. Jump Instruction
7. Call and Subroutine Instruction
8. Push and Pop Instruction
9. Data Exchange Instruction
Mrs. S. Priya, AP- CSE, 21
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
1. Data Transfer Instruction:
 A data transfer Instruction set performs the data transfer function.
Data can be transferred from or to external RAM or within the internal Memory.
S.No Instruction Addressing Modes Operation
1. MOV A, Rn Register Copies the content of the register Rn to A
2. MOV A,Direct Direct byte Copies the content of the address specified to A
3. MOV A,@Ri Register indirect Copy the content of address Ri to A
4. MOV A,#data Immediate Load data given in the instruction to A
5. MOV Rn,A Register Copies the content of A to Register Rn of the selected
register bank.
6. MOV direct, A Direct Byte Copies the content of A to the address specified within the
instruction.
7. MOV@Ri, A Register indirect Copies the content of A to the address Ri of the selected
register bank.
8. MOV DPTR, #data 16 Immediate Load data pointer with a 16-bit constant.
Table 1: Data Transfer Instruction Mrs. S. Priya, AP- CSE, 22
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
2. Instruction to Access External Data Memory:
 These instructions are used perform the data accessing from external memory.
S. No Instruction Addressing Modes Operation
1. MOVX A ,Ri Register Copy the content of external address in Ri to A.

2. MOVX A,@DPTR Register indirect Copy the content of external memory in


DPTR to A.
3. MOVX @Ri, A Register indirect Copy data from A to the external address inRi.

4. MOVX @ DPTR, A Register indirect Copy data from A to the external address in
DPTR.

Table 2: Instruction to Access External Data Memory


Mrs. S. Priya, AP- CSE, 23
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
3. Rotate and Swap Instruction:
 The instructions are used to rotate the bit position of the accumulator.

S.No Instruction Operation


1. RLA Rotate Accumulator left
2. RRA Rotate Accumulator right
3. RLCA Rotate Accumulator left through carry flag
4. RRCA Rotate Accumulator Right through carry flag
5. SWAPA Swap with in the Accumulator

Table 3: Rotate and Swap Instruction


Mrs. S. Priya, AP- CSE, 24
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
4. Logical Instructions:
 The logical instructions are used to perform AND, OR, and XOR operations.
S.No Instruction Addressing Operation Byte
 Types: Modes
Content of register is
i. Byte Level Logical Instruction 1. ANL A, Rn Register logically ANDed with that 1
of a Accumulator.
ii. Bit Level Logical Instruction
Direct byte Content of register byte is
2. ANL A,Direct logically ANDed with that 1
of Accumulator
Register indirect The content of register is
3. ORL A,@Ri an address whose content 1
is logically ORed with that
of accumulator.

4. CLR A Implied Clear the 1


accumulator
Table 4: Byte Level Logical Instruction 5. CPL A Complement the 1
Implied
Mrs. S. Priya, AP- CSE, content of accumulator 25
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
4. Logical Instructions:
ii. Bit Level Logical Instruction

S.No Instruction Addressing Modes Byte


1. CLRC Clear carry flag 1
2. CPLC Complement carry flag 1

3. ANL C , Bit AND direct bit to carry flag 2


4. ORL C, Bit Or direct to carry flag 2
5. MOV C , Bit Move direct bit to carry 2
flag
Table 5 : Bite Level Logical Instruction

Mrs. S. Priya, AP- CSE, 26


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
5. Arithmetic Instruction:
 The instructions are used to perform the arithmetic operations.

 The common arithmetic operations such as addition, subtraction, multiplication and


division are as possible with the 8051.
 According to the operation, the instructions are:
i. Incrementing and Decrementing.
ii. Addition
iii. Subtraction
iv. Multiplication and Division

Mrs. S. Priya, AP- CSE, 27


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
5. Arithmetic Instruction:
 According to the operation, the instructions are:
i. Incrementing and Decrementing.

Table 6 : Incrementing and Decrementing Instructions


Mrs. S. Priya, AP- CSE, 28
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
5. Arithmetic Instruction:
 According to the operation, the instructions are:
ii. Addition

Table 7 : Addition, Subtraction, Multiplication, Division Instruction


Mrs. S. Priya, AP- CSE, 29
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
5. Arithmetic Instruction:
 According to the operation, the instructions are
iii. Subtraction

Table 7 : Addition, Subtraction, Multiplication,


Mrs. S. Priya, AP-Division
CSE, Instruction 30
NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
5. Arithmetic Instruction:
 According to the operation, the instructions are:
iv. Multiplication and Division

Table 7 : Addition, Subtraction, Multiplication, Division Instruction

Mrs. S. Priya, AP- CSE, 31


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
CONTROL INSTRUCTION:
 The control instructions are: Jump Instruction, Call and Subroutine Instruction
 It supports the conditional jumping and call functions.

 If any conditional jump occurs, control instruction checks the status


whether it is correct or wrong.
 According to this, it stores the current address in the program
counter and jumps to the appropriate location.
 If the condition is true, it executes the next instruction.

Mrs. S. Priya, AP- CSE, 32


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
6. Jump Instructions, and 7. Call and Subroutine Instructions:

Table 8: Jump
Instructions, and
Call and
Subroutine
Instructions

Mrs. S. Priya, AP- CSE, 33


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
6. Jump Instructions, and 7. Call and Subroutine Instructions:

Table 8: Jump
Instructions,
and Call and
Subroutine
Instructions

Mrs. S. Priya, AP- CSE, 34


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
8. PUSH and POP instruction:

Table 9: PUSH and


POP Instruction.

Mrs. S. Priya, AP- CSE, 35


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
9. Data Exchange instruction:

Table 10: Data Exchange instruction

Mrs. S. Priya, AP- CSE, 36


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
Example Instruction Set Program:

Mrs. S. Priya, AP- CSE, 37


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
Example Instruction Set Program

Mrs. S. Priya, AP- CSE, 38


NIET
Microcontroller 8051:
Instruction Set and Programming
INSTRUCTION SETS OF 8051:
Example Instruction Set Program:

Mrs. S. Priya, AP- CSE, 39


NIET
Microcontroller 8051:
Instruction Set and Programming

Mrs. S. Priya, AP- CSE, 40


NIET
Microcontroller 8051:
Instruction Set and Programming

Mrs. S. Priya, AP- CSE, 41


NIET
Microcontroller 8051
Programming Parallel Ports
• The four ports P0, P1, P2, and P3
each use 8 pins, making them 8-bit
ports.
• All the ports upon RESET are
configured as inputs.
• Port 0 occupies a total of 8 pins (pins
32–39).
• Port 0 can be used for input and
output.
• Each pin must be connected
externally to a 10K-ohm pull-up
resistor.

Mrs. S. Priya, AP- CSE, 42


NIET
Microcontroller 8051
Programming Parallel Ports
 Port 0 as input
• With resistors connected to port 0, to make it an input, the port
must be programmed by writing 1 to all the bits.
• port 0 is configured first as an input port by writing 1s to it, and then data is
received from that port and sent to P1.
• port 0 provides both address and data.

Mrs. S. Priya, AP- CSE, 43


NIET
Microcontroller 8051
Programming Parallel Ports
 Port 1 as input
• In contrast to port 0, this port
does not need any pull-up
resistors since it already has
pull-up resistors internally.
• Upon reset, port 1 is
configured as an input port by
writing 1s to it,
then data is received from that
port and saved in R7, R6, and
R5.

Mrs. S. Priya, AP- CSE, 44


NIET
Microcontroller 8051
Programming Parallel Ports
 Port 2 as input
• Port 2 is configured first as an input port by writing 1s
to it. Then data is received from that port and is sent to P1
continuously.
• P2 is used as simple I/O.
• P0, P1, and P2 I/O operations.

Mrs. S. Priya, AP- CSE, 45


NIET
Microcontroller 8051
Programming Parallel Ports
 Port 3
• Port 3 occupies a total of 8 pins, pins 10 through 17.
• 3 does not need any pull-up resistors.
• ort 3 has the additional function of providing some extremely important
signals such as interrupts.

 I/O ports and bit-addressability.


• Only 1 or 2 bits of the port instead of the entire 8 bits.
• Access either the entire 8 bits or any single bit without altering the rest.
• Syntax “SETB Px.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, “SETB P1.5” sets high bit 5 of port 1.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 46


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O ports and bit-addressability.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 47


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O ports and bit-addressability.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 48


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O ports and bit-addressability.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 49


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O ports and bit-addressability.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 50


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O ports and bit-addressability.
• D0 is the LSB and D7 is the MSB.

Mrs. S. Priya, AP- CSE, 51


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O Reading a single bit into the carry flag
• The carry flag to save or examine the status of a single bit of the port.
• Example: MOV C, Px.y

Mrs. S. Priya, AP- CSE, 52


NIET
Microcontroller 8051
Programming Parallel Ports
 I/O Reading a single bit into the carry flag
• The carry flag to save or examine the status of a single bit of the port.
• Example:

Mrs. S. Priya, AP- CSE, 53


NIET
Microcontroller 8051
Programming Parallel Ports
 Reading Input Pins versus Port Latch
• Reading ports there are two possibilities:
1. Read the status of the input pin.
2. Read the internal latch of the output port.
2. Read the internal latch of the output port. “read–modify–write”

• Some instructions read the contents of an internal port latch instead of


reading the status of an external pin
• The sequence of actions taken when such an instruction is executed is as
follows.
1. The instruction reads the internal latch of the port and brings
that data into the CPU.
2.This data is ANDed with the contents of register A.
3.The result is rewritten back to the port latch.
4. The port pin data is changed and now has the same value as the port.
Mrs. S. Priya, AP- CSE, 54
NIET
Microcontroller 8051
Programming Parallel Ports
 Reading Input Pins versus Port Latch
2. Read the internal latch of the output port. “read–modify–write”
Single instruction all three actions of
(1)reading the port,
(2) modifying its value, and
(3) writing to the port.
 The instruction “XLR P1,#0FFH” performs an XOR logic operation on P1 with
1111 1111 (binary), and then writes the result back into P1

 Notice that the XOR of 55H and FFH gives AAH. Likewise, the XOR of AAH and
FFH gives 55H
Mrs. S. Priya, AP- CSE, 55
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers
• 8051 microcontrollers have two timers and counters which work on the
clock frequency.
• Timer/counter can be used for time delay generation, counting external
events, etc.
• 8051 has two timers Timer0 (T0) and Timer1 (T1), both are 16-bit wide.
• Since 8051 has 8-bit architecture, it is accessed by two separate 8-bit
registers.

Mrs. S. Priya, AP- CSE, 56


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Two Registers- TMR and TCR

8051 has a.Timer Mode Register (TMR)


and b.Timer Control Register (TCR) for
selecting a mode of operation and
controlling purpose.

Mrs. S. Priya, AP- CSE, 57


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TMOD register
 TMOD is an 8-bit register used to set the timer mode of timer0 and
timer1.

 Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1.

Mrs. S. Priya, AP- CSE, 58


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TMOD register

 Bit 5:4 & 1:0 - M1:M0: Timer/Counter mode select bit

Mrs. S. Priya, AP- CSE, 59


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TMOD register

 Bit 6,2 – C (Counter/Timer): Timer or Counter select bit


1 = Use as Counter
0 = Use as Timer

 Bit 7,3 – GATE:


1 = Enable Timer/Counter only when the INT0/INT1 pin is high and
TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
Mrs. S. Priya, AP- CSE, 60
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TCON register

 TCON is an 8-bit control register and contains a timer and interrupt flags.
 Bit 0 – IT0: External Interrupt0 Trigger Type Select Bit
1 = Interrupt occurs on falling edge at INT0 pin.
0 = Interrupt occurs on a low level at INT0 pin
 Bit 1 – IE0: External Interrupt0 Edge Flag (It is set and cleared by hardware).
1 = External interrupt0 occurred.
0 = External interrupt0 Processed.
 Bit 2 - IT1: External Interrupt1 Trigger Type Select Bit
1 = Interrupt occurs on falling edge at INT1 pin.
0 = Interrupt occurs on a low level at the INT1 pin.
Mrs. S. Priya, AP- CSE, 61
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TCON register

 TCON is an 8-bit control register and contains a timer and interrupt flags.
 Bit 3 - IE1: External Interrupt1 Edge Flag (It is set and cleared by hardware)
1 = External interrupt1 occurred.
0 = External interrupt1 Processed.
 Bit 4 – TR0: Timer0 Run Control Bit (It is set and cleared by software)
1 = Timer0 start.
0 = Timer0 stop.
 Bit 5 – TF0: Timer0 Overflow Flag (It is cleared through software)
1 = Timer0 overflow occurred (i.e. Timer0 goes to its max and roll over
back to zero).
0 = Timer0 overflow not occurred.
Mrs. S. Priya, AP- CSE, 62
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Register of TMR and TCR
TCON register

 TCON is an 8-bit control register and contains a timer and interrupt flags.

 Bit 6 - TR1: Timer1 Run Control Bit(It is set and cleared by software)
1 = Timer1 start.
0 = Timer1 stop.

 Bit 7 - TF1: Timer1 Overflow Flag (It is cleared through software)


1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back
to zero).
0 = Timer1 overflow not occurred.
Mrs. S. Priya, AP- CSE, 63
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
1. Mode 0 (13-bit timer mode)
 Mode 0 is a 13-bit timer mode for which 8-bit of THx and 5-bit of TLx (as
Prescaler) are used.
 It is mostly used for interfacing possible with old MCS-48 family
microcontrollers.

 Higher 3-bits of TLx should be written as zero while using timer mode0, or it
will affect the result.
Mrs. S. Priya, AP- CSE, 64
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1& M2
1. Mode 0 (13-bit timer mode)

Programming steps for delay function


1. Load Tmod register value i.e. TMOD = 0x00 for Timer0/1 mode0 (13-bit
timer mode).
2. Load calculated THx value i.e. here TH0 = 0xE3.
3. Load calculated TLx value i.e. here TL0 = 0x14.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing the TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat steps 1 to 7 for the delay again.
Mrs. S. Priya, AP- CSE, 65
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
1. Mode 0 (13-bit timer mode) 8051 timer mode0 code

/*8051_Timer_Mode0*/
#include <reg51.h> /* Include x51 header file */
sbit test = P1^0; /* set test pin 0 of port1 */
void timer_delay() /* Timer0 delay function */
{
TH0 = 0xE3; /* Load 8-bit in TH0 (here Timer0 used) */
TL0 = 0x14; /* Load 5-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}
Mrs. S. Priya, AP- CSE, 66
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
1. Mode 0 (13-bit timer mode) 8051 timer mode0 code

/*8051_Timer_Mode0*/
void main()
{
TMOD = 0x00; /* Timer0/1 mode0 (13-bit timer mode) */
while(1)
{
test = ~test; /* Toggle test pin */
timer_delay(); /* Call timer0 delay */
}
}

Mrs. S. Priya, AP- CSE, 67


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
2. Mode1 (16-bit timer mode)
 Mode 1 is a 16-bit timer mode used to generate a delay, it uses 8-bit of THx
and 8-bit of TLx to form a total 16-bit register.

Programming steps for delay function


1. Load Tmod register value i.e. TMOD = 0x01 for Timer0 mode1 (16-bit timer
mode).
2. Load calculated THx value i.e. here TH0 = 0xFC.
3. Load calculated TLx value i.e. here TL0 = 0x74.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the Mrs.delay again.
S. Priya, AP- CSE, 68
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
2. Mode1 – CODE
/* 8051_Timer_Mode1 */
#include <reg51.h> /* Include x51 header file */
sbit test = P1^0; /* set test pin0 of port1 */
void timer_delay() /* Timer0 delay function */
{
TH0 = 0xFC; /* Load higher 8-bit in TH0 */
TL0 = 0x74; /* Load lower 8-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}
void main()
{ TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */
while(1)
{ test = ~test; /* Toggle test pin */
timer_delay(); /* Call timer0 delay */ } } Mrs. S. Priya, AP- CSE, 69
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
3. Mode2 (8-bit auto-reload timer
 Load the THx-8 bit value only.
mode)
 THx value gets automatically loaded
into the TLx and TLx starts counting
from that value.
 After the value of TLx overflows from
the 0xFF to 0x0, the TFx flag gets set
and again value from the THx gets
automatically loaded into the TLx
register. (Called AUTO LOAD MODE).

Mrs. S. Priya, AP- CSE, 70


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
3. Mode2 (8-bit auto-reload timer
mode)
Programming steps for delay function in 8051 MCU
1. Load Tmod register value i.e. TMOD = 0x20 for Timer1 mode2 (8-bit timer auto
reload mode).
2. Load calculated THx value i.e. here TH1 = 0xA4.
3. Load same value for TLx i.e. here TL1 = 0xA4.
4. Start the timer by setting a TRx bit. i.e. here TR1 = 1.
5. Poll TFx flag till it does not get set.
6. Clear timer flag TFx bit i.e. here TF1 = 0.
7. Repeat from step 5 and 6 for the delay again.

Mrs. S. Priya, AP- CSE, 71


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
3. Mode2 (8-bit auto-reload timer mode)/ CODE
/* 8051_Timer_Mode2 */
#include <reg51.h> /* Include x51 header file */
sbit test = P1^0; /* set test pin0 of port1 */
void main()
{
TMOD = 0x20; /* Timer1 mode2 (8-bit auto reload timer mode) */
TH1 = 0xA4; /* Load 8-bit in TH1 */
TL1 = 0xA4; /* Load 8-bit in TL1 once */
TR1 = 1; /* Start timer1 */
while(1) { test = ~test; /* Toggle test pin */
while(TF1 == 0); /* Wait until timer1 flag set */
TF1 = 0; /* Clear timer1 flag */
}} Mrs. S. Priya, AP- CSE, 72
NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
Timer interrupt in 8051 Microcontroller
 8051 has two timer interrupts assigned with different vector addresses.
 When the Timer count rolls over from its max value to 0, it sets the timer flag
TFx.
 This will interrupt the 8051 microcontroller to serve ISR (interrupt service routine)
if global and timer interrupt is enabled.

 8051 microcontroller jumps directly to the vector address on the occurrence


of a corresponding interrupt.

Mrs. S. Priya, AP- CSE, 73


NIET
Microcontroller 8051
Timers
Introduction to 8051 Timers/ Modes- M0, M1 & M2
Timer interrupt in 8051 Microcontroller/ CODE
/* 8051_Timer_Interrupt */
#include<reg51.h> /* Include x51 header file */
sbit test = P1^0; /* set test pin0 of port1 */
void Timer_init() void Timer0_ISR() interrupt 1 /* Timer0
{ TMOD = 0x01; /* Timer0 mode1 */ interrupt service routine (ISR) */
TH0 = 0x4C; /* 50ms timer value */ { test = ~test; /* Toggle port pin */
TL0 = 0x00; TH0 = 0x4C; /* 50ms timer value */
TR0 = 1; /* Start timer0 */ } TL0 = 0x00; }
int main(void)
{ EA = 1; /* Enable global
interrupt */
ET0 = 1; /* Enable timer0
interrupt */
Mrs. S. Priya, AP- CSE, 74
NIET Timer_init();

You might also like