{"id":2207,"date":"2024-07-15T16:25:05","date_gmt":"2024-07-15T16:25:05","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2207"},"modified":"2024-08-11T17:50:29","modified_gmt":"2024-08-11T17:50:29","slug":"i2c-eeprom-at24c512","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2207","title":{"rendered":"I2C EEPROM AT24C512"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">EEPROM stands for electricity erasable programmable read only memory and it is a type of non-volatile memory used in different application. It is read only memory whose data can be erase and re-programmed by higher than normal current of electricity. Many microcontrollers have in build EEPROM. In this article we use AT24C512 serial EEPROM. It is a low cost EEPROM with 512K and one million read\/write cycle. It can operate from 2.7V to 5.5V, so it can be operate with 3.3V microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">A<sub>1<\/sub>:A<sub>0<\/sub> \u2013 Physical Address<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Pin WP (write Protection) : The write protection input, when connected to Ground allows normal port operation. When WP is connected to V<sub>cc <\/sub>, all write operation to the memory are inhibited.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Device address<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"869\" height=\"60\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1.jpg\" alt=\"\" class=\"wp-image-2211\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1.jpg 869w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1-300x21.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-6-1-768x53.jpg 768w\" sizes=\"(max-width: 869px) 100vw, 869px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"392\" height=\"257\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image.png\" alt=\"\" class=\"wp-image-2210\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image.png 392w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-300x197.png 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">So the write address \u2013 0xA0 and read address \u2013 0xA1. The I<sup>2<\/sup>C write protocol-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"228\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-2-1024x228.jpg\" alt=\"\" class=\"wp-image-2216\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-2-1024x228.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-2-300x67.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-2-768x171.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-2.jpg 1431w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">Here after start condition we have to write the corresponding address than data. Let\u2019s separate the start and write function separately. First send start condition with write operation.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7 wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Start(char write_address)&nbsp; -&gt; start with write address<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t I2C_Start(char write_address)\n{\n    uint8_t status;\t\t               \n\tTWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWSTA); \/\/--- TWI Interrupt desable, TWI enable, TWI Start\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));            \/\/--- Wait untial start condition is executed\n    status=TWSR&amp;0xF8;\t\t               \/\/--- Read TWI status register\n    if(status!=0x08)\t\t               \/\/--- Check weather START transmitted or not?\n    return 0;\t\t\t                   \/\/--- Return 0 to indicate start condition fail \n    TWDR=write_address;\t\t               \/\/--- Write SLA+W in TWI data register\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);\t           \/\/--- TWI Interrupt desable, TWI enable\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t           \/\/--- Wait untial transmet is complete\n    status=TWSR&amp;0xF8;\t\t               \/\/--- Read TWI status register\t\n    if(status==0x18)\t\t               \/\/--- Check for SLA+W transmitted &amp;ack received\n    return 1;\t\t\t                   \/\/--- Return 1 to indicate ack received\n    if(status==0x20)\t               \t   \/\/--- Check for SLA+W transmitted &amp;nack received \n    return 2;\t\t\t                   \/\/--- Return 2 to indicate nack received \n    else\n    return 3;\t\t\t                   \/\/--- Else return 3 to indicate SLA+W failed \n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-8 wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Write(char data) -&gt; write address word or data<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t I2C_Write(char data)\n{\n    uint8_t status;\t\t      \n    TWDR=data;\t\t     \t    \/\/--- Data write to transmet \n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);\t\/\/--- TWI Interrupt desable, TWI enable\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t\/\/--- Wait untial transmet is complete\n    status=TWSR&amp;0xF8;\t\t    \/\/--- Read TWI status register\n    if(status==0x28)\t\t    \/\/--- Check for data transmitted &amp;ack received\n    return 0;\t\t\t        \/\/--- Return 0 to indicate ack received\n    if(status==0x30)\t\t    \/\/--- Check for data transmitted &amp;nack received\n    return 1;\t\t\t        \/\/--- Return 1 to indicate nack received\n    else\n    return 2;\t\t\t        \/\/--- Else return 2 for data transmission failure\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#5c5c5c\">Now the read operation the I<sup>2<\/sup>C protocol is-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"227\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-3-1024x227.jpg\" alt=\"\" class=\"wp-image-2225\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-3-1024x227.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-3-300x66.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-3-768x170.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-3.jpg 1427w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-10 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the first 2 steps are same start and address writes. Now we need new start function with read operation-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-11 wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Read_Start_Add(char read_address) -&gt; Re-start with read<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t I2C_Read_Start_Add(char read_address)\n{\n    uint8_t status;\t\n    TWCR=(1&lt;&lt;TWSTA)|(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT); \/\/--- TWI Interrupt desable, TWI enable, TWI Start\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t           \/\/--- Wait untial start condition is executed\n    status=TWSR&amp;0xF8;\t\t               \/\/--- Read TWI status register\n    if(status!=0x10)\t\t               \/\/--- Check for repeated start transmitted\n    return 0;\t\t\t                   \/\/--- Return 0 for repeated start condition fail \n    TWDR=read_address;\t\t               \/\/--- Write SLA+R in TWI data register\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);\t           \/\/--- TWI Interrupt desable, TWI enable\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t           \/\/--- Wait untial transmet is complete\n    status=TWSR&amp;0xF8;\t\t               \/\/--- Read TWI status register\n    if(status==0x40)\t\t               \/\/--- Check for SLA+R transmitted &amp;ack received\n    return 1;\t\t\t                   \/\/--- Return 1 to indicate ack received\n    if(status==0x48)\t\t               \/\/--- Check for SLA+R transmitted &amp;nack received\n    return 2;\t\t\t                   \/\/--- Return 2 to indicate nack received\n    else\n    return 3;\t\t\t                   \/\/--- Else return 3 to indicate SLA+W failed\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-12 wp-block-paragraph\" style=\"color:#5c5c5c\">In data read operation 8bit data received with acknowledge and if we want to terminate reading we just read with not acknowledge. Let\u2019s look at the read with ack and read with nack function.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-13 wp-block-paragraph\" style=\"color:#6c8a97\">char I2C_Read_Ack(void) -&gt; read with acknowledge<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar I2C_Read_Ack(void)\t  \/\/Read data with Acknowladge\n{\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT)|(1&lt;&lt;TWEA); \/\/--- TWI Interrupt desable, TWI enable, TWI Accknowledge\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));           \/\/--- Wait untial transmet is complete\n    return TWDR;\t\t\t             \/\/--- Return received data\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-14 wp-block-paragraph\" style=\"color:#6c8a97\">char I2C_Read_Nack(void) -&gt; read with not acknowledge<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar I2C_Read_Nack(void)  \/\/Read data without Acknowladge\t\t\n{\n    TWCR=(1&lt;&lt;TWEN)|(1&lt;&lt;TWINT);\t\/\/--- TWI Interrupt desable, TWI enable\n    while(!(TWCR&amp;(1&lt;&lt;TWINT)));\t\/\/--- Wait untial transmet is complete\n    return TWDR;\t\t        \/\/--- Return received data\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-15 wp-block-paragraph\" style=\"color:#5c5c5c\">All we are done with necessary function. Now download the EEPROM.c &amp; EEPROM.h file and the main program is to just write some data to EEPROM and read the data from EEPROM. The read EEPROM data will be display into the LCD screen.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;util\/twi.h&gt;\n#include&lt;string.h&gt;\n#include&quot;EEPROM.h&quot;\n#include&quot;LCD.h&quot;\nchar display&#x5B;20];\nint main(void)\n{\n    char array&#x5B;20] = &quot;External I2C EEPROM&quot;;\t\n    LCD_INIT();\t\t\t\/* Initialize LCD *\/\n    twi_init();\t\t\t\/* Initialize I2C *\/\n    I2C_Start(EEPROM_Write_Addess);\/* Start I2C with device write address *\/\n\t\/***         since add is 2byte ***\/\n    I2C_Write(0x00); \/* Write start 1st byte of memory address for data write *\/\n\tI2C_Write(0x10); \/* Write start 2nd byte of memory address for data write *\/\n    for (int i = 0; i&lt;strlen(array); i++)\/* Write array data *\/\n\t\tI2C_Write(array&#x5B;i]); \n    I2C_Stop();\t\t\t\n    _delay_ms(10);\n    I2C_Start(EEPROM_Write_Addess);\/* Start I2C with device write address *\/\n\t\/***         since add is 2byte ***\/\n    I2C_Write(0x00);\/* Write start 1st byte of memory address for data write *\/\n\tI2C_Write(0x10);\/* Write start 2nd byte of memory address for data write *\/\n    I2C_Read_Start_Add(EEPROM_Read_Addess);\/* Repeat start I2C SLA+R *\/\n    for (int i = 0; i&lt; strlen(array); i++)\/* Read data with acknowledgment *\/\n\t\tdisplay&#x5B;i]=I2C_Read_Ack();\n    I2C_Read_Nack();\t\t\/* Read flush data with nack *\/\n    I2C_Stop();\t\t\t   \n\tLCD_write_string(1,1,display);\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/I2C-EEPROM.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-16 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/I2C-EEPROM.rar\">I2C EEPROM.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>EEPROM stands for electricity erasable programmable read only memory and it is a type of non-volatile memory used in different application. It is read only memory whose data can be erase and re-programmed by higher than normal current of electricity. Many microcontrollers have in build EEPROM. In this article we use AT24C512 serial EEPROM. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2207","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2207"}],"version-history":[{"count":18,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2207\/revisions"}],"predecessor-version":[{"id":2646,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2207\/revisions\/2646"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}