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

📄 at24c64.c

📁 AT24C64程序
💻 C
字号:
#include"reg52.h"
#include <intrins.h>
/**************************************/
#define	uchar unsigned char /*宏定义*/
#define	uint  unsigned int
#define	FADDR	0xa0	/*fixed address for AT24Cxx EEPROMs*/
#define	addr_h	0x62
#define	addr_l	0x63
/**************************************/
sbit	SDA1=P1^7;            /*模拟I2C数据传送位*/
sbit	SCL1=P1^6;            /*模拟I2C时钟控制位*/
sbit	wp_24cxx=P3^4;
bit		f_1,f_3;
uint	a1;
/*********************************************/
void	fm_ACK()
{
	SDA1=0;
	_nop_();
	_nop_();
	SCL1=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SCL1=0;
}
/**************************************/
void	fm_NAK()
{
	SDA1=1;
	_nop_();
	_nop_();
	SCL1=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SCL1=0;
}
/*************从AT24C64读出一个字节*************/
uint	fm_shin()
{
	uint	a,b=8;
	SDA1=1;
	while(b-->0)
	{
		_nop_();	
		_nop_();
		SCL1=1;
		_nop_();
		CY=SDA1;
		if(CY==1)
		{
			ACC+=1;
		}
		ACC<<=1;
		_nop_();
		SCL1=0;
	}
	a=ACC;
	return(a);
}
/***********向AT24C64写进一个字节***********************/
uint	fm_shout(i)
{
	uint	b;
	b=8;
	while(b-->0)
	{
		CY=i&0x80;
		i<<=1;
		SDA1=CY;
		_nop_();
		SCL1=1;
		_nop_();	
		_nop_();
		_nop_();
		SCL1=0;
	}
	SDA1=1;
	_nop_();
	b=10;
	while(SDA1==1&&b-->0)
	{
		_nop_();		
	}
	if(SDA1==1)
	{_nop_();}
	else
	{
		SCL1=1;
		_nop_();
		CY=SDA1;
		_nop_();
		_nop_();
		_nop_();
		SCL1=0;
		_nop_();
	}
	return(CY);	
}
/********************************************/
/***********AT24C64初始化*******************/
/*SCL1=P1.6,SDA1=P1.7*/
void	init_AT24C64()/*fm_start*/
{
	SCL1=1;
	SDA1=1;
	_nop_();
	if(SCL1&SDA1==0)
	{
		SCL1=1;
		SDA1=1;
		_nop_();		
		_nop_();
		if(SCL1&SDA1==0)
		{
			CY=1;
		}
	}
	else	if(SCL1&SDA1==1)
	{
		SDA1=0;
		_nop_();		
		_nop_();
		_nop_();		
		_nop_();
		_nop_();
		SCL1=0;
		CY=0;		
	}
	f_1=0;
}
/**********************************/
void	write_addr(uint	addr_h,uint	addr_l)
{
	init_AT24C64();
	if(CY==0)
	{
		a1=FADDR;
		a1&=0xfe;
		fm_shout(a1);
		if(CY==0)
		{
			a1=addr_h;
			fm_shout(a1);
			if(CY==0)
			{
				a1=addr_l;
				fm_shout(a1);
			}
		}
	}
}
/************读AT24C64子程序*******************/
uint	fm_read_block(uint	addr_h,uint	addr_l,uint r2)
{
	uchar	i;
	i=0;
	wp_24cxx=1;/*p3.4=1*/
	write_addr(addr_h,addr_l);
	if(CY==0)
	{
		init_AT24C64();
		if(CY==0)
		{
			a1=FADDR;
			a1&=0xfe;
			fm_shout();	
			while(--r2>0&&f_2==0)
			{
				if(CY==0)
				{
					a1=fm_shin();
					r0[i++]=a1;
					if(r2==1)
					{
						f_2=1;
						fm_NAK();
						_nop_();
					}
					else	
					{
						fm_ACK();
						_nop_();
					}
				}
			}
			CY=0;		
		}	
	}
	fm_stop();
	return(r0[i-1]);
}
/**********************************************/

⌨️ 快捷键说明

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