51单片机 I2C总线接收AT24C02数据程序不能用 看郭天祥书写的 求大神教
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
sbit SDA=P2^0;
sbit SCL=P2^1;
unsigned int Ack=1; //ó|′e3é1|±ê??
void Start();
void Stop();
void Respons();
void SendByte();
unsigned char RevByte();
void SendByte(unsigned char date);
unsigned char Rec24c02Byte(unsigned char address);
void delay()
{;;}
void delay1ms(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<110;j++)
;
}
void Init()
{
SCL=1;
SDA=1;
delay();
}
void Start()
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0; //?eê??¨á¢ê±??′óóú4.7us
delay();
}
void Stop()
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
void Respons()
{
unsigned int i=0;
Ack=1; //////////////////
SCL=1;
delay();
while((SDA==1)&&(i<250))
{i++;}
SCL=0; //?ˉ×?×ü??
delay();
if(i==250)
Ack=0;
} ///////////////////////////////
void SendByte(unsigned char date)
{
unsigned int Bit;
for(Bit=0;Bit<8;Bit++)
{
date=date<<1;
SCL=0;
delay();
SDA=CY;
delay();
SCL=1;
delay();
}
SCL=0;
delay();
SDA=1; //êí·?×ü??
delay();
}
unsigned char RecByte()
{
unsigned int Bit;
unsigned char date;
SCL=0;
delay();
SDA=1;
delay();
for(Bit=0;Bit<8;Bit++)
{
SCL=1;
delay();
date=(date<<1)|SDA;
SCL=0;
delay();
}
delay();
return(date);
}
void Send24c02Byte(unsigned char address,unsigned char date)
{
Start();
SendByte(0xa0); //?÷?tμ??·
Respons();
SendByte(address); //?ú×óμ??·
Respons();
SendByte(date);
Respons();
Stop();
}
unsigned char Rec24c02Byte(unsigned char address)
{
unsigned char receive;
Start();
SendByte(0xa0); //?÷?tμ??·
Respons();
SendByte(address); //?ú×óμ??·
Respons();
Start();
SendByte(0xa1); //?÷?tμ??·
respons();
receive=RecByte();
Stop();
return receive;
}
你用的什么型号的51单片机?有要配置引脚三模式吗?还有,你的主函数呢?
