#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sfr P4 = 0xc0; //I/O端口4 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
//位描述 P4.7 P4.6 P4.5 P4.4 P4.3 P4.2 P4.1 P4.0
//初始值=1111,1111 1 1 1 1 1 1 1 1
sbit P47 = P4^7; //I/O口P4.7
sbit P46 = P4^6; //I/O口P4.6
sbit P45 = P4^5; //I/O口P4.5
sbit P44 = P4^4; //I/O口P4.4
sbit P43 = P4^3; //I/O口P4.3
sbit P42 = P4^2; //I/O口P4.2
sbit P41 = P4^1; //I/O口P4.1
sbit P40 = P4^0; //I/O口P4.0
sbit RST = P4^7; //复位脚,可通过编程器设置
sbit EX_LVD = P4^6; //外部低压检测引脚
sbit P4SPISCLK = P4^3; //SPI总线的时钟脚
sbit P4SPIMISO = P4^2; //SPI总线的主出从入脚
sbit P4SPIMOSI = P4^1; //SPI总线的主入从出脚
sbit P4SPISS = P4^0; //SPI总线的从机选择脚
sbit P4CEX1 = P4^3; //比较/捕获/PWM1外部引脚
sbit P4CEX0 = P4^2; //比较/捕获/PWM0外部引脚
sbit P4ECI = P4^1; //比较/捕获/PWM模块的外部时钟输入脚(最大值为Fosc/2)
sbit P4TXD2 = P4^3; //串口2数据输出脚
sbit P4RXD2 = P4^2; //串口2数据输入脚
sfr P4SW = 0xbb; //P4口的功能开关 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
//位描述 - LVD_P4.6 ALE_P4.5 NA_P4.4 - - - -
//初始值=x000,xxxx x 0 0 0 x x x x
#define P46EN 0x40 //传统的EA脚 0:为LVD的信号引入脚 1:为P4.6
#define P45EN 0x20 //传统的ALE脚 0:仍为ALE 1:为P4.5
#define P44EN 0x10 //传统的PSEN脚 0:无任何功能 1:为P4.4
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
#define Ds_Bus P0
#define smgp1 P1
sbit DS_CS = P2^6;
sbit DS_AS = P4^5;
sbit DS_RW = P3^6;
sbit DS_DS = P3^7;
uchar count,shi,fen,miao,s1num,week=0,i,day=1,yue=1,year1=0,year2,year3,year4;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};
void delay(uchar i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
//往DS12CR887写数据函数,add=地址,date=数据。
void ds_write(uchar add,uchar date)
{
DS_CS=0; DS_DS=1; DS_RW=1; DS_AS=1;
Ds_Bus=add;
DS_AS=0; DS_RW=0;
Ds_Bus=date;
DS_RW=1; DS_AS=1; DS_CS=1;
}
//读取DS12CR887的数据函数
uchar ds_read(uchar add)
{
uchar z;
DS_AS=1; DS_DS=1;DS_RW=1;DS_CS=0;
Ds_Bus=add;
DS_AS=0; DS_DS=0;
Ds_Bus=0xff;
z=Ds_Bus;
DS_DS=1; DS_AS=1; DS_CS=1;
return z;
}
//DS12CR887初始化函数
void ds_int()
{
DS_AS=0; DS_DS=0; DS_RW=0;
ds_write(0x0a,0x20);//DS12CR887寄存器A功能设置,开启时钟振荡器
ds_write(0x0b,0x06);//寄存器B功能设置,不开启闹钟中断使能,数据模式为二进制,24小时模式。
}
void display()
{
// P1=(table[minute%10]& 0x7f); //加0X7F,是一个小数点
miao=ds_read(0); //读取DS12CR887的秒
//write1632GDRAM(7,2,num1632[sec/10]);
// write1632GDRAM(8,2,num1632[sec%10]);
// miao=ds_read(0);
smgp1=(table[miao/10]);
P2_0 = 0;
delay(5);
P2_0 = 1;
smgp1=(table[miao%10]& 0x7f);
P2_1 = 0;
delay(5);
P2_1 = 1;
fen=ds_read(2);
smgp1=table[fen/10];
P2_2 = 0;
delay(5);
P2_2 = 1;
smgp1=table[fen%10];
P2_3 = 0;
delay(5);
P2_3 = 1;
}
/*演示主程序*/
void main(void)
{
unsigned int a;
ds_int(); //时钟芯片初始化
while(1)
{
for(a=100;a>0;a--)
{
display();
}
}
}
|