当前位置:首页 > 单片机 > 单片机
[导读]参考DS1302数据手册,尽管数据手册是英文,但是很有帮助,再结合网上众多的代码写出了下面这个例子,除了涓流充电(trickle charger)功能没使用外,内部的RAM和寄存器功能都使用到了,包括多字节方式(burst mode)

参考DS1302数据手册,尽管数据手册是英文,但是很有帮助,再结合网上众多的代码写出了下面这个例子,除了涓流充电(trickle charger)功能没使用外,内部的RAM和寄存器功能都使用到了,包括多字节方式(burst mode)。自认为我写的比网上大多数程序清晰,当然已经硬件测试过了。


1 #include

2

3 /*DS1302部分*/

4

5 //全部为写入地址,读取地址需要+1

6 #define YEAR 0x8c

7 #define MON 0x88

8 #define DATE 0x86

9 #define DAY 0x8a

10 #define HOUR 0x84

11 #define MIN 0x82

12 #define SEC 0x80

13 #define WP 0x8e

14 #define RAM(n) (0xc0+2*(n))

15 #define CLBT 0xbe

16 #define RAMBT 0xfe

17

18 //3个通信引脚

19 sbit SCLK=P2^1;

20 sbit IO=P2^0;

21 sbit RST=P2^4; //有的使能端叫CE

22

23 unsigned char readbyte(void);

24 void writebyte(unsigned char dat);

25 void write1302(unsigned char addr,unsigned char dat);

26 unsigned char read1302(unsigned char addr);

27

28

29 /*数码管部分,6位共阴接在P0*/

30 sbit we=P2^7;

31 sbit du=P2^6;

32 void show(void);

33 void delayms(unsigned time);

34

35 unsigned char code table[] = {

36 0x3f , 0x06 , 0x5b , 0x4f,

37 0x66 , 0x6d , 0x7d , 0x07,

38 0x7f , 0x6f , 0x77 , 0x7c,

39 0x39 , 0x5e , 0x79 , 0x71,

40 0x00 };

41

42 unsigned char num[6]={0}; //用作数码管显示的数组

43

44 /*键盘部分,4个独立键盘接在P3.4-P3.7*/

45 void keyscan(void);

46 bit showdate=0;

47 bit showday=0;

48

49 void main(void)

50 {

51 unsigned char temp;

52

53 write1302(WP,0x00); //允许写入,关闭写保护

54

55 //clock burst 方式赋初值

56 RST=0;

57 SCLK=0;

58 RST=1;

59 writebyte(CLBT);

60

61 writebyte(0x30);//秒分时

62 writebyte(0x59);

63 writebyte(0x23);

64

65 writebyte(0x28);//日月

66 writebyte(0x02);

67

68 writebyte(0x02);//星期

69

70 writebyte(0x32);//年

71

72 writebyte(0x00);

73 writebyte(0x00);

74 RST=0;

75

76 // 另一种赋初值方法

77 // write1302(HOUR,0x23);

78 // write1302(MIN,0x59);

79 // write1302(SEC,0x30);

80

81 while(1)

82 {

83 keyscan();

84 if(showdate)

85 {

86 temp=read1302(YEAR+1);

87 num[5]=temp>>4;

88 num[4]=temp&0x0f;

89 temp=read1302(MON+1);

90 num[3]=temp>>4;

91 num[2]=temp&0x0f;

92 temp=showday?read1302(DAY+1):read1302(DATE+1);

93 num[1]=temp>>4;

94 num[0]=temp&0x0f;

95 }

96 else

97 {

98 temp=read1302(HOUR+1);

99 num[5]=temp>>4;

100 num[4]=temp&0x0f;

101 temp=read1302(MIN+1);

102 num[3]=temp>>4;

103 num[2]=temp&0x0f;

104 temp=read1302(SEC+1);

105 num[1]=temp>>4;

106 num[0]=temp&0x0f;

107 }

108 show();

109 }

110

111 }

112

113

114 unsigned char readbyte(void)

115 {

116 unsigned char i,ret;

117 for(i=0;i<8;i++)

118 {

119 ret>>=1;

120 SCLK=1;

121 SCLK=0;

122 ret|=IO?0x80:0x00;

123 }

124 return ret;

125 }

126

127 void writebyte(unsigned char dat)

128 {

129 unsigned char i;

130 for(i=0;i<8;i++)

131 {

132 IO=dat&1;

133 SCLK=0;

134 SCLK=1;

135 dat>>=1;

136 }

137 }

138

139 void write1302(unsigned char addr,unsigned char dat)

140 {

141 RST=0;

142 SCLK=0;

143 RST=1;

144 writebyte(addr);

145 writebyte(dat);

146 RST=0;

147 }

148

149 unsigned char read1302(unsigned char addr)

150 {

151 unsigned char ret;

152 RST=0;

153 SCLK=0;

154 RST=1;

155 writebyte(addr);

156 ret=readbyte();

157 RST=0;

158 return ret;

159 }

160

161 void show(void)

162 {

163 unsigned char i;

164

165 for(i=0;i<6;i++)

166 {

167 P0=0xff;

168 we=1;

169 we=0;

170

171 P0=table[num[i]];

172 du=1;

173 du=0;

174

175 P0=~(0x20>>i);

176 we=1;

177 we=0;

178

179 delayms(1);

180 }

181 }

182

183 void delayms(unsigned time)

184 {

185 unsigned i,j;

186

187 for(i=time;i>0;i--)

188 for(j=110;j>0;j--)

189 ;

190 }

191

192 void keyscan(void)

193 {

194 unsigned char temp;

195 unsigned char reg[9];

196

197 if((P3|0x0f)!=0xff)

198 {

199 delayms(10);

200 if((temp=P3|0x0f)!=0xff)

201 {

202 while((P3|0x0f)!=0xff);

203 switch(temp)

204 {

205 case 0xef: //显示时间还是日期

206 showdate=~showdate;

207 break;

208 case 0xdf: //日期最后两位显示日还是星期

209 showday=~showday;

210 break;

211 case 0xbf: //将时钟寄存器保存到RAM

212 RST=0;

213 SCLK=0;

214 RST=1;

215 writebyte(CLBT+1);

216 for(temp=0;temp<9;temp++)

217 reg[temp]=readbyte();

218 RST=0;

219

220 //从20号RAM开始存入,起始RAM号随意,只要不超过范围即可

221 for(temp=0;temp<9;temp++)

222 write1302(RAM(20+temp),reg[temp]);

223

224 /* 也可以用 RAM burst 写入

225 RST=0;

226 SCLK=0;

227 RST=1;

228 writebyte(RAMBT);

229 for(temp=0;temp<9;temp++)

230 writebyte(reg[temp]);

231 RST=0;

232 */

233

234 break;

235 case 0x7f: //从RAM恢复到时钟寄存器

236 /* 对应的 RAM burst 读取

237 RST=0;

238 SCLK=0;

239 RST=1;

240

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

51单片机学习笔记———13.1DS1302实时时钟原理部分

关键字: ds1302 时钟

由于昨天照着手册写了一下DS1302的驱动程序,发现耗时挺多的,并且在考场上不可能一步步去自己写驱动,所以今天看了一下蓝桥杯提供的DS1302官方驱动程序,发现直接引用还是不行的,程序当中有些小问题需要去修改。下面就对那...

关键字: ds1302 时钟

DS1302时钟模块通信原理(SPI总线)

关键字: ds1302 时钟

现在流行的串行时钟电路很多,如DS1302、 DS1307、PCF8485等。这些电路的接口简单、价格低廉、使用方便,被广泛地采用

关键字: ds1307 ds1302

现在流行的串行时钟电路很多,如DS1302、 DS1307、PCF8485等。这些电路的接口简单、价格低廉、使用方便,被广泛地采用。它可以对年、月、日、周、时、分、秒进行计时,且具有闰年补偿等多种功能。

关键字: ds1302 时钟芯片 电路

DS1302 我们前边也有提起过,是三根线,分别是 CE、I/O 和 SCLK,其中 CE 是使能线,SCLK 是时钟线,I/O 是数据线。前边我们介绍过了 SPI 通信,同学们发现没发现,这个 DS1302 的通信线定...

关键字: ds1302 通信时序

//引入文件***********************************************************#include "delay.h" #include "...

关键字: ds1302 pic16f84a 读取时钟芯片

1、DS1302时钟芯片的讲解待完成!!!2、自己写的DS1302芯片的配置文件//DS1302_SPIdriver.h//DS1302_SPIdriver.h#ifndef__SPIDRIVER_H__#define_...

关键字: ds1302 单片机 时钟芯片

发现网上一些程序在16M晶振下不能工作,自己写了个,按照2V的时序写的,但只在5V和3.3V下测试过.以下是两个主要的文件DS1302.h:/************ AVR DS1302程序 *************...

关键字: AVR ds1302 程序源代码

proteus仿真之DS1302+8位数码管显示试验。仿真效果如下图:源程序如下:/*51单片机:DS1302+数码管 Proteus 仿真程序。功能:数码管时钟显示。仿真结果:(1)8位数码管显示设定的时间与日期。(2...

关键字: ds1302 proteus仿真 数码管显示
关闭
关闭