Page 2 :
SERIAL COMMUNICATION, , Chung-Ping Young, 楊中平, Home Automation, Networking, and Entertainment Lab, , Dept. of Computer Science and Information Engineering, National Cheng Kung University
Page 3 :
BASICS OF, SERIAL, COMMUNICATION, , , , Computers transfer data in two ways:, ¾, , Parallel, Often 8 or more lines (wire conductors) are, used to transfer data to a device that is only a, few feet away, , ¾, , Serial, To transfer to a device located many meters, away, the serial method is used, The data is sent one bit at a time, Serial Transfer, , Parallel Transfer, D0, , Sender, , Receiver, , Receiver, , Sender, D7, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 2
Page 4 :
BASICS OF, SERIAL, COMMUNICATION, , , , , , (cont’), , , , , , At the transmitting end, the byte of, data must be converted to serial bits, using parallel-in-serial-out shift register, At the receiving end, there is a serialin-parallel-out shift register to receive, the serial data and pack them into byte, When the distance is short, the digital, signal can be transferred as it is on a, simple wire and requires no modulation, If data is to be transferred on the, telephone line, it must be converted, from 0s and 1s to audio tones, ¾, , HANEL, , This conversion is performed by a device, called a modem, “Modulator/demodulator”, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 3
Page 5 :
BASICS OF, SERIAL, COMMUNICATION, , , , (cont’), , , , Serial data communication uses two, methods, ¾, , Synchronous method transfers a block of, , ¾, , Asynchronous method transfers a single, , data at a time, byte at a time, , It is possible to write software to use, either of these methods, but the, programs can be tedious and long, ¾, , There are special IC chips made by many, manufacturers for serial communications, UART (universal asynchronous Receivertransmitter), USART (universal synchronous-asynchronous, Receiver-transmitter), , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 4
Page 6 :
BASICS OF, SERIAL, COMMUNICATION, Half- and FullDuplex, Transmission, , , , If data can be transmitted and received,, it is a duplex transmission, ¾, ¾, , , , If data transmitted one way a time, it is, referred to as half duplex, If data can go both ways at a time, it is full, , duplex, , This is contrast to simplex transmission, Simplex, , Half Duplex, , Full Duplex, , HANEL, , Transmitter, , Receiver, , Transmitter, , Receiver, , Receiver, , Transmitter, , Transmitter, , Receiver, , Receiver, , Transmitter, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 5
Page 7 :
BASICS OF, SERIAL, COMMUNICATION, Start and Stop, Bits, , , , A protocol is a set of rules agreed by, both the sender and receiver on, ¾, ¾, ¾, , , , Asynchronous serial data, communication is widely used for, character-oriented transmissions, ¾, ¾, , , , HANEL, , How the data is packed, How many bits constitute a character, When the data begins and ends, , Each character is placed in between start, and stop bits, this is called framing, Block-oriented data transfers use the, synchronous method, , The start bit is always one bit, but the, stop bit can be one or two bits, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 6
Page 8 :
BASICS OF, SERIAL, COMMUNICATION, Start and Stop, Bits, (cont’), , , , The start bit is always a 0 (low) and the, stop bit(s) is 1 (high), ASCII character “A” (8-bit binary 0100 0001), , Space, , Stop, , 0, , Bit, , 1, , 0, , 0, , D7, , Goes out last, , The 0 (low) is, referred to as space, , HANEL, , The transmission begins with a, start bit followed by D0, the, LSB, then the rest of the bits, until MSB (D7), and finally,, the one stop bit indicating the, end of the character, , 0, , 0, , 0, , 1, , Start Mark, Bit, , D0, , Goes out first, , When there is no, transfer, the signal, is 1 (high), which is, referred to as mark, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 7
Page 9 :
BASICS OF, SERIAL, COMMUNICATION, Start and Stop, Bits, (cont’), , HANEL, , , , Due to the extended ASCII characters,, 8-bit ASCII data is common, ¾, , , , In older systems, ASCII characters were 7bit, , In modern PCs the use of one stop bit, is standard, ¾, , In older systems, due to the slowness of, the receiving mechanical device, two stop, bits were used to give the device sufficient, time to organize itself before transmission, of the next byte, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 8
Page 10 :
BASICS OF, SERIAL, COMMUNICATION, Start and Stop, Bits, (cont’), , , , Assuming that we are transferring a, text file of ASCII characters using 1, stop bit, we have a total of 10 bits for, each character, ¾, , , , In some systems in order to maintain, data integrity, the parity bit of the, character byte is included in the data, frame, ¾, , HANEL, , This gives 25% overhead, i.e. each 8-bit, character with an extra 2 bits, , UART chips allow programming of the, parity bit for odd-, even-, and no-parity, options, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 9
Page 11 :
BASICS OF, SERIAL, COMMUNICATION, , , , , , ¾, , Data Transfer, Rate, , ¾, , , , HANEL, , The rate of data transfer in serial data, communication is stated in bps (bits per, second), Another widely used terminology for, bps is baud rate, It is modem terminology and is defined as, the number of signal changes per second, In modems, there are occasions when a, single change of signal transfers several, bits of data, , As far as the conductor wire is, concerned, the baud rate and bps are, the same, and we use the terms, interchangeably, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 10
Page 12 :
BASICS OF, SERIAL, COMMUNICATION, Data Transfer, Rate, (cont’), , HANEL, , , , The data transfer rate of given, computer system depends on, communication ports incorporated into, that system, ¾, ¾, ¾, , IBM PC/XT could transfer data at the rate, of 100 to 9600 bps, Pentium-based PCs transfer data at rates as, high as 56K bps, In asynchronous serial data communication,, the baud rate is limited to 100K bps, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 11
Page 13 :
BASICS OF, SERIAL, COMMUNICATION, RS232, Standards, , HANEL, , , , , , An interfacing standard RS232 was set, by the Electronics Industries Association, (EIA) in 1960, The standard was set long before the, advent of the TTL logic family, its input, and output voltage levels are not TTL, compatible, ¾, , In RS232, a 1 is represented by -3 ~ -25 V,, while a 0 bit is +3 ~ +25 V, making -3 to, +3 undefined, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 12
Page 14 :
RS232 DB-25 Pins, , BASICS OF, SERIAL, COMMUNICATION, RS232, Standards, (cont’), , Pin, , Description, , Pin, , Description, , 1, , Protective ground, , 14, , Secondary transmitted data, , 2, , Transmitted data (TxD), , 15, , Transmitted signal element timing, , 3, , Received data (RxD), , 16, , Secondary receive data, , 4, , Request to send (-RTS), , 17, , Receive signal element timing, , 5, , Clear to send (-CTS), , 18, , Unassigned, , 6, , Data set ready (-DSR), , 19, , Secondary receive data, , 7, , Signal ground (GND), , 20, , Data terminal ready (-DTR), , 8, , Data carrier detect (-DCD), , 21, , Signal quality detector, , 9/10, , Reserved for data testing, , 22, , Ring indicator (RI), , 11, , Unassigned, , 23, , Data signal rate select, , 12, , Secondary data carrier detect, , 24, , Transmit signal element timing, , 13, , Secondary clear to send, , 25, , Unassigned, , RS232 Connector DB-25, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 13
Page 15 :
BASICS OF, SERIAL, COMMUNICATION, RS232, Standards, (cont’), , HANEL, , , , Since not all pins are used in PC cables,, IBM introduced the DB-9 version of the, serial I/O standard, RS232 Connector DB-9, , RS232 DB-9 Pins, Pin, , Description, , 1, , Data carrier detect (-DCD), , 2, , Received data (RxD), , 3, , Transmitted data (TxD), , 4, , Data terminal ready (DTR), , 5, , Signal ground (GND), , 6, , Data set ready (-DSR), , 7, , Request to send (-RTS), , 8, , Clear to send (-CTS), , 9, , Ring indicator (RI), , Department of Computer Science and Information Engineering, National Cheng Kung University, , 14
Page 16 :
BASICS OF, SERIAL, COMMUNICATION, , , , Current terminology classifies data, communication equipment as, ¾, , ¾, , Data, Communication, Classification, , , , DTE (data terminal equipment) refers to, terminal and computers that send and, receive data, DCE (data communication equipment), refers to communication equipment, such, as modems, , The simplest connection between a PC, and microcontroller requires a minimum, of three pins, TxD, RxD, and ground, Null modem connection, , DTE, , DTE, , TxD, , TxD, , RxD, , RxD, , ground, HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 15
Page 17 :
BASICS OF, SERIAL, COMMUNICATION, , , , DTR (data terminal ready), ¾, , , , DSR (data set ready), ¾, , RS232 Pins, , , When the DTE device has byte to transmit,, it assert RTS to signal the modem that it, has a byte of data to transmit, , CTS (clear to send), ¾, , HANEL, , When DCE is turned on and has gone, through the self-test, it assert DSR to, indicate that it is ready to communicate, , RTS (request to send), ¾, , , , When terminal is turned on, it sends out, signal DTR to indicate that it is ready for, communication, , When the modem has room for storing the, data it is to receive, it sends out signal CTS, to DTE to indicate that it can receive the, data now, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 16
Page 18 :
BASICS OF, SERIAL, COMMUNICATION, , , , ¾, , , , RS232 Pins, (cont’), , HANEL, , 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, , RI (ring indicator), ¾, ¾, , An output from the modem and an input to, a PC indicates that the telephone is ringing, It goes on and off in synchronous with the, ringing sound, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 17
Page 19 :
8051, CONNECTION, TO RS232, , , , , , A line driver such as the MAX232 chip is, required to convert RS232 voltage, levels to TTL levels, and vice versa, 8051 has two pins that are used, specifically for transferring and, receiving data serially, ¾, ¾, , HANEL, , These two pins are called TxD and RxD and, are part of the port 3 group (P3.0 and P3.1), These pins are TTL compatible; therefore,, they require a line driver to make them, RS232 compatible, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 18
Page 22 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , , , To allow data transfer between the PC, and an 8051 system without any error,, we must make sure that the baud rate, of 8051 system matches the baud rate, of the PC’s COM port, Hyperterminal function supports baud, rates much higher than listed below, PC Baud Rates, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, , HANEL, , Baud rates supported by, 486/Pentium IBM PC BIOS, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 21
Page 23 :
With XTAL = 11.0592 MHz, find the TH1 value needed to have the, following baud rates. (a) 9600 (b) 2400 (c) 1200, , SERIAL, COMMUNICATION, PROGRAMMING, (cont’), , Solution:, The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz,, and 921.6 kHz / 32 = 28,800 Hz is frequency by UART to timer 1 to, set baud rate., (a) 28,800 / 3 = 9600, where -3 = FD (hex) is loaded into TH1, (b) 28,800 / 12 = 2400, where -12 = F4 (hex) is loaded into TH1, (c) 28,800 / 24 = 1200, where -24 = E8 (hex) is loaded into TH1, Notice that dividing 1/12 of the crystal frequency by 32 is the default, value upon activation of the 8051 RESET pin., 11.0592 MHz, XTAL, oscillator, , ÷ 12, , Baud Rate, , TF is set to 1 every 12, ticks, so it functions as, a frequency divider, HANEL, , Machine cycle freq, 921.6 kHz, , ÷ 32, By UART, , TH1 (Decimal) TH1 (Hex), , 9600, , -3, , FD, , 4800, , -6, , FA, , 2400, , -12, , F4, , 1200, , -24, , E8, , 28800 Hz, To timer 1, To set the, Baud rate, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 22
Page 24 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , SBUF is an 8-bit register used solely for, serial communication, ¾, , For a byte data to be transferred via the, TxD line, it must be placed in the SBUF, register, , The moment a byte is written into SBUF, it is, framed with the start and stop bits and, transferred serially via the TxD line, , SBUF Register, ¾, , SBUF holds the byte of data when it is, received by 8051 RxD line, , When the bits are received serially via RxD, the, 8051 deframes it by eliminating the stop and, start bits, making a byte out of the data received,, and then placing it in SBUF, , MOV SBUF,#’D’, MOV SBUF,A, MOV A,SBUF, , HANEL, , ;load SBUF=44h, ASCII for ‘D’, ;copy accumulator into SBUF, ;copy SBUF into accumulator, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 23
Page 26 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , SM0, SM1, ¾, , They determine the framing of data by, specifying the number of bits per character,, and the start and stop bits, SM0, , SM1, , 0, , 0, , Serial Mode 0, , 0, , 1, , Serial Mode 1, 8-bit data,, 1 stop bit, 1 start bit, , 1, , 0, , Serial Mode 2, , 1, , 1, , Serial Mode 3, , SCON Register, (cont’), , , , SM2, ¾, , HANEL, , Only mode 1 is, of interest to us, , This enables the multiprocessing capability, of the 8051, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 25
Page 27 :
, , SERIAL, COMMUNICATION, PROGRAMMING, , REN (receive enable), ¾, , It is a bit-adressable register, , When it is high, it allows 8051 to receive data on, RxD pin, If low, the receiver is disable, , , , TI (transmit interrupt), ¾, , SCON Register, , When 8051 finishes the transfer of 8-bit, character, , It raises TI flag to indicate that it is ready to, transfer another byte, TI bit is raised at the beginning of the stop bit, , (cont’), , , , RI (receive interrupt), ¾, , When 8051 receives data serially via RxD, it, gets rid of the start and stop bits and, places the byte in SBUF register, It raises the RI flag bit to indicate that a byte, has been received and should be picked up, before it is lost, RI is raised halfway through the stop bit, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 26
Page 28 :
, , SERIAL, COMMUNICATION, PROGRAMMING, Programming, Serial Data, Transmitting, , In programming the 8051 to transfer, character bytes serially, 1., , 2., 3., , 4., 5., 6., 7., , 8., HANEL, , TMOD register is loaded with the value, 20H, indicating the use of timer 1 in mode, 2 (8-bit auto-reload) to set baud rate, The TH1 is loaded with one of the values, to set baud rate for serial data transfer, The SCON register is loaded with the value, 50H, indicating serial mode 1, where an 8bit data is framed with start and stop bits, TR1 is set to 1 to start timer 1, TI is cleared by CLR TI instruction, The character byte to be transferred, serially is written into SBUF register, The TI flag bit is monitored with the use of, instruction JNB TI,xx to see if the, character has been transferred completely, To transfer the next byte, go to step 5, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 27
Page 31 :
SERIAL, COMMUNICATION, PROGRAMMING, Importance of, TI Flag, , , , The steps that 8051 goes through in, transmitting a character via TxD, 1., 2., 3., 4., , 5., , The byte character to be transmitted is, written into the SBUF register, The start bit is transferred, The 8-bit character is transferred on bit at, a time, The stop bit is transferred, It is during the transfer of the stop bit that, 8051 raises the TI flag, indicating that the last, character was transmitted, , By monitoring the TI flag, we make sure, that we are not overloading the SBUF, , If we write another byte into the SBUF before, TI is raised, the untransmitted portion of the, previous byte will be lost, , 6., , HANEL, , After SBUF is loaded with a new byte, the, TI flag bit must be forced to 0 by CLR TI, in order for this new byte to be transferred, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 30
Page 32 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , By checking the TI flag bit, we know, whether or not the 8051 is ready to, transfer another byte, ¾, ¾, , Importance of, TI Flag, , ¾, , (cont’), , , , The TI bit can be checked by, ¾, ¾, , HANEL, , It must be noted that TI flag bit is raised by, 8051 itself when it finishes data transfer, It must be cleared by the programmer with, instruction CLR TI, If we write a byte into SBUF before the TI, flag bit is raised, we risk the loss of a, portion of the byte being transferred, The instruction JNB TI,xx, Using an interrupt, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 31
Page 33 :
SERIAL, COMMUNICATION, PROGRAMMING, Programming, Serial Data, Receiving, , , , In programming the 8051 to receive, character bytes serially, 1., , 2., 3., , 4., 5., 6., , 7., 8., HANEL, , TMOD register is loaded with the value, 20H, indicating the use of timer 1 in mode, 2 (8-bit auto-reload) to set baud rate, TH1 is loaded to set baud rate, The SCON register is loaded with the value, 50H, indicating serial mode 1, where an 8bit data is framed with start and stop bits, TR1 is set to 1 to start timer 1, RI is cleared by CLR RI instruction, The RI flag bit is monitored with the use of, instruction JNB RI,xx to see if an entire, character has been received yet, When RI is raised, SBUF has the byte, its, contents are moved into a safe place, To receive the next character, go to step 5, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 32
Page 34 :
SERIAL, COMMUNICATION, PROGRAMMING, Programming, Serial Data, Receiving, (cont’), , Write a program for the 8051 to receive bytes of data serially, and, put them in P1, set the baud rate at 4800, 8-bit data, and 1 stop bit, Solution:, , HERE:, , MOV, MOV, MOV, SETB, JNB, MOV, MOV, CLR, , TMOD,#20H, TH1,#-6, SCON,#50H, TR1, RI,HERE, A,SBUF, P1,A, RI, , SJMP HERE, , HANEL, , ;timer 1,mode 2(auto reload), ;4800 baud rate, ;8-bit, 1 stop, REN enabled, ;start timer 1, ;wait for char to come in, ;saving incoming byte in A, ;send to port 1, ;get ready to receive next, ;byte, ;keep getting data, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 33
Page 35 :
SERIAL, COMMUNICATION, PROGRAMMING, Programming, Serial Data, Receiving, , Example 10-5, Assume that the 8051 serial port is connected to the COM port of, IBM PC, and on the PC, we are using the terminal.exe program to, send and receive data serially. P1 and P2 of the 8051 are connected, to LEDs and switches, respectively. Write an 8051 program to (a), send to PC the message “We Are Ready”, (b) receive any data send, by PC and put it on LEDs connected to P1, and (c) get data on, switches connected to P2 and send it to PC serially. The program, should perform part (a) once, but parts (b) and (c) continuously, use, 4800 baud rate., Solution:, , (cont’), , H_1:, , ORG, MOV, MOV, MOV, MOV, SETB, MOV, CLR, MOV, , 0, P2,#0FFH, ;make P2 an input port, TMOD,#20H ;timer 1, mode 2, TH1,#0FAH ;4800 baud rate, SCON,#50H ;8-bit, 1 stop, REN enabled, TR1, ;start timer 1, DPTR,#MYDATA ;load pointer for message, A, A,@A+DPTR ;get the character, , ..., , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 34
Page 37 :
SERIAL, COMMUNICATION, PROGRAMMING, Programming, Serial Data, Receiving, (cont’), , HANEL, , Example 10-5 (cont’), ;-----The message--------------MYDATA: DB, “We Are Ready”,0, END, , 8051, To PC, , TxD, , COM Port, , RxD, , P1, , LED, , P2, , SW, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 36
Page 38 :
SERIAL, COMMUNICATION, PROGRAMMING, Importance of, RI Flag, , , , In receiving bit via its RxD pin, 8051, goes through the following steps, 1., , It receives the start bit, Indicating that the next bit is the first bit of the, character byte it is about to receive, , 2., 3., , The 8-bit character is received one bit at, time, The stop bit is received, When receiving the stop bit 8051 makes RI = 1,, indicating that an entire character byte has, been received and must be picked up before it, gets overwritten by an incoming character, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 37
Page 39 :
(cont’), , SERIAL, COMMUNICATION, PROGRAMMING, , 4., , Importance of, RI Flag, , 5., , (cont’), , By checking the RI flag bit when it is, raised, we know that a character has been, received and is sitting in the SBUF register, We copy the SBUF contents to a safe place in, some other register or memory before it is lost, , After the SBUF contents are copied into a, safe place, the RI flag bit must be forced, to 0 by CLR RI in order to allow the next, received character byte to be placed in, SBUF, Failure to do this causes loss of the received, character, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 38
Page 40 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , By checking the RI flag bit, we know, whether or not the 8051 received a, character byte, ¾, ¾, , Importance of, RI Flag, , ¾, , (cont’), , ¾, , , , The RI bit can be checked by, ¾, ¾, , HANEL, , If we failed to copy SBUF into a safe place,, we risk the loss of the received byte, It must be noted that RI flag bit is raised by, 8051 when it finish receive data, It must be cleared by the programmer with, instruction CLR RI, If we copy SBUF into a safe place before, the RI flag bit is raised, we risk copying, garbage, The instruction JNB RI,xx, Using an interrupt, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 39
Page 41 :
SERIAL, COMMUNICATION, PROGRAMMING, , , , Doubling Baud, Rate, , , , There are two ways to increase the, The system, baud rate of data transfer, crystal is fixed, ¾, ¾, , To use a higher frequency crystal, To change a bit in the PCON register, , PCON register is an 8-bit register, ¾, ¾, , When 8051 is powered up, SMOD is zero, We can set it to high by software and, thereby double the baud rate, , SMOD, It is not a bitaddressable, register, , HANEL, , --, , --, , MOV A,PCON, SETB ACC.7, MOV PCON,A, , --, , GF1, , GF0, , PD, , IDL, , ;place a copy of PCON in ACC, ;make D7=1, ;changing any other bits, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 40
Page 42 :
SERIAL, COMMUNICATION, PROGRAMMING, Doubling Baud, Rate, (cont’), , HANEL, , SMOD = 1, , 11.0592 MHz, XTAL, oscillator, , ÷ 12, , ÷ 16, , 57600 Hz, , Machine cycle freq, 921.6 kHz, SMOD = 0, , ÷ 32, , 28800 Hz, , To timer, 1 To set, the Baud, rate, , Baud Rate comparison for SMOD=0 and SMOD=1, TH1, , (Decimal), , (Hex), , SMOD=0, , SMOD=1, , -3, , FD, , 9600, , 19200, , -6, , FA, , 4800, , 9600, , -12, , F4, , 2400, , 4800, , -24, , E8, , 1200, , 2400, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 41
Page 44 :
Example 10-6 (cont’), , SERIAL, COMMUNICATION, PROGRAMMING, Doubling Baud, Rate, , Solution:, (a) This program transfers ASCII letter B (01000010, binary) continuously, (b) With XTAL = 11.0592 MHz and SMOD = 1 in the, above program, we have:, 11.0592 / 12 = 921.6 kHz machine cycle frequency., 921.6 / 16 = 57,600 Hz frequency used by timer 1, to set the baud rate., 57600 / 3 = 19,200, the baud rate., , (cont’), , Find the TH1 value (in both decimal and hex ) to set the baud rate, to each of the following. (a) 9600 (b) 4800 if SMOD=1. Assume, that XTAL 11.0592 MHz, Solution:, With XTAL = 11.0592 and SMOD = 1, we have timer frequency =, 57,600 Hz., (a) 57600 / 9600 = 6; so TH1 = -6 or TH1 = FAH, (b) 57600 / 4800 = 12; so TH1 = -12 or TH1 = F4H, HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 43
Page 45 :
SERIAL, COMMUNICATION, PROGRAMMING, Doubling Baud, Rate, , Example 10-8, Find the baud rate if TH1 = -2, SMOD = 1, and XTAL = 11.0592, MHz. Is this baud rate supported by IBM compatible PCs?, Solution:, With XTAL = 11.0592 and SMOD = 1, we have timer frequency =, 57,600 Hz. The baud rate is 57,600/2 = 28,800. This baud rate is, not supported by the BIOS of the PCs; however, the PC can be, programmed to do data transfer at such a speed. Also,, HyperTerminal in Windows supports this and other baud rates., , (cont’), , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 44
Page 51 :
PROGRAMMING, THE SECOND, SERIAL PORT, , , , Upon reset, DS89c4x0 uses Timer 1 for, setting baud rate of both serial ports, ¾, , (cont’), , ¾, , While each serial port has its own SCON, and SBUF registers, both ports can use, Timer1 for setting the baud rate, SBUF and SCON refer to the SFR registers, of the first serial port, Since the older 8051 assemblers do not support, this new second serial port, we need to define, them in program, To avoid confusion, in DS89C4x0 programs we, use SCON0 and SBUF0 for the first and SCON1, and SBUF1for the second serial ports, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 50
Page 55 :
SERIAL PORT, PROGRAMMING, IN C, Transmitting, and Receiving, Data, , HANEL, , Example 10-15, Write a C program for 8051 to transfer the letter “A” serially at 4800, baud continuously. Use 8-bit data and 1 stop bit., Solution:, #include <reg51.h>, void main(void){, TMOD=0x20;, TH1=0xFA;, SCON=0x50;, TR1=1;, while (1) {, SBUF=‘A’;, while (TI==0);, TI=0;, }, }, , //use Timer 1, mode 2, //4800 baud rate, , //place value in buffer, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 54
Page 56 :
SERIAL PORT, PROGRAMMING, IN C, , Example 10-16, Write an 8051 C program to transfer the message “YES” serially at, 9600 baud, 8-bit data, 1 stop bit. Do this continuously., , Transmitting, and Receiving, Data, , char);, , (cont’), , HANEL, , Solution:, #include <reg51.h>, void SerTx(unsigned, void main(void){, TMOD=0x20;, TH1=0xFD;, SCON=0x50;, TR1=1;, while (1) {, SerTx(‘Y’);, SerTx(‘E’);, SerTx(‘S’);, }, }, void SerTx(unsigned, SBUF=x;, while (TI==0);, TI=0;, }, , //use Timer 1, mode 2, //9600 baud rate, //start timer, , char x){, //place value in buffer, //wait until transmitted, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 55
Page 57 :
SERIAL PORT, PROGRAMMING, IN C, Transmitting, and Receiving, Data, (cont’), , HANEL, , Example 10-17, Program the 8051 in C to receive bytes of data serially and put them, in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit., Solution:, #include <reg51.h>, void main(void){, unsigned char mybyte;, TMOD=0x20;, //use Timer 1, mode 2, TH1=0xFA;, //4800 baud rate, SCON=0x50;, TR1=1;, //start timer, while (1) {, //repeat forever, while (RI==0); //wait to receive, mybyte=SBUF;, //save value, P1=mybyte;, //write value to port, RI=0;, }, }, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 56
Page 58 :
SERIAL PORT, PROGRAMMING, IN C, Transmitting, and Receiving, Data, (cont’), , Example 10-19, Write an 8051 C Program to send the two messages “Normal Speed”, and “High Speed” to the serial port. Assuming that SW is connected, to pin P2.0, monitor its status and set the baud rate as follows:, SW = 0, 28,800 baud rate, SW = 1, 56K baud rate, Assume that XTAL = 11.0592 MHz for both cases., Solution:, #include <reg51.h>, sbit MYSW=P2^0;, //input switch, void main(void){, unsigned char z;, unsigned char Mess1[]=“Normal Speed”;, unsigned char Mess2[]=“High Speed”;, TMOD=0x20;, //use Timer 1, mode 2, TH1=0xFF;, //28800 for normal, SCON=0x50;, TR1=1;, //start timer, ....., , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 57
Page 59 :
SERIAL PORT, PROGRAMMING, IN C, , ....., , if(MYSW==0) {, for (z=0;z<12;z++) {, SBUF=Mess1[z]; //place value in buffer, while(TI==0); //wait for transmit, TI=0;, }, }, else {, PCON=PCON|0x80;, //for high speed of 56K, for (z=0;z<10;z++) {, SBUF=Mess2[z]; //place value in buffer, while(TI==0); //wait for transmit, TI=0;, }, }, , Transmitting, and Receiving, Data, (cont’), , }, , HANEL, , Department of Computer Science and Information Engineering, National Cheng Kung University, , 58