MSP430F5438 I2C Learning Notes——AT24C02

Publisher:MysticGlowLatest update time:2017-02-20 Source: eefocusKeywords:MSP430F5438  I2C  AT24C02 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0. Introduction

For most MCUs, I2C has become a long-standing problem. Since the 51 era, software simulation of I2C has become the mainstream. Even today when ARMCortex M3 is popular, software simulation of I2C is still the most widely used method. Although software simulation can solve all problems, it always feels that the hardware resources inside the MCU are not fully utilized. After consulting all the books about the MSP430F5 series, there is no application code for hardware I2C. I have explored it through debugging, summarized my experience and shared it with you. I hope you like it. At the same time, the use of I2C can be divided into waiting method and interrupt method. From the perspective of understanding, the waiting method is clear and easy to use. From the perspective of power consumption, the interrupt method can flexibly enter the low power mode, but it is not easy to understand. This article starts with the waiting method.

The use of hardware I2C of the MSP430F5 series generally has the following problems:

[I2C address setting] Generally, the 7-bit address of I2C is written as 8-bit length, and the lowest bit is invalid. For example, the I2C address of AT24C02 is 0xA0, but the real 7-bit address is 0x50. And MSP430 needs to fill in the 7-bit address 0x50.

[I2C stop bit sending] During the I2C read operation, the MCU should send a no response to the slave after reading the last byte. The MSP430F5 series MCU will automatically complete the no response operation. This means that when reading the last byte, the stop bit related register should be operated first.

[I2C start bit transmission] If you carefully analyze the MSP430F5 reference manual, you will find that the I2C start bit is slightly different when sending read and write operations. When writing, you need to write data to TXBUF first, and then you can wait for the TXSTT flag to become 0, which is slightly different from the read and write operations.

【AT24C02 operation timing diagram】


view plain  copy View code snippets on CODEDerive to my code snippet

  1. while( UCB0CTL1& UCTXSTP );  

  2. UCB0CTL1 |= UCTR; // write mode  

  3. UCB0CTL1 |= UCTXSTT; // Send start bit  

  4. // Wait for UCTXSTT=0 to change, but unfortunately the change will not be sent  

  5. while(UCB0IFG& UCTXSTT);  

  6. UCB0TXBUF = word_addr; // Send byte address  



3. Write multiple bytes

3.1 Code Implementation


  1. uint8_teeprom_writepage( uint8_t word_addr, uint8_t *pword_buf, uint8_t len)  

  2. {  

  3.   while( UCB0CTL1& UCTXSTP );  

  4.   UCB0CTL1 |= UCTR; // write mode  

  5.   UCB0CTL1 |= UCTXSTT; // Send start bit  

  6.    

  7.   UCB0TXBUF = word_addr; // Send byte address  

  8.   // Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time and wait for a flag bit  

  9.   while(!(UCB0IFG& UCTXIFG))  

  10.   {  

  11.     if(UCB0IFG& UCNACKIFG) // If no response UCNACKIFG=1  

  12.     {  

  13.       return 1;  

  14.     }  

  15.   }    

  16.    

  17.   for( uint8_t i= 0; i < len; i++)  

  18.   {  

  19.     UCB0TXBUF = *pword_buf++; // Send register content  

  20.     while(!(UCB0IFG& UCTXIFG)); // Wait until UCTXIFG=1     

  21.   }  

  22.    

  23.   UCB0CTL1 |= UCTXSTP;  

  24.   while(UCB0CTL1& UCTXSTP); // Wait for sending to complete  

  25.    

  26.   return 0;  

  27. }  



3.2 Code Analysis

      The multi-byte write function is similar to the single-byte write function and will not be explained in detail.


4. Read a single byte

The single-byte read function is the most complex of the four read and write functions. The reason for the complexity is that the UCTXSTP flag needs to be operated before reading the last byte.

4.1 Code Implementation


  1. uint8_teeprom_readbyte( uint8_t word_addr, uint8_t *pword_value)  

  2. {  

  3.   UCB0CTL1 |= UCTR; // write mode  

  4.   UCB0CTL1 |= UCTXSTT; // Send start bit and write control byte  

  5.    

  6.   UCB0TXBUF = word_addr; //Send byte address, TXBUF must be filled first  

  7.   // Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time and wait for a flag bit  

  8.   while(!(UCB0IFG& UCTXIFG))  

  9.   {  

  10.     if(UCB0IFG& UCNACKIFG) // If no response UCNACKIFG=1  

  11.     {  

  12.       return 1;  

  13.     }  

  14.   }                         

  15.    

  16.   UCB0CTL1 &= ~UCTR; //Read mode  

  17.   UCB0CTL1 |= UCTXSTT; // Send start bit and read control byte  

  18.    

  19.   while(UCB0CTL1& UCTXSTT); // Wait until UCTXSTT=0  

  20.   // If no response UCNACKIFG = 1  

  21.   UCB0CTL1 |= UCTXSTP; //Send stop bit first  

  22.    

  23.   while(!(UCB0IFG& UCRXIFG)); // Read byte content  

  24.   *pword_value = UCB0RXBUF; // Read BUF register after sending stop bit  

  25.    

  26.   while( UCB0CTL1& UCTXSTP );  

  27.    

  28.   return 0;  

  29. }  


4.2 Code Analysis

This code gives the illusion that the MSP430 sends a stop bit first and then reads a byte. In fact, this is not the case. During an I2C read operation, after the host reads the last byte, it should send a no-acknowledgement NACK (no-acknowledgement is different from an acknowledgement) to the slave, and then the host sends a stop bit. In order to complete this combined action, the MSP430 requires the user to operate the UCTXSTP flag in advance, and perform a "combined action" of sending NACK and the I2C stop bit after reading RXBUF.



  1. while(!(UCB0IFG& UCRXIFG));  

  2. *pword_value = UCB0RXBUF; // Read BUF register after sending stop bit  

  3. UCB0CTL1 |= UCTXSTP; // Send stop bit  



      The above code may cause subsequent I2C operations to fail.


5. Read multiple bytes

5.1 Code Implementation


  1. uint8_t eeprom_readpage(uint8_t word_addr, uint8_t *pword_buf, uint8_t len ​​)  

  2. {  

  3.   while( UCB0CTL1& UCTXSTP );  

  4.   UCB0CTL1 |= UCTR; // write mode  

  5.   UCB0CTL1 |= UCTXSTT; // Send start bit and write control byte  

  6.    

  7.   UCB0TXBUF = word_addr; // Send byte address  

  8.   // Wait for UCTXIFG=1 and UCTXSTT=0 to change at the same time and wait for a flag bit  

  9.   while(!(UCB0IFG& UCTXIFG))  

  10.   {  

  11.     if(UCB0IFG& UCNACKIFG) // If no response UCNACKIFG=1  

  12.     {  

  13.       return 1;  

  14.     }  

  15.   }    

  16.    

  17.   UCB0CTL1 &= ~UCTR; // Read mode  

  18.   UCB0CTL1 |= UCTXSTT; // Send start bit and read control byte  

  19.    

  20.   while(UCB0CTL1& UCTXSTT); // Wait until UCTXSTT=0  

  21.   // If no response UCNACKIFG = 1  

  22.    

  23.   for( uint8_t i= 0; i< len -1; i++)  

  24.   {  

  25.     while(!(UCB0IFG& UCRXIFG)); // Read byte content, excluding the last byte content  

  26.     *pword_buf++= UCB0RXBUF;  

  27.   }  

  28.    

  29.   UCB0CTL1 |= UCTXSTP; // Send stop bit before receiving the last byte  

  30.    

  31.   while(!(UCB0IFG& UCRXIFG)); // Read the last byte  

  32.   *pword_buf = UCB0RXBUF;  

  33.    

  34.   while( UCB0CTL1& UCTXSTP );  

  35.    

  36.   return 0;  

  37. }  



5.2 Code Analysis

      Reading a single byte is similar to writing a single byte. The only thing to note is that the write operation needs to fill the TXBUF first, while the read operation does not have this problem. Imagine that when an I2C write operation is performed, a byte of content must be written to the I2C slave, so it is reasonable to fill the TXBUF first. After filling the TXBUF, the MSP430 will perform a series of actions - sending the I2C start bit, the I2C read controller, and writing the first byte of the slave.


6 Unit Testing

      The unit test is divided into two parts. The single-byte write function and the single-byte read function are grouped together. First, use the single-byte write function to write something to a certain address, and then use the single-byte read function to read something. If the written parameters are the same as the read content, the test passes. The multi-byte write function and the multi-byte read function are grouped together. The test process is similar, except that the content written changes from one to 8 consecutive bytes. Please note that the page size of AT24C02 is 8. If starting from the page header address, the maximum number of bytes to write is 8.

      In addition, there needs to be a 10ms delay after the EEPROM write operation, otherwise the write and read operations will not be possible. Please refer to the AT24C02 data sheet for details.

6.1 Test Code



  1. void eeprom_config()  

  2. {  

  3. #ifDEBUF_EEPROM_I2C  

  4.    

  5.   uint8_t test_byte1 =0x0B;  

  6.   uint8_t test_byte2 = 0x01;  

  7.    

  8.   /* 

  9.     Step 1 Write a value to address 0x00, for example 0x0B 

  10.           Then read the address 0x00 and determine whether the value is 0x0B 

  11.   */  

  12.   eeprom_writebyte(0x00, test_byte1);   

  13.   delay_ms(10);  

  14.   eeprom_readbyte(0x00,&test_byte2);  

  15.   assert_param( test_byte1== test_byte2 );  

  16.    

  17.   if( test_byte1 == test_byte2 )  

  18.   {  

  19.     printf( "Byte Read andByte Write Test Pass\r\n" );     

  20.   }  

  21.    

  22.   /* 

  23.     Step 2: Use address 0x08 as the starting address and write 8 bytes of data continuously 

  24.           Then read 8 bytes from the starting address and compare the written and read bytes. 

  25.           The condition for success is that the contents of the bytes written and read are the same 

  26.   */  

  27.   uint8_t test_buf1[8]= {1,2,3,4,5,6,7,8};  

  28.   uint8_t test_buf2[8]= {0,0,0,0,0,0,0,0};  

  29.    

  30.   eeprom_writepage(0x08, test_buf1, 8);  

  31.   delay_ms(10);  

  32.   eeprom_readpage(0x08, test_buf2, 8);  

  33.   assert_param( memcmp( test_buf1, test_buf2,8) == 0 );  

  34.    

  35.   if(!memcmp(test_buf1, test_buf2,8))  

  36.   {  

  37.     printf("Page Read andPage Write Test Pass!\r\n");     

  38.   }  

  39.    

  40. #endif  

  41. }  


6.2 Test Results


Keywords:MSP430F5438  I2C  AT24C02 Reference address:MSP430F5438 I2C Learning Notes——AT24C02

Previous article:How to achieve low power consumption under FreeRTOS - MSP430F5438 platform
Next article:CC2530 RF part use - to achieve point-to-point transmission and reception

Recommended ReadingLatest update time:2024-11-16 15:55

24LC65 I2C EEPROM Byte Read and Write Driver
/*—————————————————————— 〖Description〗24LC65 I2C EEPROM byte read and write driver, chip A0-A1-A2 needs to be connected to VCC. Currently missing page write, page read, and CRC check programs. The following program has been verified by 50 units, and the effect of batch needs to be examined . For safety reasons, ma
[Microcontroller]
I2C 24LC02 C read and write routines (PIC microcontroller)
1 I2C bus characteristics    The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up t
[Microcontroller]
stm32 hal i2c library read and write sd3088 clock
Corrections from the previous version. sd3008 does a bus reset 0.5s at the beginning of each communication, so there is no need to consider the i2c bug problem of stm32. And, should the HAL library also consider patching this bug in the software? Using HAL_I2C_Mem_Read/Write makes the code look very nice. Simula
[Microcontroller]
Design of I2C Touch Screen Based on ARM Processor S3C2440 and Linux
0Introduction With the development of computer-related technologies, ARM embedded systems are being used more and more widely, and are becoming more and more closely integrated with people's lives. Touch screen devices are widely used in this embedded field because of their friendly human-computer interacti
[Microcontroller]
Implementation of I2C Bus Interface Based on NiosII
This paper proposes the "NiosⅡ+AT24C02" design scheme. Based on the introduction of the main characteristics of the AT24C02A chip and the analysis of the I2C communication protocol principle, it focuses on the design method of the interface circuit between Nios Ⅱ and AT24C02A, including the register definition of th
[Analog Electronics]
Implementation of I2C Bus Interface Based on NiosII
[51 MCU] EEPROM 24c02 [I2C code package-save to realize running light]
Here, EEPROM 24c02 is packaged and can be directly called in the future. Its connection method is: SDA-P2.1; SCL-P2.0; WP-VCC _ :i2c.c   1 /*-----------------------------------------------   2 Name: IIC Protocol    3 Content: The function uses software delay method to generate SCL pulse, so it is necessary to make
[Microcontroller]
Simulating I2C communication of PIC microcontroller
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;                      Copyright (C) 1997 by Innovatus ; This code may be distributed and used freely provided that this ; copyright notice stays intact and that any modifications are noted. ; For more information about Innovatus: http://www.in
[Microcontroller]
I2C protocol of 51 series microcontroller
/*------------------------------------------------ ----  Name: IIC protocol PCF8591AD/DA conversion  Compiled by: Fu Xin  Date:2012/5/9  Platform: Keil 4, Ly-51S learning board  The pin definitions are as follows:  Connect with 51:  Content: The function uses software delay to generate SCL pulses, so the high crystal
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号