本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit P_CLK_1302=P1^5;
sbit P_IO_1302=P1^6;
sbit P_RST_1302=P1^7;
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;
sbit LED5=P2^4;
sbit LED6=P2^5;
uchar vYear;
uchar vMonth;
uchar vDate;
uchar vHour;
uchar vMin;
uchar vSec;
uchar nop;
uchar sec,min,hour,date,month,year;
#define AM(X) X
#define PM(X) (X+12) /* 转成24小时制*/
void settime(uchar vYear,vMonth,vDate,vHour,vMin,vSec);
void led();
// 函数介绍:写入字节到1302
void Wrbyte_1302(uchar vB_data)
{
unsigned char i;
for(i=0;i<8;i++)
{
P_CLK_1302=0;
P_IO_1302=(bit)(vB_data&0x01);
P_CLK_1302=1;
vB_data=vB_data>>1;
}
}
//从1302读出数据
uchar Read_1302(uchar vDmd)
{
unsigned char i,vTmp;
P_RST_1302=0;
nop;
P_CLK_1302=0;
P_RST_1302=1;
Wrbyte_1302(vDmd);
P_IO_1302=1;
for(i=0;i<8;i++)
{
P_CLK_1302=0;
vTmp=vTmp>>1;
if(P_IO_1302) vTmp |= 0x80;
nop;
P_CLK_1302=1;
}
P_CLK_1302=0;
P_RST_1302=0;
return vTmp;
}
//写入数据到1302
void Write_1302(unsigned char vCmd,unsigned char vT_data)
{
P_RST_1302=0;
nop;
P_CLK_1302=0; /*CLK=0*/
P_RST_1302=1; /*RST=1*/
Wrbyte_1302(0x8e); /*写保护寄存器*/
Wrbyte_1302(0x00); /*解除写保护*/
P_RST_1302=0; /*rst=0,停止*/
P_CLK_1302=0;
P_RST_1302=1; /*rst=1,开始*/
Wrbyte_1302(vCmd);/*写XX命令 ;写入命令*/
Wrbyte_1302(vT_data);/*SEND WIRTE VALUE*/
P_CLK_1302=0;
P_RST_1302=0;
P_RST_1302=1;
Wrbyte_1302(0x8e); /*写完后将DS_1302保护起来*/
Wrbyte_1302(0x80);
P_RST_1302=0;
P_CLK_1302=0;
}
void delay(unsigned char m) /*延时程序500us*/
{
unsigned char i,j;
for(i=m;i!=0;i--);
for(j=250;j!=0;j--);
}
void led() /*数码管显示及数据处理程序*/
{
uchar i,j,k;
days=1234;
i=vSec>>4;/*BCD码转十进制*/
i*=10;
vSec=vSec&0x0f;
vSec+=i;
j=vMin>>4;/*BCD码转十进制*/
j*=10;
vMin=vMin&0x0f;
vMin+=j;
k=vHour>>4;
k*=10;
vHour=vHour&0x0f;
vHour+=k;
LED1=0;LED2=1;LED3=1; LED4=1; LED5=1;LED6=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vSec%10]; /*秒个位*/
delay(1);
LED2=0; LED1=1;LED3=1;LED4=1;LED5=1;LED6=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vSec/10]; /*秒十位 */
delay(1);
LED3=0;LED1=1;LED2=1;LED4=1;LED5=1;LED6=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vMin%10]; /*分个位 */
delay(1);
LED4=0;LED1=1; LED2=1;LED3=1; LED5=1;LED6=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vMin/10]; /*分十位 */
delay(1);
LED5=0;LED1=1;LED2=1;LED3=1;LED4=1;LED6=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vHour%10]; /*时个位 */
delay(1);
LED6=0;LED1=1; LED2=1;LED3=1; LED4=1;LED5=1;LED7=1;
LED8=1;LED9=1;LED10=1;
P0=dispcode[vHour/10]; /*时十位 */
delay(1);
}
void settime(uchar vYear,vMonth,vDate,vHour,vMin,vSec)
{
Write_1302(0x8e,0x00); /*写允许*/
Write_1302(0x8c,vYear); /*年 */
Write_1302(0x88,vMonth); /*月*/
Write_1302(0x86,vDate); /*日*/
Write_1302(0x84,vHour); /*时*/
Write_1302(0x82,vMin); /*分*/
Write_1302(0x80,vSec); /*秒*/
Write_1302(0x8e,0x80); /*写保护*/
}
void readtime(void)
{ uchar vTmp;
temp = Read_1302(0x8d);
if(temp <= 0x99){vYear = temp;}
else vYear=0;
temp = Read_1302(0x89);
if(temp <= 0x12){vMonth = temp;}
else vMonth=1;
temp = Read_1302(0x87);
if(temp <= 0x31){vDate = temp;}
else vDate=1;
temp = Read_1302(0x85);
if(temp <= 0x23){vHour = temp; }
else vHour=0;
temp = Read_1302(0x83);
if(temp <= 0x59){vMin = temp;}
else vMin=0;
temp = Read_1302(0x81);
if(temp <= 0x59){vSec = temp;}
else vSec=0;
}
void main(void)
{
settime(0x08,0x06,0x07,0x05,0x58,0x00);
up_flag=0;
down_flag=0;
done=0;
set=1;
up=1;
down=1;
out=1;
while(1)
{ readtime();
led();
}
}
哪位高手帮我写几个按键程序啊,就是用来设置时间的。 |