本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
这两个程序都是将写入EEPROM的数据读回来,并驱动LED灯亮,参考网上的程序,建议对照93c46和AT24C02时序图看
93c46源程序:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit CS=P2^4;
sbit SK=P2^5;
sbit DI=P2^6;
sbit DO=P2^7;
sbit led=P1^6;
sbit dm=P1^4;
sbit wm=P1^5;
/****************擦写使能****************/
void Ewen(void)
{
unsigned char i,temp;
CS=0;
SK=0;
CS=1;
temp=0x98; // 10011XXXX
for(i=8;i>0;i--)
{
DI=temp&0x80;//取高位
SK=1; //写入
SK=0;
temp<<=1;
}
CS=0;
}
/****************擦写禁止***************/
void Ewds(void)
{
uchar i,temp;
CS=0;
SK=0;
CS=1;
temp=0x80; // 10000XXXX
for(i=8;i>0;i--)
{
DI=temp&0x80;
SK=1;
SK=0;
temp<<=1;
}
CS=0;
}
/*****************读数据********************/
unsigned int Read(unsigned char address)
{
unsigned char i;
unsigned int result;
Ewen();//写使能
SK=0;
DI=1;
CS=0;
CS=1;
SK=1;
SK=0;
address=address&0x3|0x80;
for(i=8;i>0;i--)
{
DI=address&0x80;
SK=1;
SK=0;
address<<=1;
}
DO=1;
for(i=16;i>0;i--)
{
SK=1;
result=(result<<1)|DO;
SK=0;
}
CS=0;
Ewds();
return(result);
}
/************************写数据*********************************/
void Write(unsigned char address,unsigned int InData)
{
unsigned char i;
Ewen();
SK=0;
DI=1;
CS=0;
CS=1;
SK=1;
SK=0;
address=address&0x3f|0x40;//
for(i=8;i>0;i--)
{ // 8
DI=address&0x80;
SK=1; SK=0;
address<<=1;
}
for(i=16;i>0;i--) //16bit
{ // 16
DI=InData&0x8000;
SK=1; SK=0;
InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0) // busy test
{
SK=0; SK=1;
}
SK=0; CS=0;
Ewds();
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=113;y>0;y--);
}
void main(void)
{
led=1;
dm=0;
wm=0;
Write(0x03,0xfe);
delay(1000);
P0=Read(0x03);
while(1);
}
24c02源程序:
#include<reg52.h>
#define uchar unsigned char
sbit sda=P2^5;
sbit scl=P2^4;
sbit led_cs=P1^6;
sbit dm=P1^4;
sbit wm=P1^5;
uchar a;
void delay()
{ ;; }
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
// scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void delay1(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{ led_cs=1;
dm=0;
wm=0;
init();
write_add(23,0xfe);
delay1(100);
P0=read_add(23);
while(1);
} |