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

📄 ds12887.c

📁 基于12887和80c52的时钟程序
💻 C
字号:
//DS12887的C驱动程序

#define uchar unsigned char
#define uint unsigned int
#include <reg52.h>
#include <stdio.h>
#include <absacc.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "ds12887.h"


/************************************************************* 
文件名称:ds12c887.c
适用范围:时钟芯片ds12c887的驱动程序
*************************************************************/   
#include <absacc.h>
/* 命令常量定义 */
#define CMD_START_DS12C887     0x20            /* 开启时钟芯片 
*/
#define CMD_START_OSCILLATOR 0x70            /* 开启振荡器,
处于抑制状态 */
#define CMD_CLOSE_DS12C887     0x30            /* 关掉时钟芯片 
*/
/* 所有的置位使用或操作,清除使用与操作 */
#define MASK_SETB_SET         0x80            /* 禁止刷新 */
#define MASK_CLR_SET         0x7f            /* 使能刷新 */
#define MASK_SETB_DM         0x04            /* 使用HEX格式 
*/
#define MASK_CLR_DM             0xfb            /* 使用BCD码格式 */
#define MASK_CLR_DM_BIN             0xff//使用二进制
#define MASK_SETB_2412         0x02            /* 使用24小时模式 */
#define MASK_CLR_2412         0xfd            /* 使用12小时模式 */
#define MASK_SETB_DSE         0x01            /* 使用夏令时 */
#define MASK_CLR_DSE         0xfe            /* 不使用夏令时 
*/

/* 寄存器地址通道定义 */
xdata char chSecondsChannel _at_ 0xB700;
xdata char chMinutesChannel _at_ 0xB702;
xdata char chHoursChannel   _at_ 0xB704;
xdata char chDofWChannel    _at_ 0xB706;
xdata char chDateChannel    _at_ 0xB707;
xdata char chMonthChannel   _at_ 0xB708;
xdata char chYearChannel    _at_ 0xB709;
xdata char chCenturyChannel _at_ 0xB732;
xdata char chRegA    _at_ 0xB70a;
xdata char chRegB    _at_ 0xB70b;
xdata char chRegC    _at_ 0xB70c;
xdata char chRegD    _at_ 0xB70d;

/* 函数声明部分 */
void StartDs12c887(void);
void CloseDs12c887(void);
void InitDs12c887(void);
unsigned char GetSeconds(void);
unsigned char GetMinutes(void);
unsigned char GetHours(void);
unsigned char GetDay(void);
unsigned char GetDate(void);
unsigned char GetMonth(void);
unsigned char GetYear(void);
unsigned char GetCentury(void);
void SetTime(unsigned char chSeconds,unsigned char chMinutes,unsigned char 
chHours);
void SetDate(unsigned char chDate,unsigned char chMonth,unsigned char chYear);

//unsigned char BCD2BIN(unsigned char cData)

/************************************************************* 
函数功能:该函数用来启动时钟芯片工作
应用范围:仅在时钟芯片首次使用时用到一次
入口参数:
出口参数:
*************************************************************/   
void StartDs12c887(void)
{
    chRegA = CMD_START_DS12C887;
}

/************************************************************* 
函数功能:该函数用来关闭时钟芯片
应用范围:一般用不到
入口参数:
出口参数:
*************************************************************/   
void CloseDs12c887(void)
{
    chRegA = CMD_CLOSE_DS12C887;
}

void InitDs12c887()
{
    StartDs12c887();
    chRegB = chRegB | MASK_SETB_SET;        /* 禁止刷新 */
    chRegB = chRegB & MASK_CLR_DM | MASK_SETB_2412\
	   & MASK_CLR_DSE;
                            
                /* 使用BCD码格式、24小时模式、不使用
夏令时 */
    chCenturyChannel = 0x21;                /* 设
置为21世纪 */
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新 */
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取秒字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetSeconds(void)
{
    return(chSecondsChannel);
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取分字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetMinutes(void)
{
    return(chMinutesChannel);
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取小时字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetHours(void)
{
    return(chHoursChannel);
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取日字节
应用范围:
入口参数:
出口参数:
*************************************************************/ 
unsigned char GetDay(void)
{
    return(chDofWChannel);
}  
unsigned char GetDate(void)
{
    return(chDateChannel);
}
/************************************************************* 
函数功能:该函数用来从时钟芯片读取月字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetMonth(void)
{
    return(chMonthChannel);
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取年字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetYear(void)
{
    return(chYearChannel);
}

/************************************************************* 
函数功能:该函数用来从时钟芯片读取世纪字节
应用范围:
入口参数:
出口参数:
*************************************************************/   
unsigned char GetCentury(void)
{
    return(chCenturyChannel);
}    

/************************************************************* 
函数功能:该函数用来设置时钟芯片的时间
应用范围:
入口参数:chSeconds、chMinutes、chHours是设定时间的压缩BCD码
出口参数:
*************************************************************/   
void SetTime(unsigned char chSeconds,unsigned char chMinutes,unsigned char chHours)
{
    chRegB = chRegB | MASK_SETB_SET;        /* 禁止刷新 */
    chSecondsChannel = chSeconds;
    chMinutesChannel = chMinutes;
    chHoursChannel = chHours;
    chRegB = chRegB & MASK_CLR_SET;            /* 使能刷新 */
}

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


void setup12887(uchar *p) //设置系统时间
{
	uchar i;
	i=P12887d;
	P12887a=0x70; P12887b=0xa2; P128870=*p++; P128871=0xff; P128872=*p++;
	P128873=0xff; P128874=*p++; P128875=0xff; P128876=*p++; P128877=*p++;
	P128878=*p++; P128879=*p++; P12887b=0x22; P12887a=0x20;
	i=P12887c;
}
void read12887(uchar *p) //读取系统时间
{
	uchar a;
	do{ a=P12887a; } while((a&0x80)==0x80);
	*p++=P128870; *p++=P128872; *p++=P128874; *p++=P128876;
	*p++=P128877; *p++=P128878; *p++=P128879;
}
void start12887(void) //启动时钟
{
	uchar i;
	i=P12887d;
	P12887a=0x70; P12887b=0xa2; P128871=0xff; P128873=0xff; P128875=0xff;
	P12887b=0x22; P12887a=0x20;
	i=P12887c;
}

/*unsigned char BCD2BIN(unsigned char cData)
{
	unsigned tem=0;
	tem=cData&0x0f;
	cData=(cData&0xf0)>>4;
	tem+=cData*10;
	return tem;
}*/

⌨️ 快捷键说明

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