-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDS1302.cpp
179 lines (160 loc) · 5.53 KB
/
DS1302.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/******************************************************************************
----------------1.开发环境:Arduino IDE-----------------------------------------
----------------2.测试使用开发板型号:Arduino UNO R3-------
----------------3.单片机使用晶振:16M------------------------------------------
----------------4.作者:神秘藏宝室---------------------------------------------
******************************************************************************/
#include "DS1302.h"
SYSTEMTIME DS1302Buffer;
bool Flag_Time_Refresh = 1;
//****************************************************
//实时时钟写入一字节(内部函数)
//****************************************************
void DS1302_InputByte(unsigned char dat)
{
unsigned char i;
pinMode(DS1302_IO,OUTPUT);
for(i=0; i<8; i++)
{
if((dat & 0x01) == 1)
{
DS1302_IO_1;
}
else
{
DS1302_IO_0;
}
DS1302_CLK_0; //上升沿写入数据
delayMicroseconds(2);
DS1302_CLK_1;
delayMicroseconds(2);
dat >>= 1;
}
}
//****************************************************
//实时时钟读取一字节(内部函数)
//****************************************************
unsigned char DS1302_OutputByte(void)
{
unsigned char i;
unsigned char dat;
pinMode(DS1302_IO,INPUT);
for(i=0; i<8; i++)
{
DS1302_CLK_1; //下降沿读出数据
delayMicroseconds(2);
DS1302_CLK_0;
delayMicroseconds(2);
dat >>= 1;
if( digitalRead(DS1302_IO) == HIGH )
dat |= 0x80; //最高位置一
else
dat &= 0x7F; //最高位清零
}
return(dat);
}
//****************************************************
//ucAddr: DS1302地址, ucData: 要写的数据
//****************************************************
void DS1302_Write(unsigned char ucAddr, unsigned char ucDa)
{
DS1302_RST_0;
delayMicroseconds(2);
DS1302_CLK_0;
delayMicroseconds(2);
DS1302_RST_1;
delayMicroseconds(2);
DS1302_InputByte(ucAddr); // 地址,命令
DS1302_InputByte(ucDa); // 写1Byte数据
DS1302_CLK_1;
delayMicroseconds(2);
DS1302_RST_0;
delayMicroseconds(2);
}
//****************************************************
//读取DS1302某地址的数据
//****************************************************
unsigned char DS1302_Read(unsigned char ucAddr)
{
unsigned char ucData;
DS1302_RST_0;
delayMicroseconds(2);
DS1302_CLK_0;
delayMicroseconds(2);
DS1302_RST_1;
delayMicroseconds(2);
DS1302_InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302_OutputByte(); // 读1Byte数据
DS1302_CLK_1;
delayMicroseconds(2);
DS1302_RST_0;
delayMicroseconds(2);
return(ucData);
}
//****************************************************
//是否写保护
//****************************************************
void DS1302_SetProtect(unsigned char flag)
{
if(flag)
DS1302_Write(0x8E,0x10); //保护
else
DS1302_Write(0x8E,0x00); //不保护
}
//****************************************************
// 设置时间函数
//****************************************************
void DS1302_SetTime(unsigned char Address, unsigned char Value)
{
DS1302_SetProtect(0);
DS1302_Write(Address, ((Value/10)<<4 | (Value%10)));
}
//****************************************************
//获取实时时间
//****************************************************
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = DS1302_Read(DS1302_SECOND);
ReadValue = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //BCD码转换十进制
if(ReadValue != DS1302Buffer.Second) //跟上一次状态不同,置位刷新标志位
Flag_Time_Refresh = 1;
Time->Second = ReadValue;
ReadValue = DS1302_Read(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = DS1302_Read(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = DS1302_Read(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = DS1302_Read(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = DS1302_Read(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = DS1302_Read(DS1302_YEAR);
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
}
//****************************************************
//初始化
//****************************************************
void DS1302_Init(void)
{
unsigned char Second;
pinMode(DS1302_RST,OUTPUT);
pinMode(DS1302_IO,OUTPUT);
pinMode(DS1302_CLK,OUTPUT);
Second = DS1302_Read(DS1302_SECOND);
if(Second&0x80)
DS1302_SetTime(DS1302_SECOND,Second & 0x7f); //开启振荡
}
//****************************************************
//DS1302振荡器停止
//****************************************************
void DS1302_ON_OFF(bool FLAG_ON_OFF)
{
unsigned char Second;
Second = DS1302_Read(DS1302_SECOND);
if(FLAG_ON_OFF == 0)
DS1302_Write(DS1302_SECOND,Second | 0x80); //关闭振荡
else
DS1302_Write(DS1302_SECOND,Second & 0x7f); //开启振荡
}