at24c512驱动程序
2008-06-27 09:09 AT24C512Çý??#include
#include
#include
#include
#define BAUDRATE 9600
#define SYSCLK 11059200
#define uchar unsigned char
#define uint unsigned int
#define _Nop() _nop_(),_nop_(),_nop_(),_nop_() /*??Òå?ÕÖ?Áî*/
uchar save_buf[7];
sbit WP =P1^0;
sbit SCL =P1^1;
sbit SDA =P1^2;
sbit led =P3^5;void IICpwrite(unsigned char highaddress,unsigned char
lowaddress,unsigned char *psr);
void IICpread(unsigned char highaddress,unsigned char lowaddress,unsigned char *dst);
void ByteWrite(unsigned char bytedata,unsigned char highaddress,unsigned char lowaddress);
uchar ByteRead(unsigned char highaddress,unsigned char lowaddress);
void IICWriteByte(unsigned char sendbyte); void IICWriteByte(unsigned char sendbyte); uchar IICReadByte(void);
void IICStart(void);
void IICStop(void);
void IICAck(void);
void Noack(void);
void main()
{
WP=0;_Nop();_Nop();_Nop();_Nop();_Nop();_Nop();_Nop();_Nop();led=0
;
save_buf[0]=1;
save_buf[1]=2;
save_buf[3]=3;
IICpread(0x00,0x00,save_buf);
// IICpwrite(0x00,0x00,save_buf);
// save_buf[0]=ByteRead(0x00,0x00);
// ByteWrite(save_buf[0],0x00,0x02);
while(1);} /*******************Ò?ÃæÐ?Êý?Ý*********************/
void IICpwrite(unsigned char highaddress,unsigned char lowaddress,unsigned char *psr)
{
int i;
// WP=0;_nop_();_nop_();_nop_();_nop_();
IICStart();
IICWriteByte(0xa0);
IICWriteByte(highaddress); IICWriteByte(lowaddress); for(i=0;i<5;i++)
{ IICWriteByte(*psr++); } IICStop();
// WP=1;
}/*******************Ò?Ãæ?ÁÊý?Ý********************/
void IICpread(unsigned char highaddress,unsigned char
lowaddress,unsigned char *dst) {
int i;//WP=0;
IICStart();
IICWriteByte(0xa0);
IICWriteByte(highaddress); IICWriteByte(lowaddress); IICStart();
IICWriteByte(0xa1);
for(i=0;i<5;i++)
{
*dst++=IICReadByte();
if(i<4) IICAck();
else Noack();
}
IICStop();
// WP=1;
}
/*******************Ëæ?úÐ?Êý?Ý*********************/
void ByteWrite(unsigned char bytedata,unsigned char
highaddress,unsigned char lowaddress)
{
IICStart();
IICWriteByte(0xa0);
IICWriteByte(highaddress); IICWriteByte(lowaddress); IICWriteByte(bytedata);
IICStop();
// Delay2(10000);
}/**********************Ëæ?ú?ÁÊý?Ý**********/
uchar ByteRead(unsigned char highaddress,unsigned char lowaddress)
{
unsigned char ReadByte;
IICStart();
IICWriteByte(0xa0);
IICWriteByte(highaddress); IICWriteByte(lowaddress); IICStart();
IICWriteByte(0xa1);
ReadByte=IICReadByte();
IICStop();
return(ReadByte);
}/*******************ÏòIIC×ÜÏßÐ?Êý?Ý*********************/
void IICWriteByte(unsigned char sendbyte)
{
unsigned char j;
// SCL=0;
for(j=8;j>0;j--)
{
sendbyte<<=1; //ÎÞÂÛC51ÔõÑùʵÏÖÕâ?ö?Ù×???Ê?ÖÕ?áÊ?CY=sendbyte^7;
SDA=CY;
SCL=1;
SCL=0; }
SDA=1;
SCL=1;
while(SDA);
SCL=0;
}
/**********************?ÓIIC×ÜÏßÉÏ?ÁÊý?Ý×Ó?ÌÐò**********/
uchar IICReadByte(void)
{
uchar receivebyte=0,i=8;
// SCL=0;
SDA=1;
for(i=0;i<8;i++)
{
receivebyte=(receivebyte<<1)|SDA;
SCL=1;
SCL=0;
}
SDA=1;SCL=0;
return(receivebyte);
}/*****************Æô??×ÜÏß**********************/
void IICStart(void)
{
SDA=1; /*??ËÍÆðÊ?Ìõ?þµÄÊý?ÝÐźÅ*/
_Nop();
SCL=1;
_Nop(); /*ÆðÊ?Ìõ?þ??Á?Ê??ä?óÓÚ4.7us,ÑÓÊ?*/
_Nop();
_Nop();
_Nop();
_Nop();
SDA=0; /*??ËÍÆðÊ?ÐźÅ*/
_Nop(); /* ÆðÊ?Ìõ?þËø??Ê??ä?óÓÚ4?Ìs*/ _Nop();
_Nop();
_Nop();
_Nop();
SCL=0; /*Ç?×?I2C×ÜÏß??×?????ËÍ?ò?ÓÊÕÊý?Ý */
_Nop();
_Nop();
_Nop();
_Nop();
}/*****************Í?Ö?IIC×ÜÏß****************/
void IICStop(void)
{
SDA=0; /*??ËÍ?áÊøÌõ?þµÄÊý?ÝÐźÅ*/ _Nop(); /*??ËÍ?áÊøÌõ?þµÄÊ?ÖÓÐźÅ*/ SCL=1; /*?áÊøÌõ?þ??Á?Ê??ä?óÓÚ4?Ìs*/ _Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=1; /*??ËÍI2C×ÜÏß?áÊøÐźÅ*/ _Nop();
_Nop();
_Nop();
_Nop();
}
void IICAck(void)
{
SDA=0;_Nop();_Nop();_Nop();_Nop(); SCL=1;_Nop();_Nop();_Nop();_Nop();_Nop(); SCL=0;_Nop();_Nop();
// SDA=1;_nop_();
}
void Noack(void)
{
SDA=1;_Nop();_Nop();_Nop();_Nop(); SCL=1;_Nop();_Nop();_Nop();_Nop();_Nop(); SCL=0;_Nop();_Nop();
}