{"id":3268,"date":"2025-03-06T12:49:49","date_gmt":"2025-03-06T12:49:49","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3268"},"modified":"2025-03-15T06:45:06","modified_gmt":"2025-03-15T06:45:06","slug":"getting-started-with-stm32h5-arm-cortex-m33-i2c-in-full-duplex-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3268","title":{"rendered":"Getting Started with STM32H5 ARM Cortex M33:\u00a0I2C in Full Duplex Mode"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo.webp\" alt=\"\" class=\"wp-image-3269\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo.webp 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-768x768.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-750x750.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-06-15.14.04-A-highly-detailed-and-realistic-illustration-of-an-STM32H5-Nucleo-144-development-board-accurately-depicted-with-correct-pin-labels-and-layout.-The-bo-250x250.webp 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall take a look at the I2C bus and how to configure the i2c of STM32H5 using STM32CubeMX.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I2C bus.<\/li>\n\n\n\n<li>DS3231 module connection.<\/li>\n\n\n\n<li>STM32CubeIDE configuration.<\/li>\n\n\n\n<li>Read and write to I2C slave.<\/li>\n\n\n\n<li>Results. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. I2C Protocol:<\/h2>\n\n\n\n<p>I2C or Inter Integrated Circuit is type of synchronous serial communication that capable to communicate with up to 127 slave devices as show in figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"408\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-1024x408.png\" alt=\"\" class=\"wp-image-418\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-1024x408.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-300x119.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-768x306.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-750x299.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-400x159.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image-250x100.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/image.png 1130w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Like UART communication, I2C only uses two wires to transmit data between devices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SDA (Serial Data): \u00a0The line for the master and slave to send and receive data.\u00a0<\/li>\n\n\n\n<li>SCL (Serial Clock): The line\u00a0that carries\u00a0the clock signal.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.2: HOW I2C&nbsp;WORKS<\/h2>\n\n\n\n<p>With I2C, data is transferred in&nbsp;<em>messages.&nbsp;<\/em>Messages are broken up into&nbsp;<em>frames<\/em>&nbsp;of data. Each&nbsp;message has an address frame that contains the binary address of the slave, and one or more data frames that contain the data being transmitted. The message also includes&nbsp;start and stop conditions, read\/write bits, and ACK\/NACK bits between each data frame:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2016\/01\/Introduction-to-I2C-Message-Frame-and-Bit-2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2016\/01\/Introduction-to-I2C-Message-Frame-and-Bit-2-1024x258.png\" alt=\"Introduction to I2C - Message, Frame, and Bit\" class=\"wp-image-420891\" \/><\/a><\/figure>\n\n\n\n<p><strong>Start Condition:<\/strong>&nbsp;The SDA line switches from a high voltage&nbsp;level to a low voltage level&nbsp;<em>before<\/em>&nbsp;the SCL line switches from high to low.<\/p>\n\n\n\n<p><strong>Stop Condition:<\/strong>&nbsp;The SDA line switches from a low voltage&nbsp;level to a high voltage level&nbsp;<em>after<\/em>&nbsp;the SCL line switches from low to high.<\/p>\n\n\n\n<p><strong>Address Frame:<\/strong>&nbsp;A 7 or 10 bit sequence unique to each slave that identifies the slave when the master wants to talk to it.<\/p>\n\n\n\n<p><strong>Read\/Write Bit:&nbsp;<\/strong>A single bit specifying whether the master is sending data to the slave (low voltage level) or requesting data from it (high voltage level).<\/p>\n\n\n\n<p><strong>ACK\/NACK Bit:<\/strong>&nbsp;Each frame in a&nbsp;message is followed by an acknowledge\/no-acknowledge bit. If an&nbsp;address frame or data frame was successfully received, an ACK bit is returned to the sender from the receiving device.<\/p>\n\n\n\n<p>For more information and detailed explanations refer to this NXP documentation (<a rel=\"noreferrer noopener\" href=\"https:\/\/www.nxp.com\/docs\/en\/user-guide\/UM10204.pdf\" target=\"_blank\">here<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. DS3231 Module Connection:<\/h2>\n\n\n\n<p>Since this guide uses DS3231 module as shown in picture below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"475\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/DS3231-RTC-Module.png\" alt=\"\" class=\"wp-image-2498\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/DS3231-RTC-Module.png 600w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/DS3231-RTC-Module-300x238.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/DS3231-RTC-Module-400x317.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/DS3231-RTC-Module-250x198.png 250w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>This module has the required parts to make I2C working like the external Pull-up resistors etc.<\/p>\n\n\n\n<p>Hence, the connection as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"579\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-1024x579.jpg\" alt=\"\" class=\"wp-image-3270\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-1024x579.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-300x170.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-768x434.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-1536x868.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-2048x1157.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-1150x650.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-750x424.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-400x225.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-21-52-250x141.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. STM32CubeIDE Configuration:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating a new project with name of i2c_fullduplex as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-780x1024.jpg\" alt=\"\" class=\"wp-image-3271\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-780x1024.jpg 780w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-228x300.jpg 228w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-768x1008.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-750x985.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-400x525.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23-250x328.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-17-23.jpg 984w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>For how to create project using STM32CubeIDE, please follow this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2789\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2789\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we need to figure out which pins are connected on D14 and D15 of STM32H563Zi Nucleo-144 development board.<\/p>\n\n\n\n<p>From STM32H563Zi Nucleo-144 user manual (UM3115):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"628\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-1024x628.jpg\" alt=\"\" class=\"wp-image-3272\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-1024x628.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-300x184.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-768x471.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-1536x942.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-2048x1256.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-1150x705.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-750x460.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-400x245.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-29-37-250x153.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, when STM32CubeMX window appears, set PB8 and PB9 as i2c SCL and i2c SDA as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"541\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-1024x541.jpg\" alt=\"\" class=\"wp-image-3273\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-1024x541.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-300x158.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-768x405.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-1536x811.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-2048x1081.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-1150x607.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-750x396.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-400x211.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-31-34-250x132.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next from connectivity select I2C1 and enable it as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-905x1024.jpg\" alt=\"\" class=\"wp-image-3274\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-905x1024.jpg 905w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-265x300.jpg 265w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-768x869.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-1357x1536.jpg 1357w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-1150x1301.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-750x849.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-400x453.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14-250x283.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-06_15-32-14.jpg 1488w\" sizes=\"(max-width: 905px) 100vw, 905px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Keep the parameters as default.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, enable the UART to print the RTC value. For how to enable the uart, please refer to this guide\u00a0<a href=\"https:\/\/blog.embeddedexpert.io\/?p=2844\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>Save the project and this will generate the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Read and Write from I2C Slave:<\/h2>\n\n\n\n<p>Once the code has been generated, in main.c in user begin includes, include the following header files:<\/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;}\">#include &quot;stdio.h&quot;\n#include &quot;stdlib.h&quot;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, from the datasheet of DS3231, we need to obtain the address<\/p>\n\n\n\n<p>The address can be obtain from read\/write figures in the datasheet of the DS3231:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"193\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-1024x193.jpg\" alt=\"\" class=\"wp-image-2501\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-1024x193.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-300x56.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-768x144.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-1536x289.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-2048x385.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-1150x216.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-750x141.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-400x75.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-29_07-30-29-250x47.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We also need to shift the address to left by for the read\/write bit which is bit0 when the master transmit the read\/write request.<\/p>\n\n\n\n<p>Hence, the address as following:<\/p>\n\n\n\n<p>In user code begin PD, declare the 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;}\">#define DS3231_Addr (0x68&lt;&lt;1)<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin PV (Private variables), declare a buffer to hold the RTC data 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;}\">uint8_t rtc_data[3];<\/pre><\/div>\n\n\n\n<p>In user code begin 0, we shall declare the following two functions:<\/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;}\">int __io_putchar(int ch)\n{\n\tHAL_UART_Transmit(&amp;huart3, &amp;ch, 1, 5);\n\n\treturn ch;\n}\n\n\nint bcd_to_decimal(unsigned char x) {\n    return x - 6 * (x &gt;&gt; 4);\n}\n<\/pre><\/div>\n\n\n\n<p>First function is to use printf with serial port and the second one to convert BCD to decimal format.<\/p>\n\n\n\n<p>In user code begin 3 in while 1 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;}\">HAL_I2C_Mem_Read(&amp;hi2c1, DS3231_Addr, 0x00, 0x01, rtc_data, 3, 100);\n\t  for (int i=0;i&lt;3;i++)\n\t  {\n\t\t  rtc_data[i]=bcd_to_decimal(rtc_data[i]);\n\t  }\n\t  printf(&quot;RTC DATA H=%d M=%d S=%d\\r\\n&quot;,rtc_data[2],rtc_data[1],rtc_data[0]);\n\t  if(rtc_data[0]==5)\n\t  {\n\t\t  rtc_data[0]=0x00;\n\t\t  rtc_data[1]=(rand() % 5);\n\t\t  rtc_data[2]=(rand() % 5);\n\t\t  HAL_I2C_Mem_Write(&amp;hi2c1, DS3231_Addr, 0x00, 0x01, rtc_data, 3, 100);\n\t  }\n\n\t  HAL_Delay(200);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>First, read the memory from DS3231 0x00 which is the seconds 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;}\">HAL_I2C_Mem_Read(&amp;hi2c1, DS3231_Addr, 0x00, 0x01, rtc_data, 3, 100);<\/pre><\/div>\n\n\n\n<p>The function takes 7 parameters as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>HandleType of which i2c to be used which is hi2c1 in this case.<\/li>\n\n\n\n<li>Slave address which is already define as DS3231_Addr.<\/li>\n\n\n\n<li>Memory location to be read which the seconds register (0x00).<\/li>\n\n\n\n<li>Size of the address where 0x01 means 1 byte and 0x02 means two bytes.<\/li>\n\n\n\n<li>The buffer to hold the data to be read which is rtc_data.<\/li>\n\n\n\n<li>Number of bytes to be read which is 3 in this case.<\/li>\n\n\n\n<li>Timeout which is 100 milliseconds.<\/li>\n<\/ul>\n\n\n\n<p>After the function read the RTCdata, print the obtained data.<\/p>\n\n\n\n<p>Once the seconds become 5 second, randomise the hours and minutes and reset the seconds 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;}\">HAL_I2C_Mem_Write(&amp;hi2c1, DS3231_Addr, 0x00, 0x01, rtc_data, 3, 100);<\/pre><\/div>\n\n\n\n<p>The function shall take same parameters as HAL_I2C_MemRead.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Finally, delay by 200ms.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>That all for the guide.<\/p>\n\n\n\n<p>Save the project, build it and run it on your STM32H563Zi board.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open your favourite terminal applicaton, set the buadrate to be 115200 and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"733\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-1024x733.jpg\" alt=\"\" class=\"wp-image-2502\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-1024x733.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-300x215.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-768x550.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-1150x823.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-750x537.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-400x286.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56-250x179.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_17-06-56.jpg 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note when second part becomes 5, new data for hours and minutes are written.<\/p>\n\n\n\n<p>Happy coding\u00a0\ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the I2C bus and how to configure the i2c of STM32H5 using STM32CubeMX. In this guide, we shall cover the following: 1. I2C Protocol: I2C or Inter Integrated Circuit is type of synchronous serial communication that capable to communicate with up to 127 slave devices as [&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-3268","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\/3268"}],"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=3268"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3268\/revisions"}],"predecessor-version":[{"id":3275,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3268\/revisions\/3275"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}