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

📄 at24c04.c

📁 嵌入式TCP/IP模块
💻 C
字号:
#include "config.h"
#include "../include/cfg_net.h"

/*********************************************************/ 
uint8 ReadEEprom(uint8 *p, uint16 startaddr,uint8 length)
 {
 	uint8 i;
 	i=startaddr>>8;
 	i<<=1;
 	i&=0x0e;
 	i|=0xa0;
 	I2CStart_S();
 	if(I2CSendByte_S(i))
 	    goto   ErrorSendStop;
 	if(I2CSendByte_S(startaddr)) 	
  	    goto   ErrorSendStop;
  	    
 	I2CStart_S();
 	if(I2CSendByte_S(0xa1))
 	    goto   ErrorSendStop;  	    
  	    
 	for(i=0;i<length;i++)
 	{
 	   *p++=I2CReceiveByte_S();
 	   I2CSendAck_S();
 	}  
 	I2CReceiveByte_S();
 	I2CSendNck_S();  	
 	I2CStop_S();
 	return(0x00);
ErrorSendStop:
 	I2CStop_S();
 	return(0xff);       	  
}
/**************************************************/
uint8 WriteEEprom(uint8 *p, uint16 startaddr,uint8 length)
{
 	uint8 i;
 	i=startaddr>>8;
 	i<<=1;
 	i&=0x0e;
 	i|=0xa0;
 	I2CStart_S();
 	if(I2CSendByte_S(i))
 	    goto   ErrorSendStop;
 	if(I2CSendByte_S(startaddr)) 	
  	    goto   ErrorSendStop;

  	for(i=0;i<length;i++)
  	{
  	     if(I2CSendByte_S(*p++))
  	        goto   ErrorSendStop;  	     
  	} 
 	I2CStop_S();
	Delayms(10);
 	return(0x00);   	
ErrorSendStop:
 	I2CStop_S();
 	return(0xff);           	  	    	
}
/**********************************************************/
void I2CStart_S(void)
{
    uint32 i;
    i=SDA;
    i|=SCL;
    IO0DIR|=i;         //作为输出口
    IO0SET=i;       //输出高
	__asm
	{
	   nop
	   nop
	};
	IO0CLR=SDA; //输出低
	__asm
	{
	   nop
	   nop
	}	
	IO0CLR=SCL; //输出低	
}
/*************************************************************/
void I2CStop_S(void)
{
	IO0CLR=SDA; //输出低
	__asm
	{
	   nop
	   nop
	}	
    IO0SET=SCL;
	__asm
	{
	   nop
	   nop
	}
    IO0SET=SDA;
	__asm
	{
	   nop
	   nop
	}		    
	IO0CLR=SCL;
}
/**************************************************************/
void I2CSendAck_S(void)
{
	IO0CLR=SDA; //输出低
	__asm
	{
	   nop
	   nop
	}	
    IO0SET=SCL;
	__asm
	{
	   nop
	   nop
	}
	IO0CLR=SCL;
}
/***************************************************************/
void I2CSendNck_S(void)
{
    IO0SET=SDA;
	__asm
	{
	   nop
	   nop
	}	
    IO0SET=SCL;
	__asm
	{
	   nop
	   nop
	}
	IO0CLR=SCL;
}
/******************************************************************/
uint8 I2CSendByte_S(uint8 j)
{
     uint8 i,x;
     uint32 a;
     x=j;
     for(i=0;i<8;i++)
     {
         if(j&0x80)
               IO0SET=SDA;
         else
	           IO0CLR=SDA; //输出低
         IO0SET=SCL;
	     __asm
	     {
	          nop
	          nop
         }	
         IO0CLR=SCL;           
         j<<=1;              	 
     }	
     IO0SET=SDA;
	 __asm
	 {
	   nop
	   nop
	 }	
	 a=~SDA;           
	 IO0DIR&=a;      //SDA 作为输入
     IO0SET=SCL;
     i=200;
     while(i--)
     {
     	 if(!(IO0PIN&SDA))
     	 {
     	    IO0DIR|=SDA; //SDA 作为输出
     	    IO0CLR=SCL; 
     	    return(0); 
     	 }    
     }
     IO0DIR|=SDA; //SDA 作为输出     
     IO0CLR=SCL;  
     return(1);    
}
/****************************************************/
uint8 I2CReceiveByte_S(void)
{
     uint8 i,j;
     uint32 a;
	 a=~SDA;           
	 IO0DIR&=a;      //SDA 作为输入     
     for(i=0;i<8;i++)
     {
     	j<<=1;     	
     	IO0SET=SCL;
	    __asm
	    {
	      nop
	      nop
	    }	 
	    if((IO0PIN&SDA))    	
     	  j|=0x01;
        IO0CLR=SCL; 
     }	
     IO0DIR|=SDA; //SDA 作为输出     
     return(j);
}

⌨️ 快捷键说明

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