Serial Communication - Microprocessor Interfacing - Lab Manual, Exercises of Microprocessor and Interfacing

Key points in this lecture are: Serial, Communication, Interface, Objectives, Components, Equipment, Data, Connection.

Typology: Exercises

2011/2012

Uploaded on 10/24/2012

alia_maru
alia_maru 🇮🇳

4.5

(41)

57 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Experiment3
SerialCommunicationusingRS232Interface
1. OBJECTIVES:
1.1 Tounderstandserialcommunicationprotocol
1.2 Tounderstandtheprocessofdataframing
1.3 TointerfacethePIC18withanRS232connector
1.4 TounderstandthebaudrateofthePIC18
1.5 ToprogramthePIC18serialportinClanguage
2. COMPONENTSANDEQUIPMENTS:
2.1 SK40B
2.2 Breadboard
2.3 LED
2.4 Resistor
2.5 Switch
2.6 SerialCable
3. INTRODUCTION:
3.1 SerialCommunication
Computerstransferdataintwoways:parallelandserial.Inparalleldatatransfer,ofteneightor
morelinesareusedtotransferdatatoadevicethatisonlyafewfeetaway.Devicesthatuse
paralleltransferincludeprintersandharddisks;eachusecablewithmanywirestrips.Although
alotofdatacanbetransferredinashortamountoftimebyusingmanywiresinparallel,the
distancecannotbegreat.Totransfertoadevicelocatedmanymetersaway,theserialmethod
isused.Inserialcommunication,thedataissentonebitatatime,incontrasttoparallel
communication,inwhichthedataissentabyteormoreatatime.Serialcommunicationofthe
PIC18isthetopicofthisexperiment.ThePIC18hasserialcommunicationcapabilitybuiltininto
it,therebymakingpossiblefastdatatransferusingonlyafewwires.Figure3.1showstheserial
versusparalleldatatransfers.
Docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Serial Communication - Microprocessor Interfacing - Lab Manual and more Exercises Microprocessor and Interfacing in PDF only on Docsity!

Experiment 3

Serial Communication using RS232 Interface

1. OBJECTIVES:

1.1 To understand serial communication protocol 1.2 To understand the process of data framing 1.3 To interface the PIC18 with an RS232 connector 1.4 To understand the baud rate of the PIC 1.5 To program the PIC18 serial port in C language

2. COMPONENTS AND EQUIPMENTS:

2.1 SK40B

2.2 Breadboard 2.3 LED 2.4 Resistor 2.5 Switch 2.6 Serial Cable

3. INTRODUCTION:

3.1 Serial Communication

Computers transfer data in two ways: parallel and serial. In parallel data transfer, often eight or more lines are used to transfer data to a device that is only a few feet away. Devices that use parallel transfer include printers and hard disks; each use cable with many wire strips. Although a lot of data can be transferred in a short amount of time by using many wires in parallel, the distance cannot be great. To transfer to a device located many meters away, the serial method is used. In serial communication, the data is sent one bit at a time, in contrast to parallel communication, in which the data is sent a byte or more at a time. Serial communication of the PIC18 is the topic of this experiment. The PIC18 has serial communication capability built in into it, thereby making possible fast data transfer using only a few wires. Figure 3.1 shows the serial versus parallel data transfers.

Figure 3.1: Serial versus Parallel Data Transfer

The fact that in a single data line is used in serial communication instead of the 8 ‐bit data line of parallel communication makes serial transfer not only much cheaper but also enables two computers located in two different cities to communicate over the telephone. For serial data communication to work the byte of data must be converted to serial bits using a parallel‐in‐ serial‐out shift register; then it can be transmitted over a single data line. This also means that at the receiving end there must be a serial‐in‐parallel‐out shift register to receive the serial data and pack them into a byte.

3.2 Asynchronous Serial Communication and Data Framing

The PIC18 chip has a built‐in USART (Universal synchronous‐asynchronous receiver‐transmitter), which is has both the synchronous and asynchronous features. Asynchronous serial data communication is widely used for character‐oriented transmissions, while block‐oriented data transfers use the synchronous method. In the asynchronous method, each character is placed between start and stop bits. This is called framing. In data framing for asynchronous communications, the data, such as ASCII characters, are packed between a start bit and stop bit. The start bit is always one bit but the stop bit can be one or two bits. The start is always a 0 (low) and the stop bit(s) is 1 (high). For example, Figure 3.2 shows the framing ASCII ‘A’ (41H or 0100 0001b) between the start bit and a single stop bit. Notice that the LSB is sent out first.

Figure 3.2: Framing ASCII ‘A’ (41H)

Table 3.1: X value for the different baud rates

Baud Rate SPBRG (Decimal Value) SPBRG (Hex Value)

115200

38400

19200

9600

4800

2400

1200

4. PROCEDURE:

4.1 Programming the PIC18 to Transfer Data Serially:

4.1.1 Create your HyperTerminal communication (see Appendix 1) 4.1.2 The TXSTA register is loaded with the value 20H, indicating asynchronous mode with 8 ‐bit data frame, low baud rate and transmit enabled (see Appendix 2) 4.1.3 Make Tx pin of PORTC (RC6) an output for data to come out of the PIC 4.1.4 The SPBRG is loaded with one of the values in Table 3.1 to set the baud rate for serial data transfer 4.1.5 SPEN bit in the RCSTA register is set HIGH to enable the serial port of the PIC 4.1.6 The character byte to be transmitted serially is written into the TXREG register 4.1.7 Monitor the TXIF bit of the PIR1 register to make sure UART is ready for next byte 4.1.8 To transfer the next character, go to step 4.1.

4.2 Programming the PIC18 to Receive Data Serially:

4.2.1 Create your HyperTerminal communication (see Appendix 1) 4.2.2 The RCSTA register is loaded with the value 90H, to enable the continuous receive in addition to the 8 ‐bit data size option (see Appendix 3) 4.2.3 The TXSTA register is loaded with the value 00H to choose the low baud rate option 4.2.4 SPBRG is loaded with one of the value in Table 3.1 to set the baud rate 4.2.5 Make the Rx pin of PORTC (RC7) an input for data to come into the PIC 4.2.6 The RCIF flag bit of the PIR1 register is monitored for a HIGH to see if an entire character has been received yet

4.2.7 The RCIF is raised; the RCREG register has the byte. Its contents are moved into a safe place 4.2.8 To received the next character, go step 4.2.

5. DESIGN/PROBLEM:

By referring to Appendix 4 and 5 or from the book page 414 ‐415:

5.1 Write a program to transfer your name serially at 9600 baud rate continuously by using HyperTerminal. 5.2 Write a program to waiting the character ‘Z’ using HyperTerminal at microcontroller, after receiving that character transmit the character ‘G’ to the HyperTerminal, meanwhile if not, transmit the character ‘F’. Do it continuously. 5.3 Write a program to turn on the LED at PORTB.4 and turn off the LED at PORTB.3 when the character ‘S’ is received by the microcontroller. Meanwhile, when other character or no character received, the LED at PORTB.4 will turn off and LED at PORTB.3 will turn on. Do it continuously. 5.4 Construct your circuit by assuming the PORTB will be the output (LED). Send a data packet of 3 bytes through HyperTerminal to perform the following task continuously.

LFT = The LEDs will moving to the left RGT = The LEDs will moving to the right BLK = The LEDs will blinking in 2 seconds CNT = The LEDs will show the counting from 0 to 255

6. Appendices:

Appendix 1

Here are the steps to set up HyperTerminal:

  1. In Microsoft Windows Accessories, click on HyperTerminal (if you get a modem installation option, choose "NO").
  2. Type a dummy name, and click OK.
  3. For "Connect Using" select COM1 and click OK. Use COM2 if COM1 is used by the mouse.
  4. Pick 9600 (or whatever baud rate you programmed the PIC Trainer), 8 bit data, no parity bit, and 1 stop bit.
  5. Change the "Flow Control" to NONE or Xon/Xoff and click OK (definitely do not choose the hardware option).

Appendix 3

Receive Status and Control Register:

Appendix 4

Sample Program for Transmit Data Serially:

Appendix 5

Sample Program for Receive Data Serially: