普中开源电子分享网

 找回密码
 立即注册
搜索
查看: 8268|回复: 5

Ds1302时钟不走

 关闭 [复制链接]

2

主题

4

帖子

20

积分

新手上路

Rank: 1

积分
20
发表于 2018-9-4 09:31:07 | 显示全部楼层 |阅读模式
主程序
#include  "reg52.h"
#include  "ds1302.h"

typedef  unsigned char u8;
typedef  unsigned int u16;

sbit  lea=P2^2;
sbit  leb=P2^3;
sbit  lec=P2^4;

void delay(u16  i)
{
          while(i--);
}

u8  Disdate[8];
u8 code smgduan[]={0x3f  , 0x06 , 0x5b , 0x4f , 0x66 ,
                   0x6d , 0x7d , 0x07 , 0x7f  ,0x6f    };

void Dispros()
{        Ds1302ReadTime();
  Disdate[0]=smgduan[TIME[2]/16];
  Disdate[1]=smgduan[TIME[2]&0x0f];
  Disdate[2]=0x40;
  Disdate[3]=smgduan[TIME[1]/16];
  Disdate[4]=smgduan[TIME[1]&0x0f];
  Disdate[5]=0x40;
  Disdate[6]=smgduan[TIME[0]/16];
  Disdate[7]=smgduan[TIME[0]&0x0f];
}
void DigDisplay ()
{        u8  i;
  
           for(i=0;i<8;i++)
          { switch(i)
            { case(0):
                    lea=0;leb=0;lec=0;break;
                 case(1):
                    lea=1;leb=0;lec=0;break;
                 case(2):
                    lea=0;leb=1;lec=0;break;
                 case(3):
                    lea=1;leb=1;lec=0;break;
                 case(4):
                    lea=0;leb=0;lec=1;break;
                 case(5):
                    lea=1;leb=0;lec=1;break;
                 case(6):
                    lea=0;leb=1;lec=1;break;
                 case(7):
                    lea=1;leb=1;lec=1;break;
         }
             P0=Disdate[7-i];
                 delay(50);
                 P0=0x00;
    }
}
void  main()
{
   Ds1302WriteInt();
   while(1)
   {
     Dispros();
            DigDisplay();
   }          

}
c文件
#include "ds1302.h"
#include "intrins.h"

uchar code Write_RET_ADDR[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar code Read_RET_ADDR[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

uchar TIME[7]={0,0,0x11,0x07,0x05,0x06,0x16};

void Ds1302Write(uchar addr,uchar dat)
{
   uchar  a;
   RST=0;
   _nop_();

   SCLK=0;
   _nop_();
   RST=1;
   _nop_();
   for(a=0;a<8;a++)
  {
          DSIO=addr&0x01;
        addr>>=1;
        SCLK=1;
        _nop_();
        SCLK=0;
        _nop_();
  }
   for(a=0;a<8;a++)
  {
          DSIO=dat&0x01;
        dat>>=1;
        SCLK=1;
        _nop_();
        SCLK=0;
        _nop_();
  }
   RST=0;  
   _nop_();
}

uchar Ds1302Read(uchar addr)
{
   uchar a,dat,dat1;
   RST=0;
   _nop_();

   SCLK=0;
   _nop_();
   RST=1;
   _nop_();
   for(a=0;a<8;a++)
  {
          DSIO=addr&0x01;
        addr>>=1;
        SCLK=1;
        _nop_();
        SCLK=0;
        _nop_();
  }
   _nop_();
   for(a=0;a<8;a++)
  {
          dat1=DSIO;
        dat=(dat>>1)|(dat1<<7);
        SCLK=1;
        _nop_();
        SCLK=0;
        _nop_();
  }
    RST=0;
        _nop_();
        SCLK=1;
        _nop_();
        DSIO=0;
        _nop_();
        DSIO=1;
        _nop_();
   return  dat;
}

void Ds1302WriteInt()
{
  uchar  a;
  Ds1302Write(0x8e,0x00);
  for(a=0;a<8;a++)
  {
          Ds1302Write(Write_RET_ADDR[a],TIME[a]);
  }
  Ds1302Write(0x8e,0x80);
}
void Ds1302ReadTime()
{
  uchar  a;
  for(a=0;a<8;a++)
  {
          Ds1302Read(Read_RET_ADDR[a]) ;
  }
}
头文件定义
#ifndef  DS1302_H_
#define  DS1302_H_

#include "reg52.h"


#ifndef  uchar
#define  uchar unsigned char
#endif

#ifndef  uint
#define  uint unsigned int
#endif

sbit  DSIO=P3^4;
sbit  RST=P3^5;
sbit  SCLK=P3^6;

void Ds1302Write(uchar addr,uchar dat);
uchar Ds1302Read(uchar addr);
void Ds1302WriteInt();
void Ds1302ReadTime();

extern  uchar  TIME[7];
#endif
回复

使用道具 举报

20

主题

1344

帖子

8381

积分

论坛元老

Rank: 8Rank: 8

积分
8381
发表于 2018-9-6 14:03:03 | 显示全部楼层
试试这个,HC6800-ES-V2.0板子的例程。
如果可以了,就可以对照自己程序找问题。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 支持 反对

使用道具 举报

2

主题

4

帖子

20

积分

新手上路

Rank: 1

积分
20
 楼主| 发表于 2018-9-6 20:58:46 | 显示全部楼层
HC6800-ES-V2.0 发表于 2018-9-6 14:03
试试这个,HC6800-ES-V2.0板子的例程。
如果可以了,就可以对照自己程序找问题。

例程基本差不多,但秒还是不走
回复 支持 反对

使用道具 举报

2

主题

4

帖子

20

积分

新手上路

Rank: 1

积分
20
 楼主| 发表于 2018-9-6 21:00:02 | 显示全部楼层
回复 支持 反对

使用道具 举报

0

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2018-10-5 03:52:32 | 显示全部楼层
会不会是硬件问题.
回复 支持 反对

使用道具 举报

20

主题

1344

帖子

8381

积分

论坛元老

Rank: 8Rank: 8

积分
8381
发表于 2018-10-8 08:38:16 | 显示全部楼层
肯定是你的程序问题!!!!!!由于时间关系,没有仔细去找你的问题,只是把我的1302程序替换了你的程序,就可以走时了。
主程序:(你的程序几乎没变,只改了一个我的1302初始化函数名)#include  "reg51.h"
#include  "ds1302.h"

typedef  unsigned char u8;
typedef  unsigned int u16;

sbit  lea=P2^2;
sbit  leb=P2^3;
sbit  lec=P2^4;

void delay(u16  i)
{
          while(i--);
}

u8  Disdate[8];
u8 code smgduan[]={0x3f  , 0x06 , 0x5b , 0x4f , 0x66 ,
                   0x6d , 0x7d , 0x07 , 0x7f  ,0x6f    };

void Dispros()
{        Ds1302ReadTime();
  Disdate[0]=smgduan[TIME[2]/16];
  Disdate[1]=smgduan[TIME[2]&0x0f];
  Disdate[2]=0x40;
  Disdate[3]=smgduan[TIME[1]/16];
  Disdate[4]=smgduan[TIME[1]&0x0f];
  Disdate[5]=0x40;
  Disdate[6]=smgduan[TIME[0]/16];
  Disdate[7]=smgduan[TIME[0]&0x0f];
}
void DigDisplay ()
{        u8  i;

           for(i=0;i<8;i++)
          { switch(i)
            { case(0):
                    lea=0;leb=0;lec=0;break;
                 case(1):
                    lea=1;leb=0;lec=0;break;
                 case(2):
                    lea=0;leb=1;lec=0;break;
                 case(3):
                    lea=1;leb=1;lec=0;break;
                 case(4):
                    lea=0;leb=0;lec=1;break;
                 case(5):
                    lea=1;leb=0;lec=1;break;
                 case(6):
                    lea=0;leb=1;lec=1;break;
                 case(7):
                    lea=1;leb=1;lec=1;break;
         }
             P0=Disdate[7-i];
                 delay(50);
                 P0=0x00;
    }
}
void  main()
{
   Ds1302Init();
   while(1)
   {
     Dispros();
            DigDisplay();
   }           

}


ds1302.c程序:(我的程序)
#include"ds1302.h"

/*
?DS1302????????????????,??????
*/
unsigned char code READ_RTC_ADDR[7]={0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
unsigned char code WRITE_RTC_ADDR[7]={0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};

/*
DS1302?????2017?4?26????16?30?00?
*/
unsigned char TIME[7] = {0, 0x30, 0x16, 0x26, 0x04, 0x03, 0x17};

void Ds1302Write(unsigned char addr, unsigned char dat)//?DS1302??????
                                                                                                //addr?????,dat?????
{
        unsigned char n;
        CE = 0;
        _nop_();

        SCLK = 0;//??SCLK?????
        _nop_();
        CE = 1;//???CE?????
        _nop_();

        for (n=0; n<8; n++)//??????????
        {
                DSIO = addr & 0x01;//?????????
                addr >>= 1;
                SCLK = 1;//???????,DS1302????
                _nop_();
                SCLK = 0;
                _nop_();
        }
        for (n=0; n<8; n++)//??8???
        {
                DSIO = dat & 0x01;
                dat >>= 1;
                SCLK = 1;//???????,DS1302????
                _nop_();
                SCLK = 0;
                _nop_();       
        }       
                 
        CE = 0;//??????
        _nop_();
}

unsigned char Ds1302Read(unsigned char addr)//?DS1302??????
{
        unsigned char n,dat,dat1;//n?8???,dat????????
                                                        //dat1????????
        CE = 0;
        _nop_();

        SCLK = 0;//??SCLK?????
        _nop_();
        CE = 1;//???CE?????
        _nop_();

        for(n=0; n<8; n++)//??????????
        {
                DSIO = addr & 0x01;//?????????
                addr >>= 1;
                SCLK = 1;//???????,DS1302????
                _nop_();
                SCLK = 0;//DS1302????,????
                _nop_();
        }
        _nop_();
        for(n=0; n<8; n++)//??8???
        {
                dat1 = DSIO;//????????
                dat = (dat>>1) | (dat1<<7);//?????595??????0??1
                SCLK = 1;
                _nop_();
                SCLK = 0;//DS1302????,????
                _nop_();
        }

        CE = 0;
        _nop_();//???DS1302???????,????
        SCLK = 1;
        _nop_();
        DSIO = 0;
        _nop_();
        DSIO = 1;
        _nop_();

        return dat;       
}

void Ds1302Init()//DS1302???
{
        unsigned char n;
        Ds1302Write(0x8E,0x00);//?????,?????????
        for (n=0; n<7; n++)//??7????????:???????
        {
                Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);       
        }
        Ds1302Write(0x8E,0x80);//???????
}

void Ds1302ReadTime()//????????
{
        unsigned char n;
        for (n=0; n<7; n++)//??7????????:???????
        {
                TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
        }
}


ds1302.h程序:(我的程序)
#ifndef __DS1302_H_
#define __DS1302_H_

#include<reg51.h>
#include<intrins.h>

sbit DSIO=P3^4;//???
sbit CE=P3^5;//??RST
sbit SCLK=P3^6;//??

void Ds1302Write(unsigned char addr, unsigned char dat);
unsigned char Ds1302Read(unsigned char addr);
void Ds1302Init();
void Ds1302ReadTime();

extern unsigned char TIME[7];//??????

#endif


不知道为什么,程序复制过来,注释都变问号了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|普中开源电子分享网 粤ICP备16123577号-2

GMT+8, 2024-4-27 00:10 , Processed in 0.105517 second(s), 31 queries .

Powered by 论坛搭建 X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表