|
谢谢啦。各位!求修改!!!
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define GPIO_DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit DSIO=P3^4;
sbit RST=P3^5;
sbit SCLK=P3^6;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
void Ds1302Write(uchar addr, uchar dat);
uchar Ds1302Read(uchar addr);
char flag=0;
void Ds1302Init();
void Ds1302ReadTime();
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//---加入全局变量--//
extern uchar TIME[7]; //加入全局变量
//--定义全局变量--//
unsigned char code DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};//0x77,0x7c,0x39,0x5e,0x79,0x71
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
unsigned char DisplayData[8];
//用来存放要显示的8位数的值
uchar code READ_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, 0x0c, 0x01, 0x02, 0x13};
//--声明全局变量--//
void DigDisplay();
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//显示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//显示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//显示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//显示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//显示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//显示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//显示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//显示第7位
}
GPIO_DIG=DisplayData[i];//发送段码
j=50; //扫描间隔时间设定
while(j--);
GPIO_DIG=0x00;//消隐
}
}
void Ds1302Write(uchar addr, uchar dat)
{
uchar n;
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_();
}
/*******************************************************************************
* 函 数 名 : Ds1302Read
* 函数功能 : 读取一个地址的数据
* 输 入 : addr
* 输 出 : dat
*******************************************************************************/
uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;
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_();
return dat/16*10+dat%16;
}
/*******************************************************************************
* 函 数 名 : 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); //打开写保护功能
}
void set_ds1302()
{
Ds1302Write(0x8E,0X00);
Ds1302Write(0x82,TIME[2]/10<<4|(TIME[2]%10));
Ds1302Write(0x84,TIME[1]/10<<4|(TIME[1]%10));
Ds1302Write(0x8E,0x80);
}
void into() interrupt 0
{
key1=1;
if(key1==0)
{
delay(5);
if(key1==0)
{
flag=1;
TIME[1]=(TIME[1]+1)%60;
}
while(!key1);
}
else
key2=1;
if(key2==0)
{
delay(5);
if(key2==0)
{
flag=1;
TIME[2]=(TIME[2]+1)%24;
}
}
else
key3=1;
if(key3==0)
{
delay(5);
if(key3==0)
{
set_ds1302();
delay(5);
flag=0;
}
}
}
/*******************************************************************************
* 函 数 名 : Ds1302ReadTime
* 函数功能 : 读取时钟信息
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Ds1302ReadTime()
{
//uchar n;
//for (n=0; n<7; n++)//读取7个字节的时钟信号:分秒时日月周年
//{
TIME[1] = Ds1302Read(0X83);
TIME[2] = Ds1302Read(0X85);
}
void main()
{
Ds1302Init();
EA=1;
EX0=1;
IT0=1;
while(1)
{
if(flag==0)
{
Ds1302ReadTime();
DisplayData[3] = DIG_CODE[TIME[2]/10]; //时
DisplayData[2] = DIG_CODE[TIME[2]%10];
DisplayData[1] = DIG_CODE[TIME[1]/10]; //分
DisplayData[0] = DIG_CODE[TIME[1]%10];
DigDisplay();
}
}
}
|
|