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

📄 at24c02.c

📁 0834实现数据的采集
💻 C
字号:
#include <reg52.h>
#define uchar  unsigned char
#define uint   unsigned int

#define	PAGE_SIZE		7
sbit sda_rom=P1^4;
sbit clk_rom=P1^3;

void Set_Sda(bit sw)
{
	 sda_rom=sw;
}

void Set_Scl(bit sw)
{
	 clk_rom=sw;
}

void Set_Start_Condition()
{
	 Set_Scl(0);
	 Set_Sda(1);
	 Set_Scl(1);
	 Set_Sda(0);
}

void Set_Stop_Condition()
{
	 Set_Scl(0);
	 Set_Sda(0);
	 Set_Scl(1);
	 Set_Sda(1);
}

void Send_Byte_Memory(uchar  value)
{
	 uchar  i,shift;
	 shift=0x80;
	 for(i=0;i<8;i++)
	 {
	         Set_Scl(0);
        	 if(value&shift)
		 {
         		Set_Sda(1);
         	 }
         	 else
         	 {
			Set_Sda(0);
         	 }
		 shift>>=1;
		 Set_Scl(1);
	 }
	 Set_Scl(0);
	 Set_Sda(1);
	 Set_Scl(1);
}

uchar Revice_Byte_Memory()
{
	uchar  i,value=0;
      	for(i=0;i<8;i++)
	{
		value<<=1;
		Set_Scl(1);
		if(sda_rom) value|=1;
		Set_Scl(0);
	}
	return value;
}

bit Write_Device(uint  addr)
{
	 uchar  i,k;
	 i=(addr>>8)&0xff;
	 i<<=1;
	 i|=0xa0;

	 for(k=0;k<100;k++)
	 {
		Set_Start_Condition();    //Set_Sda(0);此指令前data=clk=1;产生起始位
	   	Send_Byte_Memory(i);
	   	if(!sda_rom) goto ack_ok;
	 }
	 return 1;
ack_ok:
	 i=addr&0xff;
	 Send_Byte_Memory(i);
	 return 0;
}

//return =0 fail
//	 =1 success
bit ReadEeprom(uchar *s,int  addr,int  len)
{
	 int i;
	 if(Write_Device(addr))	return 0;
     i=(addr>>8)&0xff;
	 i<<=1;
	 i|=0xa1;
     	Set_Start_Condition();
     	Send_Byte_Memory(i);
     	Set_Scl(0);
	Set_Sda(1);
	s[0]=Revice_Byte_Memory();
	for(i=1;i<len;i++)
	{
		Set_Sda(0);
        	Set_Scl(1);
        	Set_Scl(0);
        	Set_Sda(1);
		s[i]=Revice_Byte_Memory();
	}

     	Set_Sda(1);
	Set_Scl(1);
	Set_Stop_Condition();
	return 1;
}

//return =1 success
//	 =0 fail
bit WriteEeprom(uchar  *s, int  addr,int  len)
{
	 uchar temp;
start_label:
	 if(Write_Device(addr))return 0;
	 while(len>0)
	 {
		len--;
		Send_Byte_Memory(*s);
		//temp=addr&PAGE_SIZE;
		addr++;
		s++;
        temp=addr&PAGE_SIZE;
		if(temp==0)           //原版:if(temp==PAGE_SIZE)
		{
			Set_Stop_Condition();
	   		goto start_label;
		}
	 }
	 Set_Stop_Condition();
	 return 1;
}


⌨️ 快捷键说明

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