帮我分析一下ds1307的程序 谢谢
源程序如下,现在读不出来数,我怀疑可能是读数据的时候出问题了,请高手们帮我看看,时序肯定没问题,我以前在51上就用的这个,现在移植到avr上不好用了,#include <iom16v.h>
#include <macros.h>
#include "ds1307_h.h"
#define SETDS1307CLKPORTB|=0X80
#define CLRDS1307CLKPORTB&=0X7F
#define SETDS1307DATPORTB|=0X40
#define CLRDS1307DATPORTB&=0XBF
void SomeNop(void);
void Start(void);
void Stop(void);
void SendAck(unsigned char k);
void Send(unsigned char dat);
unsigned char Read(void);
unsigned char Write1307(unsigned char add,unsigned char dat);
unsigned char Read1307(unsigned char add);
void Initial_DS1307(void);
/******************************** ds1307 **************************/
void SomeNop(void)
{
NOP();
NOP();
NOP();
NOP();
NOP();
// NOP();
// NOP();
}
/***********************************************************************/
void Start(void)
{
DDRB=0XC0;
SETDS1307DAT;
SETDS1307CLK;
SomeNop();
CLRDS1307DAT;
SomeNop();
CLRDS1307CLK;
SomeNop();
}
/***********************************************************************/
void Stop(void)
{
DDRB=0XC0;
CLRDS1307DAT;
SomeNop();
SETDS1307CLK;
SomeNop();
SETDS1307DAT;
SomeNop();
}
/***********************************************************************/
void SendAck(unsigned chark)
{
DDRB=0XC0;
SomeNop();
if(k==1)
SETDS1307DAT;
else
CLRDS1307DAT;
// SDA=k;
SomeNop();
SETDS1307CLK;
SomeNop();
SomeNop();
SomeNop();
CLRDS1307CLK;
}
/***********************************************************************/
void Send(unsigned char dat)
{
//DDRB=0XC0;
unsigned char i,temp;
temp=dat;
DDRB=0XC0;
for (i=0;i<8;i++)
{
CLRDS1307CLK;
// SDA=(bit)(temp&0x80);
if(temp&0x80)
SETDS1307DAT;
else
CLRDS1307DAT;
temp<<=1;
SomeNop();
SETDS1307CLK;
SomeNop();
SomeNop();
}
CLRDS1307CLK;
}
/***********************************************************************/
unsigned char Read(void)
{
unsigned char i,dat;
for (i=0;i<8;i++)
{
DDRB=0XC0;
CLRDS1307CLK;
SomeNop();
SETDS1307DAT;
SomeNop();
DDRB=0X80;
SomeNop();
SETDS1307CLK;
dat<<=1;
SomeNop();
SomeNop();
if(PINB&0X40)
dat++;
SomeNop();
}
CLRDS1307CLK;
return (dat);
}
/***********************************************************************/
unsigned char Write1307(unsigned char add,unsigned char dat)
{
unsigned char temp;
temp=dat/10;
temp<<=4;
temp=dat%10+temp;
Start();
Send(0xD0);
SendAck(0);
Send(add); //发地址
SendAck(0);
Send(temp); //发数据
SendAck(1);
Stop();
return (0);
}
/***********************************************************************/
unsigned char Read1307(unsigned char add)
{
unsigned char temp,dat;
Start();
Send(0xD0);
SendAck(0);
Send(add);
SendAck(1);
Stop();
Start();
Send(0xD1);
SendAck(0);
dat=Read();
SendAck(1);
Stop(); //接收结束
temp=dat/16;
dat=dat%16;
dat=dat+temp*10;
return (dat);
}
/***********************************************************************/
void Initial_DS1307(void)
{
Write1307(0x00,23); //秒设定
Write1307(0x01,59); //分设定
Write1307(0x02,11); //小时设定
} 为什么不用M16的I2C接口呢? 此人不知道在社区不?
页:
[1]