asdy00000 发表于 2014-9-2 09:08
我的也是400多k的,您能指导一下怎么能接收到应答吗?!
我现在不确定是软件问题还是硬件问题,感觉硬件 ...
我刚弄清楚协议,别人的51测试程序
#include <intrins.h>
#include <New51.h>
#include <intrins.h>
#include <absacc.h>
#include <stdio.h>
#include <math.h>
#define _I2C_C_
unsigned char SystemError;
#define NOPS();{_nop_();_nop_();_nop_();_nop_();_nop_();}
typedef unsigned char BYTE;
typedef unsigned char byte;
typedef unsigned int word;
word tcount;
word a;
typedef unsigned int uchar;
unsigned char data_buf[1];
sbit SCL = P1^6;
sbit SDA = P1^7;
bit bdata I2C_Ack;
void I2C_Init(void);
void I2C_Start(void);
void I2C_Stop(void);
/
//---------------------------------
// Function: void I2C_Start(void)
// Discrption:
//----------------------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
NOPS(); //Initial
SDA = 0;
NOPS(); //START
SCL = 0;
}
//---------------------------------
// Function: void I2C_Stop(void)
// Discrption:
//----------------------------------
void I2C_Stop(void)
{
SCL = 0;
SDA = 0;
NOPS(); //Initial
SCL = 1;
NOPS(); //STOP
SDA = 1;
}
//---------------------------------
// Function: void I2C_Start(void)
// Discrption:
//----------------------------------
WaitAck(void)
{
unsigned char errtime = 255;
SDA = 1;
NOPS();
SCL = 1;
NOPS();
SystemError = 0x10;
while(SDA)
{
errtime--;
if(!errtime)
{
I2C_Stop();
SystemError = 0x11;
return;
}
}
SCL = 0;
}
//---------------------------------
// Function: void SendAck(void)
// Discrption:
//----------------------------------
void SendAck(void)
{
SDA =0 ;
NOPS();
SCL = 1;
NOPS();
SCL = 0;
}
//---------------------------------
// Function: void SendNotAck(void)
// Discrption:
//----------------------------------
void SendNotAck(void)
{
SDA =1 ;
NOPS();
SCL = 1;
NOPS();
SCL = 0;
}
//-----------------------------------------------------
// Function: void I2CSendByte(unsigned char send_data)
// Discrption:
//-----------------------------------------------------
void I2CSendByte(unsigned char send_data)
{
unsigned char count = 8;
for(count = 0;count<8;count++)
{
SCL = 0;
_nop_();
SDA = (bit)(send_data & 0x80);
send_data <<= 1;
NOPS();
SCL = 1;
NOPS();
}
SCL = 0;
}
//-----------------------------------------------------
// Function: unsigned char I2CReceiveByte(void)
// Discrption:
//-----------------------------------------------------
unsigned char I2CReceiveByte(void)
{
unsigned char count = 8;
unsigned char dat = 0;
SDA = 1;
for(count = 0;count<8;count++)
{
dat <<= 1;
SCL = 0;
NOPS();
SCL = 1;
NOPS();
dat |= SDA;
}
SCL = 0;
return(dat);
}
//--------------------------------------------------------------------------
// Function: void WriteCharToChip(unsigned int address,unsigned char dat)
// Discrption:
//--------------------------------------------------------------------------
void WriteCharToChip(unsigned int address,unsigned char dat)
{
I2C_Start();
//I2CSendByte(0xA0); //
I2CSendByte(0x5a);
WaitAck();
I2CSendByte((char)(address)>>8); //????AT24C02,AT24C64,AT24C128,AT24C256??,??????????
Delay(300);
WaitAck();
I2CSendByte((char)(address));
WaitAck();
I2CSendByte(dat);
Delay(300);
WaitAck();
I2C_Stop();
}
//--------------------------------------------------------------------------
// Function: unsigned char ReadCharFromChip(unsigned int Address)
// Discrption:
//--------------------------------------------------------------------------
unsigned char ReadCharFromChip(unsigned int Address)
{
unsigned char recv_data;
I2C_Start();
//I2CSendByte(0xA0); //
I2CSendByte(0x5a);
WaitAck();
I2CSendByte((char)(Address>>8));//
WaitAck();
I2CSendByte((char)(Address));
WaitAck();
I2C_Start();
//I2CSendByte(0xA1); //
I2CSendByte(0x5b);
WaitAck();
recv_data = I2CReceiveByte();
SendNotAck();
I2C_Stop();
return(recv_data);
void main(void)
{ unsigned char Read_data;
Timer_Init();
P3=0xff;
P1=0xff;
P2=0xf0;
I2C_Init();
I2C_Start();
I2C_Stop();
Send_Uart(0xa5);
Send_Byte(CMD_RESET);
Display();
while(1)
{ // WriteCharToChip(0x10,0xa3); //
// Read_data = ReadCharFromChip(0x10); //
WriteCharToChip(0xb5,0x5a);
Read_data = ReadCharFromChip(0xb5);
Display(Read_data);
Delay_us(20000);
Delay_us(20000);
}
}
|