|
|
|
|
 |
AT24XX(AT24C01~AT24C256) 的接口程序(C51)
|
|
|
|
文章编号: |
091114225733 |
文章分类: |
单片机 > 51系列 |
|
点 击: |
|
关 键 词: |
AT24XX,AT24C01,AT24C256,EEPROM |
|
文章来源: |
网络 |
|
摘 要: |
|
|
-
-
-
-
-
-
- #include <reg52.h>
- #include <intrins.h>
- #define ERROR 10 //允许ERROR的最大次数
- sbit SDA=P3^0;
- sbit SCL=P3^1;
- enum eepromtype {AT2401,AT2402,AT2404,AT2408,AT2416,AT2432,AT2464,AT24128,AT24256};
- enum eepromtype enumer;
- unsigned char code buf1 []={1,3,5,7,9,10,11,12,13,15};
- unsigned char buf2 [10];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bit RW24xx(unsigned char *DataBuff,unsigned char Length,unsigned int Addr,
- unsigned char Control,enum eepromtype enumer)
- {
- void Delay(unsigned char DelayCount);
- void Start(void);
- void Stop(void);
- bit RecAck(void);
- void NoAck(void);
- void Ack(void);
- unsigned char Receive(void);
- void Send(unsigned char sendbyte);
- unsigned char data j,i=ERROR;
- bit errorflag=1;
- while(i--)
- {
- Start();
- Send(Control & 0xfe);
- if(RecAck()) continue;
- if(enumer > AT2416)
- {
- Send((unsigned char)(Addr >> 8));
- if(RecAck()) continue;
- }
- Send((unsigned char)Addr);
- if(RecAck()) continue;
- if(!(Control & 0x01))
- {
- j=Length;
- errorflag=0;
- while(j--)
- {
- Send(*DataBuff++);
- if(!RecAck()) continue;
- errorflag=1;
- break;
- }
- if(errorflag==1) continue;
- break;
- }
- else
- {
- Start();
- Send(Control);
- if(RecAck()) continue;
- while(--Length)
- {
- *DataBuff ++= Receive();
- Ack();
- }
- *DataBuff=Receive();
- NoAck();
- errorflag=0;
- break;
- }
- }
- Stop();
- if(!(Control & 0x01))
- {
- Delay(255); Delay(255); Delay(255); Delay(255);
- }
- return(errorflag);
- }
-
-
-
- void Start(void)
- {
- SCL=0;
- SDA=1;
- SCL=1;
- _nop_(); _nop_(); _nop_();
- SDA=0;
- _nop_(); _nop_(); _nop_(); _nop_();
- SCL=0;
- SDA=1;
- }
-
-
- void Stop(void)
- {
- SCL=0;
- SDA=0;
- SCL=1;
- _nop_(); _nop_(); _nop_();
- SDA=1;
- _nop_(); _nop_(); _nop_();
- SCL=0;
- }
-
-
- bit RecAck(void)
- {
- SCL=0;
- SDA=1;
- SCL=1;
- _nop_(); _nop_(); _nop_(); _nop_();
- CY=SDA;
- SCL=0;
- return(CY);
- }
-
-
- void Ack(void)
- {
- SDA=0;
- SCL=1;
- _nop_(); _nop_(); _nop_(); _nop_();
- SCL=0;
- _nop_();
- SDA=1;
- }
-
-
- void NoAck(void)
- {
- SDA=1;
- SCL=1;
- _nop_(); _nop_(); _nop_(); _nop_();
- SCL=0;
- }
-
-
- void Send(unsigned char sendbyte)
- {
- unsigned char data j=8;
- for(;j>0;j--)
- {
- SCL=0;
- sendbyte <<= 1;
- SDA=CY;
- SCL=1;
- }
- SCL=0;
- }
-
-
- unsigned char Receive(void)
- {
- register receivebyte,i=8;
- SCL=0;
- while(i--)
- {
- SCL=1;
- receivebyte = (receivebyte <<1 ) | SDA;
- SCL=0;
- }
- return(receivebyte);
- }
-
-
- void Delay(unsigned char DelayCount)
- {
- while(DelayCount--);
- }
-
-
- void main()
- {
- unsigned char Control,*p1,*p2;
- unsigned char Length;
- unsigned int addr ;
- p1=buf1;p2=buf2;
- addr=0;
- Length=8;
- enumer=AT24256;
- Control=0xa0;
- RW24xx(p1,Length,addr,Control,enumer);
- Control=0xa1;
- RW24xx(p2,Length,addr,Control,enumer);
- }
|
|
|
|
|
|
|
|
|
|
|