逃逸电子的笔记 https://bbs.21ic.com/?107509 [收藏] [复制] [RSS] 在飞扬的电子尘埃中,寻找自己的天空.  QQ:41140388  EMAIL:zj_sx8192@yahoo.com.cn

日志

DS1302驱动

已有 1400 次阅读2007-4-14 18:21 |个人分类:单片机|系统分类:单片机| 坚持原创

接上次日历的东东, 用了一个1302的芯片,不过控制芯片我改成的是51的.还没经过测试。

//---------------------------------------------
typedef struct _TDS1302
{
 Uchar sec;
 Uchar min;
 Uchar hr;
 Uchar date;
 Uchar mth;
 Uchar wek;
 Uchar year;
} TDS1302_T;

//////////////////////////////////////
#define  WCOMMDSEC       0x80
#define  RCOMMDSEC       0x81
#define  WCOMMDMIN       0x82
#define  RCOMMDMIN       0x83
#define  WCOMMDHR        0x84
#define  RCOMMDHR        0x85
#define  WCOMMDDAT       0x86
#define  RCOMMDDAT       0x87
#define  WCOMMDMTH       0x88
#define  RCOMMDMTH       0x89
#define  WCOMMDWEK       0x8A
#define  RCOMMDWEK       0x8B
#define  WCOMMDYER       0x8C
#define  RCOMMDYER       0x8D
#define  WCOMMDPRT       0x8E

#define  WCOMMDTRCK      0x90

 

#define  WRAM0           0xB0  //
#define  RRAM0           0xB1  //
#define  WRAM1           0xB2  //
#define  RRAM1           0xB3  //         
#define  WRAM2           0xB4  //time alrm hr
#define  RRAM2           0xB5 
#define  WRAM3           0xB6  // min
#define  RRAM3           0xB7
#define  WRAM4           0xB8  // sec
#define  RRAM4           0xB9
#define  WRAM5           0xBA
#define  RRAM5           0xBB
#define  WRAM6           0xBC
#define  RRAM6           0xBD
#define  WRAM7           0xBE
#define  RRAM7           0xBF
#define  WRAM8           0xC1
#define  RRAM8           0xC2
#define  WRAM9           0xC3
#define  RRAM9           0xC4

 

//---------------------------------------------
#define  NOP() _nop_(),_nop_()
//---------------------------------------------
sbit SCK = P1^5;
sbit IOD = P1^6;
sbit RST = P1^7;

 

//---------------------------------------------
TDS1302_T DispTime, AlarmTime;
//---------------------------------------------
static const unsigned char code CtrCom[]={00,00,01,01,01,01,06,0xA5};
static const unsigned char code CtrDat[]={WCOMMDSEC,WCOMMDMIN,WCOMMDHR,WCOMMDDAT,WCOMMDMTH,WCOMMDWEK,WCOMMDYER,WCOMMDTRCK}; 
//---------------------------------------------
static void _SendCommd(Ucja Commd)
{
        Uchar i;
       
        for(i = 1; i  ; i<<=1)
        {
            SCK = 0;
            IOD = i&Commd;
            SCK = 1;
        }       
}

//---------------------------------------------
static void  _Write_Byte(Uchar Commd, Uchar dat)
{
 
        RST = 1;
        SCK = 0;
        _SendCommd(Commd);  
        _SendCommd(dat);
        SCK = 0;
        RST = 0;
}
//---------------------------------------------
static Uchar _Read_Byte(Uchar Commd)
{
        Uchar i;
        Uchar Rcv = 0;
       
        RST = 1;
        SCK = 0;
        _SendCommd(Commd); 
        for(i = 0x01; i ; i <<=1)
        {
            SCK = 0;
            NOP(); 
            if(IOD)
            {
              Rcv |= i;
            }
            SCK = 1;
        }       
        SCK = 0;
        RST = 0;
        return(Rcv);
}
//---------------------------------------------
void  Ds1302_init(void)
{
        SCK = 1;
        IOD = 1;
        RST = 1;
}
//---------------------------------------------
void  Write_Ds1302(Uchar cmd, Uchar data)
{
      
        _Write_Byte(WCOMMDPRT,0x00);
        _Write_Byte(cmd, data);
        _Write_Byte(WCOMMDPRT,0x80);
}
//---------------------------------------------
Uchar Read_Ds1302(Uchar Addr)
{
        Uchar i;

        i = _Read_Byte(Addr);

        return(i);

}

 

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)