UART Serial communication with MSP430 microcontroller (original) (raw)

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 ?

Types of serial communication

How UART communication works?

UART communcaiton using MSP430 microcontroller

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

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.

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");

}

}

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.