Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Module_4

The document discusses the configuration and usage of four 8-bit I/O ports (P0, P1, P2, P3) in microcontrollers, detailing how to set them as input or output and their specific characteristics. It also explains the functioning of timers/counters in the 8051 microcontroller, including their modes, registers, and programming steps for generating time delays. Additionally, it covers the importance of proper programming for effective input/output operations and timer functionalities.

Uploaded by

MishtuDeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module_4

The document discusses the configuration and usage of four 8-bit I/O ports (P0, P1, P2, P3) in microcontrollers, detailing how to set them as input or output and their specific characteristics. It also explains the functioning of timers/counters in the 8051 microcontroller, including their modes, registers, and programming steps for generating time delays. Additionally, it covers the importance of proper programming for effective input/output operations and timer functionalities.

Uploaded by

MishtuDeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Peripherals: I/O Ports, Timers-Counters

1
 The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins

 All the ports upon RESET are configured as input, 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 sent to the port

 To use any of these ports as an input port, it must be programmed

MODULE - 4 ECE3003–Microcontroller and Applications 2


MODULE - 4 ECE3003–Microcontroller and Applications 3
PORT-0
 It can be used for input or output, 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
 In order to make port 0 an input, the port must be
programmed by writing 1 to all the bit

MODULE - 4 ECE3003–Microcontroller and Applications 4


PORT-0

MODULE - 4 ECE3003–Microcontroller and Applications 5


PORT-0
 Port 0 is also designated as AD0-AD7, allowing it to be
used for both address and data

 When connecting an 8051/31 to an external memory,


port 0 provides both address and data

MODULE - 4 ECE3003–Microcontroller and Applications 6


PORT-0
The following code will continuously send out to port 0 the
alternating value 55H and AAH
BACK: MOV A,#55H
MOV P0,A
ACALL DELAY
MOV A,#0AAH
MOV P0,A
ACALL DELAY
SJMP BACK
MODULE - 4 ECE3003–Microcontroller and Applications 7
PORT-0
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

MOV A,#0FFH ;A=FF hex


MOV P0,A ;make P0 an i/p port by writing it all 1s
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it

MODULE - 4 ECE3003–Microcontroller and Applications 8


PORT-1
 ‰
Port 1 can be used as input or output
 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
 To make port 1 an input port, it must be programmed as
such by writing 1 to all its bits

MODULE - 4 ECE3003–Microcontroller and Applications 9


PORT-1
The following code will continuously send out to port 1 the
alternating value 55H and AAH
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK

MODULE - 4 ECE3003–Microcontroller and Applications 10


PORT-1
Port 1 is configured first as an input port by writing 1s to it,then
data is received from that port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 an input port by writing it all 1s
MOV A,P1 ;get data from P1
MOV R7,A ;save it to in regR7
ACALL DELAY ;wait
MOV A,P1 ;another data from P1
MOV R5,A ;save it to in regR5

MODULE - 4 ECE3003–Microcontroller and Applications 11


PORT-2
 Port 2 can be used as input or output
 Just like P1, port 2 does not need any pull-up resistors
since it already has pull-up resistors internally
 Upon reset, port 2 is configured as an input port
 To make port 2 an input port, it must be programmed as
such by writing 1 to all its bits

MODULE - 4 ECE3003–Microcontroller and Applications 12


PORT-2
 In many 8051-based system, P2 is used as simple I/O
 In 8031-based systems, port 2 must be used along with P0
to provide the 16-bit address for the external memory
 Port 2 is also designated as A8 –A15, indicating its dual
function
 Port 0 provides the lower 8 bits via A0 –A7

MODULE - 4 ECE3003–Microcontroller and Applications 13


PORT-3
 Port 3 can be used as input or output
 Port 3 does not need any pull-up resistors
 Port 3 is configured as an input port upon reset, this is
not the way it is most commonly used
 Port 3 has the additional function of providing some
extremely important signals

MODULE - 4 ECE3003–Microcontroller and Applications 14


PORT-3

MODULE - 4 ECE3003–Microcontroller and Applications 15


 Write a program for the DS89C420 to toggle all the bits
of P0, P1, and P2 every 1/4 of a second

MODULE - 4 ECE3003–Microcontroller and Applications 16


MODULE - 4 ECE3003–Microcontroller and Applications 17
 Sometimes we need to access only 1 or 2 bits of the port

MODULE - 4 ECE3003–Microcontroller and Applications 18


 Sometimes we need to access only 1 or 2 bits of the port

MODULE - 4 ECE3003–Microcontroller and Applications 19


Create a square wave of 50% duty cycle on bit 0 of port 1.

Solution:
The 50% duty cycle means that the “on”and “off”state (or the high and low
portion of the pulse) have the same length. Therefore, we toggle P1.0 with a
time delay in between each state.

HERE: SETB P1.0 ;set to high bit 0 of port 1


LCALL DELAY ;call the delay subroutine
CLR P1.0 ;P1.0=0
LCALL DELAY
SJMP HERE ;keep doing it

MODULE - 4 ECE3003–Microcontroller and Applications 20


Instructions that are used for signal-bit operations are as
following

MODULE - 4 ECE3003–Microcontroller and Applications 21


 The JNB and JB instructions are widely used single-bit
operations

 They allow you to monitor a bit and make a decision


depending on whether it’s 0 or 1

 These two instructions can be used for any bits of I/O


ports 0, 1, 2, and 3

 Port 3 is typically not used for any I/O, either single-bit


or byte-wise

MODULE - 4 ECE3003–Microcontroller and Applications 22


MODULE - 4 ECE3003–Microcontroller and Applications 23
MODULE - 4 ECE3003–Microcontroller and Applications 24
MODULE - 4 ECE3003–Microcontroller and Applications 25
 In order to measure time between two events it is sufficient
to count up pulses coming from this oscillator, that is
exactly what the 8051 timer does.

 As their names suggest, their main purpose is to measure


time and count external events.

 If the timer is properly programmed, the value stored in its


register will be incremented (or decremented) with each
coming pulse, i.e. once per each machine cycle.

MODULE - 4 ECE3003–Microcontroller and Applications 26


 Besides, they can be used for generating clock pulses to be used in
serial communication, so called Baud Rate.

 The 8051 microcontroller has 2 timers/counters called T0 and T1. The


timer T0 consists of two registers – TH0 and TL0 representing a low
and a high byte of one 16-digit binary number.

 Accordingly, if the content of the timer T0 is equal to 0 (T0=0) then


both registers it consists of will contain 0.

 If the timer contains for example number 1000 (decimal), then the
TH0 register (high byte) will contain the number 3, while the TL0
register (low byte) will contain decimal number 232.

MODULE - 4 ECE3003–Microcontroller and Applications 27


TIMERS
 The 8051 has two timers/counters, they can be used either
as
 Timers to generate a time delay or
 Event counters to count events happening outside the microcontroller

‰
Both Timer 0 and Timer 1 are 16 bits wide

 Since 8051 has an 8-bit architecture, each 16-bits timer is


accessed as two separate registers of low byte and high
byte

MODULE - 4 ECE3003–Microcontroller and Applications 28


TIMERS
 Accessed as low byte and high byte
 The low byte register is called TL0/TL1 and
 The high byte register is called TH0/TH1

MODULE - 4 ECE3003–Microcontroller and Applications 29


TMOD REGISTER
 Accessed like any other register
MOV TL0,#4FH
MOV R5,TH

 Both timers 0 and 1 use the same register, called TMOD


(timer mode), to set the various timer operation modes

 TMOD is a 8-bit register


 The lower 4 bits are for Timer 0
 The upper 4 bits are for Timer 1

MODULE - 4 ECE3003–Microcontroller and Applications 30


TMOD REGISTER

MODULE - 4 ECE3003–Microcontroller and Applications 31


TMOD REGISTER

MODULE - 4 ECE3003–Microcontroller and Applications 32


TMOD REGISTER
 Timers of 8051 do starting and stopping by either software
or hardware control

 In using software to start and stop the timer where GATE=0


 The start and stop of the timer are controlled by way of software
by the TR(timer start) bits TR0 and TR1
 The SETB instruction starts it, and it is stopped by the CLR
instruction

 The hardware way of starting and stopping the timer by an


external source is achieved by making GATE=1 in the TMOD
register
MODULE - 4 ECE3003–Microcontroller and Applications 33
TCON REGISTER

MODULE - 4 ECE3003–Microcontroller and Applications 34


TCON REGISTER

MODULE - 4 ECE3003–Microcontroller and Applications 35


 To generate a time delay
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to
be used and which timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx,target instruction to see
if it is raised and Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again

MODULE - 4 ECE3003–Microcontroller and Applications 36


 To calculate the values to be loaded into the TL and TH
registers, look at the following example
 Assume XTAL = 11.0592 MHz, we can use the following
steps for finding the TH, TL registers’ values
1. Divide the desired time delay by 1.085 us

2. Perform 65536 – n, where n is the decimal value we got in Step1

3. Convert the result of Step2 to hex, where yyxx is the initial hex

value to be loaded into the timer’s register

4. Set TL = xx and TH = yy To generate a time delay


MODULE - 4 ECE3003–Microcontroller and Applications 37
Timer 0 Mode 0 (13-bit timer)
 This is one of the rarities being kept only for the purpose of
compatibility with the previous versions of microcontrollers.

 This mode configures timer 0 as a 13-bit timer which consists of all 8


bits of TH0 and the lower 5 bits of TL0.

 How does it operate? Each coming pulse causes the lower register bits
to change their states. After receiving 32 pulses, this register is
loaded and automatically cleared, while the higher byte (TH0) is
incremented by 1.

 This process is repeated until registers count up 8192 pulses. After


that, both registers are cleared and counting starts from 0
MODULE - 4 ECE3003–Microcontroller and Applications 38
Timer 0 Mode 0 (13-bit timer)

MODULE - 4 ECE3003–Microcontroller and Applications 39


Timer 0 Mode 1 (16-bit timer)

 Mode 1 configures timer 0 as a 16-bit timer comprising all


the bits of both registers TH0 and TL0.

 That's why this is one of the most commonly used modes.

 Timer operates in the same way as in mode 0, with


difference that the registers count up to 65 536 as
allowable by the 16 bits.

MODULE - 4 ECE3003–Microcontroller and Applications 40


Timer 0 Mode 1 (16-bit timer)

MODULE - 4 ECE3003–Microcontroller and Applications 41


Steps to program Timer 0 Mode 1 (16-bit timer)

1. Choose mode 1 timer 0


– MOV TMOD,#01H
2. Set the original value to TH0 and TL0.
– MOV TH0,#FFH
– MOV TL0,#FCH
3. You had better to clear the flag to monitor: TF0=0.
– CLR TF0
4. Start the timer.
– SETB TR0
5. The 8051 starts to count up by incrementing the TH0-TL0.
TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H

MODULE - 4 ECE3003–Microcontroller and Applications 42


Timer 0 Mode 1 (16-bit timer)

6. When TH0-TL0 rolls over from FFFFH to 0000, the 8051 set TF0=1.
TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1)
7. Keep monitoring the timer flag (TF) to see if it is raised.
AGAIN: JNB TF0, AGAIN
8. Clear TR0 to stop the process.
CLR TR0
9. Clear the TF flag for the next round.
CLR TF0

MODULE - 4 ECE3003–Microcontroller and Applications 43


Timer 0 Mode 1 (16-bit timer)

• Assuming XTAL = 11.0592 MHz, write a program to generate a


square wave of 50 Hz frequency on pin P2.3.

Solution:

1. The period of the square wave = 1 / 50 Hz = 20 ms.


2. The high or low portion of the square wave = 10 ms.
3. 10 ms / 1.085 s = 9216
4. 65536 – 9216 = 56320 in decimal = DC00H in hex.
5. TL1 = 00H and TH1 = DCH.

MODULE - 4 ECE3003–Microcontroller and Applications 44


Timer 0 Mode 1 (16-bit timer)
CLR P2.3
MOV TMOD,#10H ;timer 1, mode 1
AGAIN: MOV TL1,#00 ;Timer value = DC00H
MOV TH1,#0DCH
SETB TR1 ;start
BACK: JNB TF1,BACK
CLR TR1 ;stop
CPL P2.3
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;reload timer since
;mode 1 is not
;auto-reload
MODULE - 4 ECE3003–Microcontroller and Applications 45
Timer 0 Mode-2 (Auto-reload Timer)
 Mode 2 configures timer 0 as an 8-bit timer. Actually, timer 0 uses
only one 8-bit register for counting and never counts from 0, but from
an arbitrary value (0-255) stored in another (TH0) register.

 If mode 1 or mode 0 is used, It is necessary to write the number 200


to the timer registers and constantly check whether an overflow has
occured, i.e. whether they reached the value 255.

 When it happens, it is necessary to rewrite the number 200 and


repeat the whole procedure.

 The same procedure is automatically performed by the microcontroller


if set in mode 2.
MODULE - 4 ECE3003–Microcontroller and Applications 46
Timer 0 Mode-2 (Auto-reload Timer)
 In fact, only the TL0 register operates as a timer, while another
(TH0) register stores the value from which the counting starts.

 When the TL0 register is loaded, instead of being cleared, the


contents of TH0 will be reloaded to it.

 Suppose it is necessary to constantly count up 55 pulses generated by


the clock.

 In order to register each 55th pulse, the best solution is to write the
number 200 to the TH0 register and configure the timer to operate in
mode 2.

MODULE - 4 ECE3003–Microcontroller and Applications 47


Timer 0 Mode-2 (Auto-reload Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 48


Timer 0 Mode-2 (Auto-reload Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 49


Timer 0 Mode-2 (Auto-reload Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 50


Timer 0 Mode-2 (Auto-reload Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 51


Timer 0 Mode-2 (Auto-reload Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 52


Timer 0 Mode-3 (Split Timer)
 Mode 3 configures timer 0 so that registers TL0 and TH0
operate as separate 8-bit timers.

 In other words, the 16-bit timer consisting of two registers


TH0 and TL0 is split into two independent 8-bit timers.

 This mode is provided for applications requiring an additional


8-bit timer or counter.

 The TL0 timer turns into timer 0, while the TH0 timer turns
into timer 1.
MODULE - 4 ECE3003–Microcontroller and Applications 53
Timer 0 Mode-3 (Split Timer)

 In addition, all the control bits of 16-bit Timer 1 (consisting


of the TH1 and TL1 register), now control the 8-bit Timer
1.

 Even though the 16-bit Timer 1 can still be configured to


operate in any of modes (mode 1, 2 or 3), it is no longer
possible to disable it as there is no control bit to do it.

 Thus, its operation is restricted when timer 0 is in mode 3.

MODULE - 4 ECE3003–Microcontroller and Applications 54


Timer 0 Mode-3 (Split Timer)

 While Timer 0 is in split mode, you may not start or stop


the real timer 1 since the bits that do that are now linked
to TH0.

 The only real use I can see of using split timer mode is if
you need to have two separate timers and, additionally, a
baud rate generator.

 In such case you can use the real Timer 1 as a baud rate
generator and use TH0/TL0 as two separate timers.
MODULE - 4 ECE3003–Microcontroller and Applications 55
Timer 0 Mode-3 (Split Timer)

MODULE - 4 ECE3003–Microcontroller and Applications 56


MODULE - 4 ECE3003–Microcontroller and Applications 57
 Timers can also be used as counters counting events
happening outside the 8051
 When it is used as a counter, it is a pulse outside of the 8051 that
increments the TH, TL registers
 TMOD and TH, TL registers are the same as for the timer discussed
previously

 Programming the timer in the last section also applies to


programming it as a counter
 Except the source of the frequency

MODULE - 4 ECE3003–Microcontroller and Applications 58


 The C/T bit in the TMOD registers decides the source of
the clock for the timer
 When C/T = 1, the timer is used as a counter and gets its pulses
from outside the 8051

 The counter counts up as pulses are fed from pins 14 and 15, these
pins are called T0 (timer 0 input) and T1 (timer 1 input)

MODULE - 4 ECE3003–Microcontroller and Applications 59


MODULE - 4 ECE3003–Microcontroller and Applications 60
 Assuming that clock pulses are fed into pin T1, write a program for
counter 1 in mode 2 to count the pulses and display the state of the
TL1 count on P2, which connects to 8 LEDs.

MODULE - 4 ECE3003–Microcontroller and Applications 61

You might also like