📄 memory.c
字号:
#include <reg51.h>
#include <main.h>
#include <memory.h>
#define PAGE_SIZE 7
//uchar code table_bit[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
extern uchar code table_bit[8];
sbit SCL=P1^6;
sbit SDA=P1^7;
void Set_Start_Condition(void)
{
SCL=LOW;
Delay();
SDA=HIGH;
Delay();
SCL=HIGH;
Delay();
SDA=LOW;
}
void Set_Stop_Condition(void)
{
SCL=LOW;
Delay();
SDA=LOW;
Delay();
SCL=HIGH;
Delay();
SDA=HIGH;
}
void Send_Byte_Memory(uchar value)
{
uchar i;
for(i=8;i>0;i--)
{
SCL=LOW;
if(TEST_BIT(value,i-1))
SDA=HIGH;
else
SDA=LOW;
SCL=HIGH;
Delay();
}
SCL=LOW;
SDA=HIGH;
SCL=HIGH;
}
uchar Revice_Byte_Memory(void)
{
uchar i,value=0;
for(i=8;i>0;i--)
{
SCL=HIGH;
Delay();
if(SDA)
SET_BIT(value,i-1);
SCL=LOW;
Delay();
}
return value;
}
uchar Write_Device(uchar addr)
{
uchar i,k;
for(k=0;k<100;k++)
{
SDA=LOW;
Delay();
Send_Byte_Memory(0xa0);
// SDA=HIGH; //ADD THIS 2002-04-19
if(SDA==HIGH)continue;
else goto ack_ok;
}
return 1;
ack_ok:
i=addr&0xff;
Send_Byte_Memory(i);
return 0;
}
uchar Read_From_Memory(uchar *s,uchar addr,uchar len)
{
uchar i;
if(Write_Device(addr))
return 1;
Set_Start_Condition();
Send_Byte_Memory(0xa1);
SCL=LOW;
SDA=HIGH;
s[0]=Revice_Byte_Memory();
for(i=1;i<len;i++)
{
SDA=LOW;
SCL=HIGH;
SCL=LOW;
SDA=HIGH;
s[i]=Revice_Byte_Memory();
}
SCL=HIGH;
SDA=HIGH;
Set_Stop_Condition();
return 0;
}
uchar Write_To_Memory(uchar *s,uchar addr,uchar len)
{
uchar temp;
start_label:
if(Write_Device(addr))
return 1;
while(len>0)
{
len--;
Send_Byte_Memory(*s);
temp=addr&PAGE_SIZE;
addr++;
s++;
if(temp==PAGE_SIZE)
{
Set_Stop_Condition();
goto start_label;
}
}
Set_Stop_Condition();
return 0;
}
void Delay(void)
{
;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -