{"id":2557,"date":"2018-09-21T18:35:27","date_gmt":"2018-09-21T18:35:27","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=2557"},"modified":"2023-08-17T23:53:04","modified_gmt":"2023-08-17T20:53:04","slug":"uart-pic-microcontroller-tutorial","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/","title":{"rendered":"UART | Serial Communication With PIC Microcontrollers Tutorial"},"content":{"rendered":"<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/eeproms-internal-in-pic-microcontrollers\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1666 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png\" alt=\"Previous Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: left;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/eeproms-internal-in-pic-microcontrollers\/\"><strong>Previous Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: center;\" colspan=\"2\"><span style=\"color: #000000;\"><strong><span style=\"font-size: 14pt;\">Tutorial 18<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: right;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial\/\"><strong>Next Tutorial<\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial\/\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1667 alignright\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png\" alt=\"Next Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<td style=\"background-color: #ffffff; text-align: center;\" colspan=\"6\"><span style=\"font-size: 14pt;\"><strong>UART\u00a0Serial Communication Protocol<\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<td style=\"width: 20%; background-color: #ffffff; text-align: center;\" colspan=\"6\"><span style=\"font-size: 10pt;\"><strong>Intermediate Level\u00a0\u2605\u2605\u2606\u2606\u2606<\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In this tutorial, we&#8217;ll be discussing our first serial communication protocol (UART). You&#8217;ll get to know what is the UART serial communication protocol? How does it work? What are the typical applications for UART? We&#8217;ll also discuss in detail the process of creating the required firmware to drive the UART module in our PIC Microcontrollers. And finally, create a simple communication application MCU-To-MCU and test it out. It&#8217;s going to be a very long read with a massive amount of information, So let&#8217;s get started!<\/span><\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/deepbluembedded.com\/spi-serial-peripheral-interface-tutorial-with-pic-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2999 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg\" alt=\"SPI Tutorial - PIC Microcontrollers\" width=\"201\" height=\"95\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg 720w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers-300x142.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers-520x245.jpg 520w\" sizes=\"auto, (max-width: 201px) 100vw, 201px\" \/><\/a>You may also need to check out the <a href=\"https:\/\/deepbluembedded.com\/spi-serial-peripheral-interface-tutorial-with-pic-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\">SPI (serial peripheral interface)<\/a> tutorial. It&#8217;s another 10k words long-read kind of gigantic tutorial. Check it out and maybe keep it bookmarked as a guide for future work!<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<blockquote>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/deepbluembedded.com\/i2c-communication-protocol-tutorial-pic\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-3738 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/09\/I2C-Tutorial-With-PIC-Microcontrollers-Thumbnail-300x136.jpg\" alt=\"I2C Tutorial With PIC Microcontrollers Thumbnail\" width=\"196\" height=\"89\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/09\/I2C-Tutorial-With-PIC-Microcontrollers-Thumbnail-300x136.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/09\/I2C-Tutorial-With-PIC-Microcontrollers-Thumbnail-980x450.jpg 980w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/09\/I2C-Tutorial-With-PIC-Microcontrollers-Thumbnail.jpg 720w\" sizes=\"auto, (max-width: 196px) 100vw, 196px\" \/><\/a>You may also need to check out the <a href=\"https:\/\/deepbluembedded.com\/i2c-communication-protocol-tutorial-pic\/\" target=\"_blank\" rel=\"noopener noreferrer\">I2C Bus tutorial<\/a>. It&#8217;s another 12k words long-read kind of gigantic tutorial. Check it out and maybe keep it bookmarked as a guide for future work!<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>[toc]<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Required Components\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<table style=\"width: 100%; border-collapse: collapse; border-style: solid; background-color: #ffffff; border-color: #000000;\">\n<tbody>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Qty.<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Component Name<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #212121;\"><span style=\"color: #ffffff;\"><strong>Buy<\/strong> On Amazon.com<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">2<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">PIC16F877A<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2S35QkL\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">2<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">BreadBoard<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LFYimS\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">8<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">LED<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XyQNWb\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2LFQ0eU\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Resistors Kit<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XufO4F\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2JhXGSK\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Capacitors Kit<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LAIQsi\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2Jjt4Aq\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Jumper Wires Pack<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2Xvs4gu\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/32cgJWk\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">USB-TTL Converter or FTDI Chip<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XuttZG\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2XxHIIj\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">LM7805 Voltage Regulator (5v)<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2XtVETJ\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Crystal Oscillator<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LBpWS9\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">1<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">PICkit2 or 3 Programmer<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LHhJvy\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 10%; text-align: center; background-color: #ffffff;\">2<\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">9v Battery or DC Power Supply<\/span><\/td>\n<td style=\"width: 33.3333%; text-align: center; background-color: #ffffff;\"><a href=\"https:\/\/amzn.to\/2LIlPDG\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2XsWoNE\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a>\u00a0 \u00a0 <a href=\"https:\/\/amzn.to\/2JhneiW\" target=\"_blank\" rel=\"noopener noreferrer\">Add<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><a href=\"https:\/\/deepbluembedded.com\/microcontroller-programming-tutorial\/#_Prototyping_Board\" target=\"_blank\" rel=\"noopener noreferrer\">The Prototyping Board Setup<\/a><\/strong><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/microcontroller-programming-tutorial\/#_Prototyping_Board\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1962\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-1024x489.jpg\" alt=\"Prototyping Board - Embedded Systems Tutorials With PIC MCUs\" width=\"750\" height=\"358\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-1024x489.jpg 1024w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-300x143.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs-768x367.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Prototyping-Board-Embedded-Systems-Tutorials-With-PIC-MCUs.jpg 1500w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Introduction To Serial Communication\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In Embedded Systems, Telecommunication, and Data Transmission applications, <strong>Serial Communication<\/strong> is known to be the process of sending data one bit at a time (bit-by-bit) sequentially, over the serial bus. It takes a complete clock cycle in order to transfer each bit from an end to the other. Conversely, parallel communication is known to be the process of sending several\u00a0bits, even bytes, as a whole only in a single clock cycle. However,\u00a0even if you transfer fewer data per cycle with a serial transmission, you can do it at much higher frequencies which results in higher net transfer rates than of the parallel communication.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0The Fundamental Concepts\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There do exist many serial communication protocols, each of which is working in a different way. However, it turns out to be some similarity that they do share in common. Frankly speaking, serial communication protocols have <strong>Shift Registers<\/strong> somehow\/somewhere in their\u00a0hardware implementation as the working-horse (core). Shift registers are used to shift out the data to be transmitted bit-by-bit each clock cycle. So, let&#8217;s dig deeper into a shift register in order to build-up a complete understanding for what is actually happening at the Register(low)-Level.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Shift registers are basically some D-Flip-Flops serially connected while sharing the same clock line. Here is a graphical animation that demonstrates\u00a0how does a shift register work internally.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2599\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/D-FF-Shift-Register.gif\" alt=\"Shift Register Flip-Flop Animation | UART Tutorial\" width=\"854\" height=\"194\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As you might have noticed, the data input (0&#8217;s and 1&#8217;s) is being shifted from the input pin to the output end at bit-0. It takes only 1-clock to transfer a single bit, which means it takes 8-clocks for a single byte transfer. For the same of simplicity, we&#8217;ll represent the shift register as a single block instead of a series of D-Flip-Flops as shown above.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is an animation for an 8-Bit shift register with a serial input &amp; serial output data lines.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2600\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Shift_Register.gif\" alt=\"Shift Register | UART Tutorial With PIC Microcontroller\" width=\"400\" height=\"213\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Well, now you should know how shift registers are actually working. Frankly speaking, serial communication is as simple as connecting a couple of shift registers together! Connecting the data output of a shift register to be the data input of the other shift register enables us of sending digital data serially from an end to another!<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In the following animation, I&#8217;m connecting a couple of 4-Bit shift registers. One at the transmitter device and the other at the receiver device. The serial bus consists of a couple of wires (data, and clock). Each clock, a bit is sent from the transmitter TX pin and received by the receiver&#8217;s RX pin.<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2601\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Serial-Com.gif\" alt=\"Serial Communication | UART Tutorial With PIC Microcontroller\" width=\"700\" height=\"325\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As you might have noticed, it takes 4-clocks to send the 4-Bit data from the transmitter to the receiver. This is simply the short answer to the &#8220;How Serial Communication Works?&#8221;. However, it&#8217;s not everything you should know about serial communication or at least to the level of being able to implement a simple serial data transfer process.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In fact, there are some other options and configurations for each serial communication protocols. Which includes, various data transmission rates, error detection, and correction mechanisms, and much more that adds to the overall complexity associated with each protocol. We&#8217;ll be working with the UART protocol in this tutorial, which has a decent steep learning curve.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Serial VS Parallel Communication\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As we&#8217;ve stated earlier, serial communication is the process of sending data each bit at a time (bit-by-bit). And conversely, the parallel communication is the process of sending multiple bits, even bytes, as a whole in a single clock cycle.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">A very basic implementation, that you can create on your own, for the parallel data transfer is shown down below. As I&#8217;m connecting a couple of 4-Bit registers, via the 5-wire parallel bus (4-data pins + 1-clock). Here is an animation that shows you how parallel data transfer is done.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2602\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Parallel-Communication.gif\" alt=\"Parallel Communication | UART Tutorial\" width=\"750\" height=\"349\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As you might have noticed, it takes only 1-clock to transfer the data from a transmitter device to the receiver! Frankly speaking, we can theoretically transfer any number of bits (or bytes) using parallel communication protocols in a single clock cycle at a time.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is a brief comparison between serial communication and parallel communication protocols.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 20%; background-color: #29a7e6; text-align: center;\"><span style=\"color: #ffffff;\"><strong>Feature<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #db7012; text-align: center;\"><strong><span style=\"color: #ffffff;\">Serial Communication<\/span><\/strong><\/td>\n<td style=\"width: 40%; background-color: #292626; text-align: center;\"><strong><span style=\"color: #ffffff;\">Parallel Communication<\/span><\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Relative Speed<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Faster<\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Slower<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Distance Range<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Much Farther<\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Shorter<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Transfer Method<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">One bit is transmitted at a time<\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Bytes are transmitted in parallel, One byte or more at a time<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Applications<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Computer small peripherals, modules interfacing, and sensors measuring &amp; sending simple data frames<\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Short distance High-Speed communication such as Computer printers, etc\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Wires Inside<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Few wires, all data bits pass only through the same data line<\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Multiple, more wires, each bit has a dedicated wire. So as to be transmitted all at once<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%; background-color: #ffffff; text-align: left;\"><span style=\"color: #333333;\"><strong>Cables Pictures<\/strong><\/span><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2606 alignnone\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/8909527049124044-300x200.jpg\" alt=\"Ethernet Serial Cable | UART Tutorial\" width=\"150\" height=\"100\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/8909527049124044-300x200.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/8909527049124044.jpg 600w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2604 alignnone\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4012199660_2d392f4809_b-300x200.jpg\" alt=\"USB Serial Cable | UART Tutorial\" width=\"150\" height=\"100\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4012199660_2d392f4809_b-300x200.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4012199660_2d392f4809_b-768x512.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4012199660_2d392f4809_b.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2605 alignnone\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/5662914060_4e4848ea8b_b-300x225.jpg\" alt=\"Serial Cable | UART Tutorial\" width=\"150\" height=\"113\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/5662914060_4e4848ea8b_b-300x225.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/5662914060_4e4848ea8b_b-768x576.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/5662914060_4e4848ea8b_b.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/td>\n<td style=\"width: 40%; background-color: #ffffff; text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2607\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/1200px-Parallel_computer_printer_port-300x225.jpg\" alt=\"Parallel Port | UART Tutorial\" width=\"150\" height=\"113\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/1200px-Parallel_computer_printer_port-300x225.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/1200px-Parallel_computer_printer_port-768x576.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/1200px-Parallel_computer_printer_port-1024x768.jpg 1024w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/1200px-Parallel_computer_printer_port.jpg 1200w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2608\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4425798078_577b06db98_b-300x210.jpg\" alt=\"Parallel Cable | UART Tutorial\" width=\"150\" height=\"105\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4425798078_577b06db98_b-300x210.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4425798078_577b06db98_b-768x539.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/4425798078_577b06db98_b.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2609\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Scsi-connectors-300x203.jpg\" alt=\"Parallel Connectors | UART Tutorial\" width=\"150\" height=\"102\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Scsi-connectors-300x203.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Scsi-connectors-768x521.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Scsi-connectors-1024x694.jpg 1024w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Applications Of Serial Communication Protocols\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There are countless situations in which we do use one or more of the serial communication protocols. Here, I&#8217;ll try to categorize the main purposes for which we use serial communication instead of listing down all possible applications that may be serving the same purposes. Hence, any application will, hopefully, possibly fall under the umbrella of the following purposes.<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">External devices\/modules communications<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Downloading \/ Updating The Firmware<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Console io<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Data Transmission<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Debugging interface<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Serial communication protocols are of fundamental importance for each embedded systems application. And as embedded systems engineers, we must have a very solid experience with almost all of the commonly used serial communication protocols. And in the next sub-section, we&#8217;ll see a brief collection of the commonly available serial communication protocols in the embedded market.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Examples Of Serial Communication Protocols\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There are many serial communication protocols that are existent and being used in various domains and sub-domains of embedded systems. Some of which are more likely to be used in the automotive industry such as LIN &amp; CAN, some of which is more likely to be used for external memory interfacing as I2C, high-speed computer interfacing as USB, and for audio applications such as I2S. Here is a list of the most common serial communication protocols in the industry.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>USB<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>CAN<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>I2C<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>I2S<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>LIN<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>SPI<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>Ethernet<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>1-Wire<\/strong><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><strong>UART\/USART<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\" colspan=\"3\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">And many more&#8230;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We&#8217;ll be focusing only on the UART serial protocol in the rest of this tutorial. Just to understand the fundamentals &amp; mechanics of UART communication. Only then, we&#8217;ll be able to develop the necessary firmware in order to transmit data from an embedded MCU to another as we&#8217;ll be doing the lab at the end of this tutorial. So, let&#8217;s get started with UART!<\/span><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Introduction To UART\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Universal Asynchronous Receiver\/Transmitter<\/strong>\u00a0or\u00a0<strong>UART\u00a0<\/strong>for short\u00a0represents the hardware circuitry (module) being used for the serial communication.\u00a0UART is sold\/shipped as a\u00a0standalone integrated circuit (<strong>IC<\/strong>) or as an internal module within microcontrollers. In this tutorial, we&#8217;re actually concerned with the internal UART module within PIC Microcontrollers.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There is a couple of io pins dedicated to the UART serial communication module highlighted in the following figure. Namely, <strong>RX<\/strong> (data input &#8211; receiving end) &amp; <strong>TX<\/strong> (data output &#8211; transmitting end).<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2638\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/PIC-UART-RX-TX-Pins.jpg\" alt=\"PIC UART RX-TX Pins\" width=\"400\" height=\"257\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/PIC-UART-RX-TX-Pins.jpg 502w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/PIC-UART-RX-TX-Pins-300x192.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Forms Of UART Communication\u00a0<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There are actually two forms of UART as follows:<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>UART<\/strong>\u00a0&#8211; Universal Asynchronous Receiver\/Transmitter<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>USART<\/strong>\u00a0&#8211; Universal Synchronous\/Asynchronous Receiver\/Transmitter<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The <strong>Synchronous<\/strong> type of transmitters generates the data clock and sends it to the receiver which works accordingly in a synchronized manner. On the other hand, the <strong>Asynchronous<\/strong> type of transmitter generates the data clock internally. There is no incoming serial clock signal, so in order to achieve proper communication between the two ends, both of them must be using the same\u00a0<strong>baud rate<\/strong>. The baud rate is the rate at which bits are being sent <strong>bps<\/strong> (bits per second).<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 50%; background-color: #3b91ed; text-align: center;\"><span style=\"color: #ffffff;\"><strong>UART<\/strong><\/span><\/td>\n<td style=\"width: 50%; background-color: #23ba23; text-align: center;\"><span style=\"color: #ffffff;\"><strong>USART<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Standalone Communication Protocol<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">It can\u00a0support multiple protocols like LIN, RS-485, IrDA, etc<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Commonly used for low-speed applications<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">More suitable for high-speed applications<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The data rate is relatively low<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The data rate is much higher<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The clock is generated locally and both devices are configured to run at the same baud rate<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The clock signal is generated by the transmitter and sent to the receiver during data transmission<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The baud rate of the receiver module must be known prior to any communication<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The baud rate is useless as the receiver module will be using the clock coming from the transmitter device<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Offer reduced low energy footprint even in sleep mode<\/span><\/td>\n<td style=\"width: 50%; background-color: #ffffff; text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Operates at high energy consumption modes<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">So, <strong>is UART and USART are the same?<\/strong> Technically, NO!\u00a0A USART generally has much more capabilities than a generic UART. Being able to generate clocked data allows the USART to operate at much higher baud rates way more than what a typical UART can handle.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Modes Of UART Communication\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The communication over the UART bus can be configured (set) to be on one of the following modes:<\/span><\/p>\n<ul>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Simplex<\/strong>\u00a0&#8211; One direction only, a transmitter to a receiver<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Half Duplex<\/strong>\u00a0&#8211; Devices take turns transmitting and receiving<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Full Duplex<\/strong>\u00a0&#8211; Devices can send and receive simultaneously (at\u00a0the same time)<\/span><\/li>\n<\/ul>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Modes Of Operation For UART Devices\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The UART serial communication bus can only have 2 devices communicating with each other in one of the 3 modes shown in the previous sub-section. Which necessarily means, each device can either be a transmitter or a receiver during the data transmission process.<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li><strong><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Transmitter<\/span><\/strong><\/li>\n<li><strong><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Receiver<\/span><\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You should know that the transmitter in synchronous mode must generate the serial data clock for the receiver. On the other hand, in asynchronous mode, there is no need to do so.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">And there can&#8217;t be more than 2 devices on the UART serial bus. Otherwise, there will be some issues introduced to the system which you&#8217;ll have to work around.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The proper connection for any 2 devices for UART serial communication goes as follows: the transmitter&#8217;s TX goes to the receiver&#8217;s RX and the receiver&#8217;s TX goes to the transmitter&#8217;s RX. Frankly speaking, the 2-wires are basically crossed!<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2650\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg\" alt=\"UART RX-TX Connection\" width=\"500\" height=\"232\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg 607w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-300x139.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0UART Data Packet\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The data being transmitted\/received in UART serial communication is organized into specific blocks called <strong>Packets<\/strong>.\u00a0In the idle state, UART lines are pulled high. This allows the receiver side to recognize that there is a transmitter device on the other end which is connected to the serial bus. UART Packets usually start with &#8220;<strong>Start Bit<\/strong>&#8221; which is a logic low and is used to signal the receiver that there is a new coming packet. The structure of a typical UART Data Packet is shown in the figure down below.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2649\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Data-Packets.png\" alt=\"UART Data Packets\" width=\"669\" height=\"255\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Data-Packets.png 669w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Data-Packets-300x114.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>Start Bit<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The UART data transmission line (TX) is normally held High (1) logic-level when there is no data transmission on the line. To start data transfer, the transmitting UART pulls-down the transmission line from high to low for one clock cycle. When the receiving UART module detects the high to low voltage transition, it begins reading the incoming bits of the entire data frame at the same frequency of the specified baud rate.<\/span><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>Data Frame<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">These are the actual data bits being transmitted from transmitter to receiver. The length of the data frame can be anywhere between 5 and 9 (9-Bits if parity is not used and 8-Bits if parity is used). In general settings, the LSB is the first bit to be shifted-out, transmitted, (unless otherwise specified).<\/span><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>Parity Bit<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Parity bit allows the receiver to check the correctness of the received data. Parity is a low\u2013level error checking mechanism and has two different options to choose from: Even Parity and Odd Parity.\u00a0After the receiver UART reads the data frame, it counts the number of bits with a value of 1 and checks if the total value is an even or odd number.\u00a0If the parity bit is a 0 (<strong>even parity<\/strong>), the number of bits with a value of 1 in the data frame should sum-up to an even number. If the parity bit is a 1 (<strong>odd parity<\/strong>), the number of bits with a value of 0 in the data frame should sum-up to an odd number. When\u00a0the parity bit matches the data, the receiver UART knows that the transmission was a success. Frankly speaking, the parity bit is optional and it&#8217;s actually not widely used.<\/span><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>Stop Bit<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">As the name suggests, this bit is used to signal the end of the data packet being sent. The sending UART drives the data transmission line from low to high for at least two-bit durations but most often only one bit is used. The data line is held high back to the idle state and ready for any future transmission.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Baud Rate\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The <strong>Baud Rate<\/strong> specifies how fast the data is sent over the bus and it is specified in bits-per-second or <strong>bps<\/strong>. You can actually choose any speed for the baud rate. However, there are some specific values that are known to be the industry standards. The most common and widely-used standardized value is\u00a0<strong>9600.<\/strong>\u00a0Other standard baud rates include: 1200, 2400, 4800, 19200, 38400, 57600 and 115200. Obviously, baud rates are always <strong>multiples of 300<\/strong>. Baud rates higher than 115200(bps) can be used with an additional probability of having, at best, missing data packets. The rule of thumb in UART communication is, both the transmitter and the Receiver UART must agree on the exact same Baud Rate for a successful data transmission.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Digital Block Diagram (UART Transmitter)\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is the block diagram for a generic UART transmitter module as found in PIC16F87XA devices&#8217; datasheet (fig-10.1, page-115).<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2620\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram.png\" alt=\"UART Transmitter Block Diagram\" width=\"600\" height=\"269\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram.png 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram-300x134.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><span style=\"color: #000000;\"><strong>A Brief Description For UART Transmission Process<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In UART transmitter mode, a byte of data is written to the 8-Bit <strong>TXREG<\/strong> register. Then it&#8217;s latched to the <strong>TSR<\/strong> transmitter shift register combined with the 9th bit (if used). The data is shifted out through the <strong>TX<\/strong>\/<strong>RC6<\/strong> physical pin at a rate specified by the <strong>BRG<\/strong> baud rate generator. The baud rate is pre-determined and pre-configured by us (the programmers) in order to match the design specifications. Setting the baud rate to be generated is as easy as writing a byte-value to the <strong>SPBRG<\/strong> register. The value is precisely calculated using a specific formula as we&#8217;ll discuss hereafter. All in all, the transmission process is as simple as you might be thinking right now.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Digital Block Diagram (UART Receiver)\u00a0<\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is the block diagram for a generic UART transmitter module as found in PIC16F87XA devices&#8217; datasheet (fig-10.4, page-117).<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2621\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram.png\" alt=\"UART Receiver Diagram\" width=\"600\" height=\"342\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram.png 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram-300x171.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><span style=\"color: #000000;\"><strong>A Brief Description For UART Receiving Process<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The data is received on the <strong>RX<\/strong>\/<strong>C7<\/strong>\u00a0physical pin and drives the data recovery block. The data recovery block is\u00a0actually a high-speed shifter, operating at x16 times the baud rate. Whereas the main receiver serial shifter operates at the bit rate or at F<sub>OSC<\/sub>.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Once the Asynchronous mode is selected, reception is enabled by setting bit <strong>CREN<\/strong>. The heart of the receiver is the Receive Serial Shift Register <strong>RSR<\/strong>. After sampling the Stop bit, the received data frame in the RSR is transferred to the <strong>RCREG<\/strong> register (if it is empty). If the transfer is complete, the flag bit, <strong>RCIF<\/strong>, is set. The actual interrupt can be enabled\/disabled by setting\/clearing the enable bit, <strong>RCIE<\/strong>. Flag bit RCIF is a read-only bit which is cleared by the hardware. It is cleared when the RCREG register has been read and is empty. The RCREG is a double-buffered register which means it&#8217;s a two-level deep FIFO.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Physical Layer Standards\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There are actually many different standards that utilize\u00a0the same UART protocol previously described. Such as: TTL level UART, RS-232, RS-422, RS-485, etc. We&#8217;ll only discuss both the <strong>TTL-level<\/strong> UART and <strong>RS-232<\/strong> in this sub-section of our tutorial. However, we are more interested in the TTL-Level supported by the microcontrollers which we&#8217;re aiming to (program) drive hereafter in this tutorial.<\/span><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>TTL-Level UART<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The majority of microcontroller chips with UART serial communication modules do use TTL (Transistor-transistor Logic) level UART. Actually, it&#8217;s the simplest form of UART. Both logic-1 and logic-0 are represented by 5V and 0V respectively.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 33.3333%; background-color: #39a8ed; text-align: center;\"><span style=\"color: #ffffff;\"><strong>Logic Level<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Logic-1 (High)<\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Logic-0 (Low)<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 33.3333%; background-color: #1c1b1b; text-align: center;\"><span style=\"color: #ffffff;\"><strong>Voltage<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">5v<\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">0v<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The TTL-level UART is more likely to be used in the communications between MCUs, modules, sensors,\u00a0and ICs. It requires only 2-wires for the full-duplex communication and it&#8217;s connected as shown in the following diagram.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2650\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg\" alt=\"UART RX-TX Connection\" width=\"500\" height=\"232\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg 607w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-300x139.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p><span style=\"color: #333333; font-size: 14pt;\"><strong>RS-232 UART<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting between a Data Terminal Equipment (DTE) and a Data Communication Equipment (DCE). It is commonly used in computers&#8217; old-school serial ports. The major differences between TTL level UART and RS-232 include the voltage-level. The digital signals in RS-232 are \u00b13 to \u2013 \u00b115V, and signals near 0V aren&#8217;t detected anyway.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 33.3333%; background-color: #39a8ed; text-align: center;\"><span style=\"color: #ffffff;\"><strong>Logic Level<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Logic-1 (High)<\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Logic-0 (Low)<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 33.3333%; background-color: #1c1b1b; text-align: center;\"><span style=\"color: #ffffff;\"><strong>Voltage<\/strong><\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">+3 to +15v<\/span><\/td>\n<td style=\"width: 33.3333%; background-color: #ffffff; text-align: center;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">-3 to -15v<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2624\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS232-UART-Data-Frames.png\" alt=\"RS232 UART Data Frames\" width=\"500\" height=\"335\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS232-UART-Data-Frames.png 722w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS232-UART-Data-Frames-300x201.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The RS-232 also has a few more pins than the TTL-level UARTs, specifically designed for the communication between PCs and Modems in the past. The pinout of the DB-9 and their functions are shown down below.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2625\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232_DE-9_Connector_Pinouts.png\" alt=\"RS-232 UART DE9 Connectors Pinout\" width=\"650\" height=\"230\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232_DE-9_Connector_Pinouts.png 1010w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232_DE-9_Connector_Pinouts-300x106.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232_DE-9_Connector_Pinouts-768x272.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2626\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232-DE9-Pinout-Functions-Table.png\" alt=\"RS-232 DE9 Pinout Functions Table\" width=\"400\" height=\"245\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232-DE9-Pinout-Functions-Table.png 506w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/RS-232-DE9-Pinout-Functions-Table-300x184.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We&#8217;re more interested in the standard TTL-Level UART modules available in our microcontroller, but you should also know the basics of RS-232 standards. Maybe in the future, I&#8217;ll consider creating a dedicated tutorial for this. For now, let&#8217;s continue our\u00a0introduction to UARTs.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Advantages Of UART Serial Communication\u00a0<\/span><\/h4>\n<ul>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Only two wires are required for a full-duplex data transmission (excluding the power lines).<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The structure of the\u00a0data packet can be changed\u00a0with coordination between both ends.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">No need for a serial clock signal (in asynchronous mode).<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Parity bit provides a hardware-level error detection.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Well-documented and widely-used protocol.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Relatively easy to set-up and run.<\/span><\/li>\n<\/ul>\n<h4 style=\"text-align: left;\"><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Disadvantages Of UART Serial Communication\u00a0<\/span><\/h4>\n<ul>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Speed for data transfer is less compared to parallel communication or even USARTs.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The baud rates of each UART must be the same within a limited small error margin.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The size of the data frame is limited to a maximum value of 9-Bits.<\/span><\/li>\n<li style=\"text-align: left;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Doesn\u2019t support multiple slaves or multiple masters capability.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0USART Module In PIC MCUs\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Description\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The Universal Synchronous Asynchronous Receiver Transmitter (USART) module is one of the two serial I\/O modules. (USART is also known as a Serial Communications Interface or SCI.) The USART can be configured as a full-duplex asynchronous system that can communicate with peripheral devices, such as CRT terminals and personal computers, or it can be configured as a half-duplex synchronous system that can communicate with sensors, modules, etc.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The USART module can be configured to operate in one of the following modes:<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Asynchronous (full-duplex)<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Synchronous \u2013 Master (half-duplex)<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Synchronous \u2013 Slave (half-duplex)<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Baud Rate Generator (BRG)\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The BRG supports both the Asynchronous and Synchronous modes of the USART. It is a dedicated 8-bit baud rate generator. The SPBRG register controls the period of a free running 8-bit timer. In Asynchronous mode, bit BRGH also controls the baud rate. In Synchronous mode, bit BRGH is ignored. Down below is the formula for computation of the baud rate for different USART modes which only apply in Master mode (internal clock).<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2636\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Baud-Rate-Generator-Equation-1.png\" alt=\"Baud Rate Generator Equation\" width=\"752\" height=\"113\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Baud-Rate-Generator-Equation-1.png 989w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Baud-Rate-Generator-Equation-1-300x45.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Baud-Rate-Generator-Equation-1-768x116.png 768w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Given the desired baud rate and F<sub>OSC<\/sub>, the nearest integer value for the SPBRG register can be calculated using the formula previously shown. From this, the error in baud rate can be determined. It may be advantageous to use the high baud rate (BRGH = 1) even for slower baud clocks. This is because the FOSC\/(16 (X + 1)) equation can reduce the baud rate error in some cases.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You can refer to table 10-4 in the datasheet for some example values for Baud Rate VS multiple values for Fosc (in Asynchronous mode, BRGH=1). Here is a snapshot for this table.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2637\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Baud-Rate-Table.png\" alt=\"UART Baud Rate Table\" width=\"647\" height=\"559\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Baud-Rate-Table.png 647w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Baud-Rate-Table-300x259.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Note<\/strong> That: Writing a new value to the SPBRG register causes the BRG timer to be reset (or cleared). This ensures the BRG does not wait for a timer overflow before outputting the new baud rate.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0UART SFR Registers\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Here is a list of the SFRs (special function registers) involved in the UART data transmission\/reception.<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">TXSTA &#8211; Transmitter status and control register<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">RCSTA &#8211; Receiver status and control register<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">SPBRG &#8211; Baud rate generator register<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">TXREG &#8211; Transmitter Buffer Register<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">RCREG &#8211; Receiver Buffer Register<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0How To Configure The UART Transmitter?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In this section, we&#8217;ll be focusing on the process of configuring a PIC microcontroller to work as a <strong>UART Asynchronous Transmitter<\/strong>. Enough of general information about UART! let&#8217;s get our microcontroller to work as a serial UART transmitter!<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0The Mechanics Of UART Transmission\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> The UART transmits and receives the LSB first. The transmitter and receiver are functionally independent but use the same data format and baud rate. The baud rate generator produces a clock, either x16 or x64 of the bit shift rate, depending on bit BRGH. Parity is not supported by the hardware but can be implemented in software (and stored as the ninth data bit). Asynchronous mode is stopped during Sleep. Asynchronous mode is selected by clearing bit SYNC. The UART Asynchronous module consists of the following important elements:<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Baud Rate Generator<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Sampling Circuit<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Asynchronous Transmitter<\/span><br \/>\n<span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">\u2022 Asynchronous Receiver<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2620\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram.png\" alt=\"UART Transmitter Block Diagram\" width=\"600\" height=\"269\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram.png 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Transmitter-Block-Diagram-300x134.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> The heart of the transmitter is the Transmit (Serial) Shift Register <strong>TSR<\/strong>. The shift register obtains its data from the Read\/Write Transmit Buffer, <strong>TXREG<\/strong>. The TXREG register is loaded with data in software. The TSR register is not loaded until the Stop bit has been transmitted from the previous loading process. As soon as the Stop bit is transmitted, the TSR is loaded with new data frame from the TXREG register (if available). Once the TXREG register transfers the data to the TSR register (occurs in one T<sub>CY<\/sub>), the TXREG register is empty and flag bit, <strong>TXIF<\/strong>, is set. This interrupt can be enabled\/disabled by setting\/clearing enable bit, <strong>TXIE<\/strong>. <\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Flag bit <strong>TXIF<\/strong> will be set regardless of the state of enable bit TXIE and cannot be cleared in software. It will reset only when new data is loaded into the TXREG register. While flag bit TXIF indicates the status of the TXREG register, another bit, <strong>TRMT<\/strong>, shows the status of the TSR register. Status bit TRMT is a read-only bit which is set when the TSR register is empty. No interrupt logic is tied to this bit so the programmer has to <strong>poll<\/strong> this bit in software to determine if the TSR register is empty or not.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Transmission is enabled by setting enable bit, <strong>TXEN<\/strong>. The actual transmission will not occur until the TXREG register has been loaded with data and the Baud Rate Generator (BRG) has produced a shift clock as in the following diagram.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2630\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Asynchronous-Master-Transmission.png\" alt=\"UART Asynchronous Master Transmission\" width=\"750\" height=\"295\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Asynchronous-Master-Transmission.png 953w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Asynchronous-Master-Transmission-300x118.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Asynchronous-Master-Transmission-768x302.png 768w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The transmission can also be started by first loading the TXREG register and then setting enable bit TXEN. Normally, when a transmission is first started, the TSR register is empty. At that point, transfer to the TXREG register will result in an immediate transfer to TSR, resulting in an empty TXREG. A back-to-back transfer is thus possible, which means we can send consecutive data frames continuously as shown in the following diagram.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2631\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Asynchronous-Master-Transmission-Back-To-Back.png\" alt=\"UART Tutorial | Asynchronous Master Transmission Back-To-Back\" width=\"750\" height=\"259\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Asynchronous-Master-Transmission-Back-To-Back.png 949w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Asynchronous-Master-Transmission-Back-To-Back-300x104.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/Asynchronous-Master-Transmission-Back-To-Back-768x265.png 768w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Clearing the enable bit TXEN during a transmission will cause the transmission to be aborted and will reset the transmitter. As a result, the TX\/RC6 pin will revert to High-impedance.<\/span><\/p>\n<blockquote><p>If it feels overwhelming, stay cool. It really does! Each of us has been at the same point and yes! it felt overwhelming. Take the time to understand each step of the mechanics.<\/p><\/blockquote>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Down below are the exact brief steps that you should follow to configure your UART Asynchronous Transmitter, even without looking up any diagram in the datasheet. However, you should consider doing so sooner or later at some point.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Steps For Configuring The UART Transmitter\u00a0<\/span><\/h4>\n<ol>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Initialize the SPBRG register for the appropriate baud rate. If a high-speed baud rate is desired, set the bit BRGH.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"> Bits TRISC&lt;7:6&gt; have to be set in order to configure pins RC6\/TX\/CK and RC7\/RX\/DT as the Universal Synchronous Asynchronous Receiver Transmitter.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If interrupts are desired, then set the enable bit TXIE.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If the 9-bit transmission is desired, then set transmit bit TX9.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Enable the transmission by setting bit TXEN, which will also set bit TXIF.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If the 9-bit transmission is selected, the 9<sup>th<\/sup> bit should be loaded in bit TX9D.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Load data to the TXREG register (this step automatically starts the transmission).<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are set.<\/span><\/li>\n<\/ol>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Implementing UART Transmitter Driver Firmware\u00a0<\/span><\/h4>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step1\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">write to the SPBRG register in order to set the desired baud rate<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Using the High-Speed baud rate even for low speeds is advantageous due to the fact that the error percentage will be significantly smaller. Doing so requires setting the BRGH bit.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Let&#8217;s say our system is running @ 4MHz Fosc and it&#8217;s desired to generate 9600bps baud rate. Using the baud rate formula shown earlier.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2639\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Baud-Rate-Ex-Equation.png\" alt=\"UART Baud Rate Ex Equation\" width=\"210\" height=\"46\" \/><\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Solving for x (the value to be written to the SPBRG register), it turns out to be<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>x = 25<\/strong> (approximately)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Therefore, the c-code for this step is as follows<\/span><\/p>\n<pre class=\"lang:c decode:true\">BRGH = 1;  \/\/ Set For High-Speed Baud Rate\r\nSPBRG = 25; \/\/ Set The Baud Rate To Be 9600 bps<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step2\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Enable The Asynchronous Serial Port<\/span><\/p>\n<pre class=\"\">\/\/ Enable The Ascynchronous Serial Port\r\nSYNC = 0;\r\nSPEN = 1;<\/pre>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We won&#8217;t be using interrupts for UART transmitter in this tutorial, so I won&#8217;t be going through any interrupt activation steps.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step3\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Set The RX-TX Pins Data Direction<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/\/ Set The RX-TX Pins to be in UART mode (not io)\r\nTRISC6 = 1;\u00a0 \/\/ As stated in the datasheet\r\nTRISC7 = 1;\u00a0 \/\/ As stated in the datasheet<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step4\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Enable The UART Transmission Process<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This is done by setting the TXEN bit.<\/span><\/p>\n<pre class=\"lang:c decode:true\">TXEN = 1; \/\/ Enable UART Transmission<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step5\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Load The Data To TXREG<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Load the data to be transmitted to the TXREG register.<\/span><\/p>\n<pre class=\"lang:c decode:true \">TXREG = data;\u00a0 \/\/ Load the data to the transmitter buffer<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Mission Complete! Transmission is done!<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0UART Transmitter Full Code Listing\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You can easily use those 3-functions within your code. Calling the UART_TX_Init() procedure will configure the UART transmitter for you. Then, calling the UART_Write(data) will shift-out the <strong>data<\/strong> frame from your transmitter to the receiving end.<\/span><\/p>\n<pre class=\"lang:c decode:true\">void UART_TX_Init(void)\r\n{\r\n  BRGH = 1; \/\/ Set For High-Speed Baud Rate\r\n  SPBRG = 25; \/\/ Set The Baud Rate To Be 9600 bps\r\n  \/\/--[ Enable The Ascynchronous Serial Port ]--\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/--[ Set The RX-TX Pins to be in UART mode (not io) ]--\r\n  TRISC6 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TRISC7 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TXEN = 1; \/\/ Enable UART Transmission\r\n}\r\n\r\nvoid UART_Write(uint8_t data)\r\n{\r\n  while(!TRMT);\r\n  TXREG = data;\r\n}\r\n\r\nuint8_t UART_TX_Empty()\r\n{\r\n  \/\/ Check the output (transmitter) buffer emptiness\r\n  return TRMT;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0How To Configure The UART Receiver?\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">In this section, we&#8217;ll be discussing the process of configuring a PIC microcontroller to work as a UART receiver. Let&#8217;s get our microcontroller to work as a serial UART receiver.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0The Mechanics Of UART Reception\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The data is received on the <strong>RX<\/strong>\/<strong>C7<\/strong>\u00a0physical pin and drives the data recovery block. The data recovery block is\u00a0actually a high-speed shifter, operating at x16 times the baud rate. Whereas the main receiver serial shifter operates at the bit rate or at F<sub>OSC<\/sub>.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2621\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram.png\" alt=\"UART Receiver Diagram\" width=\"650\" height=\"371\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram.png 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Receiver-Diagram-300x171.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Once the Asynchronous mode is selected, reception is enabled by setting bit <strong>CREN<\/strong>. The heart of the receiver is the Receive Serial Shift Register <strong>RSR<\/strong>. After sampling the Stop bit, the received data frame in the RSR is transferred to the <strong>RCREG<\/strong> register (if it is empty). If the transfer is complete, the flag bit, <strong>RCIF<\/strong>, is set. The actual interrupt can be enabled\/disabled by setting\/clearing the enable bit, <strong>RCIE<\/strong>. Flag bit RCIF is a read-only bit which is cleared by the hardware. It is cleared when the RCREG register has been read and is empty. The RCREG is a double-buffered register which means it&#8217;s a two-level deep FIFO.<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Down below are the exact brief steps that you should follow to configure your UART Asynchronous Receiver, even without looking up any diagram in the datasheet. However, you should consider doing so sooner or later at some point.<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Steps For Configuring The UART Receiver\u00a0<\/span><\/h4>\n<ol>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Initialize the SPBRG register for the appropriate baud rate. If a high-speed baud rate is desired, set bit BRGH.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Bits TRISC&lt;7:6&gt; have to be set in order to configure pins RC6\/TX\/CK and RC7\/RX\/DT as the Universal Synchronous Asynchronous Receiver Transmitter.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If interrupts are desired, then set enable bit\u00a0RCIE.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If a 9-Bit reception is desired, then set bit RX9.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Enable the reception by setting bit CREN.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Flag bit RCIF will be set when reception is complete and an interrupt will be generated if enable bit RCIE is set.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Read the RCSTA register to get the ninth bit (if enabled) and determine if any error occurred during reception.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Read the 8-bit received data by reading the RCREG register.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If any error occurred, clear the error by clearing enable bit CREN.<\/span><\/li>\n<li style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are set.<\/span><\/li>\n<\/ol>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0Implementing UART Receiver Driver Firmware\u00a0<\/span><\/h4>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step1\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">write to the SPBRG register in order to set the desired baud rate<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The same as the UART transmitter which runs @ 4MHz and the desired baud rate must be 9600 as well. Hence, the c-code for this step is as follows<\/span><\/p>\n<pre class=\"lang:c decode:true\">BRGH = 1;  \/\/ Set For High-Speed Baud Rate\r\nSPBRG = 25; \/\/ Set The Baud Rate To Be 9600 bps<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step2\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Enable the asynchronous serial port<\/span><\/p>\n<pre class=\"\">\/\/ Enable The Ascynchronous Serial Port\r\nSYNC = 0;\r\nSPEN = 1;<\/pre>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We won&#8217;t be using interrupts for UART transmitter in this tutorial, so I won&#8217;t be going through any interrupt activation steps.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step3\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Set The RX-TX Pins Data Direction<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/\/ Set The RX-TX Pins to be in UART mode (not io)\r\nTRISC6 = 1;\u00a0 \/\/ As stated in the datasheet\r\nTRISC7 = 1;\u00a0 \/\/ As stated in the datasheet<\/pre>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We&#8217;ll be using UART Data Reception interrupt to signal our microcontroller each time a data frame arrives at the UART receiving buffer. The ISR routine should read out the data from the buffer and store it in the respective (safe) place. This action will automatically set the receiver to be ready for a consecutive data reception. However, we (the programmers) should not forget clearing the interrupt flag bit in software!<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step4\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Setup UART Receiving Interrupt<\/span><\/p>\n<pre class=\"lang:c decode:true \">RCIE = 1;\u00a0 \/\/ UART Receving Interrupt Enable Bit\r\nPEIE = 1;\u00a0 \/\/ Peripherals Interrupt Enable Bit\r\nGIE = 1;\u00a0  \/\/ Global Interrupt Enable Bit<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step5\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Enable UART Data Reception<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This is done by setting the CREN bit.<\/span><\/p>\n<pre class=\"lang:c decode:true \">CREN = 1;\u00a0 \/\/ Enable Data Reception<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffcc00;\"><strong>\u00a0Step6\u00a0\u00a0<\/strong><\/span>\u00a0<span style=\"color: #000000;\">Read The Received Data Upon Reception<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This is actually done within the ISR function by reading the RCREG (the receiver buffer register).<\/span><\/p>\n<pre class=\"lang:c decode:true \">void interrupt ISR (void)\r\n{\r\n  if (RCIF == 1)\r\n  {\r\n    Destination = RCREG;  \/\/ Read The Received Data Buffer\r\n    RCIF = 0;             \/\/ Clear The Flag\r\n  }\r\n}<\/pre>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">However, you can still poll the RXREG buffer register within your main code. Almost in every situation, you&#8217;ll be missing most of the received data using this way! But, for limited applications where data received via UART is coming at a very low rate (maybe 10-Bytes\/second or whatever), then you might be using the following simple UART_Read() function and calling it from your main routine.<\/span><\/p>\n<pre class=\"\">uint8_t UART_Read()\r\n{\r\n  while(!RCIF);\r\n  return RCREG;\r\n}<\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Mission Complete! Reception Accomplished!<\/span><\/p>\n<h4><span style=\"color: #ffffff; background-color: #333333;\">\u00a0UART Receiver Full Code Listing\u00a0<\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You can easily use those functions within your code. Calling the UART_RX_Init() procedure will configure the UART receiver for you. Then, calling the UART_Read() will return the <strong>data<\/strong> frame from your receiver buffer register.<\/span><\/p>\n<pre class=\"lang:c decode:true\">void UART_RX_Init()\r\n{\r\n  BRGH = 1;    \/\/ Set For High-Speed Baud Rate\r\n  SPBRG = 25;  \/\/ Set The Baud Rate To Be 9600 bps\r\n  \/\/ Enable The Ascynchronous Serial Port\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/ Set The RX-TX Pins to be in UART mode (not io)\r\n  TRISC6 = 1;  \/\/ As stated in the datasheet\r\n  TRISC7 = 1;  \/\/ As stated in the datasheet\r\n  \/\/--[ Enable UART Receiving Interrupts ]--\r\n  RCIE = 1;    \/\/ UART Receving Interrupt Enable Bit\r\n  PEIE = 1;    \/\/ Peripherals Interrupt Enable Bit\r\n  GIE = 1;     \/\/ Global Interrupt Enable Bit\r\n  \/\/------------------\r\n  CREN = 1;    \/\/ Enable Data Continous Reception\r\n\r\n}\r\n\r\n\r\n\/\/ You can call this from your main routine\r\nuint8_t UART_Read()\r\n{\r\n  while(!RCIF);\r\n  return RCREG;\r\n}\r\n\r\n\/\/ You can include this handler within your ISR\r\nvoid interrupt ISR (void)\r\n{\r\n  if (RCIF == 1)\r\n  {\r\n    Destination = RCREG;  \/\/ Read The Received Data Buffer\r\n    RCIF = 0;             \/\/ Clear The Flag\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0\u00b5C-To-\u00b5C UART Communication &#8211; LAB\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<table style=\"background-color: #ffffff;\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Name<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">UART Basic Communication<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Number<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">16<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Level<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: left; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Intermediate<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #ffffff; text-align: left; width: 20%;\"><span style=\"font-family: arial, helvetica, sans-serif;\"><strong><span style=\"color: #000000;\">Lab Objectives<\/span><\/strong><\/span><\/td>\n<td style=\"background-color: #ffffff; text-align: justify; width: 80%;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">Learn how to create a serial communication interface between 2 microcontrollers using UART Protocol. We&#8217;ll send specific data frames from a transmitter MCU and receive it back with another one then double-check the data correctness and validate the communication process.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a01. Coding\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Open<\/strong> the MPLAB IDE and create a couple of new projects name them &#8220;UART_TX&#8221; and &#8220;UART_RX&#8221; for master and slave MCUs respectively. If you have some issues doing so, you can always refer to the previous tutorial using the link below.<\/span><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/creating-new-project-with-mplab\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1991\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject.jpg\" alt=\"Create New Project With MPLAB IDE\" width=\"375\" height=\"107\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject.jpg 700w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/link_frame_newproject-300x86.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\"><strong>Set<\/strong> the configuration bits to match the generic setting which we&#8217;ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.<\/span><\/p>\n<p><a href=\"https:\/\/deepbluembedded.com\/configuration-bits-fuses-for-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1581\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config.jpg\" alt=\"\" width=\"375\" height=\"107\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config.jpg 700w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/tutorial_link_frame_config-300x86.jpg 300w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Now, open the <strong>main.c<\/strong> file of each project and let&#8217;s start developing the firmware for our project.<\/span><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 14pt;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">1. UART Transmitter (Master)<\/span><\/span><\/strong><\/p>\n<p>[ <strong>Transmitter Firmware<\/strong> ]<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Our task is to take 3 inputs from the user using 3 push buttons to do the following tasks:<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Button1: increment the data value<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Button2: decrement the data value<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Button3: send the current data frame via UART @ 9600bps<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">We&#8217;ll use PORTD to display the current data value for the user. And 3 input pins of PORTB. Obviously, we&#8217;ll also configure our microcontroller to operate as a transmitter as we&#8217;ve seen earlier in this tutorial. Here is the full code listing for the transmitter MCU chip.<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/*\r\n* LAB Number: 15\r\n* LAB Name: UART_Basic_Communication (Master Code)\r\n* Author: Khaled Magdy\r\n* For More Information Visit My Website @ DeepBlueMbedded.com\r\n*\r\n*\/\r\n\r\n#include &lt;xc.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include \"config.h\"\r\n#define _XTAL_FREQ 4000000\r\n\/\/--------------------------------\r\n\/\/ IO Pins Defines (Mappings)\r\n#define UP RB0\r\n#define Down RB1\r\n#define Send RB2\r\n\/\/--------------------------------\r\n\/\/ Functions Declarations\r\nvoid UART_TX_Init(void);\r\nvoid UART_Write(uint8_t);\r\n\/\/--------------------------------\r\n\/\/ Main Routine\r\nvoid main(void) \r\n{\r\n  \/\/--[ Peripherals &amp; IO Configurations ]--\r\n  UART_TX_Init(); \/\/ Initialize The UART in Master Mode @ 9600bps\r\n  uint8_t Data = 0; \/\/ The Data Byte\r\n  TRISB = 0x07; \/\/ RB0, RB1 &amp; RB2 Are Input Pins (Push Buttons)\r\n  TRISD = 0x00; \/\/ Output Port (4-Pins)\r\n  PORTD = 0x00; \/\/ Initially OFF\r\n  \/\/---------------------------\r\n  while(1)\r\n  {\r\n    if (UP) \/\/ Increment The Data Value\r\n    {\r\n      Data++;\r\n      __delay_ms(250);\r\n    } \r\n    if (Down) \/\/ Decrement The Data Value\r\n    {\r\n      Data--;\r\n      __delay_ms(250);\r\n    }\r\n    if (Send) \/\/ Send The Current Data Value Via UART\r\n    {\r\n      UART_Write(Data);\r\n      __delay_ms(250);\r\n    }\r\n    PORTD = Data; \/\/ Display The Current Data Value @ PORTD\r\n  } \r\n  return;\r\n}\r\n\/\/--------------------------------\r\n\/\/ Functions Definitions\r\n\r\nvoid UART_TX_Init(void)\r\n{\r\n  BRGH = 1;   \/\/ Set For High-Speed Baud Rate\r\n  SPBRG = 25; \/\/ Set The Baud Rate To Be 9600 bps\r\n  \/\/--[ Enable The Ascynchronous Serial Port ]--\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/--[ Set The RX-TX Pins to be in UART mode (not io) ]--\r\n  TRISC6 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TRISC7 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TXEN = 1; \/\/ Enable UART Transmission\r\n}\r\n\r\nvoid UART_Write(uint8_t data)\r\n{\r\n  while(!TRMT);\r\n  TXREG = data;\r\n}<\/pre>\n<p><strong><span style=\"font-size: 14pt;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">2. UART Receiver (Slave)<\/span><\/span><\/strong><\/p>\n<p>[ <strong>Receiver Firmware<\/strong> ]<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Our task is to set up the UART module to operate in slave mode @ 9600bps and enable the UART data reception interrupt. Within the ISR handler, we should read the received byte of data and latch it out to PORTB with some LEDs for data inspection. And here is the full code listing for the receiver MCU chip.<\/span><\/p>\n<pre class=\"lang:c decode:true\">\/*\r\n* LAB Number: 15\r\n* LAB Name: UART_Basic_Communication (Slave Code)\r\n* Author: Khaled Magdy\r\n* For More Information Visit My Website @ DeepBlueMbedded.com\r\n*\r\n*\/\r\n\r\n#include &lt;xc.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include \"config.h\"\r\n#define _XTAL_FREQ 4000000\r\n\/\/--------------------------------\r\n\/\/ Functions Declarations\r\nvoid UART_RX_Init(void);\r\nuint8_t UART_Read(void);\r\n\/\/ Globals\r\nuint8_t UART_Buffer = 0;\r\n\/\/--------------------------------\r\n\/\/ Main Routine\r\nvoid main(void)\r\n{\r\n  \/\/--[ Peripherals &amp; IO Configurations ]--\r\n  UART_RX_Init(); \/\/ Initialize The UART in Master Mode @ 9600bps\r\n  TRISB = 0x00; \/\/ Output Port (4-Pins)\r\n  PORTB = 0x00; \/\/ Initially OFF\r\n  \/\/---------------------------\r\n  while(1) \r\n  {\r\n    \/\/ Stay Idle, Everything is handled in the ISR !\r\n  }\r\n  return;\r\n}\r\n\/\/--------------------------------\r\n\/\/ Functions Definitions\r\n\r\nvoid UART_RX_Init()\r\n{\r\n  BRGH = 1; \/\/ Set For High-Speed Baud Rate\r\n  SPBRG = 25; \/\/ Set The Baud Rate To Be 9600 bps\r\n  \/\/ Enable The Ascynchronous Serial Port\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/ Set The RX-TX Pins to be in UART mode (not io)\r\n  TRISC6 = 1; \/\/ As stated in the datasheet\r\n  TRISC7 = 1; \/\/ As stated in the datasheet\r\n  \/\/--[ Enable UART Receiving Interrupts ]--\r\n  RCIE = 1; \/\/ UART Receving Interrupt Enable Bit\r\n  PEIE = 1; \/\/ Peripherals Interrupt Enable Bit\r\n  GIE = 1; \/\/ Global Interrupt Enable Bit\r\n  \/\/------------------\r\n  CREN = 1; \/\/ Enable Data Continous Reception\r\n}\r\n\r\nvoid interrupt ISR (void)\r\n{\r\n  if (RCIF == 1)\r\n  {\r\n    UART_Buffer = RCREG; \/\/ Read The Received Data Buffer\r\n    PORTB = UART_Buffer; \/\/ Display The Received Data On LEDs\r\n    RCIF = 0; \/\/ Clear The Flag\r\n  }\r\n}<\/pre>\n<p><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">And We&#8217;re Done With The Firmware! Cross Your Fingers! And Hit The Compile Button!<\/span><\/p>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a02. Simulation\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Create the simulation project on your simulator and connect your circuitry as shown in the schematic diagram down below. Add the hex code to the respective microcontroller chip. The receiver code to the receiver chip and so. Click the run button and test out everything!<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2693\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-With-PIC-Microcontroller-Tutorial-LAB-Schematic.jpg\" alt=\"UART Serial Communication With PIC Microcontroller Tutorial - LAB Schematic\" width=\"1171\" height=\"516\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-With-PIC-Microcontroller-Tutorial-LAB-Schematic.jpg 1171w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-With-PIC-Microcontroller-Tutorial-LAB-Schematic-300x132.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-With-PIC-Microcontroller-Tutorial-LAB-Schematic-768x338.jpg 768w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-With-PIC-Microcontroller-Tutorial-LAB-Schematic-1024x451.jpg 1024w\" sizes=\"auto, (max-width: 1171px) 100vw, 1171px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-size: 14pt;\"><strong>The Running Simulation Test !<\/strong><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2695\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-LAB-Simulation-1024x502.gif\" alt=\"UART LAB Simulation\" width=\"1024\" height=\"502\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-LAB-Simulation-1024x502.gif 1024w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-LAB-Simulation-300x147.gif 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-LAB-Simulation-768x376.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a03. Prototyping\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">If you&#8217;ve correctly followed the schematics shown above in the simulation section, you should get your code up and running on a real breadboard flawlessly! However, you should be careful with grounding and RX-TX connections and make sure that you&#8217;ve (flashed) uploaded the right code to each chip (Master &amp; Slave).<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">And here is the final running prototype for this project in case you&#8217;re curious.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 100%;\"><a href=\"https:\/\/www.youtube.com\/watch?v=0BuLK6uqJdI\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2053\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube.jpg\" alt=\"Play Video On YouTube\" width=\"560\" height=\"70\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube.jpg 560w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/07\/Play-Video-On-YouTube-300x38.jpg 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; text-align: center;\"><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube-nocookie.com\/embed\/0BuLK6uqJdI\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><span style=\"color: #ffffff; background-color: #453f3f;\"><strong>\u00a0 \u00a0 \u00a0 \u00a04. Firmware Template\u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/span><\/h4>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The previous code listings for this lab despite being fully-functional. It may need a little bit of work to get it working at a different baud rate or at a different clock frequency. That&#8217;s why I&#8217;ll give you a template for more portable and easier to use firmware for both your transmitter &amp; receiver devices.<\/span><\/p>\n<p><strong>Transmitter ( Master Code )<\/strong><\/p>\n<pre class=\"lang:c decode:true \">\/*\r\n* File Name: UART Firmware Template For (Transmitter \/ Master)\r\n* Author: Khaled Magdy\r\n* For More Information Visit My Website @ DeepBlueMbedded.com\r\n*\r\n*\/\r\n\r\n#include &lt;xc.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include \"config.h\"\r\n#define _XTAL_FREQ 4000000\r\n#define Baud 9600\r\n\/\/--------------------------------\r\n\/\/ Functions Declarations\r\nvoid UART_TX_Init(void);\r\nvoid UART_Write(uint8_t);\r\nvoid UART_Write_String(char *);\r\n\/\/--------------------------------\r\n\/\/ Main Routine\r\nvoid main(void) \r\n{\r\n  \/\/--[ Peripherals &amp; IO Configurations ]--\r\n  UART_TX_Init(); \/\/ Initialize The UART in Master Mode @ 9600bps\r\n\r\n  \/\/---------------------------\r\n  while(1)  \r\n  {\r\n    \/\/ Do Whatever You Want !\r\n    \/\/ ...\r\n  } \r\n  return;\r\n}\r\n\/\/--------------------------------\r\n\/\/ Functions Definitions\r\n\r\nvoid UART_TX_Init(void)\r\n{\r\n  uint16_t x;\r\n  x = (_XTAL_FREQ - Baud*64)\/(Baud*64); \/\/ Low Baud Rate\r\n  if(x&gt;255)  \r\n  {\r\n    x = (_XTAL_FREQ - Baud*16)\/(Baud*16); \/\/ High Baud Rate\r\n    BRGH = 1; \r\n  }\r\n  if (x&lt;=255) \r\n  {\r\n    SPBRG = x; \/\/Writing SPBRG Register \r\n  }\r\n  \/\/--[ Enable The Ascynchronous Serial Port ]--\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/--[ Set The RX-TX Pins to be in UART mode (not io) ]--\r\n  TRISC6 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TRISC7 = 1;\u00a0 \/\/ As stated in the datasheet\r\n  TXEN = 1; \/\/ Enable UART Transmission \r\n}\r\n\r\nvoid UART_Write(uint8_t data)\r\n{\r\n  while(!TRMT);\r\n  TXREG = data;\r\n}\r\n\r\nvoid UART_Write_String(char *text)\r\n{\r\n  uint16_t i;\r\n  for(i=0;text[i]!='\\0';i++)\r\n    UART_Write(text[i]);\r\n}<\/pre>\n<p><strong>Receiver ( Slave Code )<\/strong><\/p>\n<pre class=\"lang:c decode:true \">\/*\r\n* File Name: UART Firmware Template For (Receiver \/ Slave)\r\n* Author: Khaled Magdy\r\n* For More Information Visit My Website @ DeepBlueMbedded.com\r\n*\r\n*\/\r\n\r\n#include &lt;xc.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include \"config.h\"\r\n#define _XTAL_FREQ 4000000\r\n#define Baud 9600\r\n\/\/--------------------------------\r\n\/\/ Functions Declarations\r\nvoid UART_RX_Init(void);\r\nuint8_t UART_Read(void);\r\nvoid UART_Read_String(uint8_t *);\r\n\/\/--------------------------------\r\n\/\/ Main Routine\r\nvoid main(void)\r\n{\r\n  \/\/--[ Peripherals &amp; IO Configurations ]--\r\n  UART_TX_Init(); \/\/ Initialize The UART in Master Mode @ 9600bps\r\n\r\n  \/\/---------------------------\r\n  while(1)\r\n  {\r\n    \/\/ Do Whatever You Want !\r\n    \/\/ ... \r\n  }\r\n  return;\r\n}\r\n\/\/--------------------------------\r\n\/\/ Functions Definitions\r\n\r\nvoid UART_RX_Init()\r\n{\r\n  uint16_t x;\r\n  x = (_XTAL_FREQ - Baud*64)\/(Baud*64); \/\/ Low Baud Rate\r\n  if(x&gt;255) \r\n  {\r\n    x = (_XTAL_FREQ - Baud*16)\/(Baud*16); \/\/ High Baud Rate\r\n    BRGH = 1;\r\n  }\r\n  if (x&lt;=255) \r\n  {\r\n    SPBRG = x; \/\/Writing SPBRG Register\r\n  }\r\n  \/\/ Enable The Ascynchronous Serial Port\r\n  SYNC = 0;\r\n  SPEN = 1;\r\n  \/\/ Set The RX-TX Pins to be in UART mode (not io)\r\n  TRISC6 = 1; \/\/ As stated in the datasheet\r\n  TRISC7 = 1; \/\/ As stated in the datasheet\r\n  \/\/--[ Enable UART Receiving Interrupts ]--\r\n  RCIE = 1; \/\/ UART Receving Interrupt Enable Bit\r\n  PEIE = 1; \/\/ Peripherals Interrupt Enable Bit\r\n  GIE = 1; \/\/ Global Interrupt Enable Bit\r\n  \/\/------------------\r\n  CREN = 1; \/\/ Enable Data Continous Reception\r\n}\r\n\r\nuint8_t UART_Read()\r\n{\r\n  while(!RCIF); \/\/ Wait Untill a Data Frame is Received\r\n  return RCREG;\r\n}\r\n\r\nvoid UART_Read_String(uint8_t *Output, uint16_t length)\r\n{\r\n  uint16_t i;\r\n  for(int i=0;i&lt;length;i++)\r\n    Output[i] = UART_Read();\r\n}\r\n\r\n\/\/ You Can Also Use The ISR If You Want To !\r\nvoid interrupt ISR (void)\r\n{\r\n  if (RCIF == 1) \r\n  {\r\n    \/\/ Read The RCREG Buffer Register...\r\n    RCIF = 0; \/\/ Clear The Flag\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h3 style=\"text-align: left;\"><span style=\"background-color: #7094ff;\"><strong><span style=\"color: #ffffff;\">\u00a0 \u00a0Pro Tips + Concluding Remarks\u00a0 \u00a0<\/span><\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 1\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">PC Interfacing With Microcontroller<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Practically, there do exist many ways to build a PC-MCU interface. However, it&#8217;s much easier to use the last one of the following list. We&#8217;ll be implementing a PC-controlled embedded system\u00a0to control some actuators in a dedicated LAB following this tutorial. For now, let&#8217;s check the interfacing capabilities (options) to get a microcontroller chip (<strong>MCU<\/strong>) to talk to a personal computer (<strong>PC<\/strong>) and vice versa.<\/span><\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 100%; background-color: #2590cf; text-align: center;\"><span style=\"color: #ffffff;\"><strong>TTL-UART To RS-232 Serial (Old Generic Interfacing)<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2673\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-To-RS-232-Generic-Interfacing-To-PC.jpg\" alt=\"UART To RS-232 Generic Interfacing To PC\" width=\"650\" height=\"182\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-To-RS-232-Generic-Interfacing-To-PC.jpg 731w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-To-RS-232-Generic-Interfacing-To-PC-300x84.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #2590cf; text-align: center;\"><span style=\"color: #ffffff;\"><strong>TTL-UART To RS-232 Serial To USB<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2674\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-RS232-Converter.jpg\" alt=\"UART PC Interfacing With USB-RS232 Converter\" width=\"826\" height=\"182\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-RS232-Converter.jpg 826w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-RS232-Converter-300x66.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-RS232-Converter-768x169.jpg 768w\" sizes=\"auto, (max-width: 826px) 100vw, 826px\" \/><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #2590cf; text-align: center;\"><span style=\"color: #ffffff;\"><strong>USB-TTL Converter Module<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 100%; background-color: #ffffff;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2675\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-TTL-Converter.jpg\" alt=\"UART PC Interfacing With USB-TTL Converter\" width=\"650\" height=\"173\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-TTL-Converter.jpg 713w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-PC-Interfacing-With-USB-TTL-Converter-300x80.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 2\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">RX-To-TX &amp; TX-To-RX<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">it&#8217;s a very common mistake to have your RX &amp; TX lines wrongly wired. So, please be careful and make sure you&#8217;ve connected them correctly. It may take you some time to find out &amp; debug, so be careful in the first place instead.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2650\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg\" alt=\"UART RX-TX Connection\" width=\"300\" height=\"139\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection.jpg 607w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-300x139.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-980x450.jpg 980w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 3\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Baud Rate Mismatching<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">if data is being transmitted at 9600bps Baud rate and being received at a 38400bps. The received data will be a total mess (garbage)! Baud rate must be matched at both ends (Transmitter &amp; Receiver). It&#8217;s a rule of thumb in UART serial communication. The maximum allowable shift in baud rates tends to be between (1-2%). So try to generate the exact same baud rate at both ends to avoid mismatching error.\u00a0<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 4\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Debugging With Virtual Terminal<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Communication channels can go crazy in almost any system. Whether it&#8217;s a wired or wireless communication. Let&#8217;s focus our investigation on the serial UART communication. Imagine a situation in which you&#8217;ve got a device A (master) communicating with device B (slave).<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Device A: is programmed to send a data frame of (0x05)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Device B: is programmed to latch the received data within the ISR and write it out to PORTB<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2664\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-1.jpg\" alt=\"UART Serial Debugging\" width=\"450\" height=\"209\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-1.jpg 607w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RX-TX-Connection-1-300x139.jpg 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Yet, device B is printing out some random garbage!<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The question is, who is the criminal to suspect? is it A? or B? or even the hardware bus on-board?<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">That&#8217;s why it&#8217;s kind of a tricky business to debug serial bus communications!<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">You just can&#8217;t decide which firmware has to be fixed. Arbitrarily suspicion &amp; random modifications to either of firmware at A or B can make it more difficult and time-consuming. What I prefer, is to use one of the following methods<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">An oscilloscope or Logic Analyzer (inspection)<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Standard virtual serial protocols debugger<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">I assume that everybody should be familiar with capturing signals on a generic CRO or a Logic analyzer. What I&#8217;d really like to point out to are the <strong>Virtual Serial Debugger<\/strong> Tools. Just a standard UART receiver\/transmitter which you can graphically configure using a GUI. Using it will make your life much more easier, but why?<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Well, if you&#8217;ve got a standard receiver that is guaranteed to correctly receive each and every data frame, Then you can tell if your UART transmitter (device A) is malfunctioning or not!<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Conversely, if you&#8217;ve got a standard transmitter that&#8217;s is guaranteed to generate and sens specific data frames at a specific baud rate flawlessly, Then you can tell if your UART receiver (Device B) is malfunctioning or not!<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2666\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-Virtual-Debugging-Tool.png\" alt=\"UART Serial Communication Virtual Debugging Tool\" width=\"700\" height=\"498\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-Virtual-Debugging-Tool.png 816w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-Virtual-Debugging-Tool-300x213.png 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Serial-Communication-Virtual-Debugging-Tool-768x546.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">It&#8217;s available in proteus ISIS simulator and can be easily configured\/connected as you&#8217;ve seen in the figure above. Use your own tools and what you personally prefer for debugging. Always be sure that you&#8217;re consistently stepping forward toward solving the problem or finding out who is the hiding criminal. And determine where and which changes have to be made!<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 5\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">UART Bus Length VS Baud Rate<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">UART serial buses can go for quite long distances if your application needs so. However, the maximum allowed baud rate gets limited as you go further respectively. It also depends on the hardware implementation (Physical Layer) of the UART protocol itself. We&#8217;ll only mention both of the TTL-UART &amp; RS-232 standards.<\/span><\/p>\n<p><strong>RS-232<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The maximum cable length for RS-232 is 50ft. But in practice, it depends on the baud rate, cable specific capacitance, and ambient noise. The table below contains some rules-of-thumb from experiments done by Texas Instruments years ago.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2658\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RS-232-Baud-Rate-VS-Cable-Length.png\" alt=\"UART RS-232 Baud Rate VS Cable Length\" width=\"400\" height=\"124\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RS-232-Baud-Rate-VS-Cable-Length.png 601w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-RS-232-Baud-Rate-VS-Cable-Length-300x93.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><strong>TTL-UART<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">TTL level Serial UART only has a 5V swing. Hence, you have to figure out the following 3 things.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">1- Cable Resistance: all cables have an impedance that adds up as it goes longer. The longer the cable and higher the resistance, the more the voltage change is detected at one end compared to the other.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">2- Cable Capacitance: Highly capacitive wires strongly resist changing from one voltage level to the other. The higher the capacitance of wires, the slower its state changes and more rounded-off the signal becomes. Therefore, the higher the speed and longer the distance, the signal will start to look less like a square wave and more like a noisy sine wave.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">3- Noise: it does always exist and assumed to be existent in each environment. And due to the low voltage swing and without differential signaling, it gets harder to communicate farther. However, twisted-pair cables with shielding will greatly help.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Based on that, and my own information\/experiments, as there is no sufficient info online in this area. TTL-UART can possibly go 50ft (15m) using a generic UTP Cat 5 cable. As well as the RS-232.<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 6\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Overrun Error<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">An overrun error occurs when the receiver cannot process the data frame that has just arrived in the buffer before the next one arrives. Various devices have different amounts of buffer space to hold received data. So, let&#8217;s focus on our 8-Bit PIC MCU.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">During asynchronous UART data reception, It is possible for two bytes of data to be received and transferred to the <strong>RCREG<\/strong> FIFO and a third byte to begin shifting to the RSR register. On the detection of the Stop bit of the third byte, if the <strong>RCREG<\/strong> register is still full, the Overrun Error bit, <strong>OERR<\/strong>, will be set to indicate that an Overrun Error has occurred.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The word in the RSR (the 3rd byte) will be lost. The RCREG register can be read twice to retrieve the two bytes in the FIFO. Overrun bit OERR has to be cleared in software. This is done by resetting the receive logic <strong>(CREN is cleared and then set)<\/strong>. If bit OERR is set, transfers from the RSR register to the RCREG register are inhibited and no further data will be received. It is, therefore, essential to clear error bit OERR if it is set.<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 7\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Framing Error<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">A UART receiver will detect a <strong>Framing Error<\/strong> when it does not see a stop bit at the expected time after a start bit. As the start bit is used to identify the beginning of an incoming data frame, its timing is a reference for the remaining bits. If the data line is not in the expected state (high) when the &#8220;stop&#8221; bit is expected (according to the number of data and parity bits for which the UART is set), the UART will signal a framing error. Which means the timing between data bits, start bit, and stop bit is somehow for whatever reason is just messed up! A <strong>break<\/strong> condition on the line is also signaled as a framing error.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">There exist many reasons for which a framing error may occur such as:<\/span><\/p>\n<ul style=\"text-align: justify;\">\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Baud rate mismatches<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">A too long serial data line<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Electrical noise on the serial cable<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Framing error bit, <strong>FERR<\/strong>, is set if a Stop bit is detected as clear. Bit FERR and the 9th receive bit are buffered the same way as the receive data. Reading the RCREG will load bits RX9D and FERR with new values, therefore, it is essential for the programmer to read the RCSTA register before reading the RCREG register in order not to lose the old FERR and RX9D information.<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 8\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Bus Contention<\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">This happens when you attempt to connect more than 2 devices on the UART serial bus. it&#8217;s not allowed to have more than 2 devices on the UART serial bus, as it&#8217;s possible to have 2 devices writing data to the same TX line. Which will (at least) cause data corruption at the receiving end. And for worst, the transmitters involved may get damaged (it&#8217;s a rare condition, yet mostly protected against).<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">That&#8217;s why they use what&#8217;s called (Software Serial UART) in the Arduino ecosystem, in case you&#8217;ve got a previous experience with. There&#8217;s an FTDI USB-Serial converter connected to the hardware RX\/TX pins of the MCU and mostly used for debugging via serial monitor. Hence, connecting any additional serial device (e.g. Bluetooth Module, GPS, etc.) to the same bus will definitely be a problematic act! Eventually, they tend to use software (emulated) serial UART instead. Due to the <strong>Bus Contention<\/strong> Error. We&#8217;ll briefly address the concept of the emulated-UART hereafter.<\/span><\/p>\n<p><span style=\"font-size: 18pt; color: #ffffff; background-color: #333333;\">\u00a0 9\u00a0\u00a0<\/span><\/p>\n<p><strong><span style=\"font-size: 14pt;\">Software UART <span style=\"font-size: 12pt;\">(Emulated)<\/span><\/span><\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">The software can emulate a UART transmitter\/receiver module using only some code and consuming a considerable amount of resources specifically CPU&#8217;s time and\/or a hardware timer. It can extend your available on-chip UARTs or compensate for the lack of a hardware UART at all and to some extents. There is always fundamental limitations and drawbacks for what software can emulate.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">All in all, if you&#8217;re in a situation where you need an additional UART serial module, you can emulate it using the software. Just in case your demand for a UART is moderately low. But if your application needs extensive UART communication, then you should look for a hardware solution. As overloading your CPU with unnecessarily\u00a0expensive (in time) software will be a totally inefficient way to go.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/deepbluembedded.com\/spi-serial-peripheral-interface-tutorial-with-pic-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2999 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg\" alt=\"SPI Tutorial - PIC Microcontrollers\" width=\"201\" height=\"95\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg 720w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers-300x142.jpg 300w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers-520x245.jpg 520w\" sizes=\"auto, (max-width: 201px) 100vw, 201px\" \/><\/a>You may also need to check out the <a href=\"https:\/\/deepbluembedded.com\/spi-serial-peripheral-interface-tutorial-with-pic-microcontrollers\/\" target=\"_blank\" rel=\"noopener noreferrer\">SPI (serial peripheral interface)<\/a> tutorial. It&#8217;s another 10k words long-read kind of gigantic tutorial. Check it out and maybe keep it bookmarked as a guide for future work!<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<table style=\"height: 61px;\" width=\"820\">\n<tbody>\n<tr>\n<td style=\"width: 198px; background-color: #000000; text-align: center;\"><strong><span style=\"color: #ffffff;\">Disclaimer<\/span><\/strong><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #e6e6e6;\" colspan=\"4\">\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">It took me several days to craft this resource on that topic. And most of the information here represents my knowledge and point of view which may have some pitfalls. So, I&#8217;ll appreciate any comment, modifications or suggestions to polish this work a little bit more.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-family: arial, helvetica, sans-serif; color: #000000;\">Some paragraphs, diagrams, tables are copy \/ pasted from datasheets \/ technical documentations specifically from Microchip 8-Bit PIC MCUs. Just for the sake of consistency in firmware implementations using these MCU chips. To keep the theoretical side more relevant to practice, I had to do so. It&#8217;s protected by fair use act. However, I just wanted to disclaim it for you.<\/span><\/p>\n<p>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table style=\"height: 61px;\" width=\"820\">\n<tbody>\n<tr>\n<td style=\"width: 198px; background-color: #000000; text-align: center;\"><strong><span style=\"color: #ffffff;\">References<\/span><\/strong><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<td style=\"background-color: #ffffff;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"background-color: #e6e6e6;\" colspan=\"4\">\n<p style=\"text-align: left;\"><span style=\"color: #000000; font-family: arial, helvetica, sans-serif;\">In case you&#8217;re willing to go further.<\/span><\/p>\n<p><i>serial port and Microcontrollers: Principles, Circuits, and Source Codes<\/i><\/p>\n<p><i>Serial Port Complete: COM Ports, USB Virtual COM Ports, and Ports for Embedded Systems<\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/eeproms-internal-in-pic-microcontrollers\/\"><span style=\"color: #333333;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1666 alignleft\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png\" alt=\"Previous Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256L.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/span><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: left;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/eeproms-internal-in-pic-microcontrollers\/\"><strong><span style=\"color: #333333;\"><span style=\"color: #333333;\"><span style=\"color: #333333;\">Previous Tutorial<\/span><\/span><\/span><\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: center;\" colspan=\"2\"><strong><span style=\"color: #000000;\"><span style=\"font-size: 14pt;\">Tutorial 18<\/span><\/span><\/strong><\/td>\n<td style=\"background-color: #ffffff; width: 30%; text-align: right;\" colspan=\"2\"><a href=\"https:\/\/deepbluembedded.com\/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial\/\"><strong><span style=\"color: #333333;\"><span style=\"color: #333333;\"><span style=\"color: #333333;\">Next Tutorial<\/span><\/span><\/span><\/strong><\/a><\/td>\n<td style=\"background-color: #ffffff; width: 5%;\"><a href=\"https:\/\/deepbluembedded.com\/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial\/\"><strong><span style=\"color: #333333;\"><span style=\"color: #333333;\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1667 alignright\" src=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png\" alt=\"Next Tutorial\" width=\"35\" height=\"35\" srcset=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-150x150.png 150w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-160x160.png 160w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-320x320.png 320w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256-50x50.png 50w, https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/play-256.png 256w\" sizes=\"auto, (max-width: 35px) 100vw, 35px\" \/><\/span><\/span><\/strong><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll be discussing UART. You&#8217;ll get to know what is the UART serial communication protocol? How does it work? What are the typical applications for UART? We&#8217;ll also discuss in detail the process of creating the required firmware to drive the UART module in our PIC Microcontrollers<\/p>\n","protected":false},"author":1,"featured_media":2627,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28,30,27],"tags":[13,14,15,32,33,16,17,18,34,53,22],"class_list":["post-2557","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-systems","category-embedded-tutorials","category-microchip-pic","tag-embedded-c","tag-embedded-systems-tutorials","tag-firmware","tag-learn-embedded-systems","tag-learn-microcontrollers-programming","tag-microchip-pic-tutorials","tag-microcontrollers","tag-mplab","tag-tutorials","tag-uart","tag-xc8","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33","no-featured-image-padding"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UART | Serial Communication With PIC Microcontrollers Tutorial<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn What is UART serial communication protocol? We&#039;ll be writing code to drive the UART in PIC Microcontrollers\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UART | Serial Communication With PIC Microcontrollers Tutorial\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn What is UART serial communication protocol? We&#039;ll be writing code to drive the UART in PIC Microcontrollers\" \/>\n<meta property=\"og:url\" content=\"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DeepBlue\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/khaled.elrawy.359\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/khaled.elrawy.359\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-21T18:35:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-17T20:53:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"720\" \/>\n\t<meta property=\"og:image:height\" content=\"340\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Khaled Magdy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khaled Magdy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"43 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/\"},\"author\":{\"name\":\"Khaled Magdy\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"headline\":\"UART | Serial Communication With PIC Microcontrollers Tutorial\",\"datePublished\":\"2018-09-21T18:35:27+00:00\",\"dateModified\":\"2023-08-17T20:53:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/\"},\"wordCount\":7551,\"commentCount\":33,\"publisher\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"image\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/UART-Tutorial-Thumbnail-s.jpg\",\"keywords\":[\"Embedded C\",\"Embedded Systems Tutorials\",\"Firmware\",\"Learn Embedded Systems\",\"Learn Microcontrollers Programming\",\"Microchip PIC Tutorials\",\"Microcontrollers\",\"MPLAB\",\"Tutorials\",\"UART\",\"XC8\"],\"articleSection\":[\"Embedded Systems\",\"Embedded Tutorials\",\"Microchip PIC\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/\",\"name\":\"UART | Serial Communication With PIC Microcontrollers Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/UART-Tutorial-Thumbnail-s.jpg\",\"datePublished\":\"2018-09-21T18:35:27+00:00\",\"dateModified\":\"2023-08-17T20:53:04+00:00\",\"description\":\"In this tutorial, you'll learn What is UART serial communication protocol? We'll be writing code to drive the UART in PIC Microcontrollers\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/UART-Tutorial-Thumbnail-s.jpg\",\"contentUrl\":\"https:\\\/\\\/deepbluembedded.com\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/UART-Tutorial-Thumbnail-s.jpg\",\"width\":720,\"height\":340,\"caption\":\"UART Serial Communication Tutorial With PIC Microcontroller\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/uart-pic-microcontroller-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/deepbluembedded.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UART | Serial Communication With PIC Microcontrollers Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#website\",\"url\":\"https:\\\/\\\/deepbluembedded.com\\\/\",\"name\":\"DeepBlueMbedded\",\"description\":\"Embedded Systems And Computer Engineering Tutorials &amp; Articles\",\"publisher\":{\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/deepbluembedded.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/deepbluembedded.com\\\/#\\\/schema\\\/person\\\/30259d66cf68c6c681562dbe551b7867\",\"name\":\"Khaled Magdy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\",\"caption\":\"Khaled Magdy\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g\"},\"description\":\"Principal Embedded Systems Engineer with years of experience in embedded software and hardware design. I work in the Automotive &amp; e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI\\\/ML, and other fields I'm passionate about. I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?\",\"sameAs\":[\"https:\\\/\\\/deepbluembedded.com\",\"https:\\\/\\\/www.facebook.com\\\/khaled.elrawy.359\\\/\",\"https:\\\/\\\/www.instagram.com\\\/deepbluembedded\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/khaled-magdy-\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCzLSrNZD4rCjSCOVU9hknvA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UART | Serial Communication With PIC Microcontrollers Tutorial","description":"In this tutorial, you'll learn What is UART serial communication protocol? We'll be writing code to drive the UART in PIC Microcontrollers","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"UART | Serial Communication With PIC Microcontrollers Tutorial","og_description":"In this tutorial, you'll learn What is UART serial communication protocol? We'll be writing code to drive the UART in PIC Microcontrollers","og_url":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/","og_site_name":"DeepBlue","article_publisher":"https:\/\/www.facebook.com\/khaled.elrawy.359\/","article_author":"https:\/\/www.facebook.com\/khaled.elrawy.359\/","article_published_time":"2018-09-21T18:35:27+00:00","article_modified_time":"2023-08-17T20:53:04+00:00","og_image":[{"width":720,"height":340,"url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","type":"image\/jpeg"}],"author":"Khaled Magdy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Khaled Magdy","Est. reading time":"43 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#article","isPartOf":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/"},"author":{"name":"Khaled Magdy","@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"headline":"UART | Serial Communication With PIC Microcontrollers Tutorial","datePublished":"2018-09-21T18:35:27+00:00","dateModified":"2023-08-17T20:53:04+00:00","mainEntityOfPage":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/"},"wordCount":7551,"commentCount":33,"publisher":{"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"image":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","keywords":["Embedded C","Embedded Systems Tutorials","Firmware","Learn Embedded Systems","Learn Microcontrollers Programming","Microchip PIC Tutorials","Microcontrollers","MPLAB","Tutorials","UART","XC8"],"articleSection":["Embedded Systems","Embedded Tutorials","Microchip PIC"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/","url":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/","name":"UART | Serial Communication With PIC Microcontrollers Tutorial","isPartOf":{"@id":"https:\/\/deepbluembedded.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","datePublished":"2018-09-21T18:35:27+00:00","dateModified":"2023-08-17T20:53:04+00:00","description":"In this tutorial, you'll learn What is UART serial communication protocol? We'll be writing code to drive the UART in PIC Microcontrollers","breadcrumb":{"@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#primaryimage","url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","contentUrl":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","width":720,"height":340,"caption":"UART Serial Communication Tutorial With PIC Microcontroller"},{"@type":"BreadcrumbList","@id":"https:\/\/deepbluembedded.com\/uart-pic-microcontroller-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/deepbluembedded.com\/"},{"@type":"ListItem","position":2,"name":"UART | Serial Communication With PIC Microcontrollers Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/deepbluembedded.com\/#website","url":"https:\/\/deepbluembedded.com\/","name":"DeepBlueMbedded","description":"Embedded Systems And Computer Engineering Tutorials &amp; Articles","publisher":{"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/deepbluembedded.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/deepbluembedded.com\/#\/schema\/person\/30259d66cf68c6c681562dbe551b7867","name":"Khaled Magdy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g","caption":"Khaled Magdy"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/a8b5dc7385ec807e129b584974394d27e41c9979f3a2d0fd785b552b73529032?s=96&r=g"},"description":"Principal Embedded Systems Engineer with years of experience in embedded software and hardware design. I work in the Automotive &amp; e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI\/ML, and other fields I'm passionate about. I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?","sameAs":["https:\/\/deepbluembedded.com","https:\/\/www.facebook.com\/khaled.elrawy.359\/","https:\/\/www.instagram.com\/deepbluembedded\/","https:\/\/www.linkedin.com\/in\/khaled-magdy-\/","https:\/\/www.youtube.com\/channel\/UCzLSrNZD4rCjSCOVU9hknvA"]}]}},"jetpack_featured_media_url":"https:\/\/deepbluembedded.com\/wp-content\/uploads\/2018\/09\/UART-Tutorial-Thumbnail-s.jpg","jetpack_shortlink":"https:\/\/wp.me\/p9SirL-Ff","jetpack-related-posts":[{"id":2561,"url":"https:\/\/deepbluembedded.com\/usb-ttl-converter-tutorial-pc-control-for-microcontrollers\/","url_meta":{"origin":2557,"position":0},"title":"USB-TTL Converter Tutorial | PC Control For PIC Microcontrollers","author":"Khaled Magdy","date":"January 19, 2019","format":false,"excerpt":"In this tutorial, you'll learn how to create a communication channel between your PC & the Microcontroller via UART serial port. We'll be using a serial USB-To-TTL converter module as we can't just hook the RX\/TX pins to the D+\/D- pins of a USB Port.","rel":"","context":"In &quot;Electronics&quot;","block_context":{"text":"Electronics","link":"https:\/\/deepbluembedded.com\/electronics\/"},"img":{"alt_text":"USB-TTL Converter","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/01\/USB-TTL-Thumbnail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/01\/USB-TTL-Thumbnail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/01\/USB-TTL-Thumbnail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/01\/USB-TTL-Thumbnail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2555,"url":"https:\/\/deepbluembedded.com\/embedded-systems-tutorials-final-episode\/","url_meta":{"origin":2557,"position":1},"title":"Embedded Systems Tutorials With PIC MCUs | Final Episode","author":"Khaled Magdy","date":"April 3, 2019","format":false,"excerpt":"Previous Tutorial End of The 1st Edition Next Tutorial Concluding The 1st Edition Of Embedded Systems Tutorials (With PIC MCUs) \u00a0 Congratulations to everyone who have been following my embedded systems tutorials up to this point. Up till now, you've approached reading ~100k words of tutorials. That's a lot of\u2026","rel":"","context":"In &quot;Embedded Systems&quot;","block_context":{"text":"Embedded Systems","link":"https:\/\/deepbluembedded.com\/embedded-systems\/"},"img":{"alt_text":"Embedded Systems Tutorials Edition1 End","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/04\/Embedded-Systems-Tutorials-Edition1-End.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/04\/Embedded-Systems-Tutorials-Edition1-End.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/04\/Embedded-Systems-Tutorials-Edition1-End.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/04\/Embedded-Systems-Tutorials-Edition1-End.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2559,"url":"https:\/\/deepbluembedded.com\/bluetooth-module-hc05-interfacing-pic-microcontroller-tutorial\/","url_meta":{"origin":2557,"position":2},"title":"Bluetooth Module HC-05 With PIC Microcontroller Tutorial","author":"Khaled Magdy","date":"October 2, 2018","format":false,"excerpt":"In this tutorial, we\u2019ll control the PIC microcontroller using a smartphone and a PC via Bluetooth module HC-05 interfacing.","rel":"","context":"In &quot;Electronics&quot;","block_context":{"text":"Electronics","link":"https:\/\/deepbluembedded.com\/electronics\/"},"img":{"alt_text":"Bluetooth Module HC-05 Tutorial With PIC Microcontroller Thumbnail","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/10\/Bluetooth-Module-HC-05-Tutorial-With-PIC-Microcontroller-Thumbnail.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/10\/Bluetooth-Module-HC-05-Tutorial-With-PIC-Microcontroller-Thumbnail.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/10\/Bluetooth-Module-HC-05-Tutorial-With-PIC-Microcontroller-Thumbnail.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/10\/Bluetooth-Module-HC-05-Tutorial-With-PIC-Microcontroller-Thumbnail.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2563,"url":"https:\/\/deepbluembedded.com\/spi-tutorial-with-pic-microcontrollers\/","url_meta":{"origin":2557,"position":3},"title":"SPI Tutorial With PIC Microcontrollers | Serial Peripheral Interface","author":"Khaled Magdy","date":"February 12, 2019","format":false,"excerpt":"In this tutorial, we'll be discussing the SPI (serial peripheral interface) bus. How it works and how to establish serial communication between 2 PIC microcontrollers via SPI","rel":"","context":"In &quot;Electronics&quot;","block_context":{"text":"Electronics","link":"https:\/\/deepbluembedded.com\/electronics\/"},"img":{"alt_text":"SPI Tutorial - PIC Microcontrollers","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2019\/02\/SPI-Tutorial-PIC-Microcontrollers.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":9469,"url":"https:\/\/deepbluembedded.com\/arduino-serial-communication\/","url_meta":{"origin":2557,"position":4},"title":"Arduino Serial Communication Tutorial","author":"Khaled Magdy","date":"May 9, 2023","format":false,"excerpt":"In this tutorial, we'll discuss the Arduino Serial Communication Ports (Protocols) that we can use for communicating with other sensors, modules, and microcontrollers. The Arduino's microcontroller supports 3 serial communication ports (UART, SPI, and I2C). We'll get an overview of the serial communication ports (protocols) available in the Arduino, their\u2026","rel":"","context":"In &quot;Arduino&quot;","block_context":{"text":"Arduino","link":"https:\/\/deepbluembedded.com\/embedded-systems\/embedded-tutorials\/arduino\/"},"img":{"alt_text":"Arduino Serial Communication Tutorial","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Serial-Communication-Tutorial.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Serial-Communication-Tutorial.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Serial-Communication-Tutorial.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Serial-Communication-Tutorial.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2023\/05\/Arduino-Serial-Communication-Tutorial.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":1884,"url":"https:\/\/deepbluembedded.com\/creating-new-project-with-mplab\/","url_meta":{"origin":2557,"position":5},"title":"Creating New Project With MPLAB","author":"Khaled Magdy","date":"June 22, 2018","format":false,"excerpt":"In this tutorial, we'll learn how to create a new project in MPLAB and how to add the basic code files. Just stick with me through the following steps.","rel":"","context":"In &quot;Embedded Systems&quot;","block_context":{"text":"Embedded Systems","link":"https:\/\/deepbluembedded.com\/embedded-systems\/"},"img":{"alt_text":"Creating New Project With MPLAB IDE","src":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Creating-New-Project-With-MPLAB-IDE.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Creating-New-Project-With-MPLAB-IDE.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Creating-New-Project-With-MPLAB-IDE.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/deepbluembedded.com\/wp-content\/uploads\/2018\/06\/Creating-New-Project-With-MPLAB-IDE.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/comments?post=2557"}],"version-history":[{"count":73,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2557\/revisions"}],"predecessor-version":[{"id":3742,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/posts\/2557\/revisions\/3742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/media\/2627"}],"wp:attachment":[{"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/media?parent=2557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/categories?post=2557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deepbluembedded.com\/wp-json\/wp\/v2\/tags?post=2557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}