-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS1302.c
More file actions
130 lines (115 loc) · 2.97 KB
/
Copy pathDS1302.c
File metadata and controls
130 lines (115 loc) · 2.97 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <REGX52.H>
//宏定义
#define DS1302_second 0x80
#define DS1302_minute 0x82
#define DS1302_hour 0x84
#define DS1302_date 0x86
#define DS1302_month 0x88
#define DS1302_day 0x8a
#define DS1302_year 0x8c
#define DS1302_wp 0x8e
unsigned char DS1302_TIME[]={ //1302时间数组
22,12,31,17,59,55
};
sbit DS1302_SCLK=P3^6;
sbit DS1302_IO=P3^4;
sbit DS1302_CE=P3^5;
/**
* @brief DS1302初始化函数
* @param 无
* @retval 无
*/
void DS1302_init()
{
DS1302_CE=0;
DS1302_SCLK=0;
}
/**
* @brief DS1302写入函数
* @param command 命令+地址
* @param data 数据
* @retval 无
*/
void DS1302_writebyte(unsigned char command,unsigned char Data)
{
unsigned char i;
DS1302_CE=1;
for(i=0;i<8;i++)
{
DS1302_IO=command&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
for(i=0;i<8;i++)
{
DS1302_IO=Data&(0x01<<i);
DS1302_SCLK=1;
DS1302_SCLK=0;
}
DS1302_CE=0;
}
/**
* @brief DS1302读取函数
* @param command 命令+地址
* @retval 无
*/
unsigned char DS1302_readbyte(unsigned char command)
{
unsigned char i,Data=0x00;
command|=0x01;
DS1302_CE=1;
for (i=0; i<8; i++)
{
DS1302_IO=command&(0x01<<i);
DS1302_SCLK=0;
DS1302_SCLK=1;
}
for ( i = 0; i < 8; i++)
{
DS1302_SCLK=1;
DS1302_SCLK=0;
if (DS1302_IO){Data|=(0x01<<i);}
}
DS1302_CE=0;
DS1302_IO=0;
return Data;
}
/**
* @brief DS1302芯片从DS1302_TIME数组设置时间函数
* @retval 无
*/
void DS1302_SetTime()
{
DS1302_writebyte(DS1302_wp,0x00); //关闭写保护
DS1302_writebyte(DS1302_year,DS1302_TIME[0]/10*16+DS1302_TIME[0]%10);
DS1302_writebyte(DS1302_month,DS1302_TIME[1]/10*16+DS1302_TIME[1]%10);
DS1302_writebyte(DS1302_date,DS1302_TIME[2]/10*16+DS1302_TIME[2]%10);
DS1302_writebyte(DS1302_hour,DS1302_TIME[3]/10*16+DS1302_TIME[3]%10);
DS1302_writebyte(DS1302_minute,DS1302_TIME[4]/10*16+DS1302_TIME[4]%10);
DS1302_writebyte(DS1302_second,DS1302_TIME[5]/10*16+DS1302_TIME[5]%10);
DS1302_writebyte(DS1302_day,DS1302_TIME[6]/10*16+DS1302_TIME[6]%10);
DS1302_writebyte(DS1302_wp,0x80); //打开写保护
}
/**
* @brief DS1302读取时间函数,时间存放在数组DS1302_TIME中
* @param command 命令+地址
* @retval 无
*/
void DS1302_ReadTime()
{
unsigned char temp;
temp=DS1302_readbyte(DS1302_year);
DS1302_TIME[0]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_month);
DS1302_TIME[1]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_date);
DS1302_TIME[2]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_hour);
DS1302_TIME[3]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_minute);
DS1302_TIME[4]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_second);
DS1302_TIME[5]=temp/16*10+temp%16;
temp=DS1302_readbyte(DS1302_day);
DS1302_TIME[6]=temp/16*10+temp%16;
}