Tutorial_12 : Serial Driver - sudeshmoreyos/Morey_os-demo-1.0 GitHub Wiki

Home <<Prev 12

Introduction to Serial Driver

In Morey_os, the Serial.h driver is designed to facilitate serial communication between your microcontroller and other devices, including computers and peripherals. This driver supports various functions that allow us to configure the serial port, check for incoming data, read data, and send both standard and constant string data over the serial connection.

In order to use this driver we need to include header file :

#include "Serial.h"

Note: Alphabet 'S' must should be in uppercase, while all other letters are in lowercase.

Serial driver supports following functions :

1. Serial.begin(baud_rate,SERIAL_8N1);

2. Serial.available();

3. Serial.read();

4. Serial.write(mos_uint8_t data);

5. Serial.print("Output String");

6. Serial.println("Output String with new line");

7. Serial.constPrint("Output String");

8. Serial.constPrintln("Output String with new line");

9. Serial.end();

10. Serial.flush();

11. Serial.printBytes(mos_uint8_t * data, mos_uint16_t len)

1. Serial.begin(baud_rate,SERIAL_8N1)

The Serial.begin() function initializes the serial communication by setting up the baud rate and configuration. The baud rate defines the speed of communication, typically in bits per second (bps), while SERIAL_8N1 specifies the format of the data being transmitted.

There are 2 parameters which should be passed to the function:

  • baud_rate: The speed of communication in bits per second. In Morey_os baud rates include 1200, 4800, 9600, 10000.
  • SERIAL_8N1: This is a configuration macro representing 8 data bits, no parity bit, and 1 stop bit.

Example:

1. Serial.begin(baud_rate,SERIAL_8N1);

2. Serial.available()

This function checks that the serial data on serial port is available or not by checking that how many bytes of data are available to be read from the serial buffer. It returns the number of bytes waiting in the buffer, allowing you to determine if there is data ready to be processed. This function is particularly useful for non-blocking serial communication.

Usage Example:

if (Serial.available() > 0) {
    // If Data is available then this session will execute
}

3. Serial.read()

This Serial.read() function is used to read the character from the front of the serial. This function can read only one byte of data. This function should be called only when data is available and this function returns a character. If no data is available then this return -1.

The return type of this function is mos_uint8_t, which means it returns a byte of data as output. This byte can be processed and stored as a character or used in further operations.

Example:

if (Serial.available() > 0) {
    char receivedByte = Serial.read();
    // Process the received byte
}

4. Serial.write(mos_uint8_t data)

The Serial.write(mos_uint8_t data) function in Morey_os is designed to send a single byte of data over the serial port. The function takes one parameter that is data, which is of type mos_uint8_t type representing the byte to be transmitted. This function can also be defined as the function which is used to append the data to the serial.

This function directly writes the byte to the serial port, allowing you to send binary data or specific byte values.

Example:

mos_uint8_t byteToSend = 0x5A; // Example byte in hexadecimal
Serial.write(byteToSend);

5. Serial.print("Output String")

The Serial.print() function sends data over the serial port without appending a newline character at the end. The output can be a string, character, or number. The function allows you to print data to the serial monitor or another serial device in a continuous format, making it ideal for logging or outputting data where each piece should appear on the same line.

The return type of this function is void, meaning it does not return any value. The output string is sent byte by byte over the serial port and can be processed by the receiving device as a continuous stream.

Usage Example:

Serial.print("This is Serial.print function");

This code sends the string "Hello, This is Morey_os Serial Driver" over the serial port and keep's the cursor to the same line.

6. Serial.println("Output String with new line")

The Serial.println() function is similar to 'Serial.print()` but with an important difference: it appends a newline (\n) character at the end of the output, moving the cursor to the next line on the receiving device. This function is useful for printing data in a line-by-line format, such as when displaying a list of values or logging messages where each entry should start on a new line.

The return type of this function is void, indicating that it does not return any value. The output string, followed by a newline, is transmitted byte by byte over the serial port.

This can be initialize by:

Serial.println("This is Serial.println")

7. Serial.constPrint("Output String")

The Serial.constPrint() function is designed to send constant strings stored in flash memory over the serial port without appending a newline character at the end. This function is optimized for microcontrollers with limited RAM, as it prevents the constant string from being copied into RAM before transmission, thereby conserving memory resources.

The return type of this function is void, meaning it does not return any value. The constant string is transmitted byte by byte directly from flash memory, making this function ideal for sending fixed messages or prompts efficiently.

This can be initialize by:

Serial.constPrint("This is Serial.constPrint function")

Note: alphabet 's' and alphabet 'p' should be in uppercase and rest of all are in lowercase.

8. Serial.constPrintln("Output String with new line")

The Serial.constPrintln() function works similarly to Serial.constPrint() but with the addition of appending a newline (\n) character at the end of the transmitted string. This moves the cursor to the next line on the receiving device, making it useful for sending multiple lines of constant strings where each string should start on a new line. Similar to Serial.constPrint(), it is optimized for sending constant strings stored in flash memory, which helps in saving RAM on microcontrollers.

The return type of this function is void, indicating it does not return any value. The constant string, followed by a newline, is sent directly from flash memory byte by byte, ensuring efficient memory usage while maintaining organized output on the receiving end.

This can be initialize by:

Serial.constPrintln("This is Serial.constPrintln function")

Note: alphabet 's' and alphabet 'p' should be in uppercase and rest of all are in lowercase.

9. Serial.end()

The Serial.end() function in Morey_os is used to terminate the serial communication that was previously established using Serial.begin(). When Serial.end() was called, it disables the serial port, freeing the associated hardware resources and stopping any ongoing communication.

This function is essential when the serial communication is no longer needed or when the serial port is needed to reconfigure with different settings without restarting the system.

This can be declare by:

Serial.end();

10. Serial.flush()

The Serial.flush() function in Morey_os is used to clear the serial buffer by waiting for all outgoing data to be transmitted completely. When Serial.flush() is called then the function ensures that all the bytes in the transmit buffer are sent out through the serial port before allowing the program to proceed.

This is particularly useful in scenarios where it is needed to ensure that all pending serial data has been transmitted before performing actions.

Serial.flush();

11. Serial.printBytes(mos_uint8_t * data, mos_uint16_t len)

The Serial.printBytes(mos_uint8_t * data, mos_uint16_t len) function in Morey_os is used to send an array of bytes over the serial port. This function takes two arguments. Pointer to the data array and the length of the data to be sent. It allows for the efficient transmission of multiple bytes at once, which is useful for sending blocks of binary data.

Example:

mos_uint8_t dataArray[] = {0x01, 0x02, 0x03, 0x04};
mos_uint16_t length = sizeof(dataArray) / sizeof(dataArray[0]);
Serial.printBytes(dataArray, length);

Example of Serial Communication using Serial driver in Morey_os

This code demonstrates the initialization and execution of multiple tasks in the Morey_os, including serial communication and digital pin control. It includes setting up the serial port, configuring digital pins, and creating tasks that interact with the user via serial communication and control LEDs. Main focus of this example is on the functions of Serial driver to demonstrate serial communication.

To write a program it is necessory to create three files as it is demonstrated in Tutorial 12 :

  1. Makefile
  2. config.h
  3. user_code.c

1. Makefile

PROJECT = hello-world
MOREY_OS_PATH = ../../..

ARCH = AVR_MEGA
CONTROLLER = MEGA2560
#FREQ = 1000000

#BOARD = ARDUINO_UNO

include $(MOREY_OS_PATH)/Makefile.include

2. config.h

#ifndef CONFIG_H
#define CONFIG_H

#define COMPILER AVR_GCC
#define SERIAL_ENABLE

#endif

3. user_code.c

// Declare here all header files used in the code.h , OS related files are included by default
#include "morey_os.h"
#include "Digital.h"
#include "Serial.h"

// Declare all initialization functions of controller peripherals in the setup function below
void setup(void)
{    
	Serial.begin(4800,SERIAL_8N1);
	Digital.pinmode(B5,OUTPUT);
	Digital.pinmode(B7,OUTPUT);
}

// Delcare all processes here
TASK_CREATE(hello,"hello");
TASK_CREATE(reply,"reply");
TASK_CREATE(led,"LED");

// Delcare autostart  processes here. Atleast one process must be autostarted;
AUTOSTART_PROCESSES(&hello, &reply, &led);

TASK_RUN(hello)
{
  // Declare all variables here, please read documentation to understand 
  // which variables should be declared as static variables            

  // Process starts here  
  BEGIN();    

  while(1)
  {
	Digital.write(B5,HIGH);
	Serial.constPrintln("Hello World I am Sudesh Morey and who are you?");
	DELAY_SEC(2);           
	Digital.write(B5,LOW);
	DELAY_SEC(2);           
  }
  
  // process ends here
  END();
}

TASK_RUN(reply)
{
  // Declare all variables here, please read documentation to understand 
  // which variables should be declared as static variables            
	static char x;
  // Process starts here  
  BEGIN();    

  while(1)
  {
    if(Serial.available())
	{
		x = Serial.read();
		if(x=='a')
			Serial.constPrintln("Hey how are you I mean");
		if(x=='b')
			Serial.constPrintln("No thank you Bye");
	}
	DELAY_SEC(0.01); 
  }
  
  // process ends here
  END();
}

TASK_RUN(led)
{
  // Declare all variables here, please read documentation to understand 
  // which variables should be declared as static variables            

  // Process starts here  
  BEGIN();    

  while(1)
  {
	Digital.write(B7,HIGH);
    DELAY_SEC_PRECISE(0.1);
	
	Digital.write(B7,LOW);
    DELAY_SEC_PRECISE(0.1);            
  }
  
  // process ends here
  END();
}