whycyhl
2022-05-05 683c912f9ac1935a7b109fb402bb276dba450f6b
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/************************** Copyright (c) **********************************
**                 FUZHOU FUGUANG ELECTRONICS Co.,LTD.
**                        ¸£Öݸ£¹âµç×ÓÓÐÏÞ¹«Ë¾
**                       http://www.fuguang.com
**
**-------------- File Info -------------------------------------------------
** File name:            DS1302_Driver.c
** Last modified Date:  2008-11-16
** Last Version:        1.0
** Descriptions:        DS1302 Drivers lib
**
**--------------------------------------------------------------------------
** Created by:            mxpopstar
** Created date:        2008-11-16
** Version:                1.0
** Descriptions:        The original version
**
**--------------------------------------------------------------------------
** Modified by:            mxpopstar
** Modified date:        2008-11-20
** Version:
** Descriptions:        DS1302 Drivers lib
**
***************************************************************************/
//
 
#include "DS1302_Driver.h"
 
void InitIO_DS1302(void)
{
    DDRB |= 0x70;
    RST_DS1302_0;//ÇåÁãRST
    SCLK_DS1302_0;
    _delay_ms(1);
}
//
 
void Config_DS1302(void)
{
    unsigned char temp;
    //----------- ³õʼ»¯DS1302 ------------------ 
    Ds1302_Write_Byte(0x8e,0x00);//¼Ä´æÆ÷ÔÊÐíдÈë
      
    temp=Ds1302_Read_Byte(0x81);        //read second
    temp=((temp/16)*10+(temp%16));
    if(temp>59)
    {
        temp = 0;
        Ds1302_Write_Byte(0x80,temp);    //write second
    }
    
    temp=Ds1302_Read_Byte(0x83);        //read minute
    temp =((temp/16)*10+(temp%16));
    if(temp>59)
    {
        temp = 0;
        Ds1302_Write_Byte(0x82,temp);    //write minute
    }
     
    temp =Ds1302_Read_Byte(0x85);        //read hour
    temp=((temp/16)*10+(temp%16));
    if(temp>23)
    {
        temp = 0;
        Ds1302_Write_Byte(0x84,temp);    //write hour
    }
 
    temp=Ds1302_Read_Byte(0x87);        //read day
    temp=((temp/16)*10+(temp%16));        
    if((temp<1)||(temp>31))
    {
        temp = 1;
        Ds1302_Write_Byte(0x86,temp);    //write day
    }
 
    temp=Ds1302_Read_Byte(0x89);        //read month
    temp=((temp/16)*10+(temp%16));
    if((temp<1)||(temp>12))
    {
        temp = 1;
        Ds1302_Write_Byte(0x88,temp);    //write month
    }
 
    temp=Ds1302_Read_Byte(0x8b);        //read week
    temp=((temp/16)*10+(temp%16));
    if((temp<1)||(temp>7))
    {
        temp = 1;
        Ds1302_Write_Byte(0x8a,temp);    //write week
    } 
     
    temp=Ds1302_Read_Byte(0x8d);         //read year
    temp=((temp/16)*10+(temp%16));
    if((temp>99)||(temp<8))
    {
        temp = 9;
        Ds1302_Write_Byte(0x8c,temp);   //write year
    }    
}  
//-------------------------------------------
 
void SetDateTime(const Date_Time *datetime)
{
    unsigned char temp = 0; 
       
    temp = datetime->second/10*16 + datetime->second%10;    
    Ds1302_Write_Byte(0x80,temp);    //write second
    
    temp = datetime->minute/10*16 + datetime->minute%10;
    Ds1302_Write_Byte(0x82,temp);    //write minute
     
    temp = datetime->hour/10*16 + datetime->hour%10;
    Ds1302_Write_Byte(0x84,temp);    //write hour     
    
    temp = datetime->day/10*16 + datetime->day%10;
    Ds1302_Write_Byte(0x86,temp);    //write day
                                                
    temp = datetime->month/10*16 + datetime->month%10;
    Ds1302_Write_Byte(0x88,temp);    //write month
          
    temp = datetime->year/10*16 + datetime->year%10;
    Ds1302_Write_Byte(0x8c,temp);   //write year
}
//
 
void GetDateTime(Date_Time *datetime)
    unsigned char temp = 0; 
       
    temp=Ds1302_Read_Byte(0x81);
    datetime->second = ((temp/16)*10+(temp%16));
     
    temp=Ds1302_Read_Byte(0x83);
    datetime->minute = ((temp/16)*10+(temp%16));
     
    temp =Ds1302_Read_Byte(0x85);
    datetime->hour = ((temp/16)*10+(temp%16));
     
    temp=Ds1302_Read_Byte(0x87);
    datetime->day = ((temp/16)*10+(temp%16));
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    temp=Ds1302_Read_Byte(0x89);
    datetime->month = ((temp/16)*10+(temp%16));
          
    temp=Ds1302_Read_Byte(0x8d);
    datetime->year = ((temp/16)*10+(temp%16)); 
}
//-------------------------------------------
 
void Ds1302_Write_Byte(const unsigned char command, const unsigned char data)
{
     unsigned char n;
     
     IO_Direction_OUT;    //ÉèÖÃIOÒý½ÅΪÊä³ö
     SCLK_DS1302_0;        //SCLÇåÁã
     RST_DS1302_1;        //ÖÃλRST
     _delay_us(2);
     
     for(n=0; n<8; n++)
     {
          if((command>>n) & 0x01)
            IO_DS1302_1;//SDAΪ"1"
          else 
            IO_DS1302_0;//SDAΪ"0"
          SCLK_DS1302_1;//SCLÖÃλ 
          SCLK_DS1302_0;//SCLÇåÁã
     }
 
     for(n=0; n<8; n++)
     {
          if((data>>n) & 0x01)
            IO_DS1302_1;//SDAΪ"1"
          else 
            IO_DS1302_0;//SDAΪ"0"
          SCLK_DS1302_1;//SCLÖÃλ 
          SCLK_DS1302_0;//SCLÇåÁã
     }
     
     RST_DS1302_0;//ÇåÁãRST
     SCLK_DS1302_0; 
}
//
 
 
 
unsigned char Ds1302_Read_Byte(const unsigned char command)
{
     unsigned char n=0;
     unsigned char data=0;
     
     IO_Direction_OUT;    //ÉèÖÃIOÒý½ÅΪÊä³ö
     SCLK_DS1302_0;        //SCLÇåÁã
     RST_DS1302_1;        //ÖÃλRST 
     _delay_us(2);
     
     for(n=0; n<=7; n++)
     {
          if((command>>n) & 0x01)
            IO_DS1302_1;//SDAΪ"1"
          else 
            IO_DS1302_0;//SDAΪ"0"
          SCLK_DS1302_1;//SCLÖÃλ 
          SCLK_DS1302_0;//SCLÇåÁã
     }
     
     IO_Direction_IN;          //ÉèÖÃIOÒý½ÅΪÊäÈë
     _delay_us(2); 
     
     for(n=0; n<=7; n++)
     {
          SCLK_DS1302_1;    //SCLÖÃλ 
          if(Data_DS1302_1)    //¶ÁÈ¡DS1302Êä³öµÄÊý¾Ý
                data += 0x80;
          if(n<7)
                data >>= 1;
          SCLK_DS1302_0;    //SCLÇåÁã
     }
     
     SCLK_DS1302_0;            //SCLÇåÁã
     RST_DS1302_0;            //ÇåÁãRST 
     
     IO_Direction_OUT;        //»Ö¸´IOÒý½ÅΪÊä³ö
     return data;
}
//