You are on page 1of 18

Chapter 8

RS232

By DeccanRobots
RS232

 Using RS232 we can communicate


between 2 devices.
 These 2 devices may have same or
different controller or a processor.
 In our practical we will communicate
between our Kit and computer.

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 We have to decide the speed of


communication between 2 devices.
 Speed of communication is measured in
bps (Bits Per Second)
 E.g. 2400 bps / 9600 bps
 bps is also called as a Baud Rate

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 SCON register is used to specify RS232


communication settings: Some
important
7 6 5 4 3 2 1 0 Bits from
SCON
SM0SM1SM2REN TB8 RB8 TI RI
Mode of
RS232 I want to Indicates Indicates
receive data that Data is that Data is
over RS232 transmitted received
For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India
RS232
Mode Setting Bits of SCON

SM0 SM1 Serial Explanation Baud Rate


Mode
0 0 0 8-bit Shift Oscillator / 12
Register
0 1 1 8-bit UART Set by Timer 1

1 0 2 9-bit UART Oscillator / 32

1 1 3 9-bit UART Set by Timer 1


For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India
RS232

 Remember to make 7th bit of PCON


register to 0.
 If it is set to 1 then speed will be
doubled.
 This bit is also called as SMOD.

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 We want to communicate to Computer


using Baud rate of 2400.
 We will use Mode 1 of serial
communication.
 In this mode we can send and receive 8
bit data.
 Baud rate to send this data can be set
using Timer 1.
For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India
RS232

 Remember that to write code for


RS232, you have to write code for
Timer 1 (TMOD, TCON, TH1, TL1) and
Registers for Serial Communication
(SCON, SBUF, PCON.7).

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 Timer 1 is used in 8-bit auto reload


mode to generate required BAUD rate.
 We can not use Timer 0. There no such
internal provision to do so

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 Lets understand Timer 1 Configurations:


 TMOD = 00100000b (8-bit auto reload mode)
 In 8 bit auto-reload mode, Timer 1 acts as 8
bit and not 16 bits.
 8 bits of TL1 are used to increment timer
value.
 8 bits of TH1 are used to tell TL1 to start
incrementing again from this value as it
overflows. This is called as auto-reload.

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 This re-load value which will be stored


in TH1 will be calculated using this
formula:
 TH1 = 256 - ((Crystal / 384) / Baud)
 TH1 = 256 - ((11059000 / 384) / 2400 )
TH1 = 256 - ((28,799) /2400)
TH1 = 256 – 11.99 = 244.00042=244d
We want 2400 baud rate.
Crystal Freq.
For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India
RS232

 Lets understand Timer 1 Configurations:


 TMOD = 00100000b (8-bit auto reload mode)
 TH1= 244d (as per formula)

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


Partial Code is explained here. For full
code please refer book.
RS232 Timer 1 in 8 bit
MOV TMOD,#00100000b autoreload mode
MOV TH1,#244d As per formula
MOV SCON,#01000000B Serial Communication
SETB REN Settings, Mode 1
MOV PCON,#00h
I want to receive data
SETB TR1
No Double speed
WaitLoop:
JBC RI, GetData
Start Timer 1
Sjmp WaitLoop If data is received on Serial
GetData: port then go to GetData
MOV A, SBUF
Received Data is always
………………..
stored in SBUF
SJMP WaitLoop
Write your code here
For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India
to use received data.
RS232

 This code can be used to Receive Data


from PC at 2400 baud rate.
 Refer Book to learn how to transmit
data to PC.
 Next slide will explain you part of code
used to transmit Data.

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


For Full Code please refer
RS232 book

 Partial code to transmit Data to PC:

Send character A
MOV SBUF,#’A’ to Computer
WaitForTransmit: Wait for data to be
JNB TI, WaitForTransmit transmitted
CLR TI
Clear Transmit Indicator
…..
…… Your further code

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


RS232

 Refer Book to learn more about Visual


Basic used to communicate with PC.

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


End of Chapter 8

 This is END of 8th Chapter


 Also refer book for programs to
interface
– 7 Segment Display
– ADC 0804
– TSOP 1738 (IR Receiver)

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India


Thank you!!!!
 This Computer Based Tutorial is developed
by EmbeddedMarket.com, Pune India.
 Thank you for using it.
 For more information and suggestions please
write back to:
EmbeddedMarket, 205, 2nd Floor, Decision Tower,
Next to CityPride, Satara Road, Pune 411037
Phone +91 020 24228818
www.embeddedmarket.com

For Microcontroller Development Boards --- www.EmbeddedMarket.com -- India

You might also like