📄 at17c256.c
字号:
#include "2410addr.h"
#include "at17lv.h"
unsigned char wrbuf[PAGE_SIZE];
unsigned char rdbuf[PAGE_SIZE];
unsigned char databuf[32768];
unsigned char getbuf[32768];
unsigned int temp;
unsigned char SendByte(unsigned char byte, unsigned char order);
unsigned char GetByte(unsigned char lastbyte);
void WriteSCL(unsigned char state);
void WriteSDA(unsigned char state);
void WritePage(unsigned int address, unsigned char *bufptr);
void ReadPage(unsigned int address, unsigned char *bufptr);
void Init(void);
void Delay(void);
void Delaytime(void);
void SendStopBit(void);
void SendStartBit(void);
void Reset(void);
void C_Entry()
{
unsigned int address,i, j = 0;
unsigned int error=0;
char *p = (char *)0x31000000;
Init();
for (i = 0; i < 32768; i++)
databuf[i] =*(p+i);
for(address=0,j=0;address<MAX_PAGES;address++)
{
for (i = 0; i < PAGE_SIZE; i++,j++)
wrbuf[i] =databuf[j];
WritePage(address,&wrbuf[0]);
Delaytime();
}
for(address=0,j=0;address<MAX_PAGES;address++)
{
ReadPage(address,&rdbuf[0]);
for (i = 0; i < PAGE_SIZE; i++,j++)
getbuf[j]=rdbuf[i];
}
for (i = 0,temp=0; i < 32768; i++)
if(getbuf[i]!=databuf[i])
{
temp++;
error=1;
}
Reset();
address=0x0;
ReadPage(address,&rdbuf[0]);
address=0xff;
ReadPage(address,&rdbuf[0]);
Reset();
address=0xff;
ReadPage(address,&rdbuf[0]);
address=0x1ff;
ReadPage(address,&rdbuf[0]);
for(address=0;address<MAX_PAGES;address++)
ReadPage(address,&rdbuf[0]);
temp=0;
while(1);
}
//100~400kHz
void Delay(void)
{
int i=0;
while(i++<12)
{};
return;
}
void Delaytime(void)
{
int i=0;
while(i++<6000)
{};
return;
}
void Init(void)
{
/* GPE3 = SDA - bidirectional */
/* GPE5 = SCL - output */
/* GPE7 = SER_EN - output */
/* GPE11 = CE - output */
/* GPE4 = RESET/OE - output */
rGPEUP=0x0ffff;
rGPEDAT=0;
}
void WritePage(unsigned int address, unsigned char *bufptr)
{
unsigned char i;
unsigned char addr1;
unsigned char addr2;
rGPECON = 0x0fd7757ff;
// rGPEDAT=rGPEDAT&0x0f7ff; /* bring SER_EN low */
// rGPEDAT=rGPEDAT&0x0ff7f; /* bring RESET/OE low */
// rGPEDAT=rGPEDAT&0x0fdff; /* bring CE low */
Delay();
Delay();
addr1 = (unsigned char)(address >> 2);
addr2 = (unsigned char)(address << 6);
SendStartBit();
SendByte(AT17 + WRITE,MSB_FIRST); /* send device address byte with write bit */
SendByte(addr1,MSB_FIRST); /* 1st address byte */
SendByte(addr2,MSB_FIRST); /* 2nd address byte */
for (i = 0; i < PAGE_SIZE; i++)
SendByte(bufptr[i],LSB_FIRST);
SendStopBit();
// rGPEDAT=rGPEDAT|0x0800; /* bring SER_EN high */
// rGPEDAT=rGPEDAT|0x80; /* bring RESET/OE high */
// rGPEDAT=rGPEDAT|0x0200; /* bring CE high */
Delay();
rGPEDAT=0;
Delay();
}
void ReadPage(unsigned int address, unsigned char *bufptr)
{
unsigned char i;
unsigned char addr1;
unsigned char addr2;
rGPECON = 0x0fd7757ff;
// rGPEDAT=rGPEDAT&0x0f7ff; /* bring SER_EN low */
// rGPEDAT=rGPEDAT&0x0ff7f; /* bring RESET/OE low */
// rGPEDAT=rGPEDAT&0x0fdff; /* bring CE low */
Delay();
Delay();
addr1 = (unsigned char)(address >> 2);
addr2 = (unsigned char)(address << 6);
SendStartBit();
SendByte(AT17 + WRITE,MSB_FIRST); /* send device address byte */
SendByte(addr1,MSB_FIRST); /* 1st address byte */
SendByte(addr2,MSB_FIRST); /* 2nd address byte */
SendStartBit();
SendByte(AT17 + READ,MSB_FIRST); /* send device address byte with read bit */
rGPEDAT=rGPEDAT&0x0efff;
for (i = 0; i < (PAGE_SIZE-1); i++)
bufptr[i] = GetByte(0);
bufptr[(PAGE_SIZE-1)] = GetByte(1); /* 1 signals last byte of read sequence */
rGPEDAT=rGPEDAT|0x1000;
SendStopBit();
// rGPEDAT=rGPEDAT|0x0800; /* bring SER_EN high */
// rGPEDAT=rGPEDAT|0x80; /* bring RESET/OE high */
// rGPEDAT=rGPEDAT|0x0200; /* bring CE high */
Delay();
rGPEDAT=0;
Delay();
}
unsigned char SendByte(unsigned char byte, unsigned char order)
{
unsigned char i;
unsigned char error;
for (i = 0; i < 8; i++)
{ Delay();
if (order)
{
WriteSDA(byte & 0x80);
byte = byte << 1;
}
else
{
WriteSDA(byte & 0x01);
byte = byte >> 1;
}
Delay();
WriteSCL(1);
Delay();
Delay();
WriteSCL(0);
Delay();
}
rGPECON = 0x0fd7753ff;
Delay();
WriteSCL(1);
Delay();
/* SDA at 0 ?*/
if(rGPEDAT&0x20==0)
error = 0x1; /* ack didn't happen if bit 0 = 1 */
else error =0x0;
Delay();
rGPECON = 0x0fd7757ff;
WriteSCL(0);
Delay();
return(error);
}
unsigned char GetByte(unsigned char lastbyte)
{
/* lastbyte == 1 for last byte */
unsigned char i, bit;
unsigned char result = 0;
rGPECON = 0x0fd7753ff; /* SDA to input */
//rGPEDAT=rGPEDAT&0x0ffdf;
for (i = 0;i < 8;i++)
{
/* each bit at a time, LSB first */
Delay();
WriteSCL(1);
Delay();
if((rGPEDAT&0x20)==0)bit=0x0;
else bit=0x1;
result = (bit << (i)) | result;
Delay();
WriteSCL(0);
Delay();
}
/* send ACK */
rGPECON = 0x0fd7757ff; /* SDA to output */
WriteSDA(0); /* no ack on last byte ... lastbyte = 1 for the last byte */
Delay();
WriteSCL(1);
Delay();
Delay();
WriteSCL(0);
Delay();
rGPECON = 0x0fd7753ff; /* SDA to input */
return(result);
}
void SendStartBit(void)
{
WriteSCL(0);
WriteSDA(1);
Delay();
WriteSCL(1);
Delay();
WriteSDA(0);
Delay();
WriteSCL(0);
Delay();
}
void SendStopBit(void)
{
WriteSCL(0);
WriteSDA(0);
Delay();
WriteSCL(1);
Delay();
WriteSDA(1);
Delay();
WriteSCL(0);
Delay();
}
void WriteSCL(unsigned char state)
{
if (state)
rGPEDAT=rGPEDAT|0x40;
else
rGPEDAT=rGPEDAT&0x0ffbf;
}
void WriteSDA(unsigned char state)
{
if (state)
rGPEDAT=rGPEDAT|0x20;
else
rGPEDAT=rGPEDAT&0x0ffdf;
}
void Reset(void)
{
unsigned char result = 0;
rGPECON = 0x0fd7757ff;
rGPEDAT=rGPEDAT&0x0fdff; /* bring CE low */
rGPEDAT=rGPEDAT&0x0ff7f; /* bring RESET/OE low */
Delay();
rGPEDAT=rGPEDAT|0x0800; /* bring SER_EN high */
Delay();
SendStartBit();
SendByte(AT17 + WRITE,MSB_FIRST); /* send device address byte with write bit */
SendByte(0x3f,MSB_FIRST); /* 1st address byte */
SendByte(0xff,MSB_FIRST); /* 2nd address byte */
SendByte(0xff,LSB_FIRST);
SendStopBit();
Delaytime();
SendStartBit();
SendByte(AT17 + READ,MSB_FIRST); /* send device address byte with read bit */
SendStopBit();
result=GetByte(0);
rGPEDAT=rGPEDAT|0x0800; /* bring SER_EN high */
Delay();
rGPEDAT=rGPEDAT|0x0200; /* bring CE high */
Delay();
rGPEDAT=rGPEDAT|0x080; /* bring RESET/OE high */
// rGPEDAT=rGPEDAT&0xff7f; /* bring RESET/OE low */
Delay();
rGPEDAT=0;
Delay();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -