{"id":1514,"date":"2023-02-04T05:34:14","date_gmt":"2023-02-04T05:34:14","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1514"},"modified":"2023-02-04T05:34:17","modified_gmt":"2023-02-04T05:34:17","slug":"getting-started-with-stm32f103-i2c-write-byte","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1514","title":{"rendered":"Getting Started with STM32F103: I2C Write Byte"},"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 previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1493\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1493\" target=\"_blank\">here<\/a>), we took a look at the i2c protocol and how to use the i2c to scan for the slave addresses. In this guide, we shall transmit data over i2c to slave device.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I2C write byte with memory address.<\/li><li>I2C write byte without memory address.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. I2C write byte with memory address:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open i2c.c source file and declare the following function:<\/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 i2c1_MemoryWrite_Byte(uint8_t saddr, uint8_t maddr, uint8_t data)<\/pre><\/div>\n\n\n\n<p>The function takes three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Slave address.<\/li><li>Memory address.<\/li><li>Data to be send.<\/li><\/ul>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p>We start by waiting until the bus is free by checking busy bit in SR2 register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"303\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-1024x303.png\" alt=\"\" class=\"wp-image-1515\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-1024x303.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-300x89.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-768x227.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-1536x455.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-2048x606.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-1150x341.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-750x222.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-400x118.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.52.08-AM-250x74.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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){;}          \t\/*wait until bus not busy*\/<\/pre><\/div>\n\n\n\n<p>After the bus is freed, send the start condition by setting bit start in CR1 to 1 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"219\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-1024x219.png\" alt=\"\" class=\"wp-image-1516\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-1024x219.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-300x64.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-768x164.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-1536x329.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-2048x438.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-1150x246.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-750x161.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-400x86.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.53.36-AM-250x54.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/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;                 \t\/*generate start*\/<\/pre><\/div>\n\n\n\n<p>Wait until the start is generated by check SB bit in SR1 register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"213\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-1024x213.png\" alt=\"\" class=\"wp-image-1517\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-1024x213.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-300x62.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-768x160.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-1536x320.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-2048x426.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-1150x239.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-750x156.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-400x83.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.54.54-AM-250x52.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/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_SB)){;}         \t\/*wait until start bit is set*\/<\/pre><\/div>\n\n\n\n<p>Then send the slave address shifted to left by 1 bit:<\/p>\n\n\n\n<p>The reason behind the shifting is the address takes the bit1 to bit7 from the data register and bit0 is used for read\/write operation. When bit0 is 0, it the operation is write operation and when it is 1, it means read operation.<\/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;                 \t \t\/* Send slave address with write operation*\/<\/pre><\/div>\n\n\n\n<p>Wait until the address is matched by checking ADDR bit in SR1 register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"219\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-1024x219.png\" alt=\"\" class=\"wp-image-1518\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-1024x219.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-300x64.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-768x164.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-1536x329.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-2048x438.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-1150x246.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-750x161.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-400x86.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.58.49-AM-250x54.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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_ADDR)){;}      \t\/*wait until address flag is set*\/<\/pre><\/div>\n\n\n\n<p>Clear the SR2 register (recommended):<\/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)I2C1-&gt;SR2; \t\t\t\t\t\t\/*clear SR2 by reading it *\/<\/pre><\/div>\n\n\n\n<p>Wait until transmit buffer is empty by checking TXE bit in SR1 register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"204\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-1024x204.png\" alt=\"\" class=\"wp-image-1519\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-1024x204.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-300x60.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-768x153.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-1536x307.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-2048x409.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-1150x230.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-750x150.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-400x80.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.02.11-AM-250x50.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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)){;}       \/*Wait until Data register empty*\/<\/pre><\/div>\n\n\n\n<p>Send the memory address:<\/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;                        \/* send memory address*\/<\/pre><\/div>\n\n\n\n<p>Wait until TXE bit to 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)){;}       \/*Wait until Data register empty*\/<\/pre><\/div>\n\n\n\n<p>Send the data:<\/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 = data; \t\t\t\t\t\t\/*Send the data*\/<\/pre><\/div>\n\n\n\n<p>Wait until transfer is completed by checking BTF bit in SR1 register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"204\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-1024x204.png\" alt=\"\" class=\"wp-image-1520\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-1024x204.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-300x60.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-768x153.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-1536x307.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-2048x409.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-1150x230.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-750x150.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-400x80.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.25.01-AM-250x50.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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_BTF));      \/*wait until transfer finished*\/<\/pre><\/div>\n\n\n\n<p>Finally, send the stop condition by setting stop bit in CR1 to 1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"204\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-1024x204.png\" alt=\"\" class=\"wp-image-1522\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-1024x204.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-300x60.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-768x153.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-1536x307.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-2048x409.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-1150x230.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-750x150.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-400x80.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-8.27.07-AM-250x50.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/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_STOP;\t\t\t\t\/*Generate Stop*\/<\/pre><\/div>\n\n\n\n<p>Update the header file with the new function:<\/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 i2c1_MemoryWrite_Byte(uint8_t saddr,uint8_t maddr, uint8_t data);<\/pre><\/div>\n\n\n\n<p>Hence, the entire function 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;}\">\nvoid i2c1_MemoryWrite_Byte(uint8_t saddr, uint8_t maddr, uint8_t data)\n{\n\twhile(I2C1-&gt;SR2&amp;I2C_SR2_BUSY){;}          \t\/*wait until bus not busy*\/\n\tI2C1-&gt;CR1|=I2C_CR1_START;                 \t\/*generate start*\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_SB)){;}         \t\/*wait until start bit is set*\/\n\tI2C1-&gt;DR = saddr&lt;&lt; 1;                 \t \t\/* Send slave address with write operation*\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_ADDR)){;}      \t\/*wait until address flag is set*\/\n\t(void)I2C1-&gt;SR2; \t\t\t\t\t\t\/*clear SR2 by reading it *\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}       \/*Wait until Data register empty*\/\n\tI2C1-&gt;DR = maddr;                        \/* send memory address*\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}       \/*wait until data register empty*\/\n\tI2C1-&gt;DR = data; \t\t\t\t\t\t\/*Send the data*\/\n\twhile (!(I2C1-&gt;SR1 &amp; I2C_SR1_BTF));      \/*wait until transfer finished*\/\n\tI2C1-&gt;CR1 |=I2C_CR1_STOP;\t\t\t\t\/*Generate Stop*\/\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. I2C write byte without memory address:<\/h2>\n\n\n\n<p>The function is similar to the with memory. However, minus the section of sending the memory address.<\/p>\n\n\n\n<p>Hence the function 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 i2c1_Write_Byte(uint8_t saddr, uint8_t data)\n{\n\twhile(I2C1-&gt;SR2&amp;I2C_SR2_BUSY){;}          \/*wait until bus not busy*\/\n\tI2C1-&gt;CR1|=I2C_CR1_START;                 \/*generate start*\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_SB)){;}         \/*wait until start bit is set*\/\n\tI2C1-&gt;DR = saddr&lt;&lt; 1;                 \t \/* Send slave address with write operation*\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_ADDR)){;}      \/*wait until address flag is set*\/\n\t(void)I2C1-&gt;SR2; \t\t\t\t\t\t\/*clear SR2 by reading it *\/\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE)){;}       \/*Wait until Data register empty*\/\n\tI2C1-&gt;DR = data;                        \/* send memory address*\/\n\twhile (!(I2C1-&gt;SR1 &amp; I2C_SR1_BTF));      \/*wait until transfer finished*\/\n\tI2C1-&gt;CR1 |=I2C_CR1_STOP;\t\t\t\t\/*Generate Stop*\/\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the entire code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/I2C_write_byte.zip\">I2C_write_byte<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/I2C_write_byte.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>Since this guide uses ds3231 as slave device, we shall 0x05 to the second register 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;}\">i2c1_MemoryWrite_Byte((uint8_t)0x68,(uint8_t)0x00,0x05);<\/pre><\/div>\n\n\n\n<p>Using logic analyzer, you should get the following results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"319\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-1024x319.png\" alt=\"\" class=\"wp-image-1524\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-1024x319.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-300x93.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-768x239.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-1536x478.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-2048x638.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-1150x358.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-750x234.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-400x125.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-04-at-7.28.14-AM-250x78.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/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 previous guide (here), we took a look at the i2c protocol and how to use the i2c to scan for the slave addresses. In this guide, we shall transmit data over i2c to slave device. In this guide, we shall cover the following: I2C write byte with memory address. I2C write byte without [&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-1514","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\/1514"}],"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=1514"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions"}],"predecessor-version":[{"id":1525,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions\/1525"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}