文档库 最新最全的文档下载
当前位置:文档库 › DS1302时钟程序C语言代码

DS1302时钟程序C语言代码

/*******************************************************************************
* 实验名 : 万年历实验
* 使用的IO :
* 实验效果 :1602显示时钟
* 注意 :
*******************************************************************************/

#include
#include"ds1302.h"

//数码管IO
#define DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

unsigned char code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Num=0;
unsigned int disp[8]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};

void LcdDisplay();
void Timer0Configuration();

/*******************************************************************************
* 函数名 : main
* 函数功能 : 主函数
* 输入 : 无
* 输出 : 无
*******************************************************************************/

void main()
{
Ds1302Init();
Timer0Configuration();
while(1)
{
Ds1302ReadTime();
disp[7] = DIG_CODE[TIME[0]&0x0f];
disp[6] = DIG_CODE[TIME[0]>>4];
disp[5] = 0X40;
disp[4] = DIG_CODE[TIME[1]&0x0f];
disp[3] = DIG_CODE[TIME[1]>>4];
disp[2] = 0X40;
disp[1] = DIG_CODE[TIME[2]&0x0f];
disp[0] = DIG_CODE[TIME[2]>>4];
}

}

/*******************************************************************************
* 函数名 : Timer0Configuration()
* 函数功能 : 设置计时器
* 输入 : 无
* 输出 : 无
*******************************************************************************/

void Timer0Configuration()
{
TMOD=0X02;//选择为定时器模式,工作方式2,仅用TRX打开启动。

TH0=0X9C; //给定时器赋初值,定时100us
TL0=0X9C;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}

/*******************************************************************************
* 函数名 : DigDisplay() interrupt 1
* 函数功能 : 中断数码管显示
* 输入 : 无
* 输出 : 无
*******************************************************************************/

void DigDisplay() interrupt 1
{
//定时器在工作方式二会自动重装初,所以不用在赋值。
// TH0=0X9c;//给定时器赋初值,定时1ms
// TL0=0X00;
DIG=0; //消隐
switch(Num) //位选,选择点亮的数码管,
{
case(7):
LSA=0;LSB=0;LSC=0; break;
case(6):
LSA=1;LSB=0;LSC=0; break;
case(5):
LSA=0;LSB=1;LSC=0; break;
case(4):
LSA=1;LSB=1;LSC=0; break;
case(3):
LSA=0;LSB=0;LSC=1; break;
case(2):
LSA=1;LSB=0;LSC=1; break;
case(1):
LSA=0;LSB=1;LSC=1; break;
case(0):
LSA=1;LSB=1;LSC=1; break;
}
DIG=disp[Num]; //段选,选择显示的数字。
Num++;
if(Num>7)
Num=0;
}






#include"ds1302.h"

//---DS1302写入和读取时分秒的地址命令---//
//---秒分时日月周年 最低位读写位;-------//
uchar code RE

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

//---DS1302时钟初始化2013年1月1日星期二12点00分00秒。---//
//---存储顺序是秒分时日月周年,存储格式是用BCD码---//
uchar TIME[7] = {0, 0, 0x12, 0x01, 0x01, 0x02, 0x13};

/*******************************************************************************
* 函 数 名 : Ds1302Write
* 函数功能 : 向DS1302命令(地址+数据)
* 输 入 : addr,dat
* 输 出 : 无
*******************************************************************************/

void Ds1302Write(uchar addr, uchar dat)
{
uchar n;

EA = 0;
RST = 0;
_nop_();

SCLK = 0;//先将SCLK置低电平。
_nop_();
RST = 1; //然后将RST(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_();
}

RST = 0;//传送数据结束
_nop_();
EA = 1;
}

/*******************************************************************************
* 函 数 名 : Ds1302Read
* 函数功能 : 读取一个地址的数据
* 输 入 : addr
* 输 出 : dat
*******************************************************************************/

uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;

EA =0;
RST = 0;
_nop_();

SCLK = 0;//先将SCLK置低电平。
_nop_();
RST = 1;//然后将RST(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);
SCLK = 1;
_nop_();
SCLK = 0;//DS1302下降沿时,放置数据
_nop_();
}

RST = 0;
_nop_(); //以下为DS1302复位的稳定时间,必须的。
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
EA = 1;
return dat;
}

/*******************************************************************************
* 函 数 名 : Ds1302Init
* 函数功能 : 初始化DS1302.
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/

void Ds1302Init()
{
uchar n;
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{

Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
}

/*******************************************************************************
* 函 数 名 : Ds1302ReadTime
* 函数功能 : 读取时钟信息
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/

void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//读取7个字节的时钟信号:分秒时日月周年
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}

}



#ifndef __DS1302_H_
#define __DS1302_H_

//---包含头文件---//
#include
#include

//---重定义关键词---//
#ifndef uchar
#define uchar unsigned char
#endif

#ifndef uint
#define uint unsigned int
#endif

//---定义ds1302使用的IO口---//
sbit DSIO=P3^4;
sbit RST=P3^5;
sbit SCLK=P3^6;

//---定义全局函数---//
void Ds1302Write(uchar addr, uchar dat);
uchar Ds1302Read(uchar addr);
void Ds1302Init();
void Ds1302ReadTime();

//---加入全局变量--//
extern uchar TIME[7]; //加入全局变量

#endif

相关文档
相关文档 最新文档