⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12887.c

📁 ds12c87读写程序
💻 C
字号:

#include<reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define MASK_SETB_SET         0x80            /* 禁止刷新 */
#define MASK_CLR_SET          0x7f            /*使能刷新*/
/*寄存器地址通道定义*/
#define chSecondsChannel  XBYTE[0xef00]
#define chMinutesChannel XBYTE[0xef02]
#define chHoursChannel   XBYTE[0xef04]
#define chDofWChannel    XBYTE[0xef06]
#define chDateChannel    XBYTE[0xef07]
#define chMonthChannel   XBYTE[0xef08]
#define chYearChannel    XBYTE[0xef09]
#define chCenturyChannel XBYTE[0xef32]
#define chRegA    XBYTE[0xef0a]
#define chRegB    XBYTE[0xef0b]
#define chRegC    XBYTE[0xef0c]
#define chRegD    XBYTE[0xef0d]
/*-------------------------------------------------------*/
#define  NUM_0     46                   //second  data
#define  NUM_1     31                   //minute  data
#define  NUM_2     10                   //hour  data
#define  NUM_3     16                   //day data
#define  NUM_4      8                   //month  data
#define  NUM_5     04                  //year data
/*------------------------------------------------------*/



/*函数声明部分*/
void init_DS12887();
void SetTime(uchar chSeconds,uchar chMinutes,uchar chHours);
void SetDate(uchar chDate,uchar chMonth,uchar chYear);
uchar GetRegC(void);
uchar GetRegD(void);
/*日历时钟初始化函数*/
void init_DS12887()
{
  /*--------------------*/ 
   chRegA=0x70;//
   /*-------------------*/
   
   chRegB=0x8a;/*停止更新周期,允许更新中断,BCD码24小时,禁止夏令时*/
   chSecondsChannel=NUM_0;/*秒时标单元送00*/
   chMinutesChannel=NUM_1;/*秒时标单元送00*/
   chHoursChannel=NUM_2;/*小时时标单元送12*/
  // chDofWChannel=NUM_3;/*星期时标单元送01*/
   chDateChannel=NUM_3;/*日期时标单元送01*/
   chMonthChannel=NUM_4;/*月时标单元送01*/
   chYearChannel=NUM_5; /*年时标单元送03*/
  //chCenturyChannel=0x21;/*世纪时标单元送21*/
   GetRegC();/*清状态寄存器D的URT位置“1”*/
   GetRegD();/*状态寄存器D的URT位置‘1’*/
   chRegA=0x28;//DV2~DV0=010  RS3~RS0=1000
   chRegB=0x1A;//每秒更新一次,不允许方波输出,24小时制时钟  开始运行时钟开始运行
}
  /*************************************************************
函数功能:该函数用来读取状态寄存器C
应用范围
入口参数:
出口参数:chRegC
*************************************************************/
uchar GetRegC(void)
{
    return(chRegC);
}
/*************************************************************
函数功能:该函数用来读取状态寄存器D
应用范围
入口参数:
出口参数:chRegD
*************************************************************/
uchar GetRegD(void)
{
    return(chRegD);
}
/*************************************************************
函数功能:该函数用来设置时钟芯片的时间
应用范围
入口参数:chSeconds、chMinutes、chHours是设定时间的压缩BCD码
出口参数:
*************************************************************/
void SetTime(uchar chSeconds,uchar chMinutes,uchar chHours)
{
    chRegB = chRegB | MASK_SETB_SET;        /* 禁止刷新 */
    chSecondsChannel = chSeconds;
    chMinutesChannel = chMinutes;
    chHoursChannel = chHours;
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新*/
}

/*************************************************************
函数功能:该函数用来设置时钟芯片的日期
应用范围:
入口参数:chDate、chMonth、chYe
出口参数
*************************************************************/
void SetDate(uchar chDate,uchar chMonth,uchar chYear)
{
    chRegB = chRegB | MASK_SETB_SET;         /* 禁止刷新 */
    chDateChannel = chDate;
    chMonthChannel = chMonth;
    chYearChannel = chYear;
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新 */
}

void main()
{
  uchar systime[6];
init_DS12887();
while(1)
{   while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[0]=chSecondsChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[1]=chMinutesChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[2]=chHoursChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[3]=chDateChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[4]=chMonthChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[5]=chYearChannel;//读时间
} 
29523714(志龙) 10:47:28
#include<reg51.h>
#include <absacc.h>
#define uchar unsigned char
#define MASK_SETB_SET         0x80            /* 禁止刷新 */
#define MASK_CLR_SET          0x7f            /*使能刷新*/
/*寄存器地址通道定义*/
#define chSecondsChannel  XBYTE[0xef00]
#define chMinutesChannel XBYTE[0xef02]
#define chHoursChannel   XBYTE[0xef04]
#define chDofWChannel    XBYTE[0xef06]
#define chDateChannel    XBYTE[0xef07]
#define chMonthChannel   XBYTE[0xef08]
#define chYearChannel    XBYTE[0xef09]
#define chCenturyChannel XBYTE[0xef32]
#define chRegA    XBYTE[0xef0a]
#define chRegB    XBYTE[0xef0b]
#define chRegC    XBYTE[0xef0c]
#define chRegD    XBYTE[0xef0d]
/*-------------------------------------------------------*/
#define  NUM_0     46                   //second  data
#define  NUM_1     31                   //minute  data
#define  NUM_2     10                   //hour  data
#define  NUM_3     16                   //day data
#define  NUM_4      8                   //month  data
#define  NUM_5     04                  //year data
/*------------------------------------------------------*/



/*函数声明部分*/
void init_DS12887();
void SetTime(uchar chSeconds,uchar chMinutes,uchar chHours);
void SetDate(uchar chDate,uchar chMonth,uchar chYear);
uchar GetRegC(void);
uchar GetRegD(void);
/*日历时钟初始化函数*/
void init_DS12887()
{
  /*--------------------*/ 
   chRegA=0x70;//
   /*-------------------*/
   
   chRegB=0x8a;/*停止更新周期,允许更新中断,BCD码24小时,禁止夏令时*/
   chSecondsChannel=NUM_0;/*秒时标单元送00*/
   chMinutesChannel=NUM_1;/*秒时标单元送00*/
   chHoursChannel=NUM_2;/*小时时标单元送12*/
  // chDofWChannel=NUM_3;/*星期时标单元送01*/
   chDateChannel=NUM_3;/*日期时标单元送01*/
   chMonthChannel=NUM_4;/*月时标单元送01*/
   chYearChannel=NUM_5; /*年时标单元送03*/
  //chCenturyChannel=0x21;/*世纪时标单元送21*/
   GetRegC();/*清状态寄存器D的URT位置“1”*/
   GetRegD();/*状态寄存器D的URT位置‘1’*/
   chRegA=0x28;//DV2~DV0=010  RS3~RS0=1000
   chRegB=0x1A;//每秒更新一次,不允许方波输出,24小时制时钟  开始运行时钟开始运行
}
  /*************************************************************
函数功能:该函数用来读取状态寄存器C
应用范围
入口参数:
出口参数:chRegC
*************************************************************/
uchar GetRegC(void)
{
    return(chRegC);
}
/*************************************************************
函数功能:该函数用来读取状态寄存器D
应用范围
入口参数:
出口参数:chRegD
*************************************************************/
uchar GetRegD(void)
{
    return(chRegD);
}
/*************************************************************
函数功能:该函数用来设置时钟芯片的时间
应用范围
入口参数:chSeconds、chMinutes、chHours是设定时间的压缩BCD码
出口参数:
*************************************************************/
void SetTime(uchar chSeconds,uchar chMinutes,uchar chHours)
{
    chRegB = chRegB | MASK_SETB_SET;        /* 禁止刷新 */
    chSecondsChannel = chSeconds;
    chMinutesChannel = chMinutes;
    chHoursChannel = chHours;
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新*/
}

/*************************************************************
函数功能:该函数用来设置时钟芯片的日期
应用范围:
入口参数:chDate、chMonth、chYe
出口参数
*************************************************************/
void SetDate(uchar chDate,uchar chMonth,uchar chYear)
{
    chRegB = chRegB | MASK_SETB_SET;         /* 禁止刷新 */
    chDateChannel = chDate;
    chMonthChannel = chMonth;
    chYearChannel = chYear;
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新 */
}

void main()
{
  uchar systime[6];
init_DS12887();
while(1)
{   while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[0]=chSecondsChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[1]=chMinutesChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[2]=chHoursChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[3]=chDateChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[4]=chMonthChannel;//读时间
     while((chRegA&0X80)==0X80);//判UIP位,是否可读数据
       systime[5]=chYearChannel;//读时间
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -