{"id":549,"date":"2021-11-04T04:53:54","date_gmt":"2021-11-04T04:53:54","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=549"},"modified":"2021-11-04T04:53:56","modified_gmt":"2021-11-04T04:53:56","slug":"working-with-stm32-and-i2c-reading-multiple-bytes","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=549","title":{"rendered":"Working with STM32 and I2C: Reading Multiple Bytes"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/1200px-I%C2%B2C_bus_logo.svg_-273x300.png\" alt=\"\" class=\"wp-image-417\" \/><\/figure><\/div>\n\n\n\n<p>In the pervious guid (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=524\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=524\" target=\"_blank\">here<\/a>), we took a look how to write\/read single byte using I2C and the slave device is DS3231<\/p>\n\n\n\n<p>In this guide, we shall see how to read multiple byte of DS3231 which they are seconds, minutes and hours and display them on the serial monitor.<\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I2C read multiple byte<\/li><li>I2C read multiple byte code<\/li><li>Code Download<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. I2C Read Multiple Byte:<\/h2>\n\n\n\n<p>In order to read multiple byte, first we send the start condition, then followed by the slave address with write bit then the memory location in our case ( seconds memory location 0x00). After that a repeat start must be generated and send slave address with read bit after that we enable acknowledge(ACK). We keep reading (in out case 3) until one byte remains then we disable acknowledge (NAK). The figure below shows the reading sequence.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"366\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-1024x366.png\" alt=\"\" class=\"wp-image-551\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-1024x366.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-300x107.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-768x275.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-1536x549.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-2048x732.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-1150x411.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-750x268.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-400x143.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-04-at-7.24.38-AM-250x89.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. I2C Read Multiple Byte Code:<\/h2>\n\n\n\n<p>We start of by declaring the function name as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void i2c_ReadMulti(char saddr,char maddr, int n, char* data){<\/pre><\/div>\n\n\n\n<p>it takes three arguments <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>slave address<\/li><li>starting address to read from<\/li><li>number of byte to be read<\/li><li>buffer to store the data<\/li><\/ul>\n\n\n\n<p>Inside the function we declare temp to clear flags<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">volatile int temp;<\/pre><\/div>\n\n\n\n<p>we wait until the bus is free<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while (I2C1-&gt;SR2 &amp; I2C_SR2_BUSY){;}<\/pre><\/div>\n\n\n\n<p>Send start condition and wait until start condition is generated<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tI2C1-&gt;CR1|=I2C_CR1_START;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_SB)){;}<\/pre><\/div>\n\n\n\n<p>Send the slave address with write operation and wait until address is set<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">I2C1-&gt;DR=saddr&lt;&lt;1;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_ADDR)){;}<\/pre><\/div>\n\n\n\n<p>Then clear the status register<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">temp=I2C1-&gt;SR2;<\/pre><\/div>\n\n\n\n<p>Then wait until transmit bit is set<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}<\/pre><\/div>\n\n\n\n<p>Now we send memory location <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">I2C1-&gt;DR = maddr;<\/pre><\/div>\n\n\n\n<p>Then we wait until transmit bit is set<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}<\/pre><\/div>\n\n\n\n<p>After that we regenerate start condition and wait until the start condition is generated<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">I2C1-&gt;CR1|=I2C_CR1_START;\nwhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_SB)){;}<\/pre><\/div>\n\n\n\n<p>Now, we send slave address with read bit and wait until the address is set<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">I2C1-&gt;DR=saddr&lt;&lt;1|1;\nwhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_ADDR)){;}<\/pre><\/div>\n\n\n\n<p>we clear the status register <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">temp=I2C1-&gt;SR2;<\/pre><\/div>\n\n\n\n<p>Then we enable acknowledge <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">I2C1-&gt;CR1|=I2C_CR1_ACK;<\/pre><\/div>\n\n\n\n<p>After that we start reading<\/p>\n\n\n\n<p>we start of with while loop<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while(n&gt;0U)\n\t\t{<\/pre><\/div>\n\n\n\n<p>Now we have a little bit of machine state <\/p>\n\n\n\n<p>In case we have only one byte left, we must do the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Disable acknowledge <\/li><li>Generate stop<\/li><li>Wait until receive bit is set<\/li><li>Store the last received byte to the buffer<\/li><li>Break the while loop<\/li><\/ul>\n\n\n\n<p>In case more than one byte left<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wait for receive bit to be set<\/li><li>store the data in the buffer<\/li><li>increment the buffer counter<\/li><li>decrement the counter<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if(n==1U)\n\t\t\t\t{\n\t\t\t\tI2C1-&gt;CR1&amp;=~I2C_CR1_ACK;\n\t\t\t\t\tI2C1-&gt;CR1|=I2C_CR1_STOP;\n\t\t\t\t\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_RXNE)){;}\n\t\t\t\t\t*data++=I2C1-&gt;DR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\n\t\t\t\t\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_RXNE)){;}\n\t\t\t\t\t\t(*data++)=I2C1-&gt;DR;\n\t\t\t\t\t\t\tn--;\n\t\t\t\t\t\n\t\t\t\t\t}\t\n}\/\/for the while loop<\/pre><\/div>\n\n\n\n<p>Hence the code shall be like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void i2c_ReadMulti(char saddr,char maddr, int n, char* data)\n{\n\tvolatile int temp;\n\twhile (I2C1-&gt;SR2 &amp; I2C_SR2_BUSY){;}\n\tI2C1-&gt;CR1|=I2C_CR1_START;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_SB)){;}\n\tI2C1-&gt;DR=saddr&lt;&lt;1;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_ADDR)){;}\n\ttemp=I2C1-&gt;SR2;\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}\n\tI2C1-&gt;DR = maddr;\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}\n\tI2C1-&gt;CR1|=I2C_CR1_START;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_SB)){;}\n\tI2C1-&gt;DR=saddr&lt;&lt;1|1;\n\twhile(!(I2C1-&gt;SR1 &amp; I2C_SR1_ADDR)){;}\n\ttemp=I2C1-&gt;SR2;\n\tI2C1-&gt;CR1|=I2C_CR1_ACK;\n\twhile(n&gt;0U)\n\t\t{\n\t\tif(n==1U)\n\t\t\t\t{\n\t\t\t\tI2C1-&gt;CR1&amp;=~I2C_CR1_ACK;\n\t\t\t\t\tI2C1-&gt;CR1|=I2C_CR1_STOP;\n\t\t\t\t\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_RXNE)){;}\n\t\t\t\t\t*data++=I2C1-&gt;DR;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\n\t\t\t\t\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_RXNE)){;}\n\t\t\t\t\t\t(*data++)=I2C1-&gt;DR;\n\t\t\t\t\t\t\tn--;\n\t\t\t\t\t\n\t\t\t\t\t}\t\n\t\t\t\t\n\t\t\t\n\t\t}\t\n\t\t\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code Download<\/h2>\n\n\n\n<p>You can download the code from here<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/I2C-Read-Multiple-Byte.zip\">I2C-Read-Multiple-Byte<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/I2C-Read-Multiple-Byte.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo<\/h2>\n\n\n\n<p>After you compile and upload the code, open serial monitor program and set the baudrate at 115200 and you should see the serial is printing the time <\/p>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Recording-2021-11-04-at-7.53.01-AM.mov\"><\/video><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the pervious guid (here), we took a look how to write\/read single byte using I2C and the slave device is DS3231 In this guide, we shall see how to read multiple byte of DS3231 which they are seconds, minutes and hours and display them on the serial monitor. In this guide, we will cover [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/549"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}