{"id":2311,"date":"2024-07-16T15:47:37","date_gmt":"2024-07-16T15:47:37","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2311"},"modified":"2024-08-12T12:44:12","modified_gmt":"2024-08-12T12:44:12","slug":"uart","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2311","title":{"rendered":"UART"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">USART stands for Universal Synchronous Asynchronous Transmitter and Receiver in which a packet of data enclosed in an envelope of start and stop bit. Synchronous means that single clock source would be shared by end devices to facilitate communication and asynchronous means, there would be no synchronous clock source would be the end of the device. To allow the communication between two devices we need to decide a bound rate. Here bound rate is the speed of your communication and is measured in bits per second. Let\u2019s look at the USART register available in AVR and there uses-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"975\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/usart.png\" alt=\"\" class=\"wp-image-2314\" style=\"width:618px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/usart.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/usart-182x300.png 182w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-2 wp-block-paragraph\" style=\"color:#6c8a97\">Initialize USART<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Initialize USART is very simple. Just initialize the following condition<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"120\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-5-1024x120.jpg\" alt=\"\" class=\"wp-image-2317\" style=\"width:546px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-5-1024x120.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-5-300x35.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-5-768x90.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-5.jpg 1167w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Thus for BAUD RATE=9600 with F_CPU=8MHz, the UBRR value is 51. If U2X bit is enabled then simply multiply 2 with UBRR value.<\/p>\n\n\n\n<ol style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color has-upper-heading-font-size wp-elements-5\">\n<li>Enable Transmit or Receive mode with either Synchronous or Asynchronous operation&nbsp;<\/li>\n\n\n\n<li class=\"has-upper-heading-font-size\">Select the character frame size with parity and stop bit.<\/li>\n<\/ol>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s enable USART mode with BAUD RATE=9600, F_CPU=8MHz, Asynchronous mode, 8 bit character with no parity and 1 stop bit. From the above calculation UBRR=51.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_init(void)\n{\n  UBRR0=(uint8_t)51; \/\/UBRR0H=0;\n  \/******  Asynchronous mode, 8 bit character with no parity and 1 stop bit *******\/\n  UCSRB|=(1&lt;&lt;RXEN)|(1&lt;&lt;TXEN);\n  UCSRC|=(1&lt;&lt;URSEL)|(1&lt;&lt;UCSZ1)|(1&lt;&lt;UCSZ0); \/\/For UCSRC register selection URSEL bit is set.\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7 wp-block-paragraph\" style=\"color:#6c8a97\">Send individual indications<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">Since the UDR register is full duplex, data can be transmitted by writing this register and data can be received by reading this register. To send data just write the data to UDR and it automatically send via AVR Tx pin. Now wait until the transmission complete either checking the TXC or UDRE bit in UCSRA register. Since the flag register is set when data send completed. So the checking condition is<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nUDR=ByteToSend;\nwhile(!(UCSRA&amp;(1&lt;&lt;TXC)))\n{\n;\/\/ wait until data send\n}\n\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-align-center wp-block-paragraph\">OR<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nUDR=ByteToSend;\nwhile(!(UCSRA&amp;(1&lt;&lt;UDRE)))\n{\n;\/\/ wait for possible sending\n}\n\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-9\">\n<li class=\"has-medium-font-size\"><strong>Send character<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-10 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usart_putc(\u2018c\u2019);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_putc(unsigned char c)\n{\n  while(!(UCSRA&amp;(1&lt;&lt;UDRE)));\n  UDR=c;\n  while(!(UCSRA&amp;(1&lt;&lt;UDRE)));\n}\n<\/pre><\/div>\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-12\">\n<li class=\"has-medium-font-size\">Send string<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-13 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usart_puts(\u201cSend string\u201d);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-14 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_puts( char *star)\n{\n  while(*star)\n   {\n     usart_putc(*star);\n     star++;\n   }\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color has-medium-font-size wp-elements-15\">\n<li class=\"has-medium-font-size\">Send signed 16 bit integer(int16_t)<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-16 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usart_puti16(-123);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-17 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_puti16(int16_t a)\n{\n  char s&#x5B;20];\n  itoa(a,s,10);\n  usart_puts(s);\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color has-medium-font-size wp-elements-18\">\n<li class=\"has-medium-font-size\">Send signed 16 bit integer(uint16_t)<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-19 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usart_puti16(123);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-20 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_putui16(uint16_t a)\n{\n  char s&#x5B;20];\n  utoa(a,s,10);\n  usart_puts(s);\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-21\">\n<li class=\"has-medium-font-size\">Send signed 32 bit integer(int32_t)<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-22 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp; usart_puti32(-12345); <\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-23 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_puti32(int32_t a)\n{\n char s&#x5B;20];\n ltoa(a,s,10);\n usart_puts(s);\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-24\">\n<li class=\"has-medium-font-size\">Send signed 32 bit integer(uint32_t)<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-25 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp; usart_putui32(12345);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-26 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_putui32(uint32_t a)\n{\n  char s&#x5B;20];\n  ultoa(a,s,10);\n  usart_puts(s);\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-27\">\n<li>Send float<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-28 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp; usart_putf(3.23);<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-29 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid usart_putf(float f)\n{\n  char s&#x5B;20];\n  dtostrf(f,19,3,s); \/\/19 is the string limit &amp; 3 is after period(.)\n  usart_puts(s);\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/USART-note.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-30 wp-block-paragraph\" style=\"color:#6c8a97\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/USART-note.rar\">Download the full usart header file+ C conversion with code<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-31 wp-block-paragraph\" style=\"color:#5c5c5c\">The conversion is more complicated to memorize. In that case you can use sprintf(string,\u201dConvertion\u201d,variables) function which is available in #include&lt;stdio.h&gt;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;stdio.h&gt;\n  char usart&#x5B;20];\n  \/****convert whatever data type you want to string******\/\n  sprintf(usart,\u201d%x\u201d,your_data); \n  usart_puts(usart);\n\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-32 wp-block-paragraph\" style=\"color:#6c8a97\">Receiving Indication<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-33 wp-block-paragraph\" style=\"color:#5c5c5c\">Since UDR register is full duplex, we can receive data through UDR register. In this case we first check if any data available. This can be done with the help of RXC flag register.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-34 wp-block-paragraph\" style=\"color:#5c5c5c\">Function call:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char c=usart_getc();<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-35 wp-block-paragraph\" style=\"color:#5c5c5c\">Function definition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar usart_getc(void)\n {\n  while(!(UCSRA&amp;(1&lt;&lt;RXC)))\n   {\n     ; \/\/wait until receive complete  \n   }\n  return UDR;\n }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-36 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s make a program to display ADC value of AVR to PC with the help of USART feature. In this program ADC value will be display if enter pressed. In this section we just show the result in Proteus VIRTUAL TERMINAL with parameter Baud Rate : 9600, Data Bits : 8, Parity : NONE &amp; Stops Bits : 1 . The program is very simple, look at the coding and it will be easier to understand. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n  #include&lt;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&lt;stdio.h&gt;\n  #include&quot;usart.h&quot;\n  #include&quot;adc.h&quot;\nint main(void)\n  {\n   usart_init();\n   adc_init();\n   usart_puts(&quot;********************************************\\r&quot;);\n   usart_puts(&quot;*********   shani kumar sarkar   ***********\\r&quot;);\n   usart_puts(&quot;*********   Read ADC value       ***********\\r&quot;);\n   usart_puts(&quot;********************************************\\r&quot;);\n   usart_puts(&quot;\\r press enter to get ADC value\\r&quot;);\n  while(1)\n    {\n     if(usart_getc()==&#039;\\r&#039;)\n\t   {\n\t\tusart_puti16(read_adc(0));\n\t\tusart_putc(&#039;\\r&#039;);\n\t   }\n    }\n return 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Display-ADC.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-37 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Display-ADC.rar\">Display ADC.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>USART stands for Universal Synchronous Asynchronous Transmitter and Receiver in which a packet of data enclosed in an envelope of start and stop bit. Synchronous means that single clock source would be shared by end devices to facilitate communication and asynchronous means, there would be no synchronous clock source would be the end of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2311","post","type-post","status-publish","format-standard","hentry","category-uart"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2311"}],"version-history":[{"count":20,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2311\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}