新手入门
- 积分
- 19
- 金钱
- 19
- 注册时间
- 2018-4-18
- 在线时间
- 2 小时
|
发表于 2018-4-18 16:38:01
|
显示全部楼层
本帖最后由 bolderman 于 2018-4-18 16:42 编辑
大兄弟,你的代码自己验证了吗?
还是写完了就贴上来了?
我用了你的代码根本跑不起来。
害死人呀。
第一个问题是你没依据手册上的时序加延时;
第二个问题是在读数据的时候,是下降沿读数据,并且应该先使data>>1右移,再使最高位置1: data |= 0x80;
修改后的代码如下:
[mw_shl_code=c,true]
void DS1302_WriteOneByte(uint8_t data)
{
uint8_t index = 0;
DAT_MODE_OUT();
for(index=0; index<8; index++)
{
SCK_CLR;
//DAT_MODE_OUT();
if (data & 0x01) /* 上升沿写数据 */
{
DAT_SET;
}
else
{
DAT_CLR;
}
//tdc = 200ns, tcl = 1us
delay_us(2);
SCK_SET;
//tcdh = 280ns, tch = 1us
delay_us(2);
data >>= 1;
}
}
void DS1302_WriteByte(const uint8_t addr, const uint8_t data)
{
RST_CLR;
SCK_CLR;
//tcwh = 4us
delay_us(5);
RST_SET;
//tcc = 4us
delay_us(5);
DS1302_WriteOneByte(addr);
DS1302_WriteOneByte(data);
//tcch = 240ns, tch = 1us
//delay_us(1);
RST_CLR;
SCK_CLR;
}
uint8_t DS1302_ReadByte(const uint8_t addr)
{
uint8_t index = 0, data = 0;
RST_CLR;
SCK_CLR;
//tcwh = 4us
delay_us(5);
RST_SET;
//tcc = 4us
delay_us(5);
DS1302_WriteOneByte(addr);
DAT_MODE_IN();
for(index=0; index<8; index++)
{
SCK_CLR;
//SCK_SET;
//tcdd = 800ns tcl = 1us
delay_us(2);
data >>= 1;
if (DAT_READ == 1) //下降沿读数据,不是上升沿
{
data |= 0x80;
}
//data >>= 1; //原来错误的地方被我注释了
//SCK_CLR;
SCK_SET;
//tch = 1us
delay_us(2);
}
SCK_CLR;
RST_CLR;
return data;
}
[/mw_shl_code] |
|