UART Serial communication with MSP430 microcontroller




To use UART communication module of MSP430 microcontroller, first you should understand what is UART communication? What is Serial communication? MSP430G2 launch Pad has on board MSP430G2553 microcontroller. MSP430G2553 microcontroller has one built in circuit for UART. So MSP430G2 launchpad has only on UART communication module which can be used to send and receive data serially.  This board also supports I2C and SPI communication protocols. SPI and I2C are also serial communication protocols.  UART communication is not a communication protocol but a physical dedicated circuit in the form of integrated circuit or embedded inside a microcontroller. On the other hand, both SPI and I2C are communication protocols. you may like to check getting started tutorials on MSP430:




What is UART communication ?

  • UART stand for Universal Asynchronous Receiver and Transmitter. UART can transfer data bit by bit serially.
  • There are other parallel communication protocols exists which are used to transfer data in parallel mode like 8 bit at time.   It can transfer only one bit at a time.
  • By parallel communication data can be transferred with high speed. But speed come with cost,  it required as many wires as many bits user want to transfer at a time.
  • Serial communication consists of only two wires, transmission wire and receive wire.
  • Receiver wire is used to receive data sequentially from other UART device.
  • Transmitter wire is used to send data sequentially to other UART microcontroller or device.
  • UART communication pins of MSP430G2 launchPad are shown in figure.UART communication MSP430 microcontroller
  • Pin number three is transmission pin and pin number 4 is a receiver pin.

Types of serial communication

  • Two types of serial communications are used one is synchronous and other one Asynchronous .
  • Asynchronous communication , there will be no clock signal required to send and receive data.  Start bit and stop bit is used to validate data between  transmitter and receiver.
  • While in Asynchronous communication clock is used to synchronize data transmission between two devices.

How UART communication works?

UART communcaiton using MSP430 microcontroller




Connection diagram between two UART devices is shown below. UART communication connections

  • Transmit or Tx pin of one UART device is directly connected with receive or Rx pin of other device and vice versa.
  • Other than these two pins ground pins of both devices should be connected with each other.
  • Now the question is How UART communication achieve synchronization with clock signal?
  • It achieves synchronization through baud rate and stat/stop bit.
  • Baud rate defines how many bits a device can send or receive per second. It is also know as the rate of data transfer in serial communication. Baud rate can be 4800, 9600 etc.
  • Baud rate of both receiver and transmitting device should be same.
  • Picture shows the structure of UART.
  • Each frame consists of start bit. data  bits, parity bit and stop bit. UART communication frame
  • When transmitter wants to send data to other device. It send start signal by making a transition from high to low signal and receiver acknowledge this start bit and start receiving data bit by bit. For example in above picture data is of 8 bits. By 8 bits mean by UART we can send only one character per frame.
  • After completing data transfer, transmitter sends stop signal to receiver by sending digital high signal.

How to use UART communication of MSP430 microcontroller

You must be wondering that how you are going to write complex coding for this uart frame? Well, we do not need to worry about UART data format how data will be received and sent. Because I am using Energia IDE in this series of tutorials on MSP430G2 launch Pad. Energia IDE has a built in serial library. Serial library will take care of all these things. we only need to know how to use serial communication function.

  • Serial.begin(baud_rate)
  • Serial.available()
  • Serial.print(value)
  • Serial.println(value)
  • Serial.read()
  • Serial.write(value), Serial.write(string), Serial.write(buff, length)

MSP430G2553 microcontroller has UART module as a part of microcontroller and we can access it through pin 3 and 4 of MS430G2 launchPad as you can see in picture of development board . Pin number 4 is transmitter pin and pin number 3 is receiver pin of UART module.So we can use these pins to communicate with other devices like Bluetooth module, serial ports of other microcontrollers. We will see the example of Bluetooth module interfacing with MSP430G2 lanuchPad in coming articles.

How to use UART communication library

Now let’s see the basic function of UART library which is used to send and receive data through serial communication.

  1. Serial. begin(baud_rate)

First we all we need to initialize UART module of MSP430G2553 microcontroller. As you already know we initialize everything inside the step function in Energia IDE.  Serial.begin function is used to initialized serial module of MS430G2553 microcontroller and input to serial begin function is  baud_rate.  The baud rate that will be used for serial communication Can be 4800, 9600, 14400, 19200, etc. For Example this line  Serial.begin(9600) defines 9600 baud rate for UART communication.

    2. Serial.available()

After setting baud rate. we have two functions that are used to receive data through serial communication. First of all we need to check either data is available to receive or not on serial pin which is RX pin on MS0430G2 board. Serial.available()  function checks either data is available to receive or not . If data is available, it gives output logic high and otherwise it gives output logic 0.

    3. Serial.read()

Serial.read function is used to read data byte by byte. After checking with Serial.available function either data is available or not,  we will store the receive byte in character type variable. Serial.read() function cares about data bits and stop bits. We don’t need to worry about it. Serial.Read function MSP430

As shown in above simple code, first we have defines baud rate inside the step  function.  As you know we do everything inside the loop function which we want to perform again and again.  Inside the loop function, if condition checks the output of Serial.available function, if its value is true which mean data is available to read on receive pin of MSP430 microcontroller, we will read the byte using Serial.read function and store it in character type variable name read_byte. So this how we can use MSP430 microcontroller to receive data from serial UART device. but now the question is how we can use send data through UART module of MSP430 microcontroller.

    4. Serial.print(char)

This function is used to send data from transmit pin of MSP430 microcontroller to other UART device. This function can send data in character, strings and numbers form.  The use of this function is same as the Serial.read function. you need to define baud rate inside the step up function to use this function. I will provide two examples in later part of this article on how to transmit and receive data through UART module of MSP430 microcontroller. For more information on UART library of MSP430 microcontroller check this video:

How to transmit data through UART using MSP430 microcontroller

Now I will show you the example of how to send data through uart serial communication of MSP430 microcontroller. In this example, we are sending data from MSP430G2 lanuchpad to laptop through serial communication. we are receiving data on serial monitor of Energia IDE.  Check this video lecture on how to do it with step by step guide. In this video, first I will explain how to connect MSP430G2 launchPad with computer or laptop and how to use serial monitor of MSP430G2 launchpad.  After that I will provide two examples.

How to receive data on UART  of MSP430 microcontroller

In this part, you will learn how to receive data on UART receive pin of MSP430 microcontroller.  For demonstration purpose, we have used an lighting emitting diode which will turn on and off depending on data receive value on Tx pin of MPS430 microcontroller.

Code for this part is given below.

int led=2;
char data;

void setup() 
{

pinMode(led,OUTPUT);
Serial.begin(9600);
}

void loop()

{
if(Serial.available())
{
data = Serial.read();
}

if(data=='A')
{
digitalWrite(led, HIGH);
Serial.println("led is on");
}
else
{
digitalWrite(led,LOW);
Serial.println("LED IS OFF");

}

}
  • In this code we are using pin number 2 as a digital output pin.
  • Inside the set up function, pin number 2 is initialized as a digital output pin and baud rate is set to 9600.
  • Inside the loop function, Serial.available function is used to check if data is available to receive on Tx pin of MSP430 microcontroller.
  • If data is available, it will be saved in a character type variable name ‘data’ .
  • After that, if condition checks if data=A, it will turn on the LED and send message of “LED IS ON” on the serial monitor.
  • Other if data is something other than A, LED will remain off and message of “LED IS OFF” will be send to serial monitor of Energia IDE through Tx pin of MSP430 microcontroller.

For complete demo of this part, you can watch this video:

So this is how we can transmit and receive data through UART module of MSP430G2553 microcontroller which is available on MSP430G2 launchPad.  There are many wireless communication modules which works on UART communication like HC-05 Bluetooth module, GSM module. GPS module. You can use MSP430 with these module in your embedded systems projects.



Leave a Comment