UART project report by Tarun Khaneja ( 09034406598 )

46
A Summer Training Report On Embedded System Design using 8051 based microcontroller EMSEEKER (DESIGNNOVA) Submitted to: Submitted by: Er. Nidhi Uppal Tarun Khaneja A. P. & Head 09034406598 3 rd year Under the guidance of Er. Sumeet Pahwa Director of EMSEEKER 1

description

UART project report by Tarun Khaneja ( 09034406598 )

Transcript of UART project report by Tarun Khaneja ( 09034406598 )

Page 1: UART project report by Tarun Khaneja ( 09034406598 )

A

Summer Training Report On

Embedded System Design using

8051 based microcontroller

EMSEEKER (DESIGNNOVA)

Submitted to: Submitted by:

Er. Nidhi Uppal Tarun Khaneja

A. P. & Head 09034406598

3rd year

Under the guidance of

Er. Sumeet Pahwa

Director of EMSEEKER

Department of Electronics & Communication Engineering

Doon Valley Institute of Engineering & Technology

Karnal- Haryana

(Aug. 2012)

1

Page 2: UART project report by Tarun Khaneja ( 09034406598 )

Chapter-1

1.1 The UART: What it is and how it works

A UART (Universal Asynchronous Receiver/Transmitter) is the microchip with programming that controls a computer's interface to its attached serial devices. Specifically, it provides the computer with the RS-232C Data Terminal Equipment (DTE) interface so that it can "talk" to and exchange data with modems and other serial devices. As part of this interface, the UART also:

Converts the bytes it receives from the computer along parallel circuits into a single serial bit stream for outbound transmission

On inbound transmission, converts the serial bit stream into the bytes that the computer handles

Adds a parity bit (if it's been selected) on outbound transmissions and checks the parity of incoming bytes (if selected) and discards the parity bit

Adds start and stop delineators on outbound and strips them from inbound transmissions

Handles interrupts from the keyboard and mouse (which are serial devices with special ports)

May handle other kinds of interrupt and device management that require coordinating the

computer's speed of operation with device speeds

Serial transmission is commonly used with modems and for non-networked communication between computers, terminals and other devices.

The communications links across which computers—or parts of computers—talk to one another may be either serial or parallel. A parallel link transmits several streams of data

(perhaps representing particular bits of a stream of bytes) along multiple channels (wires, printed circuit tracks, optical fibres, etc.); a serial link transmits a single stream of data.

Figure 1.1

At first sight it would seem that a serial link must be inferior to a parallel one, because it can transmit less data on each clock tick. However, it is often the case that serial links can be clocked considerably

2

Page 3: UART project report by Tarun Khaneja ( 09034406598 )

faster than parallel links, and achieve a higher data rate. A number of factors allow serial to be clocked at a greater rate:

Clock skew between different channels is not an issue (for unlocked serial links)

A serial connection requires fewer interconnecting cables (e.g. wires/fibres) and hence occupies less space. The extra space allows for better isolation of the channel from its surroundings

Crosstalk is less of an issue, because there are fewer conductors in proximity.

In many cases, serial is a better option because it is cheaper to implement. Many ICs have serial interfaces, as opposed to parallel ones, so that they have fewer pins and are therefore cheaper.

Figure 1.2

In telecommunications and computer science, serial communications is the process of sending data one bit at one time, sequentially, over a communications channel or computer bus. This is in contrast to parallel communications, where all the bits of each symbol are sent together. Serial communications is used for all long-haul communications and most computer networks, where the cost of cable and synchronization difficulties makes parallel communications impractical. Serial computer buses are becoming more common as improved technology enables them to transfer data at higher speeds.

1.2 Serial versus parallel

The Serial Port is harder to interface than the Parallel Port. In most cases, any device you connect to the serial port will need the serial transmission converted back to parallel so that it can be used. This can be done using a UART. On the software side of things, there are many more registers that you have to attend to than on a Standard Parallel Port. (SPP)

3

Page 4: UART project report by Tarun Khaneja ( 09034406598 )

So what are the advantages of using serial data transfer rather than parallel?

1. Serial Cables can be longer than Parallel cables. The serial port transmits a '1' as  -3 to - 25 volts and a '0' as +3 to +25 volts where as a parallel port transmits a '0' as 0v and a '1' as 5v. Therefore the serial port can have a maximum swing of 50V compared to the parallel port which has a maximum swing of 5 Volts. Therefore cable loss is not going to be as much of a problem for serial cables than they are for parallel.

2. You don't need as many wires than parallel transmission. If your device needs to be mounted a far distance away from the computer then 3 core cable (Null Modem Configuration) is going to be a lot cheaper that running 19 or 25 core cable. However you must take into account the cost of the interfacing at each end.

3. Infra Red devices have proven quite popular recently. You may of seen many electronic diaries and palmtop computers which have infra red capabilities build in. However could you imagine transmitting 8 bits of data at the onetime across the room and being able to (from the devices point of view) decipher which bits are which? Therefore serial transmission is used where one bit is sent at a time. IrDA-1 (The first infra red specifications) was capable of 115.2k baud and was interfaced into a UART. The pulse length however was cut down to 3/16th of a RS232 bit length to conserve power considering these devices are mainly used on diaries, laptops and palmtops.

4. Microcontrollers have also proven to be quite popular recently. Many of these have in built SCI (Serial Communications Interfaces) which can be used to talk to the outside world. Serial Communication reduces the pin count of these MPU's. Only two pins are commonly used, Transmit Data (TXD) and Receive Data (RXD) compared with at least 8 pins if you use a 8 bit Parallel method (You may also require a Strobe).

There are two primary forms of serial transmission: Synchronous and Asynchronous. Depending on the modes that are supported by the hardware, the name of the communication sub-system will usually include a A if it supports Asynchronous communications, and a S if it supports Synchronous communications. Both forms are described below.

Figure 1.3

1.3 Synchronous Serial Transmission

Synchronous serial transmission requires that the sender and receiver share a clock with one another, or that the sender provide a strobe or other timing signal so that the receiver knows when to “read” the next bit of the data. In most forms of serial Synchronous communication, if there is no data available at a given instant to transmit, a fill character must be sent instead so that data is always being transmitted. Synchronous communication is usually more efficient because only data bits are transmitted between sender and receiver, and synchronous communication can be more costly if extra wiring and circuits are required to share a clock signal between the sender and receiver. A form of

4

Page 5: UART project report by Tarun Khaneja ( 09034406598 )

Synchronous transmission is used with printers and fixed disk devices in that the data is sent on one set of wires while a clock or strobe is sent on a different wire. Printers and fixed disk devices are not normally serial devices because most fixed disk interface standards send an entire word of data for each clock or strobe signal by using a separate wire for each bit of the word. In the PC industry, these are known as Parallel devices. The standard serial communications hardware in the PC does not support Synchronous operations. This mode is described here for comparison purposes only.

Figure 1.4 serial cable

1.4 Asynchronous Serial Transmission

Asynchronous serial communication describes an asynchronous transmission protocol in which a start signal is sent prior to each byte, character or code word and a stop signal is sent after each code word. The start signal serves to prepare the receiving mechanism for the reception and registration of a symbol and the stop signal serves to bring the receiving mechanism to rest in preparation for the reception of the next symbol. A common kind of start-stop transmission is ASCII over RS-232, for example for use in teletypewriter operation.In the diagram, a start bit is sent, followed by eight data bits, no parity bit and one stop bit, for a10-bit character frame. The number of data and formatting bits, and the transmission speed, must be pre

5

Page 6: UART project report by Tarun Khaneja ( 09034406598 )

Agreed by the communicating parties. After the stop bit, the line may remain idle indefinitely, or another character may immediately be started.

1.5 Bits, Baud and Symbols

Baud is a measurement of transmission speed in asynchronous communication. Because of advances in modem communication technology, this term is frequently misused when describing the data rates in newer devices. Traditionally, a Baud Rate represents the number of bits that are actually being sent over the media, not the amount of data that is actually moved from one DTE device to the other. The Baud count includes the overhead bits Start, Stop and Parity that are generated by the sending UART and removed by the receiving UART. This means that seven-bit words of data actually take 10 bits to be completely transmitted. Therefore, a modem capable of moving 300 bits per second from one place to another can normally only move 307-bit words if Parity is used and one Start and Stop bit are present. If 8-bit data words are used and Parity bits are also used, the data rate falls to 27.27 words per second, because it now takes 11 bits to send the eight-bit words, and the modem still only sends 300 bits per second. The formula for converting bytes per second into a baud rate and vice versa was simple until error- correcting modems came along. These modems receive the serial stream of bits from the UART in the host computer (even when internal modems are used the data is still frequently serialized) and converts the bits back into bytes. These bytes are then combined into packets and sent over the phone line using a Synchronous transmission method. This means that the Stop, Start, and Parity bits added by the UART in the DTE (the computer) were removed by the modem before transmission by the sending modem. When these bytes are received by the remote modem, the remote modem adds Start, Stop and Parity bits to the words, converts them to a serial format and then sends them to the receiving UART in the remote computer, who then strips the Start, Stop and Parity bits. The reason all these extra conversions are done is so that the two modems can perform error correction, which means that the receiving modem is able to ask the sending modem to resend a block

of data that was not received with the correct checksum. This checking is handled by the modems, and the DTE devices are usually unaware that the process is occurring. By striping the Start, Stop and Parity bits, the additional bits of data that the two modems must share between themselves to perform error-correction are mostly concealed from the effective transmission rate seen by the sending and receiving DTE equipment. For example, if a modem sends ten 7-bit words to another modem without including the Start, Stop and Parity bits, the sending modem will be able to add 30 bits of its own information that the receiving modem can use to do error-correction without impacting the transmission speed of the real data. The use of the term Baud is further confused by modems that perform compression. A single 8-bit word passed over the telephone line might represent a dozen words that were transmitted to the sending modem. The receiving modem will expand the data back to its original content and pass that data to the receiving DTE. Modern modems also include buffers that allow the rate that bits move across the phone line (DCE to DCE) to be a different speed than the speed that the bits move between the DTE and DCE on both ends of the conversation. Normally the speed between the DTE and DCE is higher than the DCE to DCE speed because of the use of compression by the modems. Because the number of bits needed to describe a byte varied during the trip between the two machines plus the differing bits-per-seconds speeds that are used present on the DTE-DCE and DCE-DCE links, the usage of the term Baud to describe the overall communication speed causes problems and can misrepresent the true transmission speed. So Bits Per Second (bps) is the correct term to use to describe the transmission rate seen at the DCE to DCE interface and Baud or Bits Per Second are acceptable terms to use when a connection is made between two systems with a wired connection, or if a modem is in use that is not performing  error-correction or compression. Modern high speed modems (2400, 9600, 14,400, and 19,200bps) in reality still operate at or below 2400 baud, or more accurately, 2400 Symbols per second. High speed modem are able to encode more bits of data into each Symbol using a technique called Constellation Stuffing, which is why the effective bits per second rate of the modem is higher, but the modem continues to operate within the limited audio bandwidth that the telephone system provides. Modems operating at 28,800 and higher speeds have variable Symbol rates, but the technique is the same.

6

Page 7: UART project report by Tarun Khaneja ( 09034406598 )

1.6 Asynchronous Serial Reception

A multiplexed data communication pulse can only be in one of two states but there are many names for the two states. When on, circuit closed, low voltage, current flowing, or a logical zero, the pulse is said to be in the "space" condition. When off, circuit open, high voltage, current stopped, or a logical one, the pulse is said to be in the "mark" condition. A character code begins with the data communication circuit in the space condition. If the mark condition appears, a logical one is recorded otherwise a logical zero.

Figure. 1.5 Asynchronous Code Format.

The least-significant bit is always transmitted first. If parity is present, the parity bit comes after the data bits but before the stop bit(s).

The start bit is always a '0' (logic low), which is also called a space. Ironically, the logic low '0' corresponds to a high voltage on the data wire. The start bit signals the receiving DTE that a character code is coming. The next five to eight bits, depending on the code set employed, represent the character. In the ASCII code set the eighth data bit may be a parity bit. The next one or two bits are always in the mark (logic high, i.e., '1') condition and called the stop bit(s). They provide a "rest" interval for the receiving DTE so that it may prepare for the next character which may be after the stop bit(s). The rest interval was required by the old mechanical Teletypes which used a motor driven camshaft todecode each character. At the end of each character the motor needed time to strike the character bail (print the character) and reset the camshaft.

There are six basic steps in receiving a serial character code into a parallel register. First, to keep track of time, the receiver employs a clock which "ticks." When the line is in the space condition, the receiver samples the line 16 times the data rate. In other words, a data interval is equal to 16 clock ticks. In this way the receiver can determine the beginning of the start bit and "move over" to the center of the bit time for data sampling. Second, when the line goes into the mark state, declare a "looking for start bit" condition and wait one half the bit interval or eight clock ticks. Third, sample the line again and if it has not remained in the mark condition, consider this to be a spurious voltage change and go back to step one. Fourth, if the line was still in the mark state, then consider this a valid start bit. Shift the start bit into an eight-bit shift register and wait one bit time or 16 clock ticks. Fifth,

7

  start|<-five to eight databits   ->| stop bit(s)

0---- -- - - - - - - - - -   

Space(logic low, high data-wire voltage)           

  | | || | | || | | | |

  | S | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | S | S |

  | | || | | || | | | |

1  - - -  - - - - -- -   - --------

Mark(logic high, low data-wirevoltage)              

Page 8: UART project report by Tarun Khaneja ( 09034406598 )

after one bit time sample the line (the data should have been there for the last eight clock ticks, and should remain for eight more clock ticks). Now shift the sample into the shift register. Sixth, continue steps four and five seven more times. After the eighth shift, the start bit will "migrate" into a flip-flop indicating character received. Go to step one.

Before the transmitter and receiver UARTs will work, they must also agree on the same values of five parameters. First, both sides must agree on the number of bits per character. Second, the speed or Baud of the line must be the same on both sides. Third, both sides must agree to use or not use parity. Fourth, if parity is used, both sides must agree on using odd or even parity. Fifth, the number of stop bits must be agreed upon. Having said all this, most DTEs today employ eight data bits, no parity, and one stop bit. Thus there is a rule-of-thumb that the number of characters per second is equal to the Baud divided by 10 for a typical RS-232 or RS-423data line.

1.7 Other UART Functions

In addition to the basic job of converting data from parallel to serial for transmission and from serial to parallel on reception, a UART will usually provide additional circuits for signals that can be used to indicate the state of the transmission media, and to regulate the flow of data in the event that the remote device is not prepared to accept more data. For example, when the device connected to the UART is a modem, the modem may report the presence of a carrier on the phone line while the computer may be able to instruct the modem to reset itself or to not take calls by raising or lowering one more of these extra signals. The function of each of these additional signals is defined in the EIA RS232-C standard.

8

Page 9: UART project report by Tarun Khaneja ( 09034406598 )

Chapter-2

The RS-232 Standard 2.1 The RS232-C and V.24 Standards

In most computer systems, the UART is connected to circuitry that generates signals that comply with the EIA RS232-C specification. There is also a CCITT standard named V.24 that mirrors the specifications included in RS232-C.

Figure 2.1 RS 232

9

Page 10: UART project report by Tarun Khaneja ( 09034406598 )

2.2 RS232-C Bit Assignments (Marks and Spaces)

In RS232-C, a value of 1 is called a Mark and a value of 0 is called a Space. When a communication line is idle, the line is said to be “Marking”, or transmitting continuous 1 values.

The Start bit always has a value of 0 (a Space). The Stop Bit always has a value of 1 (a Mark). This means that there will always be a Mark (1) to Space (0) transition on the line at the start of every word, even when multiple word are transmitted back to back. This guarantees that sender and receiver can resynchronize their clocks regardless of the content of the data bits that are being transmitted.

The idle time between Stop and Start bits does not have to be an exact multiple (including zero) of the bit rate of the communication link, but most UARTs are designed this way for simplicity.

In RS232-C, the "Marking" signal (a 1) is represented by a voltage between -2 VDC and -12VDC, and a "Spacing" signal (a 0) is represented by a voltage between 0 and +12 VDC. The transmitter is supposed to send +12 VDC or -12 VDC, and the receiver is supposed to allow for some voltage loss in long cables. Some transmitters in low power devices (like portable computers) sometimes use only +5 VDC and -5 VDC, but these values are still acceptable to aRS232-C receiver, provided that the cable lengths are short.

2.3 RS232-C Break Signal

RS232-C also specifies a signal called a Break, which is caused by sending continuous Spacing values (no Start or Stop bits). When there is no electricity present on the data circuit, the line is considered to be sending Break.The Break signal must be of a duration longer than the time it takes to send a complete byte plus Start, Stop and Parity bits. Most UARTs can distinguish between a Framing Error and a Break, but if the UART cannot do this, the Framing Error detection can be used to identify Breaks.In the days of teleprinters, when numerous printers around the country were wired in series (such as news services), any unit could cause a Breakby temporarily opening the entire circuit so that no current flowed. This was used to allow a location with urgent news to interrupt some other location that was currently sending information.In modern systems there are two types of Break signals. If the Break is longer than 1.6 seconds, it is considered a "Modem Break", and some modems can be programmed to terminate the conversation and go on-hook or enter the modems' command mode when the modem detects this signal. If the Break is smaller than 1.6 seconds, it signifies a Data Break and it is up to the remote computer to respond to this signal. Sometimes this form of Break is used as an Attention or Interrupt signal and sometimes is accepted as a substitute for the ASCIICONTROL-C character.Marks and Spaces are also equivalent to “Holes” and “No Holes” in paper tape systems.

Note: Breaks cannot be generated from paper tape or from any other byte value, since bytes are always sent with Start and Stop bit. The UART is usually Spacing signal in response to a special command from the host processor.

2.4 RS232-C DTE and DCE Devices

The RS232-C specification defines two types of equipment: the Data Terminal Equipment (DTE) and the Data Carrier Equipment (DCE). Usually, the DTE device is the terminal (or computer), and the DCE is a modem. Across the phone line at the other end of a conversation, the receiving modem is also a DCE device and the computer that is connected to that modem is a DTE device. The DCE device receives signals on the pins that the DTE device transmits on, and vice versa. When two

10

Page 11: UART project report by Tarun Khaneja ( 09034406598 )

devices that are both DTE or both DCE must be connected together without a modem or a similar media translator between them, a NULL modem must be used. The NULL modem electrically re-arranges the cabling so that the transmitter output is connected to the receiver input on the other device, and vice versa. Similar translations are performed on all of the control signals so that each device will see what it thinks are DCE (or DTE) signals from the other device. The number of signals generated by the DTE and DCE devices is not symmetrical. The DTE device generates fewer signals for the DCE device than the DTE device receives from the DCE.

Table 2.1

2.5 RS232-C Pin Assignments

The EIA RS232-C specification (and the ITU equivalent, V.24) calls for a twenty-five pin connector (usually a DB25) and defines the purpose of most of the pins in that connector.

In the IBM Personal Computer and similar systems, a subset of RS232-C signals is provided via nine pin connectors (DB9). The signals that are not included on the PC connector deal mainly with synchronous operation, and this transmission mode is not supported by the UART that IBM selected for use in the IBM PC.

Depending on the computer manufacturer, a DB25, a DB9, or both types of connector may be used for RS232-C communications. (The IBM PC also uses a DB25 connector for the parallel printer interface which causes some confusion.)

11

Page 12: UART project report by Tarun Khaneja ( 09034406598 )

Chapter-3

8051 Microcontroller

The most commonly used microcontroller is 8051 families’ AT89C51 microcontroller which is produced by Atmel. It is widely used in most of the application for having an advantage of simple programming and low cost.

3.1 AT89C51AT89C51 is an 8-bit, 40 pin microcontroller that belongs to Atmel's 8051 family. ATMEL 89C51 has 4KB of Flash programmable and erasable read only memory (PEROM) and 128 bytes of RAM. It can be erased and program to a maximum of 1000 times.

In 40 pin AT89C51, there are four ports designated as P1, P2, P3 and P0. All these ports are 8-bit bi-directional ports, i.e., they can be used as both input and output ports. Except P0 which needs external pull-ups, rest of the ports have internal pull-ups. When 1s are written to these port pins, they are pulled high by the internal pull-ups and can be used as inputs. These ports are also bit addressable and so their bits can also be accessed individually.

3.1.1 Salient Features of AT89C51-

4K Bytes of In-System Reprogrammable Flash Memory Fully Static Operation: 0 Hz to 24 MHz Three-level Program Memory Lock 28 x 8-bit Internal RAM 32 Programmable I/O Lines Two 16-bit Timer/Counters Six Interrupt Sources Programmable Serial Channel Low-power Idle and Power-down Modes 40-pin DIP

3.1.2 Pin Diagram of AT89C51

12

Page 13: UART project report by Tarun Khaneja ( 09034406598 )

Figure 3.1

5.1.3 Pin Description of AT89C51

Pins 1-8 (Port 1) - Each of these pins can be configured as an input or an output.

Pin 9 (Reset) - A logic one on this pin disables the microcontroller and clears the contents of most registers. In other words, the positive voltage on this pin resets the microcontroller. By applying logic zero to this pin, the program starts execution from the beginning.

Pins 10- 17 (Port 3) -Similar to port 1, each of these pins can serve as general input or output. Besides, all of them have alternative functions:

Pin 10 (RXD) - Serial asynchronous communication input or Serial synchronous communication output.

Pin 11(TXD) - Serial asynchronous communication output or Serial synchronous communication clock output.

Pin 12 (INT 0) - Interrupt 0 input.

13

Page 14: UART project report by Tarun Khaneja ( 09034406598 )

Pin 13(INT 1) - Interrupt 1 input.

Pin 14(T0) - Counter 0 clock input.

Pin 15(T1) - Counter 1 clock input.

Pin 16(WR) -Write to external (additional) RAM.

Pin 17 (RD) -Read from external RAM.

Pin 18 and 19(X1, X2) - Internal oscillator input and output. A quartz crystal which specifies operating frequency is usually connected to these pins. Instead of it, miniature ceramics resonators can also be used for frequency stability. Later versions of microcontrollers operate at a frequency of 0 Hz up to over 50 Hz.

Pin 20 (GND) - Ground.

Pin 21-28 (Port 2) - If there is no intention to use external memory then these port pins are configured as general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-A15 will appear on this port. Even though memory with capacity of 64Kb is not used, which means that not all eight port bits are used for its addressing, the rest of them are not available as inputs/outputs.

Pin 29 (PSEN) - If external ROM is used for storing program then a logic zero (0) appears on it every time the microcontroller reads a byte from memory.

Pin 30 (ALE) - Prior to reading from external memory, the microcontroller puts the lower address byte (A0-A7) on P0 and activates the ALE output. After receiving signal from the ALE pin, the external register memorizes the state of P0 and uses it as a memory chip address. Immediately after that, the ALU pin is returned its previous logic state and P0 is now used as a Data Bus. As seen, port data multiplexing is performed by means of only one additional (and cheap) integrated circuit. In other words, this port is used for both data and address transmission.

Pin 31 (EA) - By applying logic zero to this pin, P2 and P3 are used for data and address transmission with no regard to whether there is internal memory or not. It means that even there is a program written to the microcontroller, it will not be executed. Instead, the program written to external ROM will be executed. By applying logic one to the EA pin, the microcontroller will use both memories, first internal then external (if exists).Pin 32-39 (Port 0) - Similar to P2, if external memory is not used, these pins can be used as general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven low (0).Pin 40 (Vcc) - +5V power supply.

Architecture of 8051 Microcontroller

6.1 Block Diagram of 8051 Microcontroller

14

Page 15: UART project report by Tarun Khaneja ( 09034406598 )

Figure 3.2

Address bus-For a device (memory or I/O) to be recognized by the CPU, it must beassigned an address. The address assigned to a given device must be unique. The CPU puts the address on the address bus, and the decoding circuitry finds the device.

Data bus-The CPU either gets data from the deviceor sends data to it.

Control bus-Provides read or write signals to the device to indicate if the CPU is asking for information or sending it information .

3.2 Memory and Registers

The 8051 microcontroller has a total of 256 bytes of RAM in which 128 is visible or useraccessible and extra 128 is for special function registers.

The user accessible RAM is used for temporary data storage. The user accessible RAM is from the address range 00 to 7Fh.

From the user accessible RAM, 32 bytes of RAM is used for registers and rest for Stack operations. The 32 Bytes of RAM is divided into four register Banks i.e. Bank0, Bank 1, Bank 2, Bank3. Each of these banks have 8 Registers i.e. R0 to R7 each.

RAM locations from 0 to 7 are set aside for bank 0 of R0 – R7 where R0 is RAM location 0, Rl is RAM location 1, and R2 is location 2, and so on, until memory location 7, which belongs to R7 of bank 0. The second bank of registers R0 – R7 starts at RAM location 08 and goes to location 0FH. The third bank of R0 – R7 starts at memory location 10H and goes to location 17H. Finally, RAM locations 18H to 1FH are set aside for the fourth bank of R0 – R7.

15

Page 16: UART project report by Tarun Khaneja ( 09034406598 )

Figure 3.3

Generally for normal operations, Register bank Bank0 is set by default. But we can switch to other banks by using PSW Commands.

Figure 3.4

3.2.1 SFRs (Special Function Register) - These Registers are in extra 128 bytes of the memory. This part of memory is not user accessible and these registers are used for special purposes. These registers range from 80h to FFh. There are a total of only 21 SFRs in this range and all other addresses from 80h to FFh are invalid and there use can cause errors and not valuable results.

Some of the SFRs are TCON, SBUF, ACC, B, SCON, TMOD SP, P0, PSW, TL0, and TL1. These all the registers have some specific function that has to be performed after they are programmed.

(i) Byte Addressable SFR with byte addressSP – Stack printer – 81HDPTR – Data pointer 2 bytesDPL – Low byte – 82HDPH – High byte – 83HTMOD – Timer mode control – 89HTH0 – Timer 0 Higher order bytes – 8CHTL0 – Timer 0 Low order bytes – 8AHTH1 – Timer 1 High bytes = 80HTL1 – Timer 1 Low order byte = 86HSBUF – Serial data buffer = 99HPCON – Power control – 87H.

16

Page 17: UART project report by Tarun Khaneja ( 09034406598 )

3.2.2 DPTR - Data Pointer in 8051 16 bit register; it is divided into two parts DPH and DPL. DPH for Higher order 8 bits, DPL for lower order 8 bits. DPTR, DPH, DPL these all are SFRs in 8051.

3.3 Flags and PSW (Program Status Word) Register in 8051

The program status word (PSW)register, also referred to as the flag register, is an 8 bit register.

Only 6 bits are used These four are CY (carry), AC (auxiliary carry), P(parity), and OV (overflow) They are called conditional flags, meaning that they indicate some conditions that resulted after an instruction was execute.

The PSW3 and PSW4 are designed as RS0 and RS1, and are used to change the bank. The two unused bits are user-definable.

PSW 7 PSW 6 PSW 5 PSW 4 PSW 3 PSW 2 PSW 1 PSW 0

Figure 3.5CY- PSW.7- Carry flag.AC- PSW.6- Auxiliary Carry flag.F0 (-----) - PSW.5- Available to the user for general purposeRS1 -PSW.4 - Register Bank selector bit 1.RS0- PSW.3 -Register Bank selector bit 0.OV -PSW.2 -Overflow flag.F0 (-----) - PSW.1- User definable bit.P- PSW.0 -Parity flag. Set/cleared by hardware each.

3.4 Instructions that affects Flags

Table 3.1

3.5 I/O PROGRAMMING

The four 8-bit I/O ports P0, P1, P2 and P3 each use 8 pins.

All the ports upon RESET are configured as input, ready to be used as input ports.

When the first 0 is written to a port, it becomes an output.

17

C A F0 RS1 RS0 OV --------- P

Instructions CY OV ACADD X X XADDC X X XSUBB X X XMUL 0 XDIV 0 XDA XRPC XPLC XSETB C 1CLR C 0CPL C XANL C, bit XANL C, /bit XORL C,/bit XORL C, bit XMOV C,bit XCJNE X

Page 18: UART project report by Tarun Khaneja ( 09034406598 )

To reconfigure it as an input, a 1 must be sent to the port.To use any of these ports as an input port, it must be programmed.

3.5.1 Port 0

It can be used for input or output; each pin must be connected externally to a 10K ohm pull-up resistor.

This is due to the fact that P0 is an open drain, unlike P1, P2, and P3. Open drain is a term used for MOS chips in the same way that open collector is used for TTL

chips.

Figure 3.6

In order to make port 0 an input, the port must be programmed by writing 1 to all the bits. Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data.

3.5.2 Port 1

Port 1 can be used as input or output. In contrast to port 0, this port does not need any pull-up resistors since it already has pull-up

resistors internally. Upon reset, port 1 is configured as an input port. To make port 1 an input port, it must be programmed as such by writing 1 to all its bits.

3.5.3 Port 2

Port 2 can be used as input or output. Just like P1, port 2 does not need any pull-up resistors since it already has pull-up resistors

internally. Upon reset, port 2 is configured as an input portP0.4. Port 2 is also designated as A8 – A15, indicating its dual function. Port 0 provides the lower 8

bits via A0 – A7.

3.5.3 Port 3

Port 3 can be used as input or output. Port 3 does not need any pull-up resistors.

18

Page 19: UART project report by Tarun Khaneja ( 09034406598 )

Port 3 is configured as an input port upon reset; this is not the way it is most commonly used. Port 3 has the additional function of providing some extremely important signals

Serial Communication

External Interrupts

Timers

Read/ Write Signals

Table 3.2

3.6 TIMERS AND COUNTERS

3.6.1 TimersThe 8051 comes equipped with two timers, both of which may be controlled, set, read, and configured individually. The 8051 timers have three general functions:

1) Keeping time and/or calculating the amount of time between events,

2) Counting the events themselves,

3) Generating baud rates for the serial port.

Both Timer 0 and Timer 1 are 16 bits wide.Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte.

One timer is TIMER0 and the other is TIMER1. The two timers share two SFRs (TMOD and TCON) which control the timers, and each timer also has two SFRs dedicated to itself (TH0/TL0 and TH1/TL1).The upper higher bits are TH0 and TH1 and the lower bits are TL0 AND TL1The TMOD and TCON are two control registers for the two timers.

19

P3 Function

Pin

P3.0 Rxd 10

P3.1 Txd 11

P3.2 Int0

12

P3.3Int1

13

P3.4 T0 14

P3.5 T1 15

P3.6Wr

16

P3.7Rd

17

Page 20: UART project report by Tarun Khaneja ( 09034406598 )

Figure 3.7

(i) TMOD RegisterIt is used to set the various timer operation mode.– TMOD is an 8-bit register where the lower 4 bits are set aside for timer 0 and the upper 4 bits are set aside for timer 1.

MSB LSB

Gate C/T M0 M1 GATE C/T M0 MI

Timer 1 Timer 0

Figure 3.8

GATE: To start and stop the timer GATE=1 _HW control: is enabled only while INTx pin is ‘1’and TRx control pin (in TCON)

is set. GATE=0 _SW control (used frequently) C/T: Timer or counter selection C/T = 0 _Timer (input from internal system clock) the crystal (1/12) is used to trigger the

timer. C/T = 1 _Counter (input from Tx input pin)

M1 and M0: Mode selection for timer and counterMode M1 M00 0 0 13-bit timer/counter mode1 0 1 16-bit timer/counter mode2 1 0 8-bit auto reload timer/counter mode3 1 1 split timer/counter mode

(ii) TCON Register

MSBLSB

TF1 TF0 TR1 TR0 IE1 IE0 IT1 IT0

TIMER 1 TIMER 0 TIMER1 TIMER0

20

Page 21: UART project report by Tarun Khaneja ( 09034406598 )

Figure 3.9

TF1: Timer 1 overflows flag TF1=1: Timer/counter 1 overflows. TF1=0: processor vectors to the interrupt services.

TR1: Timer 1 run control bit TR1=1: turn Timer 1 ON TR1=0: turn Timer 1 OFF

IE1: External interrupt 1 edge flag IE1=1: external interrupt is detected. IE1=0: when interrupt is processed.

IT1: Interrupt 1 type control bit IT1=1: falling edge. IT1=0: low level triggered external interrupt.

Gate=0, SETB TR1 _Run Timer 1SETB TR0 _Run Timer 0Gate=0, CLR TR1 _OFF Timer 1CLR TR0 _OFF Timer 0

Timer Mode 0 Mode 0: 13-bit Timer/counter mode 0000 ~ 1FFFH

Timer Mode 2Mode 2: 8-bit auto reload Timer/counter mode (00 ~ FFH). In auto reload, TH is loaded with the initial count and a copy of it is given to TL. This reloading leaves TH unchanged still holding a copy of original values. This mode has many applications, including setting the baud rate in serial communication.

Mode 2 Programming 8 bit - 00 ~FFH TH copy to TL Start SETB TR0, or TR1 TL increased FFH (OV monitoring) TH reloads to TL.

3.6.2 Counters

Counter is used to count input pulses.C/T=0: As Time, using 8051’s crystal as the source ofthe frequency.C/T=1: As counter, a pulse outside of the 8051 that increments the TH and TL register.When the C/T=1, the counter counts up as pulses are fed from Pins P3.4 (for counter 0) or P3.5 (for counter 1).

3.7Serial Port Communication in 8051 microcontroller

The 8051 microcontroller transmits data serially as well as parallel communication is also done. For serial communication, the microcontroller comes with serial communication pin TXD and RXD.

21

Page 22: UART project report by Tarun Khaneja ( 09034406598 )

Normally TXD is used for transmitting serial data which is in SBUF register, RXD is used for receiving the serial data. SCON register is used for controlling the operation.The two registers used for controlling the communication are SCON and SBUF.

Serial Communication Parallel Communication

Figure 3.10

Serial data communication uses two methods

Synchronous method transfers a block of data at a time Asynchronous method transfers a single byte at a time

It is possible to write software to use either of these methods, but the programs can be tedious and longThere are special IC chips made by many manufacturers for serial communications

UART (universal asynchronous Receiver transmitter) USART (universal synchronous-asynchronous Receiver-transmitter)

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.

3.7.1 SCON

Figure 3.11

RI(Receive Interrupt Flag)-Set by hardware on receiving. Must be cleared by software

TI (Transmit Interrupt Flag) -Set by hardware on transmitted, must clear by hardware

22

Page 23: UART project report by Tarun Khaneja ( 09034406598 )

RB8 (Receive bit 8) Mode 2,3 : copy of bit 8 Mode 1 & SM2 clear : copy of stop bit

TB8 Transmit bit 8- The 9th data bit of mode 2, 3. Set or clear by software

REN Receive Enable- Set by software to enable reception, if is cleared reception will be blocked.

SM2 Serial Mode (bit 2) -Use in mode 2, 3 for multiprocessor communications.

SM1 & SM0 Serial Mode (bit 6 & 7)

Operating modes Mode 0- 8-bit shift register, f/12

1Mbit with 12 MHz Oscillator Frequency Mode 1- 8-bit UART, variable baud rate Mode 2 -9-bit UART, f/64 or f/32

187.5K and 375K with 12MHz Oscillator Frequency Mode 3- 9-bit UART, variable baud rate.

3.7.2 SBUFThese are two separate data buffers for transmit and receive.The register SBUF is used to hold both the transmitter and receiver serial port data. To transmit the data, load SBUF register with data.

MOV SBUF, source When transmission is complete the TI bit will be set in the SCONregister.

When a data frame is received the RI bit in SCON is set high.The received data may then be loaded from SBUF

MOV destination, SBUF Data reception is double buffered.

3.7.3 SMOD Addition bit to double baud speed.

3.8 INTERUPTS

An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service.

The advantage of interrupts is that the microcontroller can serve many devices. Each device can get the attention of the microcontroller based on the assigned priority. The microcontroller can also ignore (mask) a device request for service.

3.8.1 Hardware and Software interruptThe interrupts in a controller can be either hardware or software. If the interrupts are generated by the controller’s inbuilt devices, like timer interrupts; or by the interfaced devices, they are called the hardware interrupts. If the interrupts are generated by a piece of code, they are termed as software interrupts.

The 8051 controller has six hardware interrupts of which five are available to the programmer.

23

Page 24: UART project report by Tarun Khaneja ( 09034406598 )

1. RESET Interrupt - This is also known as Power on Reset (POR). When the RESET interrupt is received, the controller restarts executing code from 0000H location. This is an interrupt which is not available to or, better to say, need not be available to the programmer.

 2. Timer interrupts - Each Timer is associated with a Timer interrupt. A timer interrupt notifies the microcontroller that the corresponding Timer has finished counting. Therefore these are two interrupts for the timers.

 3. External interrupts - There are two external interrupts EX0 and EX1 to serve external devices. Both these interrupts are active low. In AT89C51, P3.2 (INT0) and P3.3 (INT1) pins are available for external interrupts 0 and 1 respectively. An external interrupt notifies the microcontroller that an external device needs its service.

 4. Serial interrupt - This interrupt is used for serial communication. When enabled, it notifies the controller whether a byte has been received or transmitted.

Figure 3.12

The interrupts must be enabled by software in order for the microcontroller to respond to them.

Interrupt Enable Register- There is a register called IE (interrupt enable) that is responsible for enabling(unmasking) and disabling (masking) theinterrupts.

EA -------- ET2 ES ET1 EX1 ET0 EX0

Figure 3.13

To enable any of the interrupts, first the EA bit must be set to 1. After that the bits corresponding to the desired interrupts are enabled.

ET0, ET1 and ET2 bits are used to enable the Timer Interrupts 0, 1 and 2, respectively. In AT89C51, there are only two timers, so ET2 is not used.

EX0 and EX1 are used to enable the external interrupts 0 and 1. ES is used for serial interrupt.

24

Page 25: UART project report by Tarun Khaneja ( 09034406598 )

EA bit acts as a lock bit. If any of the interrupt bits are enabled but EA bit is not set, the interrupt will not function. By default all the interrupts are in disabled mode.

Chapter-4

INTERFACING DEVICES WITH 8051 MICROCONTROLLER

4.1 LED INTERFACINGInterfacing an LED with 8051 is easy. The I/O pins are used as output pins. When any of the bit is set to 1, the LED glows if LED n side is connected to ground and p side with bit. And if p side is connected to power and n side to bit, then on bit low, the LED glows.

25

Page 26: UART project report by Tarun Khaneja ( 09034406598 )

Figure 4.1

4.2 SEVEN SEGMENT INTERFACING

A seven segment consists of eight LEDs which are aligned in a manner so as to display digits from 0 to 9 when proper combination of LED is switched on. Seven segment uses seven LED’s to display digits from 0 to 9 and the eighth LED is used for the dot. A typical seven segment looks like as shown in the figure below. 

26

Page 27: UART project report by Tarun Khaneja ( 09034406598 )

Figure 4.2

Figure 4.3

27

Page 28: UART project report by Tarun Khaneja ( 09034406598 )

4.3 LCD INTERFACING

A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers.

 1. Command/Instruction Register - stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing, clearing the screen, setting the cursor position, controlling display etc.

 2.Data Register - stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

Code-

Figure 4.4

28

Page 29: UART project report by Tarun Khaneja ( 09034406598 )

Figure 4.5

Figure 4.6

29

Page 30: UART project report by Tarun Khaneja ( 09034406598 )

4.4 INTERFACING MOTOR WITH 8051

Interfacing DC motor to 8051 forms an essential part in designing embedded robotic projects. A well designed 8051-DC motor system has essentially two parts. Firstly an 8051 with the required software to control the motor and secondly a suitable driver circuit.

L293D-L293 is a dedicated quadruple Half H Bridge motor driver IC available in 16 pin package. The L293 is designed to provide bidirectional drive currents of up to 1 A at voltagesFrom 4.5 V to 36 V.

 

Figure 4.7

30

Page 31: UART project report by Tarun Khaneja ( 09034406598 )

Chapter-5

Components required

5.1 MAX232

The MAX232 is an IC, first created in 1987 by Maxim Integrated Products, that converts signals from an RS-232 serial port to signals suitable for use in TTL compatible digital logic circuits. The MAX232 is a dual driver/receiver and typically converts the RX, TX, CTS and RTS signals.

The drivers provide RS-232 voltage level outputs (approx. ± 7.5 V) from a single + 5 V supply via on-chip charge pumps and external capacitors. This makes it useful for implementing RS-232 in devices that otherwise do not need any voltages outside the 0 V to + 5 V range, as power supply design does not need to be made more complicated just for driving the RS-232 in this case.

The receivers reduce RS-232 inputs (which may be as high as ± 25 V), to standard 5 V TTL levels. These receivers have a typical threshold of 1.3 V, and a typical hysteresis of 0.5 V.

Figure 5.1

It is helpful to understand what occurs to the voltage levels. When a MAX232 IC receives a TTL level to convert, it changes a TTL logic 0 to between +3 and +15 V, and changes TTL logic 1 to between -3 to -15 V, and vice versa for converting from RS232 to TTL. This can be confusing when you realize that the RS232 data transmission voltages at a certain logic state are opposite from the RS232 control line voltages at the same logic state.

31

Page 32: UART project report by Tarun Khaneja ( 09034406598 )

The MAX232(A) has two receivers (converts from RS-232 to TTL voltage levels), and two drivers (converts from TTL logic to RS-232 voltage levels). This means only two of the RS-232 signals can be converted in each direction. Typically, a pair of a driver/receiver of the MAX232 is used for TX and RX signals, and the second one for CTS and RTS signals.

There are not enough drivers/receivers in the MAX232 to also connect the DTR, DSR, and DCD signals. Usually these signals can be omitted when e.g. communicating with a PC's serial interface. If the DTE really requires these signals, either a second MAX232 is needed, or some other IC from the MAX232 family can be used. Also, it is possible to directly wire DTR (DB9 pin #4) to DSR (DB9 pin #6) without going through any circuitry. This gives automatic (brain dead) DSR acknowledgment of an incoming DTR signal.

5.2 7805

The 78xx (sometimes L78xx, LM78xx, MC78xx...) is a family of self-contained fixed linear voltage regulator integrated circuits. The 78xx family is commonly used in electronic circuits requiring a regulated power supply due to their ease-of-use and low cost. For ICs within the family, the xx is replaced with two digits, indicating the output voltage (for example, the 7805 has a 5 volt output, while the 7812 produces 12 volts). The 78xx line are positive voltage regulators: they produce a voltage that is positive relative to a common ground.

Figure 5.2

78xx ICs have three terminals and are commonly found in the TO220 form factor, although smaller surface-mount and larger TO3 packages are available. These devices support an input voltage anywhere from a couple of volts over the intended output voltage, up to a maximum of 35 to 40 volts depending on the make, and typically provide 1 or 1.5 amperes of current

32

Page 33: UART project report by Tarun Khaneja ( 09034406598 )

Advantages

78xx series ICs do not require additional components to provide a constant, regulated source of power, making them easy to use, as well as economical and efficient uses of space. Other voltage regulators may require additional components to set the output voltage level, or to assist in the regulation process. Some other designs (such as a switched-mode power supply) may need substantial engineering expertise to implement.

78xx series ICs have built-in protection against a circuit drawing too much power. They have protection against overheating and short-circuits, making them quite robust in most applications. In some cases, the current-limiting features of the 78xx devices can provide protection not only for the 78xx itself, but also for other parts of the circuit.

5.3 ULN2003

The ULN2002A, ULN2003A, ULN2003AI, ULN2004A, ULQ2003A, and ULQ2004A are high-voltage high-current. Darlington transistor arrays. Each consists of seven npn Darlington pairs that feature high-voltage outputs with common-cathode clamp diodes for switching inductive loads. The collector-current rating of a single Darlington pair is 500 mA. The Darlington pairs can be paralleled for higher current capability. Applications include relay drivers, hammer drivers, lamp drivers, display drivers (LED and gas discharge), line drivers, and logic buffers.

For 100-V (otherwise interchangeable) versions of the ULN2003A and ULN2004A, see the SN75468 andSN75469, respectively. The ULN2002A is designed specifically for use with 14-V to 25-V PMOS devices. Each input of this device has a Zener diode and resistor in series to control the input current to a safe limit. The ULN2003A and ULQ2003A have a 2.7-kΩ series base resistor for each Darlington pair for operation directly with TTL or 5-V CMOS devices.

Figure 5.3

33

Page 34: UART project report by Tarun Khaneja ( 09034406598 )

5.4 Preset

A preset is a three legged electronic component which can be made to offer varying resistance in a circuit. The resistance is varied by adjusting the rotary control over it. The adjustment can be done by using a small screw driver or a similar tool. The resistance does not vary linearly but rather varies in exponential or logarithmic manner. Such variable resistors are commonly used for adjusting sensitivity along with a sensor.The variable resistance is obtained across the single terminal at front and one of the two other terminals. The two legs at back offer fixed resistance which is divided by the front leg. So whenever only the back terminals are used, a preset acts as a fixed resistor. Presets are specified by their fixed value resistance.

Figure 5.4

5.5 LCD (Liquid Crystal Display)

LCD-LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16x2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over seven segments and other multi segment LEDs. The reasons being: LCDs are economical; easily programmable; have no limitation of displaying special & even custom characters (unlike in seven segments), animations and so on.A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers, namely, Command and Data.The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc. The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

Figure 5.5

34