EIOT Upto 3unit
EIOT Upto 3unit
OL A, #Data A©AORData__| Immediate | 2 | 1
A.Rn ‘A CAORRn Register | 1 | 1
+ RL A,Direct_| A AOR (Direct) | Direct Zt
+ RLC . A.@Ri ACAt@Ri indirect | 1] 1
fa Direct, A | Direct) < (Direct) OR] — Direct Zag] 2
A
+ RRC Direct,#Data | (Direct)<—(Direct) OR | Direct a] a
+ SWAP #Data
XRL, ata 5
cae Dacription A, #Dat AAXRLData_| Immediate | 2 | 1
A.Rn AC AXRLRa Register | 1 [7
ANL Logical AND ‘A.Direct_| A (SP);ADDR16 - PC
LCALL, TCALL | ADDRIG Tong Subroutine call 3 2
ACALL PC+3-9(SP); ADDRI6->PC
ET jun tine
ae f Ri Return from subrout T 2
> (SP) > PC
: RET ;RETI = Return from Interrupt 1 2
+ IMP ~AmMP_[ADDRI_ + Absolute JumpADDRI6 —> PC 2 2
fiuaaiar UMP [ADDRI6_|~_Long JumpADDRIG > PC 3 z
SUMP Short Jump PC+2+rel—>PC 2 2
LIMP IMP ‘A+DPTR->PC T z
‘AJMP > ‘Absolute Jump (Uncons | [gaz TFA=0, Jump to PC +rel z Z
IMP Short Jump (Unconditional) INZ TFA #0, Jump to PC+el 2
Sang fA oqaal 06 | CINE |A, Direct, rel, | Compare (Direct) withA, 3 2
INZ Jump ifAisnot equal to0 | A, Data, rel Compare # Data with A, 3 z
CINE Compare and Jump if Not Equal | Jump to PC +rel if not equal
DINZ Decrement and Jump ifNot Zero | A, Data, rel Compare #Daa ya Ro s Z
Jump to PC +rel if not equal
‘NOP No Operat
oe K, Data, | rel Compare #Data with @Ri, > | 2
Lea Jump to PC +rel if not equal
ACALL Absolute Cal to Subroutine (Unconditional) DINZ |~ Rayrel Department Rn, 2] 2
RET Retum from Subroutine Jump to PC +relif not Zero
RETI ‘Retum from Interrupt Direct, rel Department (Direct) , 3 2
fae ; Jump to PC+ rel ifnot Zero
lump oan Address (Unconditional) NOP No Operation 7 Tdded Systems and Ioy
am ee
4.4.7 Instructions and Programs
Arithmetic Instructions
Addition of Unsigned Numbers
=A+ source
ADDA, source ;
‘The instruction ADD is used to add two operands
nn operand isalways in register
fate data, or in memory
+ Desting
+ Source operand can be a register, immedi
operations are never allowed in 805)
+ Memory-to-memory arithmet
Assembly language
ceris affected by the following instruction
show how the flag rey
MOVA,#0FSH ;A=FS hex
ADDA,#0BH
sA=F5+0B=0
Solution:
FSH. 1111 0101
00001011
0000 0000
& 2. Assume that RAM locations 40— 44H have the following values. Write a program
to find the sum of the values. At the end ofthe program, register A shiould contain the |
low byte and R7 the high byte.
Solution:
40 =(7D)
41 =(EB)
42=(C5)
43 = (5B)
44=(30) f
it Embedded Pr.
Mov RO,#40HR2, #5 load pointer
Mov A sload counter
CLR caso ee
Mov RIA selear R7 's
AGAIN: ADD A,@RO add the byte ptrto by RO ie
INC NEXT CY=0don'tadd carry ¢
INC R7 skeep track of carry
NEXT: INC RO increment 0
pointer DINZ.R2, AGAIN; repeat until R2 is \
zero cea
ADDC and Addition of 16- Bit Numbers a
+ When adding two 16-bit data operands, the propagation ofa carry from lowe?
byte to higher byte is concerned
When the first byte is added
(E7+8D =74, CY =1).
3c £7
+ 3B_8D ‘The carry is propagated to the
7874 higher byte, which result in
3C+3B+1=78 (all in hex)
1.4.8 BCD Number System
¥ — The binary representation of the digits 0 to 9 is called BCD (Binary Coded Decimal)
+ Unpacked BCD
+ In unpacked BCD, the lower 4 bits of the number represent the BCD
number, and the rest of the bits are 0
+ Ex, 00001001 and 00000101 are unpacked BCD for 9 and $
+ Packed BCD
+ Inpacked BCD, a single byte has two BCD number in it, one in the lower
the upper 4 bits
packed BCD for 59H
4 bits, and one
+ Ex.0101 1001
nisin 9487Embedded Systems andig,
|
126
Digit gy2) 8H2) |
0 pool errs |
I go10 1000
2 coll il |
ool
3 2 F
a vs
5 91d |
6 pool |
7 |
8 1000
9 1001
¥? Adding two BCD numbers must give a BCD result
v Mov AK, #17
ADD A,#28H
‘Adding these two
number
¥ Theresult about should have been 17+28=45 (01000101). To correct this problem, |
the programmer must add 6 (0110) to the low di
DA Instruction
DAA; decimal adjust for addition
+ TheDA instructi
with BCD addition
+ TheDA instruction will add 6 to the lower nibble or higher nibble ifneed
Example:
MOVA, 47H
6CH
MOV B,/#25H
72H
not after INC
DA works only after an ADD, but
:3F +06=45 H.
provided to correctthe aforementioned problem associated |
3A=47 H first BCD operand
+ B=2SH second BCD operand
shex (binary) addition (A =6 CH
adjust for BCD addition
(a=rH)
git Embedded Processor
127
ae
‘The “DA” instruction works only on A. In other word, whi
jon must be in register A in order for DA to
operand of any addressing mode, the dest
work.
© Summary of DA instruction
+ Afteran ADD or ADDC instruction
1.
lower 4 bits.
2. Ifthe upper nibble is greater than
the source can be an
f the lower nibble (4 bits) is greater than 9, orifAC=1, add 0110to the
9, or if CY=1, add 0110 to the upper
bits.
HEX BeD
(0010 1001,
+ + 9001 1000
0100 0001 AC=1
+ + ono
47 ‘1000111
AC=l after the
n, “DAA” will add 6 to the lower
nibble, The final result is in BCD format.
GS 3. Assume that 5 BCD data items are stored in RAM locations starting at
40H, as shown below. Write a programto find the sum ofall the numbers.
‘The result must be in BCD.
40= (71)
41s (11)
42= (65)
43= (59)
44= (37)|
embedded Systems and toy
we
Solution:
MOV —RO,#40H —_; Load pointer
MOV R2,#5 jLoadcounter |
CLRA
MOV RIA ear R7
AGAIN: ADD A,@RO add the byte pointer; to by
ROJNCNEXT -C¥=0 don’t; add carry
INC RT ceep track of carries
NEXT: INC’ RO increment pointer
DINZR2, AGAIN repeat until R2 is 0
Subtraction of Unsigned Numbers
¥ _Inmany microprocessors there are two different instructions for subtraction;
SUB and SUBB (subtract with borrow)
* Inthe 8051 we have only SUBB |
+ ‘The 8051 uses adder circuitry to perform the subtraction
SUBB A, source; A= A-source-CY
¥ Tomake SUB out of SUBB, we have to make CY = 0 prior to the execution of the
instruction
+ Notice that we use the CY flag for the borrow
SUB when CY =
1, Take the 2’s complement of the subtrahend (source operand)
2. Additto the minuend (A)
3. Invert the carry
CLR Cc
MOV A, #4C jload A with value
4CH SUBB A, #6EH subtract 6E from A
JNC ‘NEXT ifCY=0 jump to
NEXT cPLA 3ifCY=1, take 1’s complement
INC A
i and inerementto get's comp
NEXT:MOV — RLA jsaveAin RI
a Bit Embedded Processor
1.29
Solution:
4c
= OE.
22
CY=0, the result is posi
CY=1, the res
and the dest
2’s complement ofthe result
+ SUBB when CY=1
borrow of the lower operand
CLR C
MOV 4/462H ;A=62H
SUBB A, #96H 62H - 96H = CCH with CY=1
MOV R7,A_ ; save the result
MOV A#271
. #12H; 27H-12H-12H-1 = 14H
Solution:
We have 2762H - 1296H = 14CCH
This instruction is used for multi-byte numbers and will take care of the
Ors
complement
0100 ah
oa] Te
QOinvert carry
A= @2H-96H-0= CCH
A=27H-12H-1= 4HCY=bedded Systems and io,
er
139
Unsigned Mi et
“The 8051 supports byte by bytem ie ick
. Te tyearcasunedio be unset aa UAB By 16-bit
A
Mov A#25H Joad 25Hto ree-A
Mov —-B.#6SH Joad 65H t0re8. B
MUL. AB :25H * 65H= £99 where
;B=OEHand A= 99H
Unsigned Mi jon Summary | (MULAB)
Mulipicaion Operand! Operand2 Result
Byte x byte A B ° B=highbyte
A=lowbyte
Unsigned Division
+ The 8051 supports byte over byte
+ The byte are assumed to be unsigned data
131
g Bit Embedded Processor
Beit Embedded Processor
(a) Weite a program to get hex data in the range of 00 — FFH fromport I and convertit
R7,R6 and RS.
(b)_ Assuming that PI has. value of FDH for data, analyse program.
Solution:
(@)
Mov A#OFFH
Mov PLA smake P1 an input port
Mov APL ead data from PA
Mov ByI0 B=OA hex
DIV AB videby 10,
Mov RIB ssave lower digit,
Mov BAO
DIV AB vide by 10 once more
Mov R6B ve the next di
Mov RSA jsave the last
i by 10 repeatedly until the
MOY A,#95.—_jload 95toreg.A
MOV B#10 load l0toreg. B (b) Toconverta binary (he) valu to dee
DIVAB;Aby BAB = tis less than 10. After each division the remainder is saves
R
DIVAB; divide A by B,A/B
ya ma FD/OA=19 3 (lowdigit)
MOV A,##95. jload95to eg. A 19/0A=2 — S(middledigit)
MOV B#10 ;load10t0 = eg, B 2(ighdigi)
MUL AB; A=09 (quotient) and ‘Therefore, we have FDH = 253.
;B=05 (remainder) a | Signed Arithmetic Instructions
wm Summary (DIV AB), | Signed 8-bit Operands
‘Numerator | Denominator | Quotient | Remainder D7 (MSB) is the sign and DO to D6 are the magnitude of the number
Byte/byte A B in 5 IfD7=0,the operandis pastive, andifD7=1,itisnegatine
> D7] pe ps | p+] ps | p2 | pi _| Do
CYisalways 0 |
IfB # 0,0V=0 Sign [Magnitude
IfB=0, 0V=1 indicates er | : &Embedded *¥StEMs and ig,
pS oe embedle Processor 133
SSS
Positive numbers are 0 to +127 aa 3
: fon (2’s complement) e | Solution: be ° oll
Negative mumberrepresetation @SCOMPIMET a sgn) ae qu ee! ow,
1. Write the magnitude of the number in, ao 9210
+ +70 0100 o110 °
2. Inverteachbit | 06
3. Add Ltoit | E 166° 1010 0110 and OV=1 ye
34H Solution: e | 4 2.
© ‘Show how the 8051 would represent | 0000 0010 According to the CPU, the result is -90, which is wrong. The CPUsets OV=1 to
1. 00110100 —_34H/givenin inary L111 11.0} | indicatethe overflow
2 1100 1011 inate cointey 1! iyo OV Flag
3; 11001100 add 1 (whichis Ao In 8-bit signed number operations, OV is set to 1 if either occurs:
ion of 34 in2's complement is CCH |
jigned number representation of -34 in aatl 1. There isa carry from D6 to D7, but no carry out of D7 (CY= 0)
Decimal aS a 2, There is acarry from D7 out (CY=1), but no carry from D6 to D7
128 1000.00 7m MOV A,i-128—;A=1000_000(A=80H)
"127 1000 0001 eee
| MOV R42 1110(R4=FEH)
7126 1000 0010 82 —
ADD ARS 1110(A=7EH=+126,INVALID)
= - -128 1000 0000
2 T1110, FE 3
1 u FF * a Hn 1110
-130 oul Vet
! E 0 (0000 0000) 00 | 1110 and OF
+ (0000 0001 ol OV=1
42 (0000 0010 2 The result + 126 is wrong}
MOV A, #2 ;A=II11 1110(A=FEH)
127 ont it TF. | MOVRI,#-5 — ;R1=1111 1011(RI-FBH)
Overflow Problem ADDA,RI— A=III1 1001(A=F9H=-7,
7 (irseresut ‘of an operation on signed numbers is too large for the register. sCorrect, OV = 0)
An overflow has occurred and the programmer must be is) | 2 Ut 1110
sO Examine the following code and analyze the result. + 5 + Mi, 1011
i SREY ya ee T- gh. NEXT sjumpit
>
INZEXIT- jump if Ais noto Rs < 80 [CY flag always checked for
NEXT <1” ; eases of greater or less than
7 Fi but only after it is determined
both registers have the same value, 00 that they are not equal
placed in A. INZ and JZ test the contents | |
of the accumulatorEmbedded Systems and I0t | ee
138 g pit Embedded Processor
3 entation mae —_—__—___
Instruction and Data Seriatizaq,
ation
Compare Carry Flag | Rotate
ase cy=0 | rotating Right and Left
Jestnation source cy=1 | RRA} oe ae
= #. + Inrotate right
+ Notice in the CINE instruction that any Rn register ean be compared with an + The Sbits oft
Pee {The Sito theacumultrarroutedsighton bit nd
Bit DO exits from the LSB and enters into MSB, D7
+ There is no need for register A to be involved
‘except that the operands remain
+ Thecompare instruction is really a subtraction,
unchanged
aT MSB > LsB
+ Flags are changed according to the execution of the SUBB instruction
29). Writea program to read the temperature and est it for the value 75.According to Mov AM6H =A = oo on
the test results, place the temperature value into the registers indicated by the following. a in ‘A © pdt ton
IfT=75 thenA= RR A Ey ee cn
751ET <75 then RI | Ag a A= 1100 0110
RR A A=
=TIfT>75 then a reno
RLA rotate left A
© Inrotate left
Solution: + The 8 bits of the accumulator are rotated left one
+ Bit D7 exits from the MSB and enters into LSB, DO
MOV P1,#0FFH smake P1 an input
port: MOVA,P1 sread PI port
jump ifA isnot 75
MSB <— LSB
CINEA#H75,OVER
R2=T
and
‘SJMP EXIT jA=75, exit
OVER: JNCNEXT sifCY=0 then A>75 | Rotating through Carry
MOVRLA sCY=1, A<75, save in RI | RRCA;rotate right through carry
SJMP EXIT sandexit | MOV A#f72H A = OMIT 0010
RL A A = 1110 0100
= 1100 1001
NEXT: MOV R2,4, 3A>75, save it in R2
} RL A AEmbedded Systems and Ioy
Mo ee
+ InRRCA
+ Bitsare rotated from left to right
+ They exit the LSB to the carry flag, and the carry lag enters the MS,
MSB > LSB oY,
CLRC ;makeCY=0
MOV A#26H A = 00100110
RRC A A = 0001 Cane
RRC A A = 0000 1001 CY = 1
RRC A A = 1000 0100 CY = 1
RLCA jrotate left through carry
+ InRLCA
+ Bitsare shifted from right to left
+ They exit the MSB and enter the carry flag, and the carry flag enters the LSB.
oH
MSB< LSB
5 ‘Write a program that finds the number of Is ina given byte
Mov R140
Mov R7#8;count=08
MOV A,#97H
AGAIN:RLCA
INCNEXT check for CY
INCRI CY=1 add to
‘count NEXT: DINZ R7,AGAIN
a pit Embedded Processor
Serializing Data
Serializing data isa way of send
single pin of microcontroller
+ Using the serial por
ing a byte of data one bit ata time through a
iscussed in Chapter 10
‘To transfer data one bit at atime and control the sequence of data and
‘spaces in between them.
‘Transfer a byte of data serially by
18 CY toany pin of ports PO—P3
Using rotate instruction
@ Write a program to transfer value 41H serially (one bit ata time) via pin P2.1,Put
‘wo highs atthe start and end of the data. Send the byte LSB first.
Solution:
MOV A, #411
SETBP2.1 igh
SETBP2.1 high MOVRS, #8 AGAIN:
RRCA
MOV P2.1, C ; send CY to P2.1 DINZRS,
HERE
SETBP2.1;
Pin
Reiser A | cy { ma |
D7 DO
1.8 PROGRAMMING PARALLEL PORTS
ARAL EL PORTS
1.5.1 Port 0
Port 0 is one of the four I/O ports available on the 8051 microcontroller. Itis a1
8-bit bidirectional port, which means that each bit of Port O can be individually configure
as either an input or an output.142 Embedded Systems and [97
Address and SFR Name:
+ Portis associated with Special Function Register (SFR) PO
+ Theaddress of PO in the memory space is 0x80 (128 in decimal).
Open-Drain Output:
Y Port 0 isan open-drain output port. It means that it can only pull the output pins Loy
(sink current) and relies on external pull-up resistors to drive the pins high. |
YTouse Port 0.as an output and to drive the pins high, youneed to connect external
pull-up resistors to the corresponding pins.
Default Input Mode:
Y Atpower-up or reset, Port 0 is configured as an input port by default.
Y When used as an input, the port can be used to read the state of external devices o-
signals connected to the pins,
Nee
10k
eHow
OO
pull-up resistors = 10k
Fig:1.5 Port 0 with internal pull up resistors
Input Mode with Internal Pull-u
¥ Port 0 also provides an internal pull-up resistor feature, which can be enabled by
setting the POPU (Port 0 Pull-Up) bits of the POM] and POMO SFRs.
Y When the internal pull-up resistors are enabled, the input pins of Port 0 are internally
pulled up to VCC, providing a default logic high state when no external device is
connected,
a Bit Embedded Processor
eee tts
External Interrupts:
¥ Port 0 (PO.2, PO.3) also servesas the inter
ipt input for external interrupts INTO
and INTI, respectively 7
¥ Theextemal interrupts can be triggered ona filling edge, ising edge, or level change,
depending on the configuration,
bit Manipal:
¥ Individual bts of Port Ocan be manipulated using bitwise operations.
¥ Forexampl
set PO.5 high, you can use the instruction SETB PO.5.
¥ Toclear PO.3 (set it low), you can use the instruction CLR P0.3.
¥ Totoggle PO.7, youcan use the instruction XRLPO.7, #1
4.5.2 Port1
configured as either an inputoran output.
‘Address and SFR Name:
¥ Port | is associated with Sp Function Register (SFR) P1.
¥ The address of P1 in the memory space is 0x90 (144 in decimal).
Output Mode:
Y Port 1 can be used as a general-purpose output port.
When configured as an output, the port can be used to drive external devices or
signals connected to the pins.
Default Input Mode:
¥ Atpower-up or reset, Port | is configured as an input port by default,
When used as an input, the port can be used to read the state of external devices or
signals connected to the pins.
External Interrupts:
Port | (P1.2, P1.3) can serve as the interrupt input for extemal interrupts INT2 and
INT3, respectively.
¥ Theexternal interrupts can be triggered ona falling edge, rising edge, orlevel change,
depending on the configuration.Embedded Systems and lor
For example, to set P1.
Y Toclear P1.3 (set it low), you can use the instruct
Y Totoggle P1.7, you can use the instruction XRL-
1.5.3 Port2
san 8-bit bidirectional port, which means that each bit of Port 2 can be individually
‘configured as either an input oran output
Address and SFR Name:
Y Port 2isassociated with Special Function Register (SFR) P2.
Y Theaddress of P2 in the memory space is 0 A0 (160 in decimal)
Output Mode:
Y Port 2 can be used as a general-purpose output port,
Y When configured as an output, the port can be used to drive external devices or
signals connected to the pins.
Default Input Mode:
Y Atpower-up or reset, Port 2 is configured as an input port by defa
¥ When used as an input, the port can be used to read the state of external devices or
signals connected tothe pins,
External Interrupts:
¥ Port 2(P2.0, P2.1) can serve as the interrupt input for external interrupts INTO and
INTI, respectively.
Y Theextemal interrupts can be triggered ona falling edge, rising edge, or level change,
ise operations,
n‘SETB P25’,
Tow), you can use the instruction “CLR P2.3,
‘To toggle P2.7, you can use the instruction “XRLP2.7, #1°
to set P2.5 high, you can use the
To clear P2.3 (set
g pit Embedded Processor
as
4.5.4 Port3
Itisan bt bidirectional port, which me
configured as either an input oran output,
‘Address and SFR Name:
ans that each bit of Port 3 can be individually
Port is associ
¢ Theaddress ofP3
‘Output Mode:
'd with Special Function Register (SFR) P3,
‘the memory space is 0xBO (176 in decimal).
Y Port 3 can be used asa general-purpose output port,
When configured as an output, the port can be used to drive external devices or
signals connected to the pins
Input Mode:
‘orl 3 is configured as an input port by default.
the port can be used to read the state of extemal devices or
als connected to the pi
External Interrupts:
jerTupts can be triggered ona f
he configuration,
wise operations.
instruction *SETB P3.5°
Alternate Functions:
¥ — Insome variants of the 8051 microc:mbedded »ystems ae T
146 E
Port3Pin ‘Alternate Function
P3.0 RXD (Serial Receive Data)
P3.1 "TXD (Serial Transmit Data)
P32 INTO (External Interrupt 0)
P33. INTI (External Interrupt 1)
P34 TO (Timer O extemal input)
P35 TI (Timer | external input
P3.6 WR’ (Extemal data memory write strobe)
P37 RD* (External data memory read strobe)
‘Dual-Function Pins:
“Some pins of Port 3 may have dual functionality, meaning they can serve as both
general-purpose UO and address‘data bus pins when interfacing with extemal memory
or peripherals.
‘The dual-functionali
and the specific configuration.
determined by the mode of operation of the microcont
4.5.5 Programming the Ports of 8051
‘To program the pons of the 8051 microcontroller, you need to manipulate thespeci
function registers (SFRs) associated with each port. The 8051 microcontrol
VO ports, labeled as PO, P1, P2.and P3. Each port is 8 bits wide and can
be configured as cither input or ouiput.
Here isan overview of how you can program the ports of the 8051 microcontro|
1. Set the Port
¢ Toconfigure a port as an inpul
cer (PO, PI. P2, oF P3)t0 1.
et the corresponding bits in the port's direction
|
¥ To configure port asan output, set the corresponding bts in the port's direction |
a pit Embedded Processor
Se
Read from an Input Port:
‘To read the current state ofan input por, si
ut port, simply read the i
port register (PO, Pl, P2, or P3), ply read the value ofthe corresponding
Write to an Output Port:
To write data to an output port assign the desired value to the corresponding port
register (PO, P1, P2, or P3).
Manipulate Individual Pins:
‘To manipulate individual pins of a port, you can use bitwise operations. Each bitin
the port register corresponds to a pin on the port.
Y Toset.a pin high (logic 1), use the bitwise OR operation (\) with the desired bit
mask.
¥ Tocleara pin (set it low; logic 0), use the bitwise AND operation (&) with the
‘complement of the desired bit mask.
To toggle the state of apin, use the bitwise XOR operation (*) with the desired bit
mask.
Here’s an example code to illustrate seting Port I (P1)as.an output and manipulating
ividual pins:
3 Set Port 1 as output (P1.0-P1.7)
MOV P1.1, #0; Set
XRLPI.2, #1 ; Toggle P1.2
In the example above, *MOY* instructions are used to set the port direction, and
‘MOV* and *XRL¢ instructions are used to manipulate individual pins of Port 1Embedded Systems and 1oy
as git Embedded Processor -
7.5 TIMERS AND SERIAL PORT imer Timer 1 Mode Timer 0 Mode
Dj|paDenails |Gate(@] CF — Dg
aR imers | Bit Details GOT | wr ‘°
at timer registers. These registers are known a, MO |Gate(@)| CAT] Mi [Mo
3] two 16-bil
ae es canbe used in two modes, These modes ary
ean
Timermose and the Counter mode. The ely difference between these two modesis thy
source for incrementing the timer regis =
1.6.1.1 Timer Mode
In the timer mode, the intemal machine cycles are counted. So this regi
incremented in each machine eyele. So when the clock frequency is 12MHz, then
timer registers incremented in each millisecond. In this mode it ignores the extemal ti
inpurpin
4.6.1.2 Counter Mode
Inthe counter mode, the extemal events are counted. In this mode, the timer
is incremented foreach 1 10 0 transition of the external input pin. This type of transitions
istreated as events. The extemal input pins are sampled once in each machine cycle, and
to determine the Lor 0 transitions, another machine cycle will be needed. So in this
mode, at least two machine cycles are needed. When the frequency is12MHz, then the
smacimum count frequency will be 12MHz/24 = 500KHz. So for event counting the time
durations? ps.
There are four different modes of the Timer or Counter. The Mode 0 to Mode 2 are
forboth of the Timer/Counter. Mode 3 has a different meaning for each timer register.
There isa register called TMOD. This register can be programmed to configure these
timers or counters.
‘The Serial poris used for serial communication in mode 1 and 3. Timer! is used for
senerating the baud rate. So only Timer0 is available for timer or counter operations.
Cys TMOD Register
‘TMOD(Timer Mode) is an SFR. The address of this register is 89H. This is not bit-
addressable.
‘Now, letus see the circuit that controls the running of the mer.
Bit Details High value (1)
oF Configure for the Counter
operations
Timer0 of Timer!
Run Mode
Gate (G),
Bit 00 1 10 i
Details
Mi Mo
This is Mode 3
‘(The function
depends on
Timer0 or
“Timoet
‘e+ To timer increment input
Run control
1=>Run
0 => Stop.
Git
INTs Toputpin
it will be high when the timer or counter is in mode 0 to 2,Embedded Systems and loy |
so
Examples
inter and Timer! as 8-|
1 To configure the Timer 0 as 16-bit event cou
tern 0.010010
pattern, we can use t
reload counter, we can use the bit p
want to program the TMOD register wi
MOV TMOD, #25H
‘The above instruction is executed, then the tim«
re the system as hardware controlled mode, then the gate bits wil
1=ADH we can use this instru
ier/counter will be cont
software. To confi
be 1. So the bit patterns will be 1010110
MOV TMOD, soap }
Opearting Modes of Timer/Counter
pre-scaler. So
of THO@rTI
SetTFL
conditions are met
Inthis example the Timerlis selected, ase, every 32 (25)event for counter
operations or 32 machine cycles for timer operation, the THI register will be incremented |
by 1. When the TH overflows from FFH to 00H, then the TF of TCON register will be
, and it stops the timer/counter. So for an example, we can say that if the TH.
I be high after 10H * 32= 512 machi
MOV TMOD, #00H
MOVTHI, #OFOH
MOVIE, #88H SETB
TRI
In the above program, the Timer! is configured as timer mode 0. In this case
Gate =0, Then the THI will be loaded with FOH, then enable the Timer] interrupt. At |
- last set the TR1 of TCON register, and start the timer. |
|
it Embedded Processor
a Bi
are using Mode 1 for'Timer 0,
) 60
cycles. When the clock frequency is 12MHIz, then the following instructions generate an
interrupt 16 1s after Timer0 starts rus
MOVTMOD, #01H
MOVTLO, #0FOH
MOVTHO, #0FFH
MOVIE, #82H SETB
TRO
In this case
In the above program, the Timer0 is configured as timer mode
Gate = 0. Then the TLO will be loaded with FOH and THC
‘enable the Timer0 interrupt. At last set the TRO of TCON register, and start the timer.
Mode2 of Timer/Counter
‘The Mode 2 operation
diagram, we are using Mode 2for Timer!
‘case every event for counter operations or machine cycles for timer operation,
incremented by 1. When the register pair overflows from FFH to
be reloaded with the
Content of TH and starts the operation ag2"7._embedded Systems and 1Or
wa Lee
t
MOVTMOD, #20H
MOVTLI, #0FOH
MOVTHI, #0FOH
MOVIE, #88H
SETBTRI
In the above progr
Gate = 0, Then the TL1 and THI are loaded with FOH. then enable the Timer! interrupt.
At last set the TRI of TCON register, and start the timer.
Timer! in mode2 generates the desired baud rate when the serial port is working on
Mode 1 or 3
ram, the Timerl is configured as timer mode 2, In this ease
Mode3 of Timer/Counter
Mode 3is different for Timer 0 and Timer!. When the Timer 0 is working in mode 3,
led by the standard
Bit, embedded Processor
es
Timer
irement
input
To
Run contol
1S Run
0= Sup
TRI
When the Timer! is working in Mode 3, i
imply holds the count but does not run,
‘configured in one of the mode 0, 1 and 2. In this
ramming of THOis controlled by TR! bitonly. Sth gat bitin this mode for Timer Ohas
nospecific role.
‘The mode 3 is present for app!
Mode 3 of Timer 0, the 8051 hi
timer/counter by TLO, and one 16-1
imer by THO, another8-bit
ner/eounterby Timer.
deacti
vated when the gate is
ial Ports
Serial Communication
LZ ‘When a microcontroller communicates with the outside world, it provides the data
‘in byte-sized chunks. In some cases, suchas printers, the informati
“fom the 8- bit data bus and presented to the 8-bit data bus of the printer. This can work
only if the cable is not too long, since long cables diminish and even distort signals.Embedded Systems and 107
git Embedded Processor
) shows serial versus parallel data transfer. The fay
data line instead ofthe 8-bit data line of paras
Fon towark, the Byte of data must be conver,
When the distance is short, the digital signal can be transferred as iti
wire and requires no modulation. However, for long-distance data transfers, serial day
communication requires a modem to modulate (convert to Osand 1s to audio tones) an
‘demodulate (converting from audio tones to 0s and 1s).
jon uses two methods, asyitchronous and synchronous. The
synchronous method transfers a block of data(characters) at atime, while the asynchronow
method transfers a single byte at a time. There are special IC’s made by many
. These chips are commonly referred to as
‘manufacturers for serial data comm
UART (universal asynchronous receiver transmitter) and USART (universal synchronous
asynchronous receiver transmitter).
Parallel Transfer
Serial Transfer
Do
Receiver
Reeeiver ] | sender
‘D7
‘ i
computer only sends data. Duplex transmissions can be half full duplex, depending on
whether or not the data transfer canbe simultaneous. I datas transmitted one way aa
time, itis referred toas half duplex. Ifthe data can go both ways atthe same t
duplex. Full duplex requires two wire conductors for the data lines, one for transmission
and one for reception, in order to transfer and receive data simultaneo
‘Simplex
. [4 Receiver
a YW o_9fet
. Transmitter ake nly
Half Duplex —————_~_] — Receiver
a
Receiver —
L__“eewer 5 ‘Transmitter
T i |
Full Duplex ransmitter Receiver
Receiver, im] >
Fig:1.7 Serial communication forms
‘Asynchronous Serial Comm 7
nat the receiving end ofthe data line in a serial data transfer is all
jcult to make sense of the data unless the sender and:
‘The data cor
aset of rules, a protoc
‘and when the data begins and ends.
Fig:1.8 Asynchronous Serial Communication data framing
widely-used for character-o
Asynchronous serial data communi
transmissions, In this method, each characteris placed between start and stop
in data framing for asynchronous communications, the data such as ASCII
butthe
called frami
characters, are packed between astart bit an
stop bit can be one or two bits. The start bi
Look at below figure in wt 1 ASCII character “A
between the start bit and astop bi { the LSB is sent out first.
x stop bit. The start bits always onei Embedded Systems and Io,
ase
Data Transfer Rate
he rate of data transfer in s
second), Another widely used terminology
jology and s defined as the mur
for bpsis baud rate.
eof signal changes per second,
Fnput and output voltage levels are not TTL compat ible. For this reason, to connect any
RS232 toa microcontroller system we must use voltage ‘converters such as MAX232 tg
convert the TTL logic levels to RS232 vollage levels, and viee versa. MAX232 IC's are
commonly referred toas line drivers.
S232 pins
S232 cable commonly referred to as the DB-25 connector has 25 pins. Since not
all the pins are used in PC cables, IBM introduced the DB-9 version of the seri
‘standard, which uses 9 pins only.
‘Current terminology classifies data communication equipment as DTE (data terminal
equipment) or DCE (data communication equipment). DTE refers to terminals and
computers that send and receive data, while DCE refers t communication equipment,
le for transferring the data, The simplest connec
DIE DTE
TxD xD:
rx0->-axo
ground
Fig:1.9 RS232 Pinout
git embedded Processor
ot?
18232 pins description
Description
Data carrier detect (-DCD)
Received data (=D)
“Transmitted data (T*D)
‘To ensure fast and reliable data transmission between two devices, the data transfer
must be coordinated. Many of the pins of the RS232 connecter are used for handshaking
CTS (Clear'To Send): In response to RTS, when the modem has room forstoring the data
sends out signal CTS tothe DTEto indicat thatit can receive the data now.
DCD (Data Carrier Detect): The modem asserts signal DCD to inform the DTE that a
valid carrier has been detected and that contact between it and the other modem is,
established, Therefore , DCD is an output from the modem and an input to the PC.
lephone is ringing, However if the PC is in
RI (Ring Indicator):
charge of answering the phone, this signal can be used.Embedded Systems and Ig
158 _ |
1.6.2.3 MAX232
Since the RS232 is not compa
microcontrollers, we need a voltage converter (line
tible with today’s microprocessors any
driver) to convert the RS232's signay,
1d RxD pins,
to TTL voltage levels that will be acceptable 10 the ee sn einen ey
example of such converter is MAX232. Itconverts from eer i
voltage levels and vice versa, One advantage of the MAX: v
‘ource voltage for the 8051.
power source which is the same asthe s
‘The MAX232 has two sets of line drivers for transferring and rece
shown in fig(S). The lin drivers used for TXD are called TI and 72, while th
tedasR1 and R2. MAX232 requires four capacitors ranging
data,
drivers for RXD are d reso
from 1 to 22 uF. The most widely used value for these capacitors is 22 HF.
od) mwas2*
at} . 8051:
2 oy
ee * pal
rin riot]
feet pn sale a
® S >
9 deta na
~You might also like
- Embedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 1
No ratings yetEmbedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 114 pages - Embedded Systems and IoT - CS3691 - Questions With Answer
100% (1)Embedded Systems and IoT - CS3691 - Questions With Answer11 pages - CS3691 Embedded Systems and Iot L T P C 3 0 2 4
No ratings yetCS3691 Embedded Systems and Iot L T P C 3 0 2 41 page - Lecture 1.1.2 (Embedded System Characterstics and Its Example and Atmega Pin Description
No ratings yetLecture 1.1.2 (Embedded System Characterstics and Its Example and Atmega Pin Description25 pages - Cs3691-Iot Notes Material - 5uint Cover
No ratings yetCs3691-Iot Notes Material - 5uint Cover252 pages - SkillDzire - Embedded Systems - Internship Content
No ratings yetSkillDzire - Embedded Systems - Internship Content3 pages - Embedded Systems (ECE-4010) : Dr. Mayank Gupta
No ratings yetEmbedded Systems (ECE-4010) : Dr. Mayank Gupta23 pages