2020-01-16 18:27发布
node720 发表于 2017-1-8 09:48 时序不对吧?贴程序看看。
TBG3 发表于 2017-1-11 12:20 买个便宜的逻辑分析仪,几十块钱那种,AXpro, 抓一下波形。
Eric_Xue 发表于 2017-1-12 08:44 LZ,试下,看是否能解决问题 uchar ds1302_read_byte(uchar addr) {
最多设置5个标签!
sbit RST = P1^2;
sbit IO = P1^4;
sbit SCK = P1^5;
uchar time_buf[8] = {0x20,0x10,0x0c,0x05,0x0F,0x08,0x55,0x02};//³õ
unsigned char idata Rec_buf[10]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char idata Ctrl_buf[10]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
/*****Ôö¼Óds1302******/
#define ds1302_sec_add 0x80 //ÃëÊý¾ÝµØÖ·
#define ds1302_min_add 0x82 //·ÖÊý¾ÝµØÖ·
#define ds1302_hr_add 0x84 //ʱÊý¾ÝµØÖ·
#define ds1302_date_add 0x86 //ÈÕÊý¾ÝµØÖ·
#define ds1302_month_add 0x88 //ÔÂÊý¾ÝµØÖ·
#define ds1302_day_add 0x8a //ÐÇÆÚÊý¾ÝµØÖ·
#define ds1302_year_add 0x8c //ÄêÊý¾ÝµØÖ·
#define ds1302_control_add 0x8e //¿ØÖÆÊý¾ÝµØÖ·
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
void ds1302_init(void) //OK
{
RST=0; //RST½ÅÖõÍ
SCK=0; //SCK½ÅÖõÍ
}
void ds1302_write_byte(uchar addr, uchar d)
{
uchar i;
RST=1; //Æô¶¯DS1302×ÜÏß
//дÈëÄ¿±êµØÖ·£ºaddr
addr = addr & 0xFE; //×îµÍλÖÃÁ㣬¼Ä´æÆ÷0λΪ0ʱд£¬Îª1ʱ¶Á
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO=1;
}
else {
IO=0;
}
SCK=1; //²úÉúʱÖÓ
SCK=0;
addr = addr >> 1;
}
//дÈëÊý¾Ý£ºd
for (i = 0; i < 8; i ++) {
if (d & 0x01) {
IO=1;
}
else {
IO=0;
}
SCK=1; //²úÉúʱÖÓ
SCK=0;
d = d >> 1;
}
RST=0; //Í£Ö¹DS1302×ÜÏß
}
//´ÓDS1302¶Á³öÒ»×Ö½ÚÊý¾Ý
uchar ds1302_read_byte(uchar addr) {
uchar i,temp;
RST=1; //Æô¶¯DS1302×ÜÏß
//дÈëÄ¿±êµØÖ·£ºaddr
addr = addr | 0x01; //×îµÍλÖøߣ¬¼Ä´æÆ÷0λΪ0ʱд£¬Îª1ʱ¶Á
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO=1;
}
else {
IO=0;
}
SCK=1;
SCK=0;
addr = addr >> 1;
}
//Êä³öÊý¾Ý£ºtemp
for (i = 0; i < 8; i ++) {
temp = temp >> 1;
if (IO) {
temp |= 0x80;
}
else {
temp &= 0x7F;
}
SCK=1;
SCK=0;
}
RST=0; //Í£Ö¹DS1302×ÜÏß
return temp;
}
//ÏòDS302дÈëʱÖÓÊý¾Ý
void ds1302_write_time(void)
{
ds1302_write_byte(ds1302_control_add,0x00); //¹Ø±Õд±£»¤
ds1302_write_byte(ds1302_sec_add,0x80); //ÔÝͣʱÖÓ
//ds1302_write_byte(ds1302_charger_add,0xa9); //ä¸Á÷³äµç
ds1302_write_byte(ds1302_year_add,time_buf[1]); //Äê
ds1302_write_byte(ds1302_month_add,time_buf[2]); //ÔÂ
ds1302_write_byte(ds1302_date_add,time_buf[3]); //ÈÕ
ds1302_write_byte(ds1302_hr_add,time_buf[4]); //ʱ
ds1302_write_byte(ds1302_min_add,time_buf[5]); //·Ö
ds1302_write_byte(ds1302_sec_add,time_buf[6]); //Ãë
ds1302_write_byte(ds1302_day_add,time_buf[7]); //ÖÜ
ds1302_write_byte(ds1302_control_add,0x80); //´ò¿ªÐ´±£»¤
}
//´ÓDS302¶Á³öʱÖÓÊý¾Ý
void ds1302_read_time(void)
{
time_buf[1]=ds1302_read_byte(ds1302_year_add); //Äê
time_buf[2]=ds1302_read_byte(ds1302_month_add); //ÔÂ
time_buf[3]=ds1302_read_byte(ds1302_date_add); //ÈÕ
time_buf[4]=ds1302_read_byte(ds1302_hr_add); //ʱ
time_buf[5]=ds1302_read_byte(ds1302_min_add); //·Ö
time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7f;//Ã룬ÆÁ±ÎÃëµÄµÚ7룬±ÜÃⳬ³ö59
time_buf[7]=ds1302_read_byte(ds1302_day_add); //ÖÜ
}
if(RTCOK_fag==0) 读出并上传时间数据~
{Rec_buf[0]=0x55;
delayms(10);
ds1302_read_time();
Rec_buf[0]=0x55;
Rec_buf[1]=time_buf[1];//年
Rec_buf[2]=time_buf[2]; //月
Rec_buf[3]=time_buf[3]; //日
Rec_buf[4]=time_buf[4]; //时
Rec_buf[5]=time_buf[5]; //分
Rec_buf[6]=time_buf[6];//秒
Rec_buf[7]=0xE5; //
Rec_buf[8]=0xaa;//
send1();//串口发送数据据!
}
主要程序!!
求淘宝地址!!!!
LZ,试下,看是否能解决问题
uchar ds1302_read_byte(uchar addr) {
uchar i,temp;
RST=1; //此处RST 应该改为DS1302_CS
addr = addr | 0x01;
// Send Addr
for (i = 0; i < 8; i ++)
{
if (addr & 0x01) {
IO=1;
}
else {
IO=0;
}
SCK=0;
addr >>= 1;
SCK=1;
}
//Read Data
for (i = 0; i < 8; i ++) {
SCK=0;
temp >>= 1;
if (IO) {
temp |= 0x80;
}
else {
temp &= 0x7F;
}
SCK=1;
}
RST=0; //此处RST 应该改为DS1302_CS
return temp;
}
"DS1302_CS " 我的这个不是定义是这个啊!
一周热门 更多>