|
看看可以用
- #include <reg52.h>
- #include "shumaguan.h"
- #include "DS1302.h"
- //main.c文件
- unsigned char count;
- void main()
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;//50ms
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
-
- init_DS1302(); //初始化DS1302
- while(1)
- {
- gettime(); //得到DS1302時間
- ling=Time[2]/16;
- yi=Time[2]%16;
- er=10;
- san=Time[1]/16;
- si=Time[1]%16;
- wu=10;
- liu=Time[0]/16;
- qi=Time[0]%16;
- if (count==20)// 一秒后變成顯示年月日
- {
- ling=Time[6]/16;
- yi=Time[6]%16;
- er=10;
- san=Time[4]/16;
- si=Time[4]%16;
- wu=10;
- liu=Time[3]/16;
- qi=Time[3]%16;
-
- }
- show(); // 顯示在數(shù)碼管
- }
- }
- void Time0() interrupt 1
- {
- TH0=(65536-50000)/256;//50ms
- TL0=(65536-50000)%256;
- count++;
- }
- //shumaguan.h文件
- #ifndef __shumaguan_H_
- #define __shumaguan_H_
- #include <intrins.h>
- void Delay2ms();
- void show();
- unsigned char digital[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF};
- unsigned char ling,yi,er,san,si,wu,liu,qi;
- // unsigned char Time[7];
- void show()
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- P2=0xc0;
- switch(i)
- {
- case 0: P0=0x01; P2=0xE0;P0=digital[ling];break;
- case 1: P0=0x02; P2=0xE0;P0=digital[yi];break;
- case 2: P0=0x04; P2=0xE0;P0=digital[er];break;
- case 3: P0=0x08; P2=0xE0;P0=digital[san];break;
- case 4: P0=0x10; P2=0xE0;P0=digital[si];break;
- case 5: P0=0x20; P2=0xE0;P0=digital[wu];break;
- case 6: P0=0x40; P2=0xE0;P0=digital[liu];break;
- case 7: P0=0x80; P2=0xE0;P0=digital[qi];break;
- }
- Delay2ms();
- P0=0xff;
- }
- }
- void Delay2ms() //@11.0592MHz
- {
- unsigned char i, j;
- _nop_();
- i = 4;
- j = 146;
- do
- {
- while (--j);
- } while (--i);
- }
- #endif
- //DS1302文件
- #ifndef __DS1302_H_
- #define __DS1302_H_
- #include <intrins.h>
- sbit SCK = P1^7;
- sbit SDA = P2^3;
- sbit RST = P1^3; // DS1302復(fù)位
- unsigned char Read_Ds1302 ( unsigned char address );
- void Write_Ds1302_Byte(unsigned char temp);
- void Write_Ds1302( unsigned char address,unsigned char dat );
- void init_DS1302();
- void gettime();
- unsigned char Time[7];
- unsigned char init[]={15,30,3,6,2,2,18};//完成時間2018年2月6日周二凌晨2點30分15秒 BCD碼
- void Write_Ds1302_Byte(unsigned char temp)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- SCK=0;
- SDA=temp&0x01;
- temp>>=1;
- SCK=1;
- }
- }
- void Write_Ds1302( unsigned char address,unsigned char dat )
- {
- RST=0;
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address);
- Write_Ds1302_Byte(dat);
- RST=0;
- }
- unsigned char Read_Ds1302 ( unsigned char address )
- {
- unsigned char i,temp=0x00,dat1,dat2;
- RST=0;
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address);
- for (i=0;i<8;i++)
- {
- SCK=0;
- temp>>=1;
- if(SDA)
- temp|=0x80;
- SCK=1;
- }
- RST=0;
- _nop_();
- RST=0;
- SCK=0;
- _nop_();
- SCK=1;
- _nop_();
- SDA=0;
- _nop_();
- SDA=1;
- _nop_();
- dat1=temp/16;
- dat2=temp%16;
- temp=dat1*10+dat2;
- return (temp);
- }
- void init_DS1302()//初始化DS1302
- {
- unsigned char i,adr;//寫的地址是從0x80h開始加2的
- adr=0x80;
- Write_Ds1302(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- Write_Ds1302(adr,init[i]/10*16+init[i]%10);
- adr=adr+2;
- }
- Write_Ds1302(0x8e,0x80);
- }
- void gettime()//得到DS1302時間
- {
- unsigned char place=0x81,j;//讀的地址是從0x81h開始加2的
- Write_Ds1302(0x8e,0x00);
- for(j=0;j<7;j++)
- {
- Time[j]=Read_Ds1302(place);
- place=place+2;
-
- }
- Write_Ds1302(0x8e,0x80);//操作0x8e地址最高位,0表示操作,1表示關(guān)閉
- }
- #endif
復(fù)制代碼 |
|