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

📄 24cxx.c

📁 24c02-24c512 c51驱动
💻 C
字号:
/*24Cxx子程序*/
#include <REG52.H>
#include <intrins.h>

#define AT24cxxBufLen         64
#define AT24cxxDriverAddressW 0xa0
#define AT24cxxDriverAddressR 0xa1

sbit I2C_SCK=P3^4;
sbit I2C_SDA=P3^5;
sbit DOG=P1^4;

#define Delay_10_uS(void) _nop_();_nop_();
/*
void  Delay_10_uS(void)
{
//    char i=10;
//	while(i--);
//    char i=1;
//	while(i--);
}
*/

//以下是n_milisecond=10时的测试情况
//晶振频率      i值    =10时       =5时
//11.0592MHz :  i=113  10.00000ms  50.0868ms
//12MHz      :  i=123  10.01600ms
//22.1184MHz :  i=229  10.03472ms
//22.1184MHz :  i=228  09.99132ms
void Delay_N_mS( unsigned int n_milisecond)
{
	unsigned char i;
	while(n_milisecond--)
	{
        DOG=!DOG;
    	i=123;
		while(i--);
	}
}
static bit I2C_Start(void)
{
	Delay_10_uS();
	I2C_SDA =1;
	Delay_10_uS();
	I2C_SCK =1;
	Delay_10_uS();
	if ( I2C_SDA == 0) return 0;
	if ( I2C_SCK == 0) return 0;
	I2C_SDA = 0;
	Delay_10_uS();
	I2C_SCK = 0;
	Delay_10_uS();
	return 1;
}
static void  I2C_Stop(void)
{
	Delay_10_uS();
	I2C_SDA = 0;
	Delay_10_uS();
	I2C_SCK = 1;
	Delay_10_uS();
	I2C_SDA = 1;
	Delay_10_uS();
}

static void I2C_Ack(void)
{
	Delay_10_uS();
	I2C_SDA=0;
	Delay_10_uS();
	I2C_SCK=1;
	Delay_10_uS();
	I2C_SCK=0;
	Delay_10_uS();
}

static void I2C_Nack(void)
{
	Delay_10_uS();
	I2C_SDA=1;
	Delay_10_uS();
	I2C_SCK=1;
	Delay_10_uS();
	I2C_SCK=0;
	Delay_10_uS();
}
static bit I2C_Send_Byte(unsigned char d)
{
	unsigned char i = 8;
	bit bit_ack;
	while( i-- )
	{
    DOG=!DOG;
		Delay_10_uS();
		if ( d &0x80 )   I2C_SDA =1;
		else             I2C_SDA =0;
		Delay_10_uS();
		I2C_SCK = 1;
		Delay_10_uS();
		I2C_SCK = 0;
		d = d << 1;
	}
	Delay_10_uS();
	I2C_SDA = 1;
	Delay_10_uS();
	I2C_SCK = 1;
	Delay_10_uS();
	bit_ack = I2C_SDA;
	I2C_SCK =0;
	Delay_10_uS();
	return bit_ack;
}
static unsigned char I2C_Receive_Byte(void)
{
	unsigned char i = 8, d;
	Delay_10_uS();
	I2C_SDA = 1;
	while ( i--)
	{
    DOG=!DOG;
		d = d << 1;
		Delay_10_uS();
		I2C_SCK =1;
		if ( I2C_SDA ) d++;
		Delay_10_uS();
		I2C_SCK =0;
	}
	return d;
}

void AT24Cxx_W(void *McuAddress,unsigned int AT24CxxAddress,unsigned int count)
{
		I2C_Start();
		I2C_Send_Byte( AT24cxxDriverAddressW );
		I2C_Send_Byte( (unsigned char )AT24CxxAddress / 256 );			//24c16及以下注释掉这句
		I2C_Send_Byte( (unsigned char )AT24CxxAddress % 256 );
	while(count--)
	{
		I2C_Send_Byte( *(unsigned char*)McuAddress );
		((unsigned char*)McuAddress)++;
		AT24CxxAddress++;
		if((AT24CxxAddress%AT24cxxBufLen)==0)
		{
			I2C_Stop();
            Delay_N_mS(20);
			DOG=!DOG;
			I2C_Start();
			I2C_Send_Byte( AT24cxxDriverAddressW );
			I2C_Send_Byte( (unsigned char )AT24CxxAddress / 256 );		//24c16及以下注释掉这句
			I2C_Send_Byte( (unsigned char )AT24CxxAddress % 256 );
            DOG=!DOG;
		}
	}
	I2C_Stop();
	Delay_N_mS(20);
}
void AT24Cxx_R(void *McuAddress,unsigned int AT24CxxAddress,unsigned int count)
{

	{
		I2C_Start();
		I2C_Send_Byte( AT24cxxDriverAddressW );
		I2C_Send_Byte( (unsigned char )AT24CxxAddress / 256 );			//24c16及以下注释掉这句
		I2C_Send_Byte( (unsigned char )AT24CxxAddress % 256 );
		I2C_Start();
		I2C_Send_Byte( AT24cxxDriverAddressR );
		*(unsigned char*)McuAddress = I2C_Receive_Byte();
		while(--count)
		{
            DOG=!DOG;
			I2C_Ack();
			((unsigned char*)McuAddress)++;
			*(unsigned char*)McuAddress = I2C_Receive_Byte();
			DOG=!DOG;
		}
		I2C_Nack();
		I2C_Stop();
	}
}



⌨️ 快捷键说明

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