|
- <P>程序如下</P>
- <P>/*************** DS1302.C ******************/
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit IO = P1^5; //DS1302's I/O
- sbit SCLK = P1^6; //DS1302's clock
- sbit RST = P1^7; //DS1302's Reset
- void Write_A_Byte_TO_DS1302(uchar x)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- IO=x&0x01;SCLK=1;SCLK=0;x>>=1;
- }
- }
- uchar Get_A_Byte_FROM_DS1302()
- {
- uchar i,b=0x00;
- for(i=0;i<8;i++)
- {
- b |= _crol_((uchar)IO,i);
- SCLK=1;SCLK=0;
- }
- return b/16*10+b%16; //返回BCD码
- }
- uchar Read_Data(uchar addr) //从DS1302指定位置读数据
- {
- uchar dat;
- RST = 0;SCLK=0;RST=1;
- Write_A_Byte_TO_DS1302(addr);
- dat = Get_A_Byte_FROM_DS1302();
- SCLK=1;RST=0;
- return dat;
- }
- void Write_DS1302(uchar addr,uchar dat)
- {
- SCLK = 0; RST = 1;
- Write_A_Byte_TO_DS1302(addr);
- Write_A_Byte_TO_DS1302(dat);
- SCLK = 0; RST = 0;
- }
- void GetTime(uchar *DateTime ) //读取当前日期时间
- {
- uchar i,addr = 0x81;
- for(i=0;i<7;i++)
- {
- DateTime[i]=Read_Data(addr);addr+=2;
- }
- }
- void SET_DS1302(uchar *DateTime)
- {
- uchar i;
- Write_DS1302(0x8E,0x00);//写控制位,取消保护
- for(i=1;i<7;i++)
- Write_DS1302(0x80+2*i,(DateTime[i]/10<<4)|(DateTime[i]%10));
- Write_DS1302(0x8E,0x80); //加保护
- }
- *********************Display********************
- #include "Display.h"
- extern unsigned char TempData[8]; //存储显示值的全局变量
- /*------------------------------------------------
- 显示函数,用于动态扫描数码管
- ------------------------------------------------*/
- void NormalDisplay(void)
- {
- if((time_buf1[4]/10)==0)
- {LATCH1=1;}
- else
- {LATCH1=0;}
- DataPort=TempData[0];
- DelayMs(4);
- LATCH1=1;
- LATCH2=0;
- DataPort=TempData[1];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH2=1;
- LATCH3=0;
- DataPort=TempData[2];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH3=1;
- LATCH4=0;
- DataPort=TempData[3];
- DelayMs(4);
- LATCH4=1;
- void HourDisplay(void)
- {
- if((time_buf1[4]/10)==0)
- {LATCH1=1;}
- else
- {
- if(((time_buf1[6]%10)%2)==0)
- {LATCH1=0;}
- else
- {LATCH1=1;}
- }
- DataPort=TempData[0];
- DelayMs(4);
- LATCH1=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH2=0;}
- else
- {LATCH2=1;}
- DataPort=TempData[1];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH2=1;
- LATCH3=0;
- DataPort=TempData[2];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH3=1;
- LATCH4=0;
- DataPort=TempData[3];
- DelayMs(2);
- LATCH4=1;
- }
- void MinuteDisplay(void)
- {
- if((time_buf1[4]/10)==0)
- {LATCH1=1;}
- else
- {LATCH1=0;}
- DataPort=TempData[0];
- DelayMs(4);
- LATCH1=1;
- LATCH2=0;
- DataPort=TempData[1];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH2=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH3=0;}
- else
- {LATCH3=1;}
- DataPort=TempData[2];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(2);
- LATCH3=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH4=0;}
- else
- {LATCH4=1;}
- DataPort=TempData[3];
- DelayMs(2);
- LATCH4=1;
- }
- void DataDisplay(void)
- {
- LATCH1=0;
- DataPort=TempData[4];
- DelayMs(4);
- LATCH1=1;
- LATCH2=0;
- DataPort=TempData[5];
- //SEC=((time_buf1[6]%10)%2); //跳秒处理
- SEC=0;
- DelayMs(4);
- LATCH2=1;
- LATCH3=0;
- DataPort=TempData[6];
- //SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH3=1;
- LATCH4=0;
- DataPort=TempData[7];
- SEC=0;
- DelayMs(4);
- LATCH4=1;
- }
- void MoothDisplay(void)
- {
- if(((time_buf1[6]%10)%2)==0)
- {LATCH1=0;}
- else
- {LATCH1=1;}
- DataPort=TempData[4];
- DelayMs(4);
- LATCH1=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH2=0;}
- else
- {LATCH2=1;}
- DataPort=TempData[5];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH2=1;
- LATCH3=0;
- DataPort=TempData[6];
- //SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(2);
- LATCH3=1;
- LATCH4=0;
- DataPort=TempData[7];
- DelayMs(2);
- LATCH4=1;
- }
- void DayDisplay(void)
- {
- LATCH1=0;
- DataPort=TempData[4];
- DelayMs(4);
- LATCH1=1;
- LATCH2=0;
- DataPort=TempData[5];
- //SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(4);
- LATCH2=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH3=0;}
- else
- {LATCH3=1;}
- DataPort=TempData[6];
- //SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(2);
- LATCH3=1;
- if(((time_buf1[6]%10)%2)==0)
- {LATCH4=0;}
- else
- {LATCH4=1;}
- DataPort=TempData[7];
- SEC=((time_buf1[6]%10)%2); //跳秒处理
- DelayMs(2);
- LATCH4=1;
- }
- *******************Delay**************
- #include "delay.h"
- void DelayUs(unsigned char t)
- {
- while(--t);
- }
- void DelayMs(unsigned char t)
- {
- while(t--)
- {
- //大致延时1mS
- DelayUs(245);
- DelayUs(245);
- }
- }
- **********************main.c*******************
- #include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
- #include "ds1302.h"
- #include "Display.h"
- #include "Delay.h"
- sbit LED0=P1^0;
- sbit LED1=P1^1;
- sbit K1=P2^4; //定义按键端口,选择调整什么
- sbit K2=P2^5; //加键
- bit ReadTimeFlag=1;//定义读时间标
- //bit SecondFlag=0;
- unsigned char code DuanMa1[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};// 共阳显示段码值0~9
- unsigned char code DuanMa2[10]={0xc0,0xcf,0xa4,0x86,0x8b,0x92,0x90,0xc7,0x80,0x82};//第三个数码管的数组
- unsigned char TempData[8]; //存储显示值的全局变量
- unsigned char SetTime=0;
- void Init_Timer0(void);//定时器初始化
- /*------------------------------------------------
- 主函数
- ------------------------------------------------*/
- void main(void)
- {
- Init_Timer0();
- Ds1302_Init();
- Ds1302_Read_Time();
- while(1)
- {
- if(!K1)
- {
- DelayMs(10);
- if(!K1)
- {
- while(!K1);
- {
- LED0=~LED0;
- SetTime++;
- if(SetTime==5) SetTime=0;
- }
- }
- }
- if(!K2)
- {
- DelayMs(10);
- if(!K2)
- {
- while(!K2);
- {
- LED1=~LED1;
- if(SetTime==0)
- {SetTime=50;LED0=~LED0;}
- else
- { //
- switch(SetTime)
- {
- case 1:time_buf1[4]++;if(time_buf1[4]==24)time_buf1[4]=0;
- Ds1302_Write_Time();break; //正常时间 小时 加1
- case 2:time_buf1[5]++;if(time_buf1[5]==60)time_buf1[5]=0;
- Ds1302_Write_Time();break;//分加1
- case 3:time_buf1[2]++;if(time_buf1[2]==13)time_buf1[2]=1;
- Ds1302_Write_Time();break; //正常时间 月 加1
- case 4:time_buf1[3]++;if(time_buf1[3]==31)time_buf1[3]=1;
- Ds1302_Write_Time();break;//日 加1
- default:break;
- }
- }
- }
- }
- } //
- if(ReadTimeFlag==1) //大致100ms
- {
- ReadTimeFlag=0;
- TempData[0]=DuanMa1[time_buf1[4]/10];//时 //数据的转换,
- TempData[1]=DuanMa1[time_buf1[4]%10];//因我们采用数码管0~9的显示,将数据分开
- TempData[2]=DuanMa2[time_buf1[5]/10];//分
- TempData[3]=DuanMa1[time_buf1[5]%10];
- TempData[4]=DuanMa1[time_buf1[2]/10];//月
- TempData[5]=DuanMa1[time_buf1[2]%10];
- TempData[6]=DuanMa2[time_buf1[3]/10];//日
- TempData[7]=DuanMa1[time_buf1[3]%10];
- }
- }
- }
- /*------------------------------------------------
- 定时器初始化子程序 //
- ------------------------------------------------*/
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
- //TH0=0x00; //给定初值
- //TL0=0x00;
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关打开
- /*------------------------------------------------
- 定时器中断子程序
- ------------------------------------------------*/
- void Timer0_isr(void) interrupt 1
- {
- static unsigned int num;
- TH0=(65536-500)/256; //重新赋值 0.5ms
- TL0=(65536-500)%256;
- num++;
- if(num==1)
- {
- Ds1302_Read_Time();
- switch(SetTime)
- {
- case 0: NormalDisplay();break;
- case 1: HourDisplay();break;
- case 2: MinuteDisplay();break;
- case 3: MoothDisplay();break;
- case 4: DayDisplay();break;
- case 50:
- while(SetTime--)
- {
- DataDisplay();DelayMs(20000);;
- } SetTime=0;break;
- default:break;
- }
- }
- if(num==50) //大致100ms
- {
- num=0;
- ReadTimeFlag=1;
- }
- }</P>
复制代码
|
|