找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

关于AT24C02读写老是不成功,求指导???

[复制链接]
admin 发表于 2012-9-2 06:49:32 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
#include<reg51.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int


sbit SCL = P3^4;
sbit SDA = P3^5;

code DSY_CODE[]=
{0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0xff};

void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i= 0;i<120;i++);
}
}
//开始
void at24c02_start()
{
SCL = 0;
_nop_();
SDA = 1;
_nop_();
SCL = 1;
_nop_();
SDA = 0;
_nop_();
}
//停止
void at24c02_stop()
{
SCL = 0;
_nop_();
SDA = 0;
_nop_();
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
//应答
void at24c02_ack()
{
SCL = 0;
_nop_();
SDA = 0;
SCL = 1;
_nop_();
SCL = 0;
_nop_();
SDA = 1;
}

//写一个字节
bit write_8bit(uchar input)
{
uchar i;
for(i = 0;i < 8; i++)
{
SCL = 0;
input<<=1;
SDA = CY;
SCL = 1;
}
SCL = 0;
_nop_();
SDA = 1;
SCL = 1;
_nop_();
CY = SDA;
return(CY);
}

//读一个字节
uchar read_8bit()
{
uchar temp ,rbyte = 0;
for(temp = 8;temp!=0;temp--)
{
SCL = 0;
_nop_();
rbyte = (rbyte<<1)|SDA;
SCL = 1;
_nop_();
}
return rbyte;
}

//给一个地址,写值
void write_add( uchar address,uchar date )
{
at24c02_start();
write_8bit(0xa0);
at24c02_ack();
write_8bit(address);
at24c02_ack();
write_8bit(date);
at24c02_ack();
at24c02_stop();
}
//从一个地址读值
uchar read_add(uchar address)
{
uchar date;
at24c02_start();
write_8bit(0xa0);
at24c02_ack();
write_8bit(address);
at24c02_ack();
at24c02_start();
write_8bit(0xa1);
at24c02_ack();
date=read_8bit();
at24c02_stop();
return date;
}

void main()
{
uchar date=0x0;
write_add(0x23,1);
date=read_add(0x23);

while(1)
{//在数码管上显示该值
P0=0xff;//消隐
P0=0x00;
P2=DSY_CODE[date];
DelayMS(2);
P0=0xff;//消隐
P2=0xff;
}
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-24 05:52 , Processed in 0.052709 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表