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

📄 at24c01.h

📁 利用单片机普通I/0口进行模拟IIC通信
💻 H
字号:
/***********************AT24C01.H**************************/

/****************AT24CXX.h***********************/

#define WD_address 0xb0 // 24c02写命令字    
#define RD_address 0xb1   // 24c02读命令字

#define SDA  PORTA_BIT0
#define SCL  PORTA_BIT1

#define DDR_SDA DDRA_BIT0

//unsigned char F0;

void nop(void)    //Delay  4.7us
{
	 unsigned char d=7;
	 while(d--);
}

void IICstart(void)
{
    DDR_SDA=1;
    SDA=1;
    SCL=1;
    nop();
    SDA=0;
    nop();
    SCL=0;
}

void IICstop(void)
{
	 DDR_SDA=1;
	 SDA=0;
	 SCL=1;
	 nop();
	 SDA=1;
	 nop();
	 SCL=0;
}

void IICack(void)
{
	 DDR_SDA=1;
	 SDA=0;
	 SCL=1;
	 nop();
	 SCL=0;
	 SDA=1;
}

void IICnack(void)
{
	 DDR_SDA=1;
	 SDA=1;
	 SCL=1;
	 nop();
	 SCL=0;
	 SDA=0;
}

void IICwrite_byte(unsigned char c)
{
	 unsigned char i;
	 DDR_SDA=1;
	 for(i=0;i<8;i++)
	 {
	 	  if(c&0x80) SDA=1;
	 	  else SDA=0;
	 	  
	 	  SCL=1;
	 	  nop();
	 	  SCL=0;
	 	  c<<=1;
	 }
	 SDA=1;
	 SCL=1;
	 nop();
	 nop();
	/* if(SDA==1) F0=0;
	 else F0=1;*/
	 SCL=0;
}

unsigned char IICread_byte(void)
{
	  unsigned char i;
	  unsigned char r=0;
	  SDA=1;
	  DDR_SDA=0;
	  for(i=0;i<8;i++)
	  {
	  	 r=r<<1;
	  	 SCL=1;
	  	 nop();
	  	 if(SDA==1) r++;
	  	 SCL=0;
	  }
	  return r;
}

void WriteData_to_24c01(unsigned char address,unsigned char da)
{
    IICstart();
    IICwrite_byte(WD_address);
    IICwrite_byte(address);
    IICwrite_byte(da);
    IICstop();
}
unsigned char GetData_from_24c01(unsigned char address)
{
    unsigned char get_data;
    IICstart();
    IICwrite_byte(WD_address);
    IICwrite_byte(address);
    IICstart();
    IICwrite_byte(RD_address);
    get_data=IICread_byte();
    IICnack();
    IICstop(); 
    return get_data;
}

/*void WriteData_to_24c01(unsigned char address,int da)
{
    unsigned char ch;
    IICstart();
    IICwrite_byte(WD_address);
    IICwrite_byte(address);
    ch=((da&0xff00)>>8);
    IICwrite_byte(ch);
    ch=da&0x00ff;
    IICwrite_byte(ch);
    IICstop(); 
}

int GetData_from_24c01(unsigned char address)
{
    int get_data16;
    unsigned char get_data8;
    
    IICstart();
    IICwrite_byte(WD_address);
    IICwrite_byte(address);
    IICstart();
    IICwrite_byte(RD_address);
    get_data8=IICread_byte();
    get_data16=get_data8;
    IICack();
    get_data8=IICread_byte();
    get_data16<<=8;
    get_data16|=get_data8; 
    IICnack();
    IICstop(); 
    return get_data16;
} */
























⌨️ 快捷键说明

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