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

📄 24c64.c

📁 EEPROM 24C64的读写操作
💻 C
字号:
#include <reg52.h>
#include "sel_def.h"
//-----------------------------------------------------------------------------
/********************************************************************
bit   I2C_Start(void);
void  I2C_Stop(void);
void  I2C_Ack(void);
void  I2C_Nack(void);
bit   I2C_Send_Byte( uchar);
uchar I2C_Receive_Byte(void);
void  AT24C64_R(void *mcu_address,uint AT24C64_address,uint count);
void  AT24C64_W(void *mcu_address,uint AT24C64_address,uint count);
*********************************************************************/
unsigned int E_addr=0x0000;
uchar R_d;
//-----------------------------------------------------------------------------
void  Delay_10_uS(void)
{
    char i=5;
	while(i--);
}
void Delay_N_mS( uchar n_milisecond)  /* n mS delay */
{
	uchar i;
  	while(n_milisecond--)
    {
      	i=37;
		while(i--);
	}
}
//-----------------------------------------------------------------------------
bit I2C_Start(void)
{
	Delay_10_uS();
    SDA =1;
    Delay_10_uS();
    SCL =1;
    Delay_10_uS();
    if ( SDA == 0) return 0;
    if ( SCL == 0) return 0;
    SDA = 0;
    Delay_10_uS();
    SCL = 0;
    Delay_10_uS();
    return 1;
}
//-----------------------------------------------------------------------------
void  I2C_Stop(void)
{
    Delay_10_uS();
    SDA = 0;
    Delay_10_uS();
    SCL = 1;
    Delay_10_uS();
    SDA = 1;
    Delay_10_uS();
}
//-----------------------------------------------------------------------------
void I2C_Ack(void)
{
    Delay_10_uS();
    SDA=0;
    Delay_10_uS();
    SCL=1;
    Delay_10_uS();
    SCL=0;
    Delay_10_uS();
}
//-----------------------------------------------------------------------------
void I2C_Nack(void)
{
    Delay_10_uS();
    SDA=1;
    Delay_10_uS();
    SCL=1;
    Delay_10_uS();
    SCL=0;
    Delay_10_uS();
}
//-----------------------------------------------------------------------------
//送数据(字节),返回结果BIT_ACK
//成功返回0,失败返回1
bit I2C_Send_Byte( uchar d)
{
    uchar i = 8;
    bit bit_ack;
    while( i-- )
    {
        Delay_10_uS();
        if ( d &0x80 )   
			SDA =1;
        else            
	    	SDA =0;
        Delay_10_uS();
        SCL = 1;
        Delay_10_uS();
        SCL = 0;
        d = d << 1;
    }
    Delay_10_uS();
    SDA = 1;
	Delay_10_uS();
    SCL = 1;
    Delay_10_uS();
    bit_ack = SDA;
    SCL =0;
    Delay_10_uS();
    return bit_ack;
}
//-----------------------------------------------------------------------------
//从EEPROM中读取数据
uchar I2C_Receive_Byte(void)
{
	uchar i = 8, d;
	Delay_10_uS();
    SDA = 1;
    while ( i--)
    {
		d = d << 1;
        Delay_10_uS();
        SCL =1;
        if ( SDA )         d++;
        Delay_10_uS();
        SCL =0;
    }
    return d;
}
//-----------------------------------------------------------------------------
//
void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
{
	bit i=1;
    while(count--)
    {
		while(i)
		{
			i=I2C_Start();
		}
  		/*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/  /* 24C16  USE */
        //I2C_Send_Byte( 0xa0 );
        //I2C_Send_Byte(  AT24C64_address/256 );
		I2C_Send_Byte( 0xa0 + (AT24C64_address / 256)<<1);
        I2C_Send_Byte( AT24C64_address %256 );
        I2C_Send_Byte( *(uchar*)mcu_address );
        I2C_Stop();
        //Delay_N_mS(10);       /* waiting for write cycle to be completed */
        ((uchar*)mcu_address)++;
        AT24C64_address++;
	}
}
//-----------------------------------------------------------------------------
void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)
{
    while(count--)
    {
		
		I2C_Start();
  		I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );   // 24C16 USE 
        //I2C_Send_Byte( 0xa0 );
        //I2C_Send_Byte( AT24C64_address/256 );
        I2C_Send_Byte( AT24C64_address % 256 );
        I2C_Start();
  		I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );
        //I2C_Send_Byte( 0xa1 );
        *(uchar*)mcu_address = I2C_Receive_Byte();
        I2C_Nack();
        I2C_Stop();
        ((uchar*)mcu_address)++;
        AT24C64_address++;
	}
}
//-----------------------------------------------------------------------------
void main(void)
{
	uchar d[2]={1,2};
	while(E_addr<0x1FF0)
	{
		AT24C64_W(d,E_addr,2);
		E_addr+=2;
	}
	RCAP2L=0xDC;
	RCAP2H=0xFF;
	RCLK=1;						//接收波特率发生器
	TCLK=1;						//发送波特率发生器
	C_T2=0;						//定时模式
	CP_RL2=0;					//重装载
	TR2=1;
	SCON=0x50;	
	ES=1;
	EA=1;
	E_addr=0x0000;
	AT24C64_R(&R_d,E_addr,1);
	E_addr+=1;
	SBUF=R_d;
	while(1);
}

void interrupt_serial(void) interrupt 4
{
	if(RI)
	{
		RI=0;
	}
	if(TI)
	{
		AT24C64_R(&R_d,E_addr,1);
		SBUF=R_d;
		E_addr+=1;
	}
}

⌨️ 快捷键说明

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