AVR (ATMEGA_I2C, WTI function) external EEPROM_AT24C64 read and write

Publisher:数字梦想Latest update time:2016-10-19 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/***************************AT24C header file****************************/

#ifndef __i2c_24c64_H__
#define __i2c_24c64_H__

/****************************
 I2C status definition
 MT master mode transmission MR master mode reception
******************************/
#define START 0x08
#define RE_START 0x10
#define MT_SLA_ACK 0x18
#define MT_SLA_NOACK 0x20
#define MT_DA TA_ACK 0x28
#define MT_DA TA_NOACK 0x30

#define MR_SLA_ACK  0x40
#define MR_SLA_NOACK 0x48
#define MR_DATA_ACK  0x50
#define MR_DATA_NOACK 0x58

#define RD_DEVICE_ADDR 0xA1 //The first 4 bits are fixed, the last 3 bits depend on the connection, and the last bit is the read/write instruction bit
#define WD_DEVICE_ADDR 0xA0

/*Common TWI operations (master mode write and read)*/
#define Start() (TWCR=(1< #define Stop() (TWCR=(1< #define Wait() {while(!(TWCR&(1< #define TestAck() (TWSR&0xf8) //Observe the return status
#define SetAck (TWCR|=(1< #define SetNoAck (TWCR&=~(1< #define Twi() (TWCR=(1< #define Write8Bit(x) {TWDR=(x);TWCR=(1<

void twi_init(void); //TWI initialization
uchar I2C_Write(uchar Wdata,uint RegAddress); //write a byte
uchar I2C_Read(uint RegAddress); //read a byte
uchar I2C_Write_num(uchar *Wdata,uint RegAddress,uchar num); //write NUM bytes

#endif

/***************************AT24C64C file****************************/

#include "config.h"

void twi_init(void)
{
    TWBR=0X20;
 TWSR=0;
 TWCR=0X44;
}

/************************************************
I2C bus writes a byte
Return 0: write successful
Return 1: write failed
**********************************************/
uchar I2C_Write(uchar Wdata,uint RegAddress)
{
   Start(); //I2C starts
   Wait();
   if(TestAck()!=START) 
   return 1; //ACK
   
   Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
   Wait();
   if(TestAck()!=MT_SLA_ACK) 
   return 1; //ACK
      
   Write8Bit(RegAddress/256); //Write the corresponding register address of the device Note:

  Write8Bit(RegAddress/256);                                                  24c02

   Wait();
   if(TestAck()!=MT_DA TA_ACK)
  return 1;
         //ACK
      Write8Bit(RegAddress%256); 
     Write8Bit(RegAddress%256); 
     Wait();
     if (TestAck()!=MT_DA TA_ACK) 
 return 1;
  
   Write8Bit(Wdata); //Write data to the corresponding register of the device
   Wait();
   if(TestAck()!=MT_DA TA_ACK) 
   return 1; //ACK 
   
   Stop(); //I2C stops
   delayms(100); //Delay
   return 0;

/************************************************
I2C bus reads a byte
Returns 0: Read successful
Returns 1: Read failed
**********************************************/
uchar I2C_Read(uint RegAddress)
{
   uchar temp;
   Start();//I2C starts
   Wait();
   if (TestAck()!=START) 
    return 1; //ACK    
    
   Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
   Wait(); 
   if (TestAck()!=MT_SLA_ACK) 
    return 1; //ACK
    
   Write8Bit(RegAddress/256); //Write device corresponding register address
    Write8Bit(RegAddress/256); 
  Wait();
   if (TestAck()!=MT_DA TA_ACK) 
  return 1;
  
    Write8Bit(RegAddress%256); 
    Write8Bit(RegAddress%256); 
     Wait();
     if (TestAck()!=MT_DA TA_ACK) 
 return 1;
    
   Start(); //I2C restart
   Wait();
   if (TestAck()!=RE_START)  
    return 1;
    
   Write8Bit(RD_DEVICE_ADDR); //Write I2C slave device address and read mode
   Wait();
   if(TestAck()!=MR_SLA_ACK)  
    return 1; //ACK
    
   Twi(); //Start master I2C read mode
   Wait();
   if(TestAck()!=MR_DA TA_NOACK) 
    return 1; //ACK 
    
   temp=TWDR; //Read I2C receive data
   Stop(); //I2C stop
   return temp;
}

/****************************************************
I2C bus writes multiple bytes
Return 0: write successful
Return 1: write failed
**********************************************/
uchar I2C_Write_num(uchar *Wdata,uint RegAddress,uchar num)
{
   uchar j;
   Start(); //I2C start
   Wait();
   if(TestAck()!=START) 
   return 1; //ACK
   
   Write8Bit(WD_DEVICE_ADDR); //Write I2C slave device address and write mode
   Wait();
   if(TestAck()!=MT_SLA_ACK) 
   return 1; //ACK
      
   Write8Bit(RegAddress/256); //Write device corresponding register address
  Write8Bit(RegAddress/256); 
   Wait();
   if(TestAck()!=MT_DA TA_ACK) 
  return 1; 
         //ACK
      Write8Bit(RegAddress%256); 
     Write8Bit(RegAddress%256); 
     Wait();
     if (TestAck()!=MT_DA TA_ACK) 
 return 1;
  for(j=0;j    {
      Write8Bit(*Wdata); //Write data to the corresponding register of the device
      Wait();
      if(TestAck()!=MT_DA TA_ACK) 
      return 1; //ACK 
   Wdata++;
   RegAddress++; //Address plus 1
   }  
      Stop(); //I2C stops
      delayms(100); //Delay
      return 0;
}

Keywords:AVR Reference address:AVR (ATMEGA_I2C, WTI function) external EEPROM_AT24C64 read and write

Previous article:AVR MCU external interrupt INT0 example
Next article:AVR serial communication program RS232 header file

Latest Microcontroller Articles
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号