Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to Communicate with PC using RS232 Protocol- (Part 9/9)

By Dishant Shah February 26, 2014

In embedded systems, it is often required to communicate with other peripherals like GSM, GPS and even PC. Various protocols like I2C, SPI, BLUETOOTH, WI-FI etc. are used for this purpose. Serial communication using RS232 protocol is the most common and widely used protocol in embedded systems. In this section, we will explain basics of serial communication and how to communicate with PC using RS232 protocol. LPC2148 controller from Philips of ARM7 family is used in this section.

[[wysiwyg_imageupload:11869:]]

Fig. 1: Communicate with PC using RS232 Protocol Prototype

 

 


 

There are two types of communication possible between two devices depending upon the number of wires required.

1. Parallel communication: In this, more than one wire is connected between two communicating node. Each bit is sent separately on dedicated wire. In this approach, numbers of wires required=width of data bus. This is preferred for short way communication. Over a long distance, this increases cost of wires and maintenance.

2. Serial Communication: In this, data is combined into a packet and sent bit by bit on a single wire between two communicating devices. This requires less costly implementation and maintenance. However, synchronization between communicating devices is necessary. Sometimes separate wires are required for two-way communication. This approach is widely used for long distance, high speed and reliable communication.

Serial communication using UART (Universal Asynchronous Receiver/Transmitter) is widely used in embedded systems. Almost all controller has inbuilt UART available for easy communication. IBM PC also comes with serial COM PORTS so that we can communicate with them using controllers. Using USB to serial bridge, we can create virtual COM PORTS and communicate with controllers using the same.

Baud rate is the measurement of speed of data transfer for serial communications. Various baud rate supported by UART for given maximum distance is as follows.

Various baud rate supported by UART

Fig. 2: Various baud rate supported by UART

Controllers are connected with device using DB9 connector. As the name suggests, it has 9 pins as shown in image. Function of pins required for serial communication are explained below.

1.CD/DCD (CARRIER DETECT/ DATA CARRIER DETECT)

2.RxD (Receiver Pin)

3.TxD (Transmission Pin)

4.DTR (Data Terminal Ready)

5.GND (Signal Ground)

6.DSR (Data Set Ready)

7.RTS (Request To Send)

8.CTS (Clear To Send)

9.RI (Ring Indicator)

Pin configuration of  RS232

Fig. 3: Pin configuration of RS232

Out of these 9 pins, only 3 (RxD, TxD and GND) pins are necessary for serial communication between PC and controller. Connection is as shown.

Image showing pin numbers for serial communication between PC and controller

Fig. 4: Image Showing Pin Numbers for Serial Communication Between PC and Controller

Microcontroller works on TTL or CMOS voltage logic levels. But PC operates on RS232 voltage logic level which is different from CMOS or TTL.

TTL or CMOS Voltage logic levels for Microcontrollers

Fig. 5: TTL or CMOS Voltage Logic Levels for Microcontrollers

So we need a voltage converter which can convert one logic level to another. MAX232 IC with four capacitors does this task. MAX233 IC performs the same task without external capacitors connected. However, MAX232 is widely used because of low cost and availability. Connections with MAX232 IC are as shown below.

Circuit Diagram of RS232 interfacing PC

Fig. 6: Circuit Diagram of RS232 interfacing PC

Now let us start with programming.

LPC 2148 has two UARTs named as UART0 and UART1. UART0 is used for programming LPC 2148.

Pin numbers for programming LPC2148  using UART

Fig. 7: Pin Numbers for Programming LPC2148 using UART

Important registers for UART0 with functions are listed here.

1. U0RBR- Receiver Buffer- Received data from UART0 is first stored in this register.

2. U0THR-Transmit Hold Register- Data to be transmitted on UART0 must be given to this register.

3. U0LCR- Line Control Register- It has bits to enable DLL (BIT 7), Set break (BIT 6), Stick Parity select (BIT 5), Even Parity select (BIT 4), Parity Enable (BIT 3), Number of stop bits (BIT 2) and Word length (BIT 1 and BIT 0)

4. U0LSR- Line Status Register- It reflects the status of UART0. 8 bits of this register from MSB to LSB are RX FIFO Error, TEMT, THRE,BI,FE,PE,OE and DR.

Baud rate for UART can be calculated by formula,

Formula to calculate Baud rate for UART in LPC2148

Fig. 8: Formula to Calculate Baud Rate for UART in LPC2148

Where, PCLK= Peripheral Clock Frequency

    U0DLM, U0DLL are standard UART0 baud rate generator divider registers

    MULVAL and DIVADDVAL are fraction generator values. They must meet following conditions.

    0<MULVAL, DIVADDVAL<=15 with MULVAL=0 treated as MULVAL=1.

Let us understand all registers individually.

1.UORBR

There is a stack allocated for the received data storage. The U0RBR (UART0 RECEIVER BUFFER) is the top byte of the UART0 Rx FIFO. The top byte of the Rx FIFO contains the oldest character received via serial port which can be read from the bus. The LSB (bit 0) represents the “oldest” received data bit. If the character received is less than 8 bits, the unused MSBs are padded with zeroes.

The Divisor Latch Access Bit (DLAB) in U0LCR must be cleared in order to access the U0RBR. The U0RBR is always Read Only register used to store the received data.

2.UOTHR

Data to be sent over serial com has to be first kept in register UOTHR (UART0 transmit holding register). The U0THR is the top byte of the UART0 TX FIFO. The top byte is the newest character in the TX FIFO and can be written via the bus interface. The LSB represents the first bit to transmit.

The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to access the U0THR. The U0THR is always Write only register to temporary store data to be transmitted over serial com.

3.UOLCR

UOLCR (UART0 LINE CONTROL REGISTER) determines the format of the data to be transmitted over UART0. It is an 8 bit register. Function of each pin is as follows.

·         Bit 0:1 for data length

o   00: 5 bits

o   01: 6 bits

o   10: 7 bits

o   11: 8 bits

·         Bit 2 for stop bit length

o   0: 1 stop bit

o   1: 2 stop bits

·         Bit 3 for parity configurations

o   0: Parity generation and checking is disabled

o   1: Parity generation and checking is enabled

·         Bit 5:4 for parity select (required only if bit 3 is set to logic 1)

 

o   00: Odd parity. Number of 1s in the transmitted character and theattached parity bit will be odd

 

o   01: Even Parity. Number of 1s in the transmitted character and theattached parity bit will be even

 

o   10: Forced “1” stick parity

o   11: Forced “0” stick parity

·         Bit 6 for Break control

o   0: Disable breaks transmission

o   1: Enable breaks transmission. Output pin UART0 TXD is forcedto logic 0 when U0LCR [6] is active high

·         Bit 7 for DLAB (Divisor Latch Access Bit)

o   0: Disable access to Divisor Latch

o   1: Enableaccess to Divisor Latch

  1. UOLSR

The U0LSR (UART0 LINE STATUS REGISTER) is a read-only register that provides status information on the UART0 TX andRX blocks.It is an 8 bit register. Function of each pin is as follows.

·         Bit 0- Receiver Data Ready (RDR)

o   U0LSR0 is set when the U0RBR holds an unread character and is clearedwhen the UART0 RBR FIFO is empty.

o   0:U0RBR is empty.

o   1: U0RBR contains valid data

·         Bit 1- Overrun error (OE)

o   The overrun error condition is set as soon as it occurs. An U0LSR read clearsU0LSR1. U0LSR1 is set when UART0 RSR has a new character assembledand the UART0 RBR FIFO is full. In this case, the UART0 RBR FIFO will notbe overwritten and the character in the UART0 RSR will be lost.

o   0: Overrun error status is inactive

o   1: Overrun error status is active

·         Bit 2- Parity Error (PE)

o   When the parity bit of a received character is in the wrong state, a parity erroroccurs. An U0LSR read clears U0LSR[2]. Time of parity error detection isdependent on U0FCR[0].

o   0: Parity error status is inactive

o   1: Parity error status is active

·         Bit 3- Framing Error (FE)

o   When the stop bit of a received character is at logic 0, a framing error occurs.An U0LSR read clears U0LSR[3]. The time of the framing error detection isdependent on U0FCR0. Upon detection of a framing error, the Rx will attemptto resynchronize to the data and assume that the bad stop bit is actually anearly start bit. However, it cannot be assumed that the next received byte willbe correct even if there is no Framing Error.

o   0: Framing error status is inactive.

o   1: Framing error status is active

·         Bit 4- Break Interrupt (BI)

o   When RXD0 is held in the spacing state (all 0’s) for one full charactertransmission (start, data, parity, stop), a break interrupt occurs. Once thebreak condition has been detected, the receiver goes idle until RXD0 goes tomarking state (all 1’s). An U0LSR read clears this status bit. The time of breakdetection is dependent on U0FCR[0].

o   0: Break interrupt status is inactive.

o   1: Break interrupt status is active.

·         Bit 5- TransmitterHoldingRegister Empty (THRE)

o   THRE is set immediately upon detection of an empty UART0 THR and iscleared on a U0THR write.

o   0: U0THR contains valid data

o   1: U0THR is empty

·         Bit 6- TransmitterEmpty(TEMT)

o   TEMT is set when both U0THR and U0TSR are empty; TEMT is cleared wheneither the U0TSR or the U0THR contain valid data.

o   0: U0THR and/or the U0TSR contain valid data.

o   1: U0THR and the U0TSR are empty.

·         Bit 7- Error in RXFIFO(RXFE)

o   U0LSR[7] is set when a character with a Rx error such as framing error, parityerror or break interrupt, is loaded into the U0RBR. This bit is cleared when theU0LSR register is read and there are no subsequent errors in the UART0FIFO.

o   0: U0RBR contains no UART0 RX errors or U0FCR[0] =0

o   1: UART0 RBR contains at least one UART0 RX error

 

MP NOTE: It is recommended to read user manual of LPC 2148 or whatever controller you are using for perfect understanding of all registers.

Project Source Code

###



#include <LPC21xx.H>                     /* LPC21xx definitions*/  
#include "serial.h"                    
int main(void)
{
int j=0;
init_serial (); // initialise serial port with 9600bps
SendChar ('A'); // transmit char A
SendChar ('n'); // transmit new line character
SendString("Dishant"); // transmit string Dishant
SendChar ('n'); // transmit new line character
SendDigit(236); // transmit digit 236
}

###

 


Project Source Code

###



#include <LPC21xx.H>                /*    LPC21xx definitions        */
void init_serial (void)
  {                           /*Initialize Serial Interface  */
  PINSEL0 = PINSEL0 | 0X00000005;     /* Enable RxD0 and TxD0              */
  U0LCR = 0X83;                       /*8 bits, no Parity, 1 Stop bit      */
  U0DLL = 0XC3; 
  U0DLM = 0X00;                         /* 9600bps baud rate */
  U0LCR = 0X03;                     /* DLAB = 0                      */
  }
int GetChar (void)  
{                                 /* Read character from Serial Port   */
  while (!(U0LSR & 0x01));
  return (U0RBR);
}
char SendChar (char SDat)
  {                                /* Write character to Serial Port    */
while (!(U0LSR & 0x20));
return (U0THR = SDat);
}
void SendString(char *lcd_string)
{
while (*lcd_string) 
{
SendChar(*lcd_string++);
}
}
void SendDigit(unsigned int dat)
{
unsigned int temp,i=0, buf[10];
while(dat>=10)
{
buf[i]=dat%10;
dat=dat/10;
i++;
} 
buf[i]=dat;
for(temp=0;temp<=i;temp++)
{
SendChar(buf[i-temp]+'0');
}
}

###

 


Circuit Diagrams

How-to-Communicate-with-PC-using-RS232-Protocol

Project Video

)


Filed Under: ARM

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Snooping Around is All
  • mosfet driver problem in regeneration mode
  • Industrial Relay Board Design for Motorcycle Use
  • connector model question
  • ADEM III ECM — No CAN Signal & Power Supply Issue

RSS Electro-Tech-Online.com Discussions

  • Sump pit water alarm - Kicad 9
  • Pic18f25q10 osccon1 settings swordfish basic
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe