1472 views|0 replies

3836

Posts

19

Resources
The OP
 

AT24C1024 full capacity read and write NXPLPC11XX source program [Copy link]

AT24C1024 full capacity read and write program, as long as the read and write start address and read and write length, the program automatically crosses pages, absolutely original, absolutely easy to use, including test program.

The microcontroller source program is as follows:
  1. #include "nxplpc11xx.h"
  2. #include "i2c.h"
  3. #include "AT24C1024.h"
  4. /************************************************************/
  5. /* Function: Read data from AT24C1024 */
  6. /* Entry parameter: StartAddr: starting address. When the page is crossed after testing, the address will automatically increase by 1. */
  7. /* *pData: put the read data into this array */
  8. /* nBytes: number of data bytes to be written */
  9. /************************************************************/
  10. void AT24C1024_Read(uint32 StartAddr, uint16 nBytes, uint8 *pData)
  11. {
  12. uint8 StartAddr_L, StartAddr_H, P0;
  13. StartAddr_L = StartAddr & 0x0000ff;
  14. StartAddr_H = (StartAddr >> 8) & 0x0ff;
  15. P0 = (StartAddr / 0x10000) << 1;
  16. I2C_Send_Ctrl(AT24C1024_Addr | P0);
  17. I2C_Send_Byte(StartAddr_H);
  18. I2C_Send_Byte(StartAddr_L);
  19. I2C_Stop();
  20. I2C_Send_Ctrl((AT24C1024_Addr | P0) + 1);
  21. while(nBytes != 1)
  22. {
  23. *pData++=I2C_Recieve_Byte(1); // The response bit is ACK, do not release the bus, continue reading
  24. nBytes--;
  25. }
  26. *pData++=I2C_Recieve_Byte(0); // The response bit is NACK, stop reading, and release the bus
  27. I2C_Stop(); // Generate a stop condition
  28. delay_us(10); // This delay is the minimum time interval when reading two segments of data at any address, ensuring that the next start bit is generated normally after the stop bit.
  29. }
  30. /*******************************************************************/
  31. /* Function: write data to AT24C1024 */
  32. /* Entry parameter StartAddr: starting address. When writing across pages, the address data needs to be rewritten. */
  33. /* *pData: write the data in this array */
  34. /* nBytes: number of data bytes to be written */
  35. /*******************************************************************/
  36. void AT24C1024_Write(uint32 StartAddr, uint16 nBytes, uint8 *pData)
  37. {
  38. uint8 StartAddr_L, StartAddr_H, P0;
  39. while(nBytes != 0) // All data has been sent, exit the loop
  40. {
  41. StartAddr_L = StartAddr & 0x0000ff; // Calculate the low address
  42. StartAddr_H = (StartAddr >> 8) & 0x0ff; // Calculate the high address
  43. P0 = (StartAddr / 0x10000) << 1; // Calculate the 17th bit, which is P0
  44. I2C_Send_Ctrl(AT24C1024_Addr | P0); // Send device address with P0
  45. I2C_Send_Byte(StartAddr_H); // Send high address
  46. I2C_Send_Byte(StartAddr_L); // Send low address
  47. while(nBytes != 0) // Data is not sent yet, loop
  48. {
  49. I2C_Send_Byte(*pData);
  50. pData++;
  51. nBytes--;
  52. StartAddr++;
  53. if((StartAddr & 0x00ff) == 0) // Cross page, terminate the current page loop, and rewrite the new address
  54. break;
  55. }
  56. I2C_Stop(); // Generate a stop condition
  57. delay_ms(3); // This delay is the minimum interval between two read and write operations. Do not delete it. It is useful when writing a large amount of data continuously.
  58. }
  59. }
Copy code
  1. /************************************************/
  2. /* AT24C1024 full capacity read and write program */
  3. /* CPU model: LPC1114FBD/302 */
  4. /************************************************/
  5. #include "nxplpc11xx.h"
  6. #include "i2c.h"
  7. #include "AT24C1024.h"
  8. int main()
  9. {
  10. union Initial_Value{uint8 Val_Hex[400];float Val_Float[100];}Init; //Define a floating point array for writing and reading. A floating point number uses 4 bytes.
  11. uint16 i;
  12. float j;
  13. SysCLK_config(); // Clock configuration
  14. I2C_Init(0); // Initialize I2C fast mode
  15. delay_ms(10);
  16. for(i=0; i<100; i++) // Initialize the floating-point array to 0-99 respectively.
  17. {
  18. j = i;
  19. Init.Val_Float[i] = j;
  20. }
  21. AT24C1024_Write(0xff80, 400, Init.Val_Hex); // Write
  22. delay_ms (3);
  23. AT24C1024_Read(0xff80, 400, Init.Val_Hex);
  24. while(1)
  25. {};
  26. }
This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list