Sie sind auf Seite 1von 45

Demonstration of Embedded C Program based on Serial Communication

Prepared By: Prof. Ankit D. Shah


Assistant Professor EC Department, IDS, NU
1

Significance of serial Communication:

In modern multiprocessor distributed system, one computer must be able to communicate with other computers. One cost-effective way to communicate is to send and receive data bits serially. The 8051 has a serial data communication circuit that uses register SBUF to hold data, Register SCON controls data communication, register PCON controls data rates, and pins RXD (P3.0) and TXD (P3.1) connect to serial data network.

Saturday, February 23, 2013

Prof. Ankit D. shah

Hardware for Serial Communication:

SBUF SFR - physically two registers. One is write only and is used to hold data to be transmitted out of the 8051 via TXD. The other is read only and holds received data from external sources via RXD. Both mutually exclusive registers use address 99 H. SCON SFR - controls data communication PCON SFR - controls data rates Pins RXD (P3.0) and TXD (P3.1) connect to serial data network. UART for Asynchronous and Shift register for Synchronous serial communication.
Saturday, February 23, 2013 Prof. Ankit D. shah 3

8051 Serial Communication:

The 8051 has a single full duplex (means it can transmit and receive data at a time) serial port that can be used for either asynchronous or synchronous communication. The port has 4 modes of operation:

Mode 0: Synchronous at 1/12 oscillator frequency. Mode 1: 8 bit Asynchronous - variable baud rate. Mode 2: 9 bit Asynchronous -1/32 OR 1/64 of the oscillator frequency. Mode 3: 9 bit asynchronous - variable baud rate.
Saturday, February 23, 2013 Prof. Ankit D. shah 4

Serial Communication Block Diagram:

In the memory map we have serial buffer special function register (SBUF) at location 99H. Unlike any other register in the 8051, SBUF is in fact two distinct registers the write-only register and the read-only register. Transmitted data is sent out from the write-only register while received data is stored in the read-only register. There are two separate data lines, one for transmission (TXD)(P3.1) and one for reception (RXD)(P3.0). Therefore, the serial port can be transmitting data down the TXD line while it is at the same time receiving data on the RXD line
Saturday, February 23, 2013 Prof. Ankit D. shah 5

SBUF register

Data to be transmitted via the TXD line must be placed in SBUF register. SBUF holds the byte of data when it is received by 8051 RXD line. MOV SBUF, #45H - this sends the byte 45H down the serial line MOV A, SBUF - this takes whatever data was received by the serial port and puts it in the accumulator Once byte is written in SBUF, it is framed with the start and stop bits and transferred serially via the TXD pin. When the bits are received serially via RXD, the 8051 deframes it by eliminating the stop and start bits and then placed it in SBUF register.
Saturday, February 23, 2013 Prof. Ankit D. shah 6

Serial control (SCON) Register:


7 6 5 4 3 2 1 0

SM0

SM1

SM2

REN

TB8

RB8

TI

RI

SM0 (SCON.7) : mode specifier SM1 (SCON.6) : mode specifier SM2 (SCON.5) : used for multi processor communication REN (SCON.4) : receive enable (by software enable/disable) TB8 (SCON.3) : transmit bit8 RB8 (SCON.2) : receive bit 8 TI (SCON.1) : transmit interrupt flag set by HW clear by SW RI (SCON.0) : receive interrupt flag set by HW clear by SW

Saturday, February 23, 2013

Prof. Ankit D. shah

Mode of operation:
SM0 SM1 MODE 0 0 0 0 1 1 1 0 2 1 1 3 operation transmit rate shift register fixed (xtal/12) 8 bit UART variable (timer1) 9 bit UART fixed (xtal/32 or xtal/64) 9 bit UART variable (timer1)

Saturday, February 23, 2013

Prof. Ankit D. shah

Cont
SM2 : Multiprocessor Communication Bit. Set / Cleared by program to enable multiprocessor communications in modes 2 and 3. When set to 1 an interrupt is generated if bit 9 of the received data is 1; no interrupt is generated if bit 9 is a 0. If set to 1 for mode1, no interrupt will be generated unless a valid stop bit is received. Clear to 0 if mode 0 is in use. REN : Receive Enable bit. Set to 1 to enable reception; cleared to 0 to disable reception. (by software enable/disable) when high, it allows the 8051 to receive data on the RXD pin of the 8051 8051 in simultaneous transmit and receive mode REN must be set to 1 REN = 0 means receiver is disabled TB8 : transmit bit8 not widely used make it 0 used for serial mode 2 and 3 RB8 : receive bit 8 not widely used make it 0 in mode 1 this bit gets a copy of the stop bit, when 8 bit data is received . Also used for mode 2 and 3.
Saturday, February 23, 2013 Prof. Ankit D. shah 9

Cont
TI : Transmit Interrupt flag. Set to one at the end of bit 7 times in mode 0 and at the beginning of the stop bit for other modes. Must be cleared by the program. set by H/W after send , clear by SW. when 8051 finishes the transfer of the 8 bit character, it raises the TI flag to indicate that it is ready to transfer another byte. TI is raised at the beginning of the stop bit. RI : Receive Interrupt flag. Set to one at the end of bit 7 times in mode 0 and halfway through the stop bit for other modes. Must be cleared by the program. set by HW after received ,clear by SW. when 8051 receives data serially via RXD it gets rid of the start and stop bit and places the byte in SBUF register. Then it raises the RI bit to indicate that the byte has been received and should be picked up before it is lost. RI is raised halfway through the stop bit.

Saturday, February 23, 2013

Prof. Ankit D. shah

10

Serial Data Interrupts:


Serial data communication is a relatively slow process, occupying many milliseconds per data byte to accomplish. In order not to tie up valuable processor time, Serial data flags are included in SCON to aid in efficient data transmission and reception. Notice that data transmission is under the complete control of the program, but reception of data is unpredictable and at random times that are beyond the control of the program. The serial data flags in TCON, TI and RI, are set whenever a data byte is transmitted (TI) or received (RI). These flags are ORed together to produce an interrupt to the program. The program must read these flags to determine which caused the interrupt and then clear the flag. This is unlike the timer flags that are cleared automatically; it is the responsibility of the programmer to write routines that handle the serial data flags.
Saturday, February 23, 2013 Prof. Ankit D. shah 11

Serial Data Transmission:


Transmission of serial data bits begins anytime data is written to SBUF. TI is set to 1 when the data has been transmitted and signifies that SBUF is empty (for transmission purpose) and that another data byte can be sent. If the program fails to wait for the TI flag and overwrites SBUF while a previous data byte is in process of being transmitted, the results will be unpredictable (garbage out).

Saturday, February 23, 2013

Prof. Ankit D. shah

12

Serial Data Reception:


Reception of serial data will begin if the receive enable bit (REN) in SCON is set to 1 for all modes. In addition, for mode 0 only, RI must be cleared to 0. Receiver Interrupt flag RI is set after data has been received in all modes. Setting REN is the only direct program control that limits the reception of unexpected data; the requirement that RI also be 0 for mode 0 prevents the reception of new data until the program has dealt with the old data and reset RI. Reception can begin in modes 1, 2 and 3 if RI is set when the serial stream of bits begins. RI must have been reset by the program before the last bit is received or the incoming data will be lost. Incoming data is not transferred to SBUF until the last bit has been received so that the previous transmission can be read from SBUF while new data is being received.

Saturday, February 23, 2013

Prof. Ankit D. shah

13

Serial data Mode:


The 8051 designers have included four modes of serial data transmission that enable data communication to be done in a variety of ways and a multitude of baud rates. Modes are selected by the programmer by setting the modes bits SM0 and SM1 in SCON. Baud rates are fixed for mode 0 and variable, using timer 1 and the serial baud rate modify bit (SMOD) in PCON, for modes 1, 2 and 3.

Saturday, February 23, 2013

Prof. Ankit D. shah

14

Cont

Mode 0 : simply a shift register Serial data enters and exits through RxD TxD outputs the shift clock. 8 bits are transmitted/received (LSB first) The baud rate is fixed at 1/12 the oscillator frequency.

Application

Port expansion
Saturday, February 23, 2013 Prof. Ankit D. shah 15

Cont

Mode 0 : Serial data enters and exits through RxD TxD outputs the shift clock. 8 bits are transmitted/received(LSB first) The baud rate is fixed a 1/12 the oscillator frequency.

Application

Port expansion 8051


TXD RXD

clk Shift register data

Saturday, February 23, 2013

Prof. Ankit D. shah

16

Cont

As can be seen in the diagram the terms TXD and RXD are misleading in mode 0. TXD serves as the clock while RXD is used for both receiving and transmitting data. In mode 0, the serial port is only half duplex; it cannot transmit and receive data at the same time because the same line (RXD) is being used for both transmit and receive. The serial port in mode 0 is an example of synchronous communication; the clock signal is sent with the data on the TXD line. TXD pulses at the same frequency as the machine cycle means TXD runs at 1/12th the frequency of the system clock. If we are using a 12MHz system clock, then the frequency of TXD is 1MHz, which implies its cycle length is 1us. Therefore, each bit is active on the RXD line for 1us. To shift the entire 8-bit word along RXD takes 8us.
Prof. Ankit D. shah 17

Saturday, February 23, 2013

Serial Data Mode 1 Standard UART:


When SM0 and SM1 are set to 01b, SBUF becomes a 10-bit fullduplex receiver-transmitter that may receive and transmit data at the same time. Pin RXD receives all data and pin TXD transmits all data. Following figure shows the format of a data word. Data Transmission: Transmitted data is sent as a start bit, eight data bits (least significant bit, LSB, first) and a stop bit. Interrupt flag TI is set once all ten bits have been sent. Each bit interval is the inverse of the baud rate frequency and each bit is maintained high or low over that interval. Data Reception: Received data is obtained in the same order; reception is triggered by the falling edge of the start bit and continues if the start bit is true (0 level) halfway through the start bit interval. This is an anti-noise measure; if the reception circuit is triggered by noise on the transmission line, the check for a low after half a bit interval should limit false data reception.
Saturday, February 23, 2013 Prof. Ankit D. shah 18

Timing Diagram of Mode 1 Serial Communication:

Saturday, February 23, 2013

Prof. Ankit D. shah

19

Equation of Baud Rates for Mode - 1 Serial Communication:


Timer 1 is used to generate the baud rate for mode 1 by suing the Overflow flag of the timer to determine the baud frequency. Typically, timer 1 is used timer mode 2 as an auto load 8-bit timer that generates the baud frequency: 2SMOD oscillator frequency Fbaud = -------- X --------------------------32 12 X (256- TH1) SMOD is the control bit in PCON and can be 0 or 1, which raises the 2 in equation to a value of 1 or 2. If timer 1 is not run in timer mode 2, then the baud rate is found using following equation: 2SMOD Fbaud = --------- X (timer 1 overflow frequency) 32 and timer 1 can be run using the internal clock or as a counter that receives clock pulses from any eternal source via pin T1.
Saturday, February 23, 2013 Prof. Ankit D. shah 20

Mode 1 of operation

Mode 1

Total Ten bits are transmitted (through TxD) or received (through RxD) (A start bit (0), 8 data bits (LSB first), and a stop bit (1) ) On receive, the stop bit goes into RB8 in SCON The baud rate is determined by the Timer 1 overflow rate. Timer1 clock is 1/32 machine cycle (MC=1/12 XTAL)

Timer clock can be programmed as 1/16 of machine cycle Transmission is initiated by any instruction that uses SBUF as a destination register.
Saturday, February 23, 2013 Prof. Ankit D. shah 21

Setting the Serial Port Baud Rate:


Serial ports baud rate setting is required for modes 1 and 3. The Baud Rate is determined based on the oscillators frequency when in mode 0 and 2. In mode 0, the baud rate is always the oscillator frequency divided by 12. This means if your crystal is 11.0592MHz, mode 0 baud rate will always be 921,600 baud. In mode 2 the baud rate is always the oscillator frequency divided by 64 or divide by 32, so a 11.0592MHz crystal speed will yield a baud rate of 172,800 or 345,600. In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows. The more frequently timer 1 overflows, the higher the baud rate. There are many ways one can cause timer 1 to overflow at a rate that determines a baud rate, but the most common method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate.
Saturday, February 23, 2013 Prof. Ankit D. shah 22

Calculating Load Count Value:

To determine the value that must be placed in TH1 to generate a given baud rate, we may use the following equation (assuming PCON.7 is clear). TH1 = 256 - ((Crystal / 384) / Baud) TH1 = 256 - ((Crystal / 384) / Baud) TH1 = 256 - ((11059000 / 384) / 9600 ) TH1 = 256 - ((28,799) / 9600) TH1 = 256 2.9999 = 253.0001 = FD H

Saturday, February 23, 2013

Prof. Ankit D. shah

23

Programming steps to transfer data serially ( in mode 1 )


1. 2.

3.

4. 5. 6. 7.

8.

TMOD SFR is loaded with 20H (timer 1 in mode 2) to set the baud rate TH1 is loaded with the count for specific baud rate and TL1 is also loaded with the same count value for first time. SCON with 50H if simultaneous transmit and receive operations are required else only for transmit load 40 H for mode 1. TR1 is set to start time - 1 TI is cleared by TI = 0; statement means make it 0 during the starting The character byte to be transferred serially is written into SBUF. TI flag is to be monitored to if the character has been transferred completely. To transfer the next character go to step 5
Saturday, February 23, 2013 Prof. Ankit D. shah 24

Programming steps to receive data serially ( in mode 1 )


1.

2. 3. 4. 5.

6.

7.

8.

TMOD Register is loaded with 20H (timer 1 in mode 2) to set the baud rate TH1 is loaded with the count for specific baud rate SCON with 50H in mode 1, receive enable must be turned on TR1 is set to start time -1 RI is cleared by RI = 0; statement means make it 0 during the starting The character byte to be transferred serially is written into SBUF. RI flag is to be monitored to if the character has been transferred completely. To transfer the next character go to step 5
Saturday, February 23, 2013 Prof. Ankit D. shah 25

Generation of Baud Rate Clock:

Saturday, February 23, 2013

Prof. Ankit D. shah

26

Power Control Register:

Saturday, February 23, 2013

Prof. Ankit D. shah

27

What is SMOD

Bit 7 of PCON register If SMOD =1 double baud rate PCON is not bit addressable How to set SMOD
MOV A, PCON SETB ACC.7 MOV PCON, A

Saturday, February 23, 2013

Prof. Ankit D. shah

28

Practical Aspects for Serial Communication:


Since IBM PC compatible computers are so widely used to communicate with 8051 based systems, we still emphasize serial communications of the 8051 with the COM port of the PC. To allow data transfer between the PC and an 8051 system without any error, we must make sure that the baud rate of the 8051 system matches the baud rate of the PCs COM port. The baud rates supported by PC BIOS are given in below table. You can examine these baud rates by going to the Windows Terminal program and clicking on communication settings option (All program / Accessories / Communication / Hyper terminal/ Make new connection / port speed or baud rate).

Saturday, February 23, 2013

Prof. Ankit D. shah

29

Cont
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 PCs COM port Hyper terminal function supports baud rates much higher than listed below: PC Baud Rates: 110 150 300 600 1200 2400 4800 9600 19200
Saturday, February 23, 2013 Prof. Ankit D. shah 30

Steps to be consider while developing program based on Serial Communication:


In programming the 8051 to transfer character bytes serially, the following steps must be taken: The TMOD SFR is loaded with the value 20 H (00100000b), indicating the use of tuner 1 in mode- 2 (8-bit auto reload) to set the baud rate. The TH1 is loaded with one of the values as shown in below table to set the baud rate for serial data transfer (assume crystal frequency = 11.0562 Mhz). The SCON SFR is loaded with the value 50 (01010000b), indicating serial mode-1 (standard UART), where 8-bit data framed with start and stop bits and data reception is enable and also RI and TI is cleared, if you want disable data reception then load 40 H (01000000b) in SCON SFR. TR1 set to 1 to start timer 1.
Saturday, February 23, 2013 Prof. Ankit D. shah 31

Cont
The character byte to be transferred serially is written into the SBUF SFR. The TI (Transmit Interrupt) flag bit is monitored with the use of the statement while (TI == 0); to see if the character has been transferred completely. Before transferring next character, cleared the TI by the TI = 0; statement. Go to step 5. During receiving you have to make sure that before last bit of received data the RI flag must be cleared by the RI = 0; statement otherwise the incoming data will be lost.
Saturday, February 23, 2013 Prof. Ankit D. shah 32

Table Showing the load count value for standard Baud Rates:

Note: Assuming that the SMOD bit in PCON SFR is 0, the crystal frequency is 11.0592 MHz and timer 1 is operating in Mode -2 for calculating the load count value in TH1 to generate above standard baud rate. # SMOD bit (D7 bit) of PCON SFR is 1.

Saturday, February 23, 2013

Prof. Ankit D. shah

33

Baud Rate in the 8051:


The 8051 transfers and receives data serially at many different baud rates. The baud rate in the 8051 is programmable. This is done with the help of timer 1 and also using SMOD bit of PCON SFR. But, we consider the SMOD bit of PCON SFR is 0 throughout out our discussion. Now, first we will look at the relationship between the crystal frequency and the baud rate in the 8051. As discussed in clock and oscillator circuit, the 8051 divides the crystal frequency by 12 to get the machine cycle frequency. In the case of crystal having crystal frequency is 11.0592 MHz. The machine cycle frequency is 11.0592 MHz / 12 = 921.6 KHz. The 8051 Serial communication UART circuitry divides the machine cycle frequency of 921.6 KHz by 32 once more before it is used by timer 1 to set the baud rate. Therefore, 921.6 KHz divided by 32 gives 28,800 Hz. This is the value we will use throughout the discussion of serial communication to find the timer 1 load count value to set the baud rate. When timer 1 is used to set the baud rate it must be programmed in mode 2, that is 8-bit auto-reload. To get baud rates compatible with the PC, we must load TH1 with the values shown in above table.
Saturday, February 23, 2013 Prof. Ankit D. shah 34

SBUF SFR:
SBUF SFR is an 8-bit register used solely for serial communication in 8051. For a byte of data to be transferred via the TXD line, it must be placed in SBUF SFR. Similarly, SBUF SFR holds the byte of data when it is received by the 8051s RXD line. SBUF can be accessed like any other SFRs in the 8051. MOV SBUF, # D;load SBUF = 44H, ASCII for D MOV SBUF, A ;load the content of Accumulator in SBUF MOV A, SBUF ;copy the content of SBUF in to Accumulator The moment a byte is written into SBUF; it is framed with the start and stop bits and transferred serially via the TXD pin. Similarly, when the bits are received serially via RXD, the 8051 deframes it by eliminating the stop and start bits, making a byte out the data received and then placing it in the SBUF. Saturday, February 23,
2013 Prof. Ankit D. shah 35

Example to find out the load count value for TH1: Find the TH1 value needed to have 9600 bps baud rates. Assume that the crystal is
having standard crystal frequency. Solution: The machine cycle frequency of the 8051 is crystal frequency divided by 12. In our case the crystal is having the standard crystal frequency and the value of standard crystal frequency is 11.0592 MHz. So, the machine cycle frequency is 11.0592 MHz / 12 = 921.6 KHz. Now, normally we do serial communication using mode-1 and to generate the required baud rate, we use timer 1 in mode-2. Now, when we did serial communication using mode-1 then the machine cycle frequency is divided by 32 using UART and that frequency provided by UART to timer 1 to set baud rate. So, in our case the machine cycle frequency is 921.6 KHz is divided by 32 that is 921.6 KHz / 32 = 28,800 Hz, which provided by UART to timer 1 to set required baud rate. To generate the baud rate of 9600 bps, timer -1 must generates the signal having frequency of 9600 Hz. To generate the signal having frequency of 9600 Hz by timer 1, the actual count value is clock frequency of timer divided by the frequency of signal generated by timer 1. So, in our case the actual count is 28,800 Hz / 9600 Hz = 3. To find out the load count in case of timer 1 operating in mode-2, take 2s complement of actual count in 8-bits size and that is -3 = FD H or you can subtract the hexa-decimal value of actual count from 100 H, becomes the load count value. So, to do serial communication at baud rate of 9600 bps, we have to load FD H in timer 1 higher byte (TH1).
Saturday, February 23, 2013 Prof. Ankit D. shah

36

Programming the serial communication interrupt:


TI will be raised when the last bit of the framed data the stop bit is transferred indicating that SBUF register is ready to transfer the next byte. RI is raised when the entire frame of data including stop bit is received. Implementation either by polling or an interrupt IE.4 in IE register is enabled , when RI or TI is raised the 8051 is interrupted and jumps to memory location 0023 H to execute the ISR. In the ISR we must examine the TI and RI flag to see which caused the interrupt and responds accordingly. The 8051 does not automatically clear the RI and TI flags you must clear these bits in your interrupt handler where as in external and timer interrupt it is the job of the 8051 to clear the interrupt flags.

Saturday, February 23, 2013

Prof. Ankit D. shah

37

How to communicate 8051 to PC:


Connect TXD to RXD and RXD to TXD from pc to 8051 Use max232 to transform signal from TTL level to RS232 level The baud rate of the 8051 must matched the baud rate of the pc PC standard baud rate

2400-4800-9600-14400-19200-28800-33600-57600

Serial mode 1 is used Timer 1 is used The 8051 UART divides the machine cycle frequency by 32 Machine cycle is 1/12 XTAL frequency We use timer1 in mode 2 (auto reload)
Prof. Ankit D. shah 38

Saturday, February 23, 2013

RxD and TxD pins in the 8051


TxD pin 11 of the 8051 (P3.1) RxD pin 10 of the 8051 (P3.0)

SBUF register
MOV MOV MOV SBUF,#D SBUF,A A,SBUF ;load SBUF=44H, ASCII for D ;copy accumulator into SBUF ;copy SBUF into accumulator

Saturday, February 23, 2013

Prof. Ankit D. shah

39

Saturday, February 23, 2013

Prof. Ankit D. shah

40

Saturday, February 23, 2013

Prof. Ankit D. shah

41

Embedded C program for the 8051 to transfer letter A serially at 4800 baud, continuously. Solution:
#include ankit.h ankit.h void main (void) { TMOD = 0X20; Timer 1 in mode 2, TH1 = 0XFA; 4800 baud rate TL1 = 0XFA; To load count value first time SCON = 0X40; 8-bit, 1 stop bit and REN disable TR1 = 1; To start timer 1 while (1) { SBUF = A; ASCII value of A while (TI == 0); Wait for the last bit TI = 0; To clear the transmit interrupt

} }
Saturday, February 23, 2013 Prof. Ankit D. shah 42

;An example of sending a message. #include "ankit.h Void serial (unsigned char); void main ( ) { TMOD = 0X20; TH1 = 0XF4; TL1 = 0XF4; SCON = 0X40; TR1 = 1; while (1) { serial (G); serial (O); serial (O); serial (D); } } void serial (unsigned char j) { SBUF = j; while (TI == 0); TI = 0; }
Saturday, February 23, 2013 Prof. Ankit D. shah 43

Question Answer Session


Saturday, February 23, 2013 Prof. Ankit D. shah 44

Thank You

Saturday, February 23, 2013

Prof. Ankit D. shah

45

Das könnte Ihnen auch gefallen