首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

为什么不能读取AT24C01?内有程序,请大家帮忙!

为什么不能读取AT24C01?内有程序,请大家帮忙!

#include
#include "derivative.h"
#include "Global.h"
//-----***-IIC延时-***-----//
void IicWait(void)
{
asm("NOP"); //01
//asm("NOP"); //02
//asm("NOP"); //01
}
//-----***-IIC启示-***-----//
void IicStart(void)
{
IicSda=1;
IicWait();
IicScl=1;
IicWait();
IicSda=0;
IicWait();
IicScl=0;
IicWait();
}
//-----***-IIC停止-***-----//
void IicStop(void)
{
IicSda=0;
IicWait();
IicScl=1;
IicWait();
IicSda=1;
IicWait();
}
//-----***-IIC应答-***-----//
void SendAcknowledge(unsigned char IicAck)
{
IicSda=IicAck;
IicScl=1;
IicWait();
IicScl=0;
IicWait();
}
//-----***-从slave端读取一个数据-***-----//
unsigned char IicReceiveByte(void)
{
unsigned char i;
unsigned char bytedata=0;
IicSda=1;
for(i=0;i<8;i++)
{
IicScl=1;
IicWait();
bytedata<<=1;
if (IicSda) bytedata |=0x01;
IicScl=0;
IicWait();
}
return bytedata;
}
//-----***-传送一个 Byte 数据到 slave-***-----//
unsigned char IicSentByte(unsigned char DataByte)
{
unsigned char i,IicAck;
for(i=0;i<8;i++)
{
if(DataByte & 0x80)
{
IicSda=1;
}
else
{
IicSda=0;
}
DataByte <<=1;
IicWait();
IicScl=1;
IicWait();
IicScl=0;
IicWait();
}
IicSda=1;
IicWait();
IicScl=1;
IicWait();
IicAck=IicSda;
IicScl=0;
IicWait();
return IicAck;
}
//-----***-无扇区读---单字节-***-----//
unsigned char FgTimeout;
//读取指定器件无扇区指定地址单字节的数据
//Device=器件地址---单字节
//DataAdd=字节地址---单字节
unsigned char IicByteRead(unsigned char DataAdd)
{
unsigned char bytedata;
DisableInterrupts;//关中断
IicStart();
IicSentByte(0xa0);
IicSentByte(DataAdd);
IicStart();
IicSentByte(0xa1);
bytedata=IicReceiveByte();
//SendAcknowledge(1);
IicStop();
EnableInterrupts;//开中断
return bytedata;
}
//-----***-无扇区写---单字节-***-----//
//写入指定器件无扇区指定地址中的单字节数据
//Device=器件地址---单字节
//DataAdd=字节地址---双字节
void IicByteWrite(unsigned char DataAdd,unsigned char DataByte)
{
unsigned char i,IicAck;
if(BitData&0x01)
{
DisableInterrupts;//关中断
for(i=0;i<3;i++)
{
IicStart();
IicAck=IicSentByte(0xa0);if(IicAck==1){IicStop();continue;}
IicAck=IicSentByte(DataAdd);if(IicAck==1){IicStop();continue;}
IicAck=IicSentByte(DataByte);if(IicAck==1){IicStop();continue;}
IicStop();
if(IicAck==0)break;
}
EnableInterrupts;//开中断
Delay(100);
}
}
\',\'\'\'\',\'\'\',,,;,.\',\'\'\',,\',.╭⌒╮⌒╮\',\'\';; ⌒╮\'╭⌒╮⌒╮.宠辱不惊,看庭前花开花落;; ╱◥██◣\'\';\',,,,;\';\';\'\'\';,\'.\'\',\'\'\'\',\';; ︱田︱田田| ⌒╮去留无意,望天空云卷云舒;; ╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
你有没有用示波器看过通讯的波形是否正确?
海纳百川  有容乃大


是否在等待应答的时候需要把IicSda端口设置为输入?
\',\'\'\'\',\'\'\',,,;,.\',\'\'\',,\',.╭⌒╮⌒╮\',\'\';; ⌒╮\'╭⌒╮⌒╮.宠辱不惊,看庭前花开花落;; ╱◥██◣\'\';\',,,,;\';\';\'\'\';,\'.\'\',\'\'\'\',\';; ︱田︱田田| ⌒╮去留无意,望天空云卷云舒;; ╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
你是用IIC模块还是用软件模拟?如果是软件模拟,那是需要设置成输入的。
海纳百川  有容乃大
同意
还有一点就是你的SDA和SCL是否考虑上拉电阻问题?
返回列表