Let’s see some basic functions related to serial communication that are frequently used in Energia IDE.
MSP430 UART Communication Functions
a) Serial.begin(baud_rate)
baud_rate
: The baud rate that will be used for serial communication. Can be 4800, 9600, 14400, 19200, etc.- This function is used to define the baud rate that will be used for serial communication. For communicating with specific devices, the device baud rate needs to be used.
- Example
Serial.begin(9600); //defines 9600 baud rate for communication.
b) Serial.available()
- This function is used to get the number of bytes available for reading from the serial port. It gives the number of bytes of data that has arrived and is stored in the serial receive buffer.
- Example
if(Serial.available(); //If data available at the serial port, take action.
c) Serial.print(value)
value
: character, string, number to be printed.- This function is used to print data to a serial port in a form that is human readable (character, strings, numbers).
- Example
Serial.print(“Hi 1234”); //Prints Hi 1234 on the serial monitor.
d) Serial.println(value)
value
: character, string, number to be printed.- This function is used to print data to a serial port in a form that is human readable (character, strings, numbers) followed by a carriage return (\r) and a new line character (\n).
e) Serial.read()
- This function returns a character that was received on the Rx pin of Launchpad.
- Example
char read_byte;
read_byte = Serial.read(); //Byte of data read is stored in read_byte.
f) Serial.write(value), Serial.write(string), Serial.write(buff, length)
value
: value to be sent as a single byte.string
: string to be sent as a series of bytes.buff
: an array of data to be sent as bytes.length
: a number of bytes to be sent.- This function writes data in binary form to the serial port. Data is sent in form of bytes or series of bytes.
- Example
Serial.write(100);
Serial.write(“Hello”);
MSP430 Launchpad Serial Communication Program
/* Serial Communication Functions */
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
Serial.begin(9600); /* opens serial port, sets data rate to 9600 bps */
}
/* Loop runs over and over after the startup function */
void loop()
{
int data = 0;
if(Serial.available()>0) /* If data available at serial port, enter if loop */
{
data = Serial.read(); /* Read data present at serial port */
Serial.println("Data Received is : "); /* Print string with \r\n */
Serial.write(data); /* Print data received */
}
}
MSP430 SPI Communication Functions
a) SPI.begin(slaveSelectPin)
slaveSelectPin
: Pin to be selected as a slave select pin. If this parameter is not passed, pin 2 is used as the default slave select pin.- This function initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low and SS high.
b) SPI.end(slaveSelectPin)
slaveSelectPin
: Used only if different slave select pin was used (other than default pin number 2) forSPI.begin()
function.- This function disables the SPI bus without changing the pin modes.
c) SPI.transfer(slaveSelectPin , value)
slaveSelectPin
: slave select pin for the device to be selected.value
: The byte to be sent out over the bus.- The value returned can be stored in a data variable.
- SPI transfer is based on a simultaneous send and receive.
- If slaveSelectPin is specified, that specific pin is pulled low before the transfer and pulled high after the transfer is completed.
- Example
char received_val;
received_val = SPI;
MSP430 Launchpad SPI Communication Program
/* SPI Communication Functions */
#include <SPI.h>
const int slavePin = 6; /* Pin used as chip select pin for the Slave device */
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
pinMode(slavePin, OUTPUT); /* Configure slavePin as an output pin */
SPI.begin(); /* Initialize SPI */
}
/* Loop runs over and over after the startup function */
void loop()
{
digitalWrite(slavePin, LOW); /* Chip Select pin low to initialize communication with slave */
SPI.transfer(0x45); /* Transfer data */
/* 0x45 is sent to slave device. At the same time, a byte of data is received from the slave */
digitalWrite(slavePin, HIGH); /* Chip Select pin high to end communication with slave */
}
MSP430 I2C Communication Functions
a) Wire.begin(address)
address
: 7-bit slave address of the device. If not specified, the device joins the bus as a master.- This function is used to initiate the wire library and join the bus as a master or slave.
b) Wire.beginTransmission(address)
address
: 7-bit address of the device to transmit to.- This function is used to begin transmission to the I2C slave device with the given address. After this, queue bytes for transmission using the Wire.write() function and transmit them by calling the Wire.endTransmission() function.
- Example
Wire.beginTransmission(0x23); //Begin transmission to I2C device with address 0x23.
c) Wire.endTransmission(stop)
stop
: Boolean data type. True will send a stop message after the request, thus releasing the bus. False will continually send a restart after the request, thus keeping the connection active.- This function ends transmission to the slave device started using Wire.beginTransmission() function and transmits the bytes that were queued by
Wire.write()
function.
d) Wire.write(value), Wire.write(string), Wire.write(data, length)
value
: value to be sent as a single byte.string
: string to be sent as a series of bytes.data
: an array of data to be sent as bytes.length
: number of bytes to be sent.- This function writes data from the slave device in response to a request from the master, or queues bytes for transmission from master to a slave device in between calls to
Wire.beginTransmission()
andWire.endTransmission()
e) Wire.available()
- This function returns the numbers of bytes available to be read with the
Wire.read()
function.
f) Wire.read()
- This function reads a byte that was transmitted from a slave device to a master device after a
Wire.requestFrom()
function, or read a byte that was transmitted from a master device to a slave device.
g) Wire.requestFrom(address, quantity, stop)
address
: 7-bit address of the device to request bytes from.quantity
: number of bytes to be requested.stop
: Boolean data type. True will send a stop message after the request, thus releasing the bus. False will continually send a restart after the request, thus keeping the connection active.- This function is used by the master to request bytes from a slave device.
MSP430 Launchpad I2C Communication Program
/* I2C Communication Functions */
#include <Wire.h>
/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
Wire.begin(); /* Join I2C bus as master */
Serial.begin(9600); /* opens serial port, sets data rate to 9600 bps */
}
/* Loop runs over and over after the startup function */
void loop()
{
char rx_data;
Wire.requestFrom(45, 8); /* Request 8 bytes of data from slave with address 45 */
while(Wire.available()) /* While data is available from the slave, enter while loop */
{
rx_data = Wire.read(); /* Read byte of data */
Serial.println(rx_data); /* Print the received data on serial terminal */
}
delay(500); /* Wait for 500 milliseconds */
Wire.beginTransmission(70); /* Begin transmission to device with address 70 */
Wire.write(0x30); /* Data to be transmitted */
Wire.write(0x50);
Wire.endTransmission(); /* End transmission */
}
Components Used |
||
---|---|---|
TI Launchpad MSP-EXP430G2 TI Launchpad MSP-EXP430G2 |
X 1 |