4.实时时钟rtc
#include <reg52.h>
#include <intrins.h>
sbit SCK=p3^6;
sbit sda=p3^4;
sbit rst = p3^5; // ds1302复位
bit readrtc_flag;
unsigned char l_tmpdate[7]={0,0,12,15,5,3,8};//秒分时日月周年08-05-15 12:00:00
unsigned char l_tmpdisplay[8];
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//共阴数码管 0-9 '-' '熄灭‘表
code unsigned char table1[]={0,1,2,3,4,5,6,7};
//显示位码表
void write_ds1302_byte(unsigned char temp);
void write_ds1302( unsigned char address,unsigned char dat );
unsigned char read_ds1302 ( unsigned char address );
void read_rtc(void);//read rtc
void set_rtc(void); //set rtc
void inittimer0(void);//inital timer0
void main(void)
{
inittimer0();
set_rtc();
while(1){
if(readrtc_flag)
{
readrtc_flag=0;
read_rtc();
switch (l_tmpdate[0]/5) //设计每个5秒交替显示年月日 时分秒
{
case 0:
case 2:
case 4:
case 6:
case 8:
case 10:
l_tmpdisplay[0]=l_tmpdate[2]/16; //数据的转换,因我们采用数码管0~9的显示,将数据分开
l_tmpdisplay[1]=l_tmpdate[2]&0x0f;
l_tmpdisplay[2]=10; //加入"-"
l_tmpdisplay[3]=l_tmpdate[1]/16;
l_tmpdisplay[4]=l_tmpdate[1]&0x0f;
l_tmpdisplay[5]=10;
l_tmpdisplay[6]=l_tmpdate[0]/16;
l_tmpdisplay[7]=l_tmpdate[0]&0x0f;
break;
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
l_tmpdisplay[0]=l_tmpdate[6]/16;
l_tmpdisplay[1]=l_tmpdate[6]&0x0f;
l_tmpdisplay[2]=10;
l_tmpdisplay[3]=l_tmpdate[4]/16;
l_tmpdisplay[4]=l_tmpdate[4]&0x0f;
l_tmpdisplay[5]=10;
l_tmpdisplay[6]=l_tmpdate[3]/16;
l_tmpdisplay[7]=l_tmpdate[3]&0x0f;
break;
default:
break;
}
}
}
}