8051 Timer Programming Techniques
8051 Timer Programming Techniques
MICROPROCESSOR, MICROCONTROLLER,
AND INTERFACING
TECHNIQUES
Contents
2
The timer is used as a time delay An event counter given by external pin
generator, input by internal crystal Count the number of events
oscillator Show the number of events on
Set the initial value of registers registers
Start the timer and then the 8051 counts up. External input from T0 input pin
Input from internal system clock (machine cycle) (P3.4) for Counter 0
When the registers equal to 0 and the 8051 sets
a bit to denote time out
External input from T1 input pin
(P3.5) for Counter 1
External input from Tx input
pin.
We use Tx to denote
8051 T0 or T1.
8051
TH0
P1
to
P2 P1 to TL0 LCD
Set LCD P3.4
Timer 0 TH0 a switch T0
TL0
This bit is used to decide whether the
timer is used as a delay generator or an
event counter.
C/T = 0 : timer
C/T = 1 : counter
PROGRAMMING 8051 TIMERS
6
Timer 0 registers
low byte register is called TL0 (Timer 0 low byte)
and the high byte register is referred to as TH0
(Timer 0 high byte)
can be accessed like any other register, such as A, B,
R0, R1, R2, etc.
"MOV TL0, #4FH" moves the value 4FH into TL0
"MOV R5, TH0" saves TH0 (high byte of Timer
0) in R5
PROGRAMMING 8051 TIMERS
8
Timer 1 registers
also 16 bits
split into two bytes TL1 (Timer 1 low byte) and TH1
(Timer 1 high byte)
accessible in the same way as the registers of Timer
0.
PROGRAMMING 8051 TIMERS
10
Mode 1 programming
16-bit timer, values of 0000 to FFFFH
TH and TL are loaded with a 16-bit initial value
timer started by "SETB TR0" for Timer 0 and "SETB TR1"
for Timer l
timer count ups until it reaches its limit of FFFFH
rolls over from FFFFH to 0000H
sets TFX (timer flag)
when this timer flag is raised, can stop the timer with "CLR
TR0" or "CLR TR1“
after the timer reaches its limit and rolls over, the registers TH
and TL must be reloaded with the original value and TF must
be reset to 0
Steps of Mode 1
(1/3)
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
Steps of Mode 1 (2/3)
5. The 8051 starts to count up by
incrementing the TH0-TL0.
TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H
TR0=1 TR0=0
TH0 TL0
Start
timer
FFFC FFFD FFFE FFFF 0000
Stop
timer
TF = TF = TF = TF = TF =
TF 0 0 0 0 1
Monitor TF until
TF=1
Steps of Mode 1 (3/3)
(a) in hex
(FFFF – YYXX + 1) × 1.085 s
(b) in decimal
Convert YYXX values of the TH, TL register to
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.
Example 2.5
Assuming XTAL = 11.0592 MHz, write a program to generate a
square wave of 50 Hz frequency on pin P2.3.
32
PROGRAMMING 8051 TIMERS
33
Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.
MOV TMOD,#10H
MOV R3,#200
AGAIN: MOV TL1,#08
MOV TH1,#01
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
Solution:
TH – TL = 0108H = 264 in decimal
65536 – 264 = 65272.
One of the timer delay = 65272 × 1.085 s = 70.820 ms
Total delay = 200 × 70.820 ms = 14.164024 seconds
Example
Examine the following program and find the time delay in seconds.
Exclude the time delay due to the instructions in the loop.
35
PROGRAMMING 8051 TIMERS
36
Mode 0
works like mode 1
13-bit timer instead of 16bit
13-bit counter hold values 0000 to 1FFFH
when the timer reaches its maximum of
1FFFH, it rolls over to 0000, and TF is set
PROGRAMMING 8051 TIMERS
37
Mode 2 programming
8-bit timer, allows values of 00 to FFH
TH is loaded with the 8-bit value
a copy is given to TL
timer is started by ,"SETB TR0" or "SETB TR1“
starts to count up by incrementing the TL
register
counts up until it reaches its limit of FFH
when it rolls over from FFH to 00, it sets high TF
TL is reloaded automatically with the value in
TH
To repeat, clear TF
mode 2 is an auto-reload mode
PROGRAMMING 8051 TIMERS
38
Solution:
Some 8051 assemblers provide this way.
-200 = -C8H 2’s complement of –200 = 100H – C8H = 38
H
2’s complement (TH
Decimal
value)
-200 = - C8H 38H
- 60 = - 3CH C4H
- 3 FDH
- 12 F4H
- 48 D0H
(MSB) (LSB)
TCON TF1 TR1
Timer 1
TF0 TR0
Timer0
IE1 IT1 IE0
for Interrupt
IT0
For timer 1
SETB TR1 = SETB TCON.6
CLR TR1 = CLR TCON.6
Solution:
MOV TMOD,#01100000B ;mode 2, counter 1
MOV TH1,#0
SETB P3.5 ;make T1 input port
AGAIN:SETB TR1 ;start
BACK: MOV A,TL1
MOV P2,A ;display in P2
JNB TF1,Back ;overflow
CLR TR1 ;stop
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Example 2.7
1
Programming IE
60
Example 2.8
61
Solution:
(a) MOV IE, #10010110B; enable serial, timer 0,
EX1
(b) CLR IE.1; mask (disable) timer 0 interrupt only
LED
Vcc
P1.3
INT0
How 8051 knows that an interrupt is
activated?
Interrupt LED
Activated
P1.3
Device INT0
What is the minimum duration for which
INT 0/1 pin must be held low for
interrupt to be activated?
LED
High-to-low P1.3
transition
Device INT0
Role of
TCON
Register
D0 IT0 TCON.0 0, INT0 becomes level-triggered interrupt
1, INT0 becomes edge-triggered
IE0
interrupt
IT1 TCON.2 0, INT1 becomes level-triggered interrupt
1, INT1 becomes edge-triggered
IE1
interrupt
TR0
To make INT0 edge-triggered the instruction
TF0 is SETB TCON.0
TR1
To make INT1 edge-triggered the instruction
D7 TF1
is SETB TCON.2
How Edge-triggered Interrupts are
activated and serviced by 8051?
to be continued…
Role of TCON Register
D0 IT0
Display
• 7 segment display is a 10 pin electronic component with eight
led’s embedded inside it to displays number from 0 to 9 even we
can display few characters like A, B, C, H, E, e, F, etc. .
• It is available in two configurations:
• common cathode
• common anode
• In Common Anode to light up any given segment the
corresponding Cathode pin should be connected to ground or
reference ,which completes the circuit and LED will be in
forward bias.
• In Common Cathode to corresponding LED Anode pin the
circuit will be completed and the particular LED will be light up
since it will be in forward bias.
Prepared By: Ms. K. D. Patil 15
8051 Interfacing
ADC
• Digital computers use binary values but
in physical world everything is Analog
(continuous) eg. Temperature, Pressure
• A physical quantity is converted into
electrical (voltage, current) signals using
a device called as Transducer (also
referred as Sensors).
• We need Analog-to-Digital converter
(ADC) to translate Analog signals to
Digital number so that Microcontroller
can read and process them.
• ADC0808 allows us to monitor upto 8
different analog input channels using
single chip
Prepared By: Ms. K. D. Patil 18
ADC080
8
Prepared By: Ms. K. D. Patil 19
Address Lines
(A,B,C)
• ADC0808 has 8-bit
Selected ADC
channel
C B A data output
IN0 0 0 0 • 8 analog input channels
IN1 0 0 1 are multiplexed and
IN2 0 1 0
selected using three
IN3 0 1 1
IN4 1 0 0
address pins A, B & C.
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
Prepared By: Ms. K. D. Patil 20
8051 Interfacing
ADC
Prepared By: Ms. K. D. Patil 21
If the clock frequency is high D Flip Flop is added to reduce the frequency by
2^(N), N- no of flipflops
Prepared By: Ms. K. D. Patil 23
Signal
Conditioning
Analog World
• Sensors produce the output in the (temperature, pressure)
Connecting LM35 to
ADC
Temperature Vs Vout for
ADC Since ADC has 8 bit
resolution with a max of
Temp (C) Vin (mV) Vout (D7-
D0)
256 steps and LM35
produces 10mV for every
0 0 0000 0000
degree of temperature
1 10 0000 0001
change, we can condition
2 20 0000 0010 Vin of the ADC to produce
3 30 0000 0011 a Vout of 2.56Vfor full
10 100 0000 1010 scale output. So Vref= 2.56
30 300 0001 1110
Prepared By: Ms. K. D. Patil 26
Interfacing
Diagram
8051 INTERFACING WITH DAC
Prepared By: Ms. K. D. Patil 27
8051 Interfacing
DAC
• Microcontroller are used in wide variety of applications like for
measuring and control of physical quantity like temperature,
pressure, speed, distance, etc.
• In these systems microcontroller generates output which is in
digital form but the controlling system requires analog signal as
they don't accept digital data thus making it necessary to use
DAC which converts digital data into equivalent analog voltage
• Digital to Analog Converter is a device used to convert digital
pulses to analog signals.
• In the figure shown, we use 8-bit DAC 0808. This IC converts
digital data into equivalent analog Current. Hence we require an I
to V converter to convert this current into equivalent voltage.
• DAC0808 provides 256 discrete voltage (or current) levels of
output.
Prepared By: Ms. K. D. Patil 28
DAC08
08
• In the MC1408 (DAC0808), the digital inputs are converted
to current (Iout), and by connecting a resistor to the I out pin,
we convert the result to voltage.
• The total current provided by the I out pin is a function of the
binary numbers at the D0 – D7 inputs of the DAC0808 and
the reference current (Iref), and is as follows:
• where D0 is the LSB, D7 is the MSB for the inputs, and I ref
is the input current that must be applied to pin 14. The I ref
current is generally set to 2.0 mA.
Prepared By: Ms. K. D. Patil 29
Prepared By: Ms. K. D. Patil 30
Interfacing
Diagram
125