想使用这个芯片做一个时钟 但是读出来的始终是0x7F,查了不少资料,始终不知道怎么解决 已经做了3天了,感觉快疯了。 如果有人有这方面的经验,麻烦指点下,先谢过了 #include "AT89X52.h" #include "absacc.h" #include "intrins.h"
sbit clk = P3^4; sbit dat = P3^5; sbit rst = P2^7;
unsigned char init[]={0x00,0x00,0x21,0x21,0x10,0x01,0x04};//秒,分,时,日,月,星期,年
//向DS1302写一个字节数据 void WriteOneByte(unsigned char data1) { unsigned char i; for(i=8;i>0;i--) {
clk = 0; if(data1&0x01) dat = 1; else dat = 0; clk = 1; _nop_();_nop_(); data1 = data1>>1; } }
//从DS1302读一个字节数据 unsigned char ReadOneByte() { unsigned char i,temp = 0; dat = 1; for(i =8;i>0;i--) { clk = 0; if(dat == 1) temp= temp|0x80; else temp= temp&0x7f; clk = 1; temp= temp>>1; } return temp; }
void WriteData(unsigned char addr, unsigned char num) { rst = 1; WriteOneByte(addr); WriteOneByte(num); rst =0; clk =0; }
unsigned char ReadData(unsigned char addr) { unsigned char data1; rst = 1; WriteOneByte(addr); data1 = ReadOneByte(); rst = 0; return data1; }
unsigned char ReadSec() { unsigned char data1; data1 = ReadData(0x81); return data1; } unsigned char ReadMin() { unsigned char data1; data1 = ReadData(0x83); return data1; } void initds1302() { rst=0; clk=0; WriteData(0x80,0x00); WriteData(0x90,0x00);//禁止涡流充电 } void set_ds1302(unsigned char addr,unsigned char *p,unsigned char n)//写入n个数据 { WriteData(0x8e,0x00);//写控制字,允许写操作 for(;n>0;n--) { WriteData(addr,*p); p++; addr=addr+2; } WriteData(0x8e,0x80);//写保护,不允许写 } void main() { unsigned char sec,min; initds1302(); set_ds1302(0x80,init,7); while(1) { sec = ReadData(0x81); min = ReadData(0x85); } } |