|
謝謝啦。各位!求修改!!!
#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();
}
}
}
|
|