8051 Serial Communication
8051 Serial Communication
8051 Serial Communication
Communication-
UART
www.pantechsolutions.net
SERIAL COMMUNICATION WITH 8051
• RS232 standards
– most widely used serial I/O interfacing standard
– input and output voltage levels are not TTL compatible
– 1 bit is represented by -3 to -25 V
– 0 bit is +3 to +25 V
– -3 to +3 is undefined
– to connect RS232 to a microcontroller system must use voltage
converters such as MAX232 to convert the TTL logic levels to the
RS232 voltage levels, and vice versa
– MAX232 IC chips are commonly referred to as line drivers
DB-9 9-Pin
Connector
IBM PC DB-9
Signals
Technology beyond the Dreams™ Copyright © 2006 Pantech Solutions Pvt
DATA COMMUNICATION
• MAX232
– converts from RS232 voltage levels to TTL voltage levels
– uses a +5 V power source
– MAX232 has two sets of line drivers for transferring and receiving
data
– line drivers used for TxD are called T1 and T2
– line drivers for RxD are designated as R1 and R2
– T1 and R1 are used together for TxD and RxD of the 8051
– second set is left unused
Case I: SMOD=0.
(a) 28,800/9,600 = 3. 256-3 = 253 = FDh
(b) 28,800/2,400 = 12. 256-12 = 244 = F4h
(c) 28,800/1,200 = 24. 256-24 = 232 = E8h
should be loaded into TH1.
Write a program to transfer the message “YES” serially at 9600 baud, 8bit data, 1 stop bit.
Do this continuously.
Solution:
MOV TMOD,#20H ;timer 1, mode 2
MOV TH1,#-3 ;9600 baud
MOV SCON,#50H ;8-bit, 1 stop bit, REN enabled
SETB TR1 ;start timer 1
AGN: MOV A,#"Y" ;transfer "Y"
ACALL XMIT
MOV A,#"E" ;transfer "E"
ACALL XMIT
MOV A,#"S" ;transfer "S"
ACALL XMIT
SJMP AGN ;keep doing it
;serial data transfer subroutine
XMIT: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for last bit to transfer
CLR TI ;get ready for next byte
RET
void serial_init(void);
void serial_init(void)
{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 9600 baud @ 11.0592MHz*/
TR1 = 1; /* TR1: timer 1 run */
TI = 1;
void main(void)
{
serial_init();
printf (" PS - PrimerC51 UART Demo\n\n\r");
while (1)
{
printf ("Hello World!! \n\r"); /* Print "Hello World" */
}
}