File size: 1,785 Bytes
9f43458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*	# 	DS1302代码片段说明

	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。

	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题

		中对单片机时钟频率的要求,进行代码调试和修改。

*/								

//

#include "ds1302.h"

void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}



void write_ds13(unsigned char *ds_time)
{
	unsigned char temp;
	Write_Ds1302_Byte(0x8e,0x00);
	temp=(ds_time[0]/10*16)|(ds_time[0]%10);
	Write_Ds1302_Byte(0x80,temp);
	temp=(ds_time[1]/10*16)|(ds_time[1]%10);
	Write_Ds1302_Byte(0x82,temp);	
	temp=(ds_time[2]/10*16)|(ds_time[2]%10);
	Write_Ds1302_Byte(0x84,temp);
	Write_Ds1302_Byte(0x8e,0x80);

}

void read_ds13(unsigned char *ds_time)
{
	unsigned char temp;
	temp=Read_Ds1302_Byte(0x81);
	ds_time[0]=((temp>>4)*10)+(temp&0x0f);
	temp=Read_Ds1302_Byte(0x83);
	ds_time[1]=((temp>>4)*10)+(temp&0x0f);	
	temp=Read_Ds1302_Byte(0x85);
	ds_time[2]=((temp>>4)*10)+(temp&0x0f);	

}