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

📄 i2c._c

📁 AVR系列被广泛应用的单片机MEGA8开发(原理图)及例程
💻 _C
字号:
#include <iom8v.h>
#include <macros.h>
#include "Uart.h"
#include"I2C.H"

void I2C_Delay(unsigned int delay_time)
{
while(delay_time)
delay_time--;

}
unsigned char i2c_Write(unsigned char RomAddrL,unsigned char Wdata) 
{       PORTC|=0x38;
	  Delayms(10  );
	  Start();//I2C启动
	  Wait();
	  if(TestAck()!=START) 
	  {PutStr("ERROR1");goto end_i2c_write;} //ACK
	  
	  Write8Bit(wr_device_add);//写I2C从器件地址和写方式
	  Wait();
	  if(TestAck()!=MT_ADDR_ACK)
	    	{PutStr("ERROR2");goto end_i2c_write;} //ACK
	    	
	  Write8Bit(RomAddrL);//写24C02的ROM地址
	  Wait();
	  if(TestAck()!=MT_DATA_ACK)
	  	{PutStr("ERROR4");goto end_i2c_write;} //ACK

	  Write8Bit(Wdata);//写数据到24C02的ROM
	  Wait();
	  if(TestAck()!=MT_DATA_ACK) 	
	  	{PutStr("ERROR5");goto end_i2c_write;} //ACK
	  Stop();//I2C停止
	    twi_Disable();
Delayms(10  );
	  return 0;
end_i2c_write:
	
	 Stop();//I2C停止
	  twi_Disable();
	  	  return 1;
	
}

/******************************************
      I2C总线读一个字节(Mega8 as Master)
		   如果读失败也返回0
*/
unsigned char i2c_Read(unsigned char RomAddrL)       //this is a sample of reading 24c01(EEPROM)
      {
	   unsigned char temp;
	   PORTC|=0x38;
	   Delayms(10  );
	   Start();//I2C启动
	   Wait();
	   if (TestAck()!=START) return 0;//ACK	   
	   Write8Bit(wr_device_add);//写I2C从器件地址和写方式
	   Wait(); 
	   if (TestAck()!=MT_ADDR_ACK) return 0;//ACK
	  
	   Write8Bit(RomAddrL);//写24C02的ROM地址
	   Wait();
	   if (TestAck()!=MT_DATA_ACK) return 0;

	   Start();//I2C重新启动
	   Wait();
	   if (TestAck()!=RE_START)  return 0;
	   Write8Bit(rd_device_add);//写I2C从器件地址和读方式
	   Wait();
	   if(TestAck()!=MR_ADDR_ACK)  return 0;//ACK
	   Twi();//启动主I2C读方式
	   Wait();
	   if(TestAck()!=MR_DATA_NOACK) return 0;//ACK	
	   temp=TWDR;//读取I2C接收数据
           Stop();//I2C停止
            twi_Disable();
	   return temp;
      }


/*
unsigned char i2c_Write(unsigned char RomAddrH,unsigned char RomAddrL,unsigned char Wdata) 
{
	  Start();//I2C启动
	 // PutStr("ERROR10");
	//  Wait();
	//  if(TestAck()!=START) 
	//  {PutStr("ERROR1");goto end_i2c_write;} //ACK
	  Write8Bit(wr_device_add);//写I2C从器件地址和写方式
	  PutStr("ERROR11");
	  Wait();
	  if(TestAck()!=MT_ADDR_ACK)
	    	{PutStr("ERROR2");goto end_i2c_write;} //ACK
	  Write8Bit(RomAddrH);//写24C02的ROM地址
	  PutStr("ERROR12");
	  Wait();
	  if(TestAck()!=MT_DATA_ACK)
	  	{PutStr("ERROR3");goto end_i2c_write;} //ACK
	  Write8Bit(RomAddrL);//写24C02的ROM地址
	  PutStr("ERROR13");
	  Wait();
	  if(TestAck()!=MT_DATA_ACK)
	  	{PutStr("ERROR4");goto end_i2c_write;} //ACK

	  Write8Bit(Wdata);//写数据到24C02的ROM
	  PutStr("ERROR14");
	  Wait();
	  if(TestAck()!=MT_DATA_ACK) 	
	  	{PutStr("ERROR5");goto end_i2c_write;} //ACK
	  	PutStr("ERROR15");
	  Stop();//I2C停止
	    twi_Disable();
// 	  delay_ms(10);//延时等EEPROM写完

	  return 0;
end_i2c_write:
	
	 Stop();//I2C停止
	  twi_Disable();
	  return 1;
	
}*/

/******************************************
      I2C总线读一个字节(Mega8 as Master)
		   如果读失败也返回0
*/
/*unsigned char i2c_Read(unsigned char RomAddrH,unsigned char RomAddrL)       //this is a sample of reading 24c01(EEPROM)
      {
	   unsigned char temp;
	   Start();//I2C启动
	   Wait();
	   if (TestAck()!=START) return 0;//ACK	   
	   Write8Bit(wr_device_add);//写I2C从器件地址和写方式
	   Wait(); 
	   if (TestAck()!=MT_ADDR_ACK) return 0;//ACK
	   Write8Bit(RomAddrH);//写24C02的ROM地址
	   Wait();
	   if (TestAck()!=MT_DATA_ACK) return 0;
	   Write8Bit(RomAddrL);//写24C02的ROM地址
	   Wait();
	   if (TestAck()!=MT_DATA_ACK) return 0;

	   Start();//I2C重新启动
	   Wait();
	   if (TestAck()!=RE_START)  return 0;
	   Write8Bit(rd_device_add);//写I2C从器件地址和读方式
	   Wait();
	   if(TestAck()!=MR_ADDR_ACK)  return 0;//ACK
	   Twi();//启动主I2C读方式
	   Wait();
	   if(TestAck()!=MR_DATA_NOACK) return 0;//ACK	
	   temp=TWDR;//读取I2C接收数据
           Stop();//I2C停止
            twi_Disable();
	   return temp;
      }


*/

⌨️ 快捷键说明

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