MPMC Unit 5 Scanned
MPMC Unit 5 Scanned
7148 BCD to
Seven-segment
dscoder
abcdef g abcdef g
I
I
P1.0
P1.1
P1.2
P1.3
Pl.4
P1.5
P1.6
P1.7
RST
(RXD)P3.0
(TXo) P3.1
(rNTo) P3.2
1ffieor P2.7
l;u
(T0)P3.4 P2.6 Switch
(IUP3.s P2.5
(wR) P3.6 P2A
P2.3
P2.2
P2.1
P2.0
initialized to displ
Fig. 12.19 Circuit diagram for timerlstopwatch operation
LSB of porr 2. If r
starting the timer I
in the four displays, we have to connect the pins of two ports to the display code keeps looping in tt
converter. Here, the port pins of P0 and Pl are used. The BCD data of the display
display is increme
is given to the port pins. pon pins by writin
For stopwatch operation, an additional switch is needed to switch on and off as long as the su irr
counting in the stopwatch. A separate push-to-on switch is connected to the LSB
The hardqare.
of port 2. The voltage on this pin can be sensed to control the counting and display
additional I'eatures
ip the seven-segment displays.
stop the stopwatch
The program for stopwatch operation involves a timer register, which is
incremented at regular intervals. The interval can be programmed to be either
12,11 MTCROCON
(1/10)'h of a second or I second. with the interval of (1/10)th of a second and a
TMFFIC LI
4-digit display, the hardware arrangement can be used to count up to 999.9
seconds. with an interval of 1 second, the hardware ,urangement can count up Figure 12.21 shou
ta 9,999 seconds. The timer register can be programmed to generate an interrupt The follorvin_e a
after every interval. rre tiee left rurns ir
The flowchart for the stopwatch example is given in Fig. 12.20.The program .l-i"-ir,'in in Figures i
involves first initializing the timer to the required time interval. The display is also ,: traffic signals._
805.1 INTERFACE EX{V9.ES 393
Write TMOD, TCON, and timer values into the timer registel
oulput data to initialize ihe dlsplay
initialized to display all zeros. Then the status of the switch is sensed from the
LSB of port2. If the switch is pressed, the program moves to the next step, i.e.,
r*atch operation
starting the timer and giving data output to the displays. Otherwise, the program
oi tu'o ports to the disPlaY code keeps looping in the status check on P2.0. After every timer overflow, the data for
x The BCD data of the disPlaY display is incremented and converted into BCD form. New data is given to the
port pins by writing them to the port registers. Data in the display is incremented
:. needed to switch on and off as long as the switch on P2.0 is pressed.
rlch is connected to the LSB
-'.r The hardware, flowchart, and program can be improved further to include
..;ntrol the counting and display additional features suchas resetting the display, using separate switches to start and
stop the stopwatch, and using a press switch to switch on and off the stopwatch.
r:r a timer register, which is
r-., be programmed to be either 12.1 1 MICROCONTROLLER APPLICATION EXAMPLE-
.i ,.f (I/10)'h of a second and a TRAFFIC LIGHT CONTROL
:e used to count up to 999.9
Figure 12.21 shows the basic traffic control signals at a four-road junction.
lr J.re arrangement can count up
The following assumptions are made for designing the traffic light control. There
rrnrmed to generate an interruPt
are free left turns in all the four directions. Traffic is allowed in the four sequencrs
shown in Figures 12.22 (a)-I2.22 (d). Each sequence again has two combination'
r',rn in Fig. 12.20. The Program
of traffic signals-one for red and green and the other for red. green. and rellor''
r::rc interval. The display is also
8051 INTERFACE EXAV>-!S I}7
in different directions. This program is written using the look-up table concept. So
the same program can be used if LEDs are connected through the 8255 PPI pon
pins.
di spl ay data.
L00P: M0VX A, @DPTR Get the hex data to be given to port 0.
MOV PO, A Give it to port 0.
lNC DPTR Point to the next data.
I4OVX A, @DPTR Get the data for port 1.
MOV P1,A 0utput it to port 1.
LCALL DELAYl Call the r"equired time de1ay.
INC DPTR Point to the next data.
MOVX A, @DPTR Get the hex data to be given to port 0.
MOV PO,A Give it to port 0.
INC DPTR Poi nt to the next data.
I.4OVX A. @DPTR Get the data for port 1.
MOV P1, A 0utput it to port 1.
LCALL DELAYZ CalI the nequired tjme delay for the yelIow
agram for traffic lights
I i sht.
I are used for the lights in the east
INC DPTR Point to the next data.
outh direction.
DJNZ RO, LOOP Decrement counl; if it is not zero. loop for
: port pins for the sequences shown
the next sequence.
KTAL2, and RST in Fig. 12.23 can
SJI"]P START Loop again for contjnuous sequencin,o.
pins and clock inputs are connected
LED_DATA: DB 0C, 09,
14, 09, ?1, 09. 21.
r Eaffic light conkol 0A,09,0c,09,14.
49, ?1, 0A, 21 Table for the hex values fcr por*,s.
I NR TYG WY VUR port 0 Port 1 END Termi nate program executi on.
L4 P0.3 P0.2 P0.l pg.6 Data Data
This program can be rewritten using the SETB and CLRB instructions, as all
I l0 O OCH 09H the port pins are bit-addressable. However, the program becomes lengthy as each
0 10 0 14H 09H LED has to be individually controlled.
0 00 I zlH 09H
1 2,12 MICROCONTROLLER APPLICATION EXAMPLE-THERMOMETER
0 00 I 2IH OAH
I 00 1 09H OCH Temperature measurement is one of the most common tasks in industries. The
thermistor (temperature-sensitive resistor) is an electrical component capable
1 00 1 09H 14H
of measuring temperature variations, relying on the change in its resistance
I 00 1 09H 2tH u'ith changing temperature. Once calibrated against the thermistor equation, it
1 O OAH ztH is possible to electronically determine the temperature around the thermistor by
measuring the change in voltage across it, as its resistance changes.
ntroller-based traffic light control is Thermistors operate in a limited temperature range (typically -80 "C to 150 "C).
r change the operation of the traffic The significant advantages of the thermistor as a tool for temperature measurement
lity of different delays for the traffic are as follorvs:
398 MrcRopRocESSoRSANDMlcRocoNTRorLERS
the resistance goes up as the temperature goes down. of all passive temperature rr
I ln
measurement sensors, thermistors have the highest sensitivity (resistance change per
degree of temperature L_l
change). Thermistors I
Freclslon
rrrJnce for longer periods of time
id-range temperatures
Vo to ADC inpul
Thermistor
2C 30 40 50 60 70 80 90 100
Fig. 12.25 Simple circuit to measure temperature using thermistor
Tempelature (eC)
7
P1 0 00
a
P1.1 D1
I
P1.2 D2
10
P1.3 D3
11 LCD
P1.4 D4
8051 12
P1.5 D5 Fig.
P1.6 D6
14 fast mode and 10-b
E R/W RS GND Vo
P1.7 D7
V.. and more address s1
P?.0
6 4 1 3l z I2C bus, with speed'
P2.1
* existing and future
cc
P2.2
12.13.1Details of F
Tat
Fig.12.26 Circuit diagram for temperature measurement using microcontroller
Term Dt
any physical quantity. To measure quantities other than temperature, instead of
the thermistor and bridge alrangement, a circuit should be provided to convert the
)laster fi
tcr
physical quantity into a voltage.
Slave Tl
12,13 RTC INTERFACING USING I'C STANDARD Transmitter Tl
There are a number of peripheral integrated circuits (ICs) in modern electronic Receiver Tl
systems that have to communicate with one another and with the outside wodd.
To simplify the circuit design, Philips developed a simple bidirectional two- It is the respon
wire. serial data (SDA) and serial clock (SCL) bus for inter-IC control. This is I:C bus. Each mas
called I:C bus. The I2C bus supports chips manufactured by any IC fabrication I{ bus. Bus clock
process (NMOS, CMOS, and bipolar) and also the extremely broad range of I2C- br a slave that is r
compatible chips from Philips and other suppliers. It has become the worldwide ihe clock line in tl
industry standard proprietary control bus. Figure 12.27 shows the interconnection rrming diagram oi
of various devices using the I2C bus. rrf data from and t
The basic I2C bus, with a data transferrate ofup to 100 Kbits/s andT-bit addressing, 12,13.1.1 Sfarf an
was introduced twenty years ago. However, as data transfer rates and application \nr data transt-er
functionality rapidly increased, the I2C bus specification was enhanced to include :ne data transler
8051 TNTERFACE EXAUP.ES 393
Write TMOD, TCoN, and limer values into the timer register
Output data to initialize the display
initialized to display all zeros. Then the status of the switch is sensed from the
LSB of port2.If the switch is pressed, the program moves to the next step, i.e.,
tscwatch operation
starting the timer and giving data output to the displays. Otherwise, the program
L\i two ports to the display code keeps looping in the status check on P2.0. After every timer overflow, the data for
r.l The BCD data of the display display is incremented and convefted into BCD form. New data is given to the
port pins by writing them to the port registers. Data in the display is incremented
h i. needed to switch on and off as long as the switch on P2.0 is pressed.
r i,,i itch is connected to the LSB The hardware, flowchart, and program can be improved further to include
r --r-rntrol the counting and display additional features such as resetting the display, using separate switches to start and
stop the stopwatch, and using a press switch to switch on and off the stopwatch.
i'.es a timer register, which is
:.rn be programmed to be either 12.1 1 MICROCONTROLLER APPLICATION EXAMPLE-
r rl oi (1/10)'h of a second and a TRAFFIC LIGHT CONTROL
r bc used to count uP to 999"9
Figure 12.21 shows the basic traffic control signals at a fbur-road junction.
l'x are arrangement can count up
The following assumptions are made for designing the traffic light control. There
r^.rmmed to generate an interuPt
are free left tums in all the four directions. Traffic is allowed in the four sequence q
shown in Figures L2.22 (a)-L2.22 (d). Each sequence again has two combination.
Err cn in Fig. 12.20. The program
of traffic signals----oneforredandgreenandtheotherforred. green. and relltlu,
I :rme interval. The display is also
394 MtcRoPRocESSoRSANDMIcRoc0NTRoLLERS
Figures 12.22 (aY12.22 (d) shows the traffic signals that are on' For example, in
Fi;. D.Z2 (a), the geen light (WG) in the west direction is on and the red light is
on in all the other three directions. All other lights are off and they are not
shown in
Fie. r?.?2 (a). \.1
()
- ,/
<-----
I I
ONR I
I CNv
I CHe I
w WRWYWG
occ COO
EG EY ER
E
Fig 1
SGC
SYC [- it
sRo I
:
I
I --\'
I
I
,1-
i
I
r_
!\&
Fig.12.21 Traffic light controls at four road junction
Trafflc light control using a microcontroller can be done easily with parallel ports'
The port pins can be connected to each light, LED, or group of LEDs through SR
a proper driver circuit. The data in the parallel ports can be changed using the
p*gru-, for turning on and off the lights. Figure 12.23 shows the connection
aiugru* for all the lights, which are assumed to be LEDs in this example. The port
pins of port 0 and port 1 are used. The least significant three bits of port 0 are used
Fig '
ior the west direction. The port 0 pins 3, 4, and 5 are used for the north direction'
-.iL)
GEYER
L---l :-- I
r-] wQ
WY
r-l
I -a
r-)E l/' r) E
ER Ipn
t^
s0lU
I
.l
Fig. 12.22 (d) Traffic light control sequence 4
r::,sequence 1
396 MTcRopRocESSoRSANDMtcRocoNTRoLLERS
in different direcr
the same prograr
pins.
START: l40V R
I1|]V DPTR.
t00P: MOVX i, .
r'10v P0 , A
] IIC DPTR
i.'c\/Y A,
@DP--
,,,i1/ Dt I
-ALL DELAY.
..,C DPTR
'.'_,, A, @DPlr
.,
. , DPTR
. .\. (vur -
" Dl A
' nal r 1
Fig.12.23 Hardware connection diagram for traffic lights
Similarly, the least significant three bits of port 1 are used for the lights in the east
direction and pins 3,4, and 5 are used for the south direction.
- .'- i0, LC--
Table 12.8 shows the data to be given to the port pins for the sequences shown
in Fig. l2.22.The other pins such as XTALI, XTALZ, and RST in Fig. 12.23 can
be connected as shown in Fig. 12.19. The reset pins and clock inputs are connected ,..,: START
:_- - -_- ,-.a.!!!. nD
to the corresponding circuit. l. 10
Table 12.8 Data for port pins for traffic light control : -c
SG SY SR EG EY ER NG NY NR WG
:,2-,'-:. _
WY wR 0
Port Port 1
Sequence
p1.5 p1.4 p1.3 p1.2 p1.1 p1.0 p0.5 p0.4 p0.3 p0.2 p0.1 p6.g Data Data
This program
00100100110 O OCH O9H
:.port pins are t
Sequence 1
00100101010 0 14H 09H I ED has to be inc
00100110000 I zTH O9H
Sequence 2 12,12 MtCRoCot
00101010000 I zIH OAH
00110000100 1 O9H OCH ;rrnpcrature mea
Sequence 3
:..3:mi\tor (temF
01010000100 1 09H t4H ': n:eesuring ten
I 0000100 00 1 09H 21H :.:: ;hanging ter
Sequence 4
I 0000100 0t O OAH 21H r 3-,ii1frlg to ele.
-siirnng the cha
One of the advantages of this type of microcontroller-based traffic light control is I::ermrstors t'rp
the ease of programming and the flexibility to change the operation of the traffic --:,: .:5tnCant ad'
lights. Another advantage is the programmability of different delays for the traffic --; f. ilrl^!)\\!l
8051 INTERFACE EXAV}.:S 3!}7
in different directions. This program is written using the look-up table concept. Scr
the same program can be used if LEDs are connected through the 8255 PPI port
pins.
di spl ay data.
L00P: MOVX A, @DPTR Get the hex data to be given t0 port 0.
MOV PO, A Give jt to port 0.
INC DPTR Point to the next data.
I'4OVX A, @DPTR Get the data for port 1.
1.10v P1 , A 0utput it to port 1.
LCALL DELAYl Call the requined time delay.
INC DPTR Point to the next data.
MOVX A, @DPTR Get the hex data to be given t0 por"t 0.
Gi ve t to port 0.
.i
t40v P0 , A
lNC DPTR Point to the next data.
MOVX A, @DPTR Get the data fon port 1.
MOV P1, A 0utput it to port 1.
agram for traffic lights
LCALL DELAYZ Call the required time delay for the yellow
I i ght.
I are used for the lights in the east
lNC DPTR Point to the next data.
outh direction.
DJNZ RO, LOOP Decrement count: if it is not zer0, loop for
pon pins for the sequences shown
(TAL2, and RST in Fig. 12.23 can the next sequence.
SJMP START Loop agai n f or ccnt i"uoi.rs secuenci ng .
7
P1 0 DO
o
P1.1 D1
I
Pl.2 D2
10
P1.3 D3
11 LCD
Pl.4 M
8051 12
P1.5 D5 Fig.
IJ
D6
fast mode and lGt
P'1.6
14
E RiW RS GND Vo
P1.7 D7
V.. and more address s;
P2.0
6 5 4 1 3l z IrC bus, with speed
P2.1
* existing and future
cc
P2.2
12.13.1Details of F
N
J as an LCD driver
ADCOSO4
+R, rR be considered as r
?2.3 e^
uY Table 12.9. The u
P2.4 Conversion complete bus. A master is tt
P2.5 RD the clock signals t,
addressed by the n
Ta!
Fig. 12.26 Circuit diagram for temperature measurement using microcontroller
Term Ix
any physical quantity. To measure quantities other than temperature, instead of
the thermistor and bridge affangement, a circuit should be provided to convert the
Master Tt
tel
physical quantity into a voltage.
Slave TI
12,13 RTC INTERFACING USING I'C STANDARD Transmitter Tl
There are a number of peripheral integrated circuits (ICs) in modern electronic Receiver Tl
systems that have to communicate with one another and with the outside wofld'
To simplify the circuit design, Philips developed a simple bidirectional two- It is the respon
wire. serial data (SDA) and serial clock (SCL) bus for inter-IC control. This is I:C bus. Each mas
called I:C bus. The I2C bus supports chips manufactured by any IC fabrication I{ bus. Bus clock
'nr a slave that is r
process (NMOS, CMOS, and bipolar) and also the extremely broad range of I2C-
compatible chips from Philips and other suppliers. It has become the worldwide rhe clock line in tl
industry standard proprietary control bus. Figure 12.27 shows the interconnection ruming diagram of
of various devices using the I2C bus. urf data from and t
The basic I2C bus, with a data transfer rate of up to 100 Kbits/s and 7-bit addressing, 12.13.1.1 Starf an,
was introduced twenty years ago. However, as data transfer rates and application \ny data transt'er
functionality rapidly increased, the I2C bus specification was enhanced to include :ne data transtcr
8051 INTERFACE EXAMPLES 401
fast mode and i0-bit addressing, thereby satisfying the demand for higher speeds
and more address space. More recently, the high speed mode has been added in the
I2C bus, with speeds of up to 3.4 Mbits/s, which ensures that the I:C bus can support
existing and future high speed serial transfer rates for different applications.
:ircuits (ICs) in modern electronic Receiver The device that receives data from the bus
nother and with the outside wodd.
loped a simple bidirectional two- It is the responsibility of the master device to generate the clock signals on the
-r bus for inter-IC control. This is I:C bus. Each master generates its own clock signals when transferring data on the
anufactured by any IC fabrication I:C bus. Bus clock signals from a master can only be altered when they are stretched
r the extremely broad range of I2C- by a slave that is operating slowly and holding down the clock line (i.e., keeping
hers. It has become the woddwide the clock line in the low state) or by another master when arbitration occurs. The
timing diagram of the various conditions in the I2C bus, and the reading and writing
're 12.27 shows the interconnection
of data frorn and to the bus are explained in Sections I2.I3.l.l-12.13.1.6.
p to 100 Kbits/s and 7-bit addressing, 12.13.1.1 Sfarf and Sfop Condifions
r data transfer rates and application
.\ny data transfer in the I2C bus must be initiated with a start (S) condition. .\fter
--ification was enhanced to include rhe data transfer is completed, it must be terminated by a stop (P) condition.
402 MrcRopRocEssoRS AND MtcRocoNTRoLLERS
Figure 12.28 shows the timing diagram for the start and stop condirions. A high-
to-low transition on rhe SDA line when SCL is high defines a start condition. A
low-to-high rransirion on the SDA line when scl- is high defines a stop condition.
The start and stop conditions are always generated by the master. Th6 bus is
considered to be busy after the start condition is generated. The bus is considered
to be free again, a certain time after the stop condition is generated.
START or
repeated STMT
condilbn
Fi1.12.28 Start (S) and stop (p) conditions in t2C bus
The bus stays busy if a 'repeated start' (Sr) is generated instead of a stop
condition. In this respect, the start (S) and repeated sta( (Sr) conditions are
12.13.1.4 Ackno
functionally identical. Detection of start and stop conditions by the devices
connected to the I2c bus is simple if they incorporate the necessary interfacing Data transf er u r
hardware. However, microcontrollers with no such interface have to sample the related clock pu
SDA line at least twice per clock period to sense the transition. -rne to the high
Jou n the SDA lr
12.13.1.2 Data Validig Condition
Frg. 12.31. The
The data bit on the SDA line (high or low) must be stable during the high period :eieiver that hai
of the clock. The state of the data line can change only when the clock signal on
the SCL line is low, as shown inFig. 12.29.
h€
iSDA) ril
soA
Ca
iSDA) 16
q
't
Data line stabie;
dah vatid
scL
\J'\,-ti$i
ACK l---i
STOP or
repeated START
condition
receiver that has been addressed must generate an Acknowledge (A) signal after
st be stable during the high period
nre only when the clock signal on
{sDA)
----\_ (SDA)
f
Clock pulse for
ackno\,rledgemsnt
each byte has been received. The master can then generate either a stop condition
to
prcr data transfer in the I2C bus is abort the data transfer or a repeated start condition to start a new data transf'er' lf a
(A) signal from master that is acting as a receiver is involved in a data transfer, it must signal the end
an Acknowledge
of data transfer to the slave transmitter by generating a Not-acknowledge signal
(A)
rrticant bit (MSB) first, as shown in
mit another complete byte of data on the last byte that was clocked out of the slave. The slave transmitter must release
r.. servicing an internal or external the SDA line to allow the master to generate a stop or repeated start condition'
' low state to force the master into a 12.19.1.5 Writing Data to Slave Receiver by Master Transrnifrer
hc slave is ready for anotherbyte of
Figure 12.32 shows the format of the I2C frame when a master transmitter writes
\tate. of data tiorn
data to a slave receiver. The shaded portion indicates the transfer
404 MrcRopRocESsoRSANDMrcRocoNTRoLLERS
Fig' 12.33 Master receiver reading data from slave transmitter having sub-addresses
:lA EOU P0.6 : I.=
:'L
--! Enlr Dn a
LUU tU./ . -^-'=
|
8051 INTERFACE EXAIq.ES .05
c- ',,
If
W
the master wants to read data bytes from a specific location or internal
register in the slave successively, the sequence of operations done by the master is
L Data translened (in J as follows:
o rw'ritet
bytes + Acknowledge) (i) The master sends the start (S) condition.
bslrw A=Acknowledge($DAlow)
(ii) The master sends the 7-bit slave address, with the read/write (R/W-) bit
A = Not-acknowlodge (SDA high)
) irtsiot S = $TART mndition (which is 0) appended as the LSB. The slave sends the Acknowledge (A)
P = STOP condition
signal.
fuster transmitter sending data to slave
(iii) The master sends the sub-address of a particular location or register in the
receiver in l2C bus slave from where it wants to read data. This is called dummy write. The
slave sends the Acknowledge (A) signal.
a slave receiver: (iv) The master sends a repeated start condition (Sr).
(v) The master again sends the 7-bit slave address, with the read/write (R/W-)
ess. with the read,/write (R/W-) bit bit, which is set to 1 now, to indicate read operation.
xample, if the master wants to write (vi) The slave sends data bytes one by one to the master and the master sends an
r 7-bit address 40H (i.e., 1000000 Acknowledge signal (A) after each byte read. [f it is the last byte to be read
appended as the LSB to this 7-bit from the slave then the master sends a Not-acknowledge signal (A).
.. l0000000inbinary) anditis sent (vii) The master sends a stop (P) condition to end the data transfer,
When data is being read from a slave transmitter with no sub-addresses (such
ignal.
as an analog-to-digital converter) by a master receiver in the I2C bus, the following
l€ to the slave and the slave sends
sequence of operations has to be performed:
transferred. For the last byte sent by
acknowledge signal (A).
(i) The master sends the start (S) condition.
cnd the data transfer.
(ii) The master sends the 7-bit slave address, with the read/write (R/W) bit
(which is 1) appended as the LSB. The slave sends the Acknowledge (A)
rnd RTC, which have sub-adf,resses signal.
Gfrffilocations within the slave), (iii) The slave sends data bytes one by one to the master and the master sends
r! te that is sent by the master is the an Acknowledge signal (A) after each byte read. If it is the last byte to be
egister within the slave from where read from the slave,
ll be written in the slave. The sub- the master sends a
ss pointer in the slave to point to a Not-acknowledge
Data transfoned (in
signal (A).
t by Master Receiver (iv) The master sends a ffi Frommaslertoslave f] Fromslavelomaster
stop (P) condition to
ner by a master receiver on the I2C
end the data transfer" Fig.12.34 Master receiver reading data from slave
upon whether the addressed slave
transmifter having no sub-addresses
u*ually needed to access data from This is shown inFig. L2.34.
pu s the transfer of data between a
12.13.2 8051 Subroutines used to lmplement l2C Bus
s sub-addresses.
We can implement the I2C bus by using any two pins of the 8051. The 8051
W r rl.rat
L Datatransfened(in
byt6s + acknowledge)
]
subroutines (written in assembly language programs), which are used to implement
various functions involved in the I2C bus are given here. Port pins P0.6 and P0.7 of
the 8051 are assumed to be used as the SDA line and the SCL line, respectivelr'.
of the I2C bus.
t1@s suLaddrgss
; Ports used for I2C bus
pe ransmitter having sub-addresses
SDA EOU P0.6 ; The symbol SDA corresponds to pin P0.6.
SCL EOU P0.7 ; The symbol SCL conresponds to pin P0.7.
406 MTcRopRocESSoRSAND MTcRocoNTRoLLERS
RLC A Rotate A left through the canr"y to transfer one bjt : :ner the 2.1-hou:
in A to the carry, starting from lhe l4SB. :-r pin diagram
vn\/ qrl^ f Move the bit in the carr^y flag to SDA. -<-..e circuit that
5:r3 SCL Set SCL to 1. ) ..::.heS to the I
DiriZ ri, REP Decrement R6 and go to REP, if R6 is not 0 (to :':.lected to it.
transmit the next bit). , - .e the DS1-1(
CLR SC. Set SCL to 0. ..i,- has a progra
SITB SDA Set SDA to 1. - . :..:rmes less th:
REI Ret u rn -: .'.cillator run:
; Sending the Ac know.l edge si gnal . . - .,r'ole in plasr
I2C-ACK: -' .re rSOt forrr
C LR SI]A Set SDA to 0. I-.c iunctional
SETB SCL Set SCL to 1. -. -.:.:.:ins an oi,
CLR SCL Set SCL to 0. -. :a, ,-: h,anen -b
SETB SDA Set SDA to 1. - -.:: : ::fer. nruitrl
, : -:. anJ ;crnir(
8051 TNTERFACE =-XdtPj: {07
RET ; Return,
; Sending the Not-acknowl edge si gnal
1 2C_NACK:
SETB SDA Set SDA to 1.
SETB SCL Set SCL to 1.
CLR SCL Set SCL to 0.
RET Ret u rn
; Rece i vi ng data from a slave to negister A of the 8051 (master)
12C_RECEIVE:
[10v R6, /108 Load the number of bits in A (i'e" B) in R6.
REP2:
CLR SCL Set SCL to 0.
SETB SCL Set SCL to 1.
MOV C. SDA Move the bit SDA to-the ea.rry flag'
RLC A Rotate A I eft thr"ough {h. .u.ty, to rece.i ve one bi t
from the carry, star"ting from the l4SB.
DJNZ R6, RtP2 Decrement R6 and qo to REP2, if R6 is not 0 (to
receive the next bit).
CLR SCL Set SCL to 0.
SETB SDA Set SDA to 1.
RET Ret u nn
RET ; Return,
; Sending the Not-acknowl edge si gnal
I ZC_NAC K :
SETB SDA Set SDA to 1.
SETB SCL Set SCL to 1.
CLR SCL Set SCL to 0.
RET Ret u rn
; Receiving data from a slave to register" A of the 8051 (master)
I2C_RECElVE:
r40\/ R6, ,108 Load the number of bits in A (i'e', B) in R6.
REP2:
CLR SCL Set SCL to 0.
SETB SCL Set SCL to 1.
I4OV C, SDA Move the bit SDA to-the eariY flag.
RLC A Rotate A Ieft thnough {h. ca.ry' to receive one bit
from the carry, star"ting from the MSB.
DJNZ R6, RTP2 Decrement R6 and go to REPZ, if R5 is not 0 (to
receive the next bit).
CLR SCL Set SCL to 0.
SETB SDA Set SDA to 1.
DFT Ret u nn
12.13.3.2 Detaited Da
The DS1307 operates
implementing a start c
is 1i01000 (in binan
Subsequent registers
executed. When V..
access in progress anr
device will not be rec
being written to the de'
Fig. 12.36 Functionat btock diagram of DS1307 V"o, the device switcl
12.13.3.1 Pin Details of D51307 the device switches frr
and recognizes inputs ,
is set to 1, the oscillator is disabled. When CH is cleared to 0, the oscillator is RS1 RSO
enabled. On first application of power to the DS1307, the time and date registers
are typically reset to 01/01/00 01 00:00:00 (which corresponds to month/datelyear
00
day-of-week hour:minute:second). The CH bit in the seconds register will be set OI
to 1. clue to which the oscillator is disabled. The clock can be halted whenever the 10
timekeeping tunctions are not required. This minimizes the battery current. 11
The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours XX
register at address 02H is defined as the l2-hour or 24-hour mode-select bit. When XX
it is high, the 12-hour mode is selected. ln the 12-hour mode, bit 5 is fhe AM/PM
bit. rvith logic high used to represent PM. In the 24-hour mode, bit 5 is the second Rate select (RS l:
l0-hour b\t (20-23 hours). The hour value must be re-entered whenever the 12124- -:nut when the squa
hour mode bit is changed. ,, .'' e tiequencies thi
When reading from or writing into the time and date registers, user buffers i.' .i er to the DS 1301
(i.e., secondary registers) are used to prevent errors when the internal registers
'2.13.3.3 Data Trans;
update. During this process, the secondary registers are synchronized to the
intemal registers on any I2C start condition. The time information is read from
l:.: DSl307 can ol
- ::: l-i \l'ritten into tl
these secondary registers while the clock continues to run. This eliminates the
need to re-read the registers, in case the internal registers update during a read.
:-.initter mode (dr"
' -,r. :: a microcontr,
The internal divider chain in the DS1307 is reset whenever the seconds register
8051 INTERFAC€ E\{Pi! a'l t
is written. Write transfers occur on the I2C acknowledge from the DSl3Oi Orx^e
ep€r registers
the divider chain is reset, to avoid rollover issues, the remaining time and date
B[2 Bttl Bit0 Func'tion Range registers must be written within one second.
Seconds Seconds 00-59
Control Register
Minutes Minutes 00-59
The DS1307 control register is used to control the operation of the SQW/OLT
t--12 pin. The bits in the control register and the function of different bits in the control
Hours +AIWPM
Hours register are shown inFig.12.37.
00-23
Day Day 01-07 Bit 7 Bit 6 Bir 5 Bit4 Bir 3 B[2 Bit 1 Bir 0
address byte contains the DS 1307's 7 -btt address, which is 1 101000, followed by
the direction bit (R/W), which is 1 for a read. After receiving and decoding the
Fig.
Slave address RiW Data (n) Data (n + 1) Data (n + 2) Data (n + X)
Figure 12.41 shows the r
;[ffi;l fw;ffi
Data (n+1) Data (n+X)
slave and generates an Acknowledge signal for each byte received. After the last
byte is received, the master sends a Not-acknowledge signal to the slave and the
t Dula transfened
slave terminates the transfer. The master sends the stop condition. Figure 12.40
, (X*1) b(es + Acknoudedge) shows the complete details of this data transfer.
I 32 768 kHz
ui'
t- -l
l uty5Ld
) I
I
R". n,, f li lL
l----_i
(
I irllrl
_r__
I
----l _ _t I
I
I
I
l
.il
I Yt \2
l
l-
I
I i l
p0.7 I
I
J
I
-l
I --] t""'
o., ,),*,orr l-
8os1 l
I
I
i
I
I
l --
poo L I _-]roo
I
v* I-_ -
;-_-'_l
I
t olo _[-l
I
1
I
I -r-
I
.l
+
Fi1.12.41 lnteriacing 8051 with DS1307
f,**.**o*.***#t
l f)iscuss in derair rhe interfacing of the stepper motor
with the g0-5 r.
2. What is the need lbr the g255 in a microcontroller_based
system?
3. Explain the interfacing of push b,tton switches and
t.EDs with the g051
microcontroller.