#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit WR1=P2^2;
sbit CS1=P2^1;
sbit CS2=P2^0;
sbit pcf8563_scl=P3^0;
sbit pcf8563_sda=P3^1;
void delay1(uchar ij)
{
uchar i,j;
for (i=0;i<ij;i++)
for (j=0;j<255;j++);
}
uchar code shuma[]=
{
0xC0,/*0*/
0xF9,/*1*/
0xA4,/*2*/
0xB0,/*3*/
0x99,/*4*/
0x92,/*5*/
0x82,/*6*/
0xF8,/*7*/
0x80,/*8*/
0x90,/*9*/
};
uchar code wei[]=
{
0XFE, //11111110
0XFD, //11111101
0XFB, //11111011
0XF7, //11110111
0XEF, //11101111
0XDF, //11011111
};
char xian[6];
/*
uchar num[10]=
{
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39
};
//数字表
*/
bit busy=0;
uchar sg;
uchar sd;
uchar fg;
uchar fd;
uchar mg;
uchar md;
uchar hou=0;
uchar min=0;
uchar sec=0;
uchar subadd;
uchar dat;
uchar number;
void start_pcf8563();
void send_pcf8563_byte();
void stop_pcf8563();
void receive_pcf8563_byte();
void spit_time();
void zi_shuma(void)
{
static uchar j;
j++;
if(j==6)j=0;
P0=0;
P0=wei[j];
CS2=0;
WR1=0;
WR1=1;
CS2=1;
P0=shuma[xian[j]];
CS1=0;
WR1=0;
WR1=1;
CS1=1;
delay1(2);
P0=0xff;
CS1=0;
WR1=0;
WR1=1;
CS1=1;
}
void Send_pcf8563_byte(uchar bb) //向PCF8563发送一个字节
{
uchar aa;
pcf8563_scl=0;
for(aa=0;aa<8;aa++)
{
if((bb&0x80)==0x80)
{
pcf8563_sda=1;
}
else
{
pcf8563_sda=0;
}
pcf8563_scl=1;
pcf8563_scl=0;
bb=bb<<1;
}
_nop_();
_nop_();
pcf8563_sda=1;
pcf8563_scl=1;
busy=0;
if(pcf8563_sda)
{
busy=1;
}
else
{
_nop_();
_nop_();
pcf8563_scl=0;
busy=0;
}
}
void write_pcf8563(uchar subadd,uchar dat)// 向PCF8563对应地址写数据
{
start_pcf8563();
Send_pcf8563_byte(0xa2);
if(!busy)
{
Send_pcf8563_byte(subadd);
if(!busy)
{
Send_pcf8563_byte(dat);
}
}
stop_pcf8563();
}
void read_pcf8563() //读当时的时,分,钞
{
start_pcf8563();
Send_pcf8563_byte(0xa2);
if(!busy)
{
Send_pcf8563_byte(0x02);
if(!busy)
{
start_pcf8563();
Send_pcf8563_byte(0xa3);
receive_pcf8563_byte();
sec=number&0x7f;
start_pcf8563();
Send_pcf8563_byte(0xa3);
receive_pcf8563_byte();
min=number&0x7f;
start_pcf8563();
Send_pcf8563_byte(0xa3);
receive_pcf8563_byte();
hou=number&0x3f;
}
}
stop_pcf8563();
}
void receive_pcf8563_byte() //从PCF8563接受一个字节
{
uchar cc;
pcf8563_sda=1;
number=0;
for(cc=0;cc<8;cc++)
{
number<<=1;
pcf8563_scl=0;
pcf8563_scl=1;
_nop_();
_nop_();
number= number|pcf8563_sda;
}
pcf8563_scl=0;
_nop_();
_nop_();
}
void start_pcf8563() //开启PCF8563IIC
{
pcf8563_sda=1;
pcf8563_scl=1;
pcf8563_sda=0; //SCL为低,SDA执行一个上跳
pcf8563_scl=0; //SCL为低,嵌住数据线
}
void stop_pcf8563() //关闭PCF8563IIC
{
pcf8563_sda=0;
pcf8563_scl=1;
pcf8563_sda=1; //SCL为高,SDA执行一个上跳
pcf8563_scl=0; //SCL为低,嵌住数据线
}
void spit_time()
{
sg=(int)hou/10;
sd=(int)hou%10;
fg=(int)min/10;
fd=(int)min%10;
mg=(int)sec/10;
md=(int)sec%10;
}
void zb(void)
{
xian[0]=sg;//时
xian[1]=sd;//时
xian[2]=fg;//分
xian[3]=fd;//分
xian[4]=mg;//秒
xian[5]=md;//秒
}
/************************************************************
*BCD 格式寄存器概况 *
*标明“-”的位无效 *
*地址 寄存器名称 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 *
*02h 秒 VL 00~59BCD 码格式数 *
*03h 分钟 - 00~59BCD 码格式数 *
*04h 小时 - - 00~59BCD 码格式数 *
*05h 日 - - 01~31BCD 码格式数 *
*06h 星期 - - - - - 0~6 *
*07h 月/世纪 C - - 01~12 BCD 码格式数 *
*08h 年 00~99 BCD 码格式数 *
*09h 分钟报警 AE 00~59 BCD 码格式数 *
*0Ah 小时报警 AE - 00~23 BCD 码格式数 *
*0BH 日报警 AE - 01~31 BCD 码格式数 *
*0CH 星期报警 AE - - - - 0~6 *
************************************************************/
void main(void)
{
uchar ll1;
/*
sec=40;
min=52;
hou=16;
write_pcf8563(0x02,sec); //写钞
write_pcf8563(0x03,min); //写分
write_pcf8563(0x04,hou); //写时
*/
while(1)
{
ll1++;
if(ll1==52)
{
read_pcf8563(); //读当前时间
spit_time(); //切割时间,为显示做准备
zb();
ll1=0;
}
zi_shuma(); //显示当前时间
}
}
- 1
- 2
前往页