📄 ds1302.c
字号:
; ----------D1302充电程序---------------------------
CD: LCALL ST02 ; D1302充电程序
MOV A,#90H
LCALL WR02
MOV A,#0A5H
LCALL WR02
RET
;---------------D1302写秒程序---------------------------------------
WRCLOCK: mov r0,#blk1
LCALL ST02 ; 起动D1302
MOV A,#86H ;写入日期
lcall wr02
mov a,@r0
lcall wr02
lcall stop
lcall st02
inc r0
MOV A,#84H ;写入小时
lcall wr02
mov a,@r0
lcall wr02
lcall stop
lcall st02
inc r0
MOV A,#82H ;写入分
lcall wr02
mov a,@r0
lcall wr02
lcall stop
lcall st02
inc r0
MOV A,#80H ;写入秒
lcall wr02
mov a,@r0
lcall wr02
RET
;----------------D1302读秒,分,时,年,月程序--------------
CSCLOCK1: LCALL ST02 ;起动D1302
MOV A,#0BFH ;BFH为读D1302时间
LCALL WR02 ;写D1302
MOV R0,#NUM4 ;秒,分,时地址
LP: LCALL RD02 ;调读D1302
MOV @R0,A
DEC R0
CJNE R0, #NUM0,LP
LCALL STOP ;停止D1302
RET
; ----------起动D1302子程序-------------------
ST02: CLR RST
CLR CLK
SETB RST
RET
; -----------------写D1302子程序------------------
WR02: MOV R3,#08H
LP02: SETB RST
RRC A
MOV IO,C
CLR CLK
NOP
SETB CLK
DJNZ R3,LP02
RET
; --------- 读D1302子程序-----------------
RD02: SETB IO ; 读时钟
SETB RST
MOV R6,#08H
SETB RST
LP021: CLR CLK
MOV C,IO
RRC A
SETB CLK
DJNZ R6,LP021
RET
STOP: CLR RST ;停止D1302
NOP
RET
#pragma cd db oe sb
#include <reg51.h>
#include <intrins.h>
#define byte unsigned char
#define word unsigned int
sbit clk=0x95;
sbit sio=0x96;
sbit rst=0x97;
byte code time1[]={0x37,0x34,0x10,0x20,9,5,2,0};/*秒、分、时、日、月、星期、年、控制*/
byte code number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98}; /*0-9*/
byte disdata[4];
byte data time2[8];
/*=========================Ds1302 driver program begin=======================*/
byte si_char() //read a byte from Ds1302
{ //out status: clk=0
byte i,rdata=0;
sio=1;
for(i=0;i<8;i++)
{
clk=1;
rdata>>=1;
clk=0; //read data after falling egde
if(sio)rdata|=0x80;
}
return rdata;
}
void so_char(byte wdata)//write a byte to Ds1302
{ //out status: clk=1
byte i;
for(i=0;i<8;i++)
{
clk=0;
sio=(bit)(wdata&1);
wdata>>=1;
clk=1; //write data at rising edge
}
}
void burstwrite(byte *wd)
{
byte i;
rst=0;
clk=0;
rst=1;
so_char(0xbe);
for(i=0;i<8;i++)so_char(wd[i]);
rst=0;
}
void burstread(byte *rd)
{
byte i;
rst=0;
clk=0;
rst=1;
so_char(0xbf);
for(i=0;i<8;i++)*(rd+i)=si_char();
rst=0;
}
void randomwrite(byte addr,byte wdata)
{
rst=0;
clk=0;
addr=(addr<<1)|0x80;
rst=1;
so_char(addr);
so_char(wdata);
rst=0;
}
byte randomread(byte addr)
{
byte rdata;
rst=0;
clk=0;
addr=(addr*2)|0x81;
rst=1;
so_char(addr);
rdata=si_char();
rst=0;
return rdata;
}
/*=========================Ds1302 driver program end=========================*/
void delay(word time)
{
for(;time>0;time--);
}
void display()
{
byte selbit=0xef,i;
for(i=0;i<4;i++)
{
P0=selbit;
P2=disdata[i];
delay(250);
selbit<<=1;
P2=0xff;
}
}
void main()
{
byte temp;
randomwrite(7,0x80); //disable write protect
burstwrite(time1);
burstread(time2);
_nop_();
burstread(time2);
// randomwrite(2,0x23);
temp=randomread(2);
_nop_();
temp=randomread(2);
while(1)
{
temp=randomread(2);
disdata[0]=number[temp>>4];
disdata[1]=number[temp&0xf];
temp=randomread(1);
disdata[2]=number[temp>>4];
disdata[3]=number[temp&0xf];
display();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -