本文包含原理图、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;
}
} |