#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
#define MT_DA
#define MR_SLA_ACK 0x40
#define MR_SLA_NOACK 0x48
#define MR_DA
#define MR_DA
#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)*/ void twi_init(void); //TWI initialization #endif /***************************AT24C64C file****************************/ #include "config.h" void twi_init(void) /************************************************ Write8Bit(RegAddress/256); 24c02 Wait(); /************************************************ /****************************************************
#define Start() (TWCR=(1<
#define SetAck (TWCR|=(1<
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
{
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:
if(TestAck()!=MT_DA
return 1;
//ACK
Write8Bit(RegAddress%256);
Write8Bit(RegAddress%256);
Wait();
if (TestAck()!=MT_DA
return 1;
Write8Bit(Wdata); //Write data to the corresponding register of the device
Wait();
if(TestAck()!=MT_DA
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
return 1;
Write8Bit(RegAddress%256);
Write8Bit(RegAddress%256);
Wait();
if (TestAck()!=MT_DA
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
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
return 1;
//ACK
Write8Bit(RegAddress%256);
Write8Bit(RegAddress%256);
Wait();
if (TestAck()!=MT_DA
return 1;
for(j=0;j
Write8Bit(*Wdata); //Write data to the corresponding register of the device
Wait();
if(TestAck()!=MT_DA
return 1; //ACK
Wdata++;
RegAddress++; //Address plus 1
}
Stop(); //I2C stops
delayms(100); //Delay
return 0;
}
Previous article:AVR MCU external interrupt INT0 example
Next article:AVR serial communication program RS232 header file
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- The clock signal is followed by an inverter.
- EEWORLD University Hall----Live Replay: TI Wireless Product Update: Wi-Sun Standard Helps Smart City Construction
- TI automotive solution puzzle, do you dare to challenge it?
- [Fifth Batch of Shortlist] GigaDevice GD32L233 Review Event
- [SAMR21 new gameplay] 8. Serial communication-1
- Mobile station update development board, welcome to borrow it!!
- 3. "Wanli" Raspberry Pi car - Python learning (timing task)
- This is a camera development board, but where is the camera?
- Sharing the principle of the adapter solution for charging while listening to music
- Make announces its return