{"id":2496,"date":"2024-04-29T05:12:47","date_gmt":"2024-04-29T05:12:47","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2496"},"modified":"2024-04-29T05:12:50","modified_gmt":"2024-04-29T05:12:50","slug":"getting-started-with-stm32g0-and-stm32cubeide-i2c-full-duplex","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2496","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: I2C Full Duplex"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp\" alt=\"\" class=\"wp-image-2497\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-4-2-1-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall take a look at the I2C protocol and how to use HAL API to get the RTC data from DS3231 and write data to DS3231.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I2C Protocol.<\/li><li>DS3231 Module Connection with STM32G070.<\/li><li>Driver Development.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. I2C Protocol:<\/h2>\n\n\n\n<p><\/p>\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\"><li>SDA (Serial Data): &nbsp;The line for the master and slave to send and receive data.&nbsp;<\/li><li>SCL (Serial Clock): The line&nbsp;that carries&nbsp;the clock signal.<\/li><\/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<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. DS3231 Module Connection:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Since this guide uses DS3231 module as shown in picture below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><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><\/div>\n\n\n\n<p><\/p>\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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-779x1024.png\" alt=\"\" class=\"wp-image-2499\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-779x1024.png 779w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-228x300.png 228w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-768x1010.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-1168x1536.png 1168w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-1150x1512.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-750x986.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-400x526.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM-250x329.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-29-at-7.33.43\u202fAM.png 1398w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>STM32G070<\/td><td>DS3231<\/td><\/tr><tr><td>5V<\/td><td>Vcc<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>PB6<\/td><td>SCL<\/td><\/tr><tr><td>PB7<\/td><td>SDA<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note: It is also possible to run the module from 3V3 if you want.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Driver Development:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating new project with name of I2C_DS3231. For how to create project, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>From pinout and configuration, then communication, then I2C1, enable the peripheral and set PB6 and PB7 to I2C pins 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=\"576\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-1024x576.jpg\" alt=\"\" class=\"wp-image-2500\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-1024x576.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-300x169.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-768x432.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-1536x863.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-2048x1151.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-1150x647.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-750x422.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-400x225.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-28_16-29-40-250x141.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Keep the I2C configuration as is.<\/p>\n\n\n\n<p>Also, enable the UART to print some data, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2316\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a> for UART.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the project and that shall generate the project.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c<\/p>\n\n\n\n<p>In user code begin includes, add 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;}\">\/* USER CODE BEGIN Includes *\/\n#include &quot;stdio.h&quot;\n#include &quot;stdlib.h&quot;\n\/* USER CODE END Includes *\/<\/pre><\/div>\n\n\n\n<p>The stdio for printf functionality and stdlib for random operation (useful later).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin PD, declare the address of the DS3231 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;}\">#define DS3231_Addr (0x68&lt;&lt;1)<\/pre><\/div>\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 size-large\"><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><\/p>\n\n\n\n<p>In user code begin PV, declare an array of 3 elements to hold the RTC data as following:<\/p>\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;}\">\/* USER CODE BEGIN PV *\/\nuint8_t rtc_data[3];\n\/* USER CODE END PV *\/<\/pre><\/div>\n\n\n\n<p>In user code begin 0, 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;huart2, &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}<\/pre><\/div>\n\n\n\n<p>First one shall retarget printf to print over UART.<\/p>\n\n\n\n<p>Second one will convert the BCD to decimal value which is readable by human.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 3 in while 1 loop:<\/p>\n\n\n\n<p>First read the I2C memory location 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>This function shall take 7 parameters as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pointer to a I2C_HandleTypeDef structure (hi2c1).<\/li><li>Target device address (DS3231_Addr).<\/li><li>Internal memory address (0x00 the starting address of the seconds).<\/li><li>Size of internal memory address(0x01 which is 1).<\/li><li>Pointer to data buffer (rtc_data).<\/li><li>Amount of data to be read (size of the buffer which is 3).<\/li><li>Timeout duration (100 millisecond). <\/li><\/ul>\n\n\n\n<p>After reading the data, convert the BCD to decimal:<\/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;}\">\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  }<\/pre><\/div>\n\n\n\n<p>Print 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;}\">printf(&quot;RTC DATA H=%d M=%d S=%d\\r\\n&quot;,rtc_data[2],rtc_data[1],rtc_data[0]);<\/pre><\/div>\n\n\n\n<p>Next, check if the seconds part is 5 seconds ad 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;}\">if(rtc_data[0]==5)<\/pre><\/div>\n\n\n\n<p>If yes, fill the buffer with random numbers for minutes and hours and reset seconds to 0 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;}\">rtc_data[0]=0x00;\nrtc_data[1]=(rand() % 5);\nrtc_data[2]=(rand() % 5);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Write the new data using HAL_I2C_Mem_Write 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>It takes  the same parameters as the read function.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Finally delay by 200ms.<\/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_Delay(200);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the project, build and run it on your MCU.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. 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<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><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><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the I2C protocol and how to use HAL API to get the RTC data from DS3231 and write data to DS3231. In this guide, we shall cover the following: I2C Protocol. DS3231 Module Connection with STM32G070. Driver Development. Results. 1. I2C Protocol: I2C or Inter Integrated [&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-2496","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\/2496"}],"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=2496"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2496\/revisions"}],"predecessor-version":[{"id":3267,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2496\/revisions\/3267"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}