⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cfunctions.c

📁 采用IAR在线调试ADI公司基于ARM7内核的ADUC7026的I2CDownloadMaster代码
💻 C
字号:
/*********************************************************************

 Author        : ADI - Apps            www.analog.com/MicroConverter

 Date          : August. 2006

 File          : main.c, I2C_Functions.c, I2C_Functions.h

 Hardware      : ADuC702x Rev I silicon, MicroChip 24LC128

                  ADuC702x        ->      24LC128
                  P1.2  ( SCL )           SCL     ( Pin 6 )
                  P1.3  ( SDA )           SDA     ( Pin 5 )
                  P3.2                    WP      ( Pin 7 )

                 24LC128 Write Address = 0xA0
                 24LC128 Read  Address = 0xA1

 Description   : Example program for integration of MicroChip 24LC128
                 I2C Serial EEPROM Module.

                 8 Bytes of data are written to the Serial EEPROM and 
                 then read back.

                 This file contains Example I2C functions used in main.c

*********************************************************************/

#include "ioADuC7024.h"

unsigned char Read_Current_Address(void);
void Byte_Write(unsigned short Destination_Address, unsigned char c_data);
void Page_Write(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes);
unsigned char Byte_Read(unsigned short Destination_Address);
void Byte_Read_8(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes);

/*********************************************************************

 Author         : ADI - Apps            www.analog.com/MicroConverter

 Date           : Aug. 2006

 Function Name  : void Byte_Read_8(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes)

Arguement      :  unsigned short Destination_Address:  16 Bit Data Address.  
                  unsigned char *c_data: Pointer to Data to Read
                  unsigned char Number_of_Bytes: Number of bytes to Read
 
 Description    : This function reads the number ( Number_of_bytes) of 8 bit bytes 
                  specified in c_data from the location specified by Destination_Address.

 Note           : This function does not return until command is completed

                  The maximuim number of bytes read at any one time must not exceed
                  8 bytes. 

                  The address counter in the 24LC128 is corrupted after this function

*********************************************************************/

void Byte_Read_8(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes)
{
  unsigned long i;

  I2C1ADR  =    0xA0;       // Set Address Write Command  

  // Fill FIFO with Destination Address
  I2C1MTX  =    ( Destination_Address & 0xff00 ) >> 8;
  I2C1MTX  =    Destination_Address & 0xff ;

  
  while( ( I2C1MSTA & 0x40 ) == 0x40 )
//  while( ( I2C1MSTA & 0x2 ) == 0x0 )    
  {}                        // What until Master is finished command

  I2C1CNT = Number_of_Bytes -1; // Set number of bytes to read
  I2C1ADR = 0xA1;               // Transmit Recieve Address

  for ( i = 0 ; i <  Number_of_Bytes ;i++)
  {
    // Wait until Master recieve data
    while((I2C1MSTA & 0x8) == 0x0)
    {}
    *c_data = I2C1MRX;          // Read Data
    *c_data ++ ;                // Increment pointer    
  }   
  
  while( ( I2C1MSTA & 0x40 ) == 0x40 )
  {}                        // What until Master is finished command 
}

/*********************************************************************

 Author         : ADI - Apps            www.analog.com/MicroConverter

 Date           : Aug. 2006

 Function Name  : void Page_Write(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes)

Arguement      :  unsigned short Destination_Address:  16 Bit Data Address.  
                  unsigned char *c_data: Pointer to Data to Write
                  unsigned char Number_of_Bytes: Number of bytes to write
 
 Description    : This function writes the number ( Number_of_bytes) of 8 bit bytes 
                  specified in c_data to location specified by Destination_Address.

 Note           : This function does not return until command is completed

                  The maximuim number of bytes written at any one time should not exceed
                  64 bytes. Otherwise data will be overwritten on the 24LC128.
                  Please refer to Note on Page 8 of DS21191N

                  The address counter in the 24LC128 is corrupted after this function

*********************************************************************/

void Page_Write(unsigned short Destination_Address, unsigned char *c_data, unsigned char Number_of_Bytes)
{
  unsigned long I2C1MSTA_TEMP= 0xFF;
  unsigned char I2C_NACK=1;
  int i;

  // Fill FIFO with Destination Address
  
  I2C1MTX  =    ( Destination_Address & 0xff00 ) >> 8;
  I2C1MTX  =    Destination_Address & 0xff ;

  I2C1ADR  =    0xA0;       // Set Address Write Command
  
  while( I2C1MSTA & 0x1 )   // Wait until Master TX FIFO is empy
  {}
    
  for ( i = 0; i < Number_of_Bytes ; i++ )
  {
    I2C1MTX  = ( *c_data  & 0xFF );// Transmit data
    while( I2C1MSTA & 0x1 )   // Wait until Master TX FIFO is empy
    {}
    *c_data ++ ;              // Increment pointer
  }

  while( ( I2C1MSTA & 0x40 ) == 0x40 )
  {}                        // What until Master is finished command
  
  // The following code polls the 24LC128 until it recieves an ACK which
  // signifies that the Data has been written to the EEPROM.
  // This is Documented in MircoChip's 24LC128 Datasheet DS21191N Page9 
  while (I2C_NACK)
  {
    I2C_NACK = 0;
    I2C1ADR = 0xA0;
    while (( ( I2C1MSTA_TEMP = I2C1MSTA ) & 0x40 ) ==0x40 )
    {
      if (( I2C1MSTA_TEMP & 0x10 ) == 0x10)
      {
        I2C_NACK = 1;
      }      
    }
  }
}

/*********************************************************************

 Author         : ADI - Apps            www.analog.com/MicroConverter

 Date           : Aug. 2006

 Function Name  : unsigned char Byte_Read(unsigned short Destination_Address)

Arguement      :  unsigned short Destination_Address:  16 Bit Data Address.  
 
 Description    : This function returns the 8 bit specified by Destination_Address.

 Note           : This function does not return until command is completed

*********************************************************************/

unsigned char Byte_Read(unsigned short Destination_Address)
{
  // Fill FIFO with Destination Address
  
  I2C1MTX  =    ( Destination_Address & 0xff00 ) >> 8;
  I2C1MTX  =    Destination_Address & 0xff ;

  I2C1ADR  =    0xA0;       // Set Address Write Command

  while( ( I2C1MSTA & 0x40 ) == 0x40 )
  {}                        // What until Master is finished command

  I2C1CNT = 0;              // Read a Single 8 Bit Byte
  I2C1ADR = 0xA1;           // Transmit read Address

  // Wait until Master recieve data
  while((I2C1MSTA & 0x8) == 0x0)
  {}

  return(I2C1MRX);          // Read Data Read

}

/*********************************************************************

 Author         : ADI - Apps            www.analog.com/MicroConverter

 Date           : Aug. 2006

 Function Name  : void Byte_Write(unsigned short Destination_Address, unsigned char c_data)

Arguement      :  unsigned short Destination_Address:  16 Bit Data Address.  
                  unsigned char c_data: 8 Bit Data to write

 Description    : This function writes 8 bit specified in c_data to location
                  specified by Destination_Address.

 Note           : This function does not return until command is completed

*********************************************************************/

void Byte_Write(unsigned short Destination_Address, unsigned char c_data)
{
  unsigned long I2C1MSTA_TEMP= 0xFF;
  unsigned char I2C_NACK=1;

  // Fill FIFO with Destination Address
  
  I2C1MTX  =    ( Destination_Address & 0xff00 ) >> 8;
  I2C1MTX  =    Destination_Address & 0xff ;

  I2C1ADR  =    0xA0;       // Set Address Write Command
  while( I2C1MSTA & 0x1 )   // Wait until Master TX FIFO is empy
  {}
  I2C1MTX  =    c_data ;    // Transmit Data to write

                            // Wait until Master I2C is finsihed
  while( ( I2C1MSTA & 0x40 ) == 0x40 )
  {}

  // The following code polls the 24LC128 until it recieves an ACK which
  // signifies that the Data has been written to the EEPROM.
  // This is Documented in MircoChip's 24LC128 Datasheet DS21191N Page9
  
  while (I2C_NACK)
  {
    I2C_NACK = 0;
    I2C1ADR = 0xA0;
    while (( ( I2C1MSTA_TEMP = I2C1MSTA ) & 0x40 ) ==0x40 )
    {
      if (( I2C1MSTA_TEMP & 0x10 ) == 0x10)
      {
        I2C_NACK = 1;
      }      
    }
  }
}

/*********************************************************************

 Author         : ADI - Apps            www.analog.com/MicroConverter

 Date           : Aug. 2006

 Function Name  : unsigned char Read_Current_Address()

 Arguement      : none.

 Description    : This function reads the current address counter of the 24LC128

 Note           : This function does not return until command is completed

*********************************************************************/

unsigned char Read_Current_Address()
{

  I2C1CNT =     0;        // Set number of bytes to read equal to 1
  I2C1ADR = 	0xA1;	  // set i2c address	

  // Wait until Master recieve data
  while((I2C1MSTA & 0x8) == 0x0)
  {}
  return(I2C1MRX);      // Return Current Address
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -