本帖最后由 lhb8605 于 2009-11-4 15:43 编辑
#include <reg51.h>
#include <math.h>
#include <intrins.h>
#include <serial_t0_interrupt.h>
/******************************************************************************************
*函数原型: 变量定义
*功 能: 寄存器和引脚定义
*******************************************************************************************/
#define uchar unsigned char
#define uint unsigned int
uchar i,j,k,Temp;
bit AckTemp;
/******************************************************************************************
*函数原型: 宏定义和变量定义
*功 能: 寄存器和引脚定义
*******************************************************************************************/
void Delay (unsigned int delay_time)
{
while(delay_time)
delay_time--;
}
/*--------------------------------------------------------------------------------------
*函数:起始信号
*功能:向24c02发送起始信号
---------------------------------------------------------------------------------------*/
void I2cStart ()
{
SCL=0;
Delay(100);
SDA=1;
Delay(100);
SCL=1;
Delay(100);
SDA=0;
Delay(100);
SCL=0;
}
/*--------------------------------------------------------------------------------------
*函数:停止信号
*功能:向24c02发送停止信号
---------------------------------------------------------------------------------------*/
void I2cStop ()
{
SDA=0;
Delay(100);
SCL=1;
Delay(100);
SDA=1;
Delay(100);
SCL=0;
}
/*--------------------------------------------------------------------------------------
*函数:发送0
*功能:向24c02发送0
---------------------------------------------------------------------------------------*/
void Send0 ()
{
SDA=0;
Delay(100);
SCL=1;
Delay(100);
SCL=0;
Delay(100);
}
/*--------------------------------------------------------------------------------------
*函数:发送1
*功能:向24c02发送1
---------------------------------------------------------------------------------------*/
void Send1()
{
SDA=1;
Delay(100);
SCL=1;
Delay(100);
SCL=0;
Delay(100);
}
/*--------------------------------------------------------------------------------------
*函数:应答信号
*功能:向24c02发送应答信号,函数返回应答信号F0
---------------------------------------------------------------------------------------*/
bit CheckAck ()
{
SCL=0;
Delay(100);
SDA=1;
Delay(100);
SCL=1;
Delay(100);
F0=SDA;
Delay(100);
SCL=0;
Delay(100);
return F0;
}
/*--------------------------------------------------------------------------------------
*函数:应答信号
*功能:向24c02发送应答信号,函数返回应答信号F0
---------------------------------------------------------------------------------------*/
void SendCheckAck ()
{
SCL=0;
Delay(100);
SDA=0;
Delay(100);
SCL=1;
Delay(100);
SCL=0;
Delay(100);
}
/*--------------------------------------------------------------------------------------
*函数:发送一个字节
*功能:向24c02发送一个字节
---------------------------------------------------------------------------------------*/
void SendI2cByte (unsigned char SByte)
{
for (i=0;i<8;i++)
{
if ( (SByte<<i)&0x80 )
{
Send1();
Delay(40);
}
else
{
Send0();
Delay(40);
}
}
}
/*--------------------------------------------------------------------------------------
*函数:读一个字节
*功能:向24c02读取一个字节,函数返回I2c读到的字节
---------------------------------------------------------------------------------------*/
unsigned char ReadI2cByte ()
{
unsigned char RByte=0;
for (i=0;i<8;i++)
{
SDA=1; //释放总线
Delay(100);
SCL=1;
Delay(100);
F0=SDA;
Delay(100);
SCL=0;
Delay(100);
if(F0)
{
RByte=RByte<<1;
RByte=RByte|0x01;
}
else
RByte=RByte<<1;
}
return RByte;
}
/*--------------------------------------------------------------------------------------
*函数:写一个字节给24c02
*功能:向24c02写一个字节,进行函数调用时应送地址WAdd,送字节数据WData
---------------------------------------------------------------------------------------*/
void WriteOneByte (unsigned char WAdd,unsigned char WData)
{
I2cStart();
SendI2cByte (0xA0); //器件地址00,写24c02
AckTemp=CheckAck();
SendI2cByte(WAdd);
AckTemp=CheckAck();
SendI2cByte(WData);
AckTemp=CheckAck();
I2cStop();
}
/*--------------------------------------------------------------------------------------
*函数:写一串字节给24c02
*功能:向24c02写一串字节
---------------------------------------------------------------------------------------*/
/*void WriteOnePage (unsigned char WAdd,unsigned char WDNum)
{
unsigned char WData[5];
I2cStart();
SendI2cByte(0xA0); //器件地址00,写24c02
AckTemp=CheckAck();
SendI2cByte(WAdd);
AckTemp=CheckAck();
SendI2cByte(WData);
AckTemp=CheckAck();
I2cStop();
}
*/
/*--------------------------------------------------------------------------------------
*函数:读一个字节
*功能:向24c02读一个字节,进行函数调用时应该写入要读的地址RAdd,函数返回读到的字节
---------------------------------------------------------------------------------------*/
unsigned char ReadOneByte (unsigned char RAdd)
{
unsigned char ReadData;
I2cStart();
SendI2cByte(0xA0); //器件地址,发送0x00,控制0号24c02,执行伪写操作
AckTemp=CheckAck();
SendI2cByte(RAdd);
AckTemp=CheckAck();
I2cStart(); //开始读操作
SendI2cByte(0xA1);
AckTemp=CheckAck();
ReadData=ReadI2cByte();
// SendCheckAck();
I2cStop();
return ReadData;
}
/**************************************************************************************
*函数:Main
*功能:程序运行
**************************************************************************************/
main()
{
serial_initialization();
EA=0;
while(1)
{
WriteOneByte(0x01,0x01);
Delay(60000);
Delay(60000);
SBUF=0x11;
while(!TI);
TI=0;
SBUF=ReadOneByte(0x01);
while(!TI);
TI=0;
SBUF=0xEE;
while(!TI);
TI=0;
Delay(60000);
Delay(60000); //*/
}
}
结果串口发送出来数据为: 11 FF EE
大家好,写了一个24c02B的程序,可是就是通讯不上,还请高手帮忙看看。 |