📄 24c02wan.c
字号:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
//24c02 管脚定义
sbit SCL=P2^5; //定义时钟端口
sbit SDA=P2^4; //定义数据端口
//数码管位码定义
/*低位P1***************-0----1----2----3----4----5----6----7----8----9---全选-**/
uchar code shuma1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
/*高位P0***************-0----1----2----3----4----5----6----7----8----9---全选-**/
uchar code shuma0[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18,0xff};
/******************************24c02 声明**********************************/
/************************************
延时程序
*************************************/
void delay()
{
uchar i;
for(i=248;i>0;i--);
}
/************************************
开始总线
*************************************/
void start()
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/*************************************
结束总线
**************************************/
void stop()
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/*************************************
发 ack0
**************************************/
void noack()
{
SDA=1;
SCL=1;
SCL=0;
}
/*************************************
应答子函数
**************************************/
bit ack()
{
bit e;
SDA=1;
SCL=1;
e=SDA;
SCL=0;
return(e);
}
/**************************************
写 8个 bit到24C02
为写字节作准备
**************************************/
void wb(uchar input)
{
uchar j;
for(j=8;j>0;j--)
{
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
/***************************************
写一个字节到 24c02
****************************************/
void w(uchar d,uchar a)
{
start();
wb(0xa0); //写入指令0xa0,该指令表示准备对 24c02 进行写操作
ack();
wb(a); //先写要存数据的地址
ack();
wb(d); //再写要存数据
ack();
stop();
delay();
}
/**************************************
从 24C02中读 8个 bit
为读字节作准备
***************************************/
uchar rb()
{
uchar j,rb=0;
for(j=8;j>0;j--)
{
SCL=1;
rb=rb<<1;
rb=rb|((uchar)(SDA));
SCL=0;
}
return(rb);
}
/****************************************
从 24c02中读一个字节
*****************************************/
uchar r(uchar a)
{
uchar d; //读操作得到的数据
start();
wb(0xa0); //写入指令0xa0,该指令表示准备对 24c02 进行写操作
ack();
wb(a); //写入要读数据的地址a
ack();
start();
wb(0xa1); //写入指令0xa1,该指令表示准备对 24c02 进行读操作
ack();
d=rb(); //从地址a 中读出数据
noack();
stop();
return(d); // 返回得到的数据
}
/****************************24c02 声明结束********************************/
void main(void)
{
bit t=0;
uchar temp0;
uchar temp1;
temp0=r(0x22); //从0x22 中P0(高位)读出值
while(1)
{
temp0=shuma0[7];
temp1=shuma1[5];
w(temp0,0x02); //写 temp0到地址 0x22
delay();delay();delay();
P0=r(0x02); //从0x22 中P0(高位)读出值
delay();delay();delay();
w(temp1,0x12); //写 temp0到地址 0x22
delay();delay();delay();
P1=r(0x12); //从0x22 中P0(高位)读出值
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -