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

📄 e2prom.c

📁 单相 CPU卡表复费率电能表,内部有提供I2C驱动,CPU卡驱动,NEC单片机0513等
💻 C
字号:
#include "includes.h"

unsigned short E2PTemp1;
unsigned char E2PTemp0;
unsigned char E2pErrIndication = 0;

void AptoticE2promStart(void)
{
    E2P_SDA = 1;
    E2P_SCL = 1;
    E2P_SDA = 0;
    E2P_SCL = 0;
}

void AptoticE2promStop(void)
{
    E2P_SDA = 0;
    E2P_SCL = 1;
    E2P_SDA = 1;
    E2P_SCL = 0;
}

void AptoticE2promSck(void)
{
    E2P_SCL = 1;
    NOP();
    NOP();
    E2P_SCL = 0;
    NOP();
}

void AptoticE2promNoAck(void)
{
    E2P_SCL = 0;
    E2P_SDA = 1;
    E2P_SCL = 1;
    E2P_SCL = 0;
}

void AptoticE2promAck(void)
{
    E2P_SCL = 0;
    E2P_SDA = 0;
    E2P_SCL = 1;
    E2P_SCL = 0;
}

unsigned char AptoticE2promReadByte(void)
{
    unsigned char rdbyte = 0, BitCounter = 8;

    E2P_SDA_DIR = 1;
    do{
        rdbyte<<=1;
        E2P_SCL = 1;
        if(E2P_SDA) rdbyte++;
        E2P_SCL = 0;
    }while(--BitCounter);
    E2P_SDA_DIR = 0;
    
    return(rdbyte);
}

void AptoticE2promWriteByte(unsigned char wrbyte)
{
    unsigned short wait = 0;
    unsigned char ii;

    for( ii = 0; ii < 8; ii++ )
    {
        if( wrbyte & 0x80 ) 
        {
            E2P_SDA = 1;
        }
        else
        {
            E2P_SDA = 0;
        }
        AptoticE2promSck();
        wrbyte<<=1;
    }

    E2P_SDA_DIR = 1;
    E2P_SCL = 1;

    while( E2P_SDA )
    {
        if( wait++>= 1000 )
        {
            E2pErrIndication = 1;
            break;
        }
    }
    
    E2P_SCL = 0;
    E2P_SDA_DIR = 0;       
}

void E2promBegin()
{	
    AptoticE2promStart();
    AptoticE2promWriteByte(E2PTemp0);
    AptoticE2promWriteByte((unsigned char)(E2PTemp1>>8));
    AptoticE2promWriteByte((unsigned char)(E2PTemp1));
}

void E2promRead(unsigned short addr, unsigned char * data, unsigned char len)
{
#if 0
    unsigned char  temph,templ;
    unsigned char  i,j = 0;

    temph = (unsigned char)( addr / 256 );
    templ = (unsigned char)( addr % 256 );
    
ReadRestar:	
    E2pErrIndication = 0;
    AptoticE2promStart();
    AptoticE2promWriteByte(0xA0);
    AptoticE2promWriteByte(temph);
    AptoticE2promWriteByte(templ);
    AptoticE2promStart();
    AptoticE2promWriteByte(0xA1);

    for( i = 0;i < len-1; i++ )
    {
        *(data+i) = AptoticE2promReadByte();	
        AptoticE2promAck();
    }
    
    *(data+i) = AptoticE2promReadByte();
    AptoticE2promNoAck();
    AptoticE2promStop();
    E2P_SCL = 1;
    E2P_SDA = 1;
    if( E2pErrIndication )
    {
        j++;
        if( j < 3 ) goto ReadRestar;
    }	
#endif
    unsigned char i,j=0;	
	
ReadRestar:
    E2pErrIndication=0;
    E2PTemp0=0xA0;
    E2PTemp1=addr;		    			
    for(i=0;i<len;i++,E2PTemp1++)
    {    
        E2promBegin();
        AptoticE2promStart();
        AptoticE2promWriteByte(E2PTemp0+1);  		
        data[i]=AptoticE2promReadByte();             
        AptoticE2promNoAck();
        AptoticE2promStop();		
    }	
    E2P_SCL = 1;
    E2P_SDA = 1;   
    if(E2pErrIndication)
    {
        j++;
        if(j<3) goto ReadRestar;
    } 	  
}

void E2promWrite(unsigned short addr, unsigned char * data, unsigned char len)
{
#if 0
    unsigned char temph,templ;
    unsigned char i, j = 0;

    temph = (unsigned char)( addr / 256 );
    templ = (unsigned char )( addr % 256 );
     
WriteRestar:
    E2pErrIndication = 0;
    AptoticE2promStart();
    AptoticE2promWriteByte(0xA0 );
    AptoticE2promWriteByte(temph);
    AptoticE2promWriteByte(templ);      
    E2P_WP = 0;
    for(i=0;i<len;i++)
    {
        AptoticE2promWriteByte(*(data+i));
        delay(3000);
    }
    AptoticE2promStop();
    E2P_WP = 1;
    E2P_SCL = 1;
    E2P_SDA = 1;
    if( E2pErrIndication )
    {
        j++;
        if( j < 3 ) goto WriteRestar;
    }
 #endif
     unsigned char i,j=0;
	
WriteRestar:
    E2pErrIndication=0;
    E2PTemp0=0xA0;
    E2PTemp1=addr;        		
    E2P_WP = 0;    
    for(i=0;i<len;i++,E2PTemp1++)
    {
        E2promBegin();
        AptoticE2promWriteByte(data[i]);
        AptoticE2promStop();			
        delay(800);
        clr_wdt();
    }        
    AptoticE2promStop();	   	
    E2P_WP = 1;
    E2P_SCL = 1;
    E2P_SDA = 1;
    if(E2pErrIndication)
    {
        j++;
        if(j<3) goto WriteRestar;
    }
}

⌨️ 快捷键说明

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