📄 main.lst
字号:
C51 COMPILER V7.08 MAIN 01/03/2011 21:01:17 PAGE 1
C51 COMPILER V7.08, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /*
2 * DS1302 突发方式,连读,连写
3 *
4 */
5
6 #include <reg52.h>
7 #include <intrins.h>
8
9 typedef unsigned char uint8;
10 typedef unsigned int uint16;
11
12 sbit SCK = P3^5; //时钟
13 sbit SDA = P3^4; //数据
14 sbit RST = P1^7; //DS1302复位(片选)
15
16 #define DS1302_W_ADDR 0xBE
17 #define DS1302_R_ADDR 0xBF
18
19
20 uint8 time[7]={50,59,23,15,8,7,10};//秒分时日月周年10-08-15 23:59:50
21
22
23 void delay(uint16 n)
24 {
25 1 while (n--);
26 1 }
27
28 /**
29 * 写一个字节
30 */
31 void write_ds1302_byte(uint8 dat)
32 {
33 1 uint8 i;
34 1
35 1 for (i=0;i<8;i++)
36 1 {
37 2 SDA = dat & 0x01;
38 2 SCK = 1;
39 2 dat >>= 1;
40 2 SCK = 0;
41 2 }
42 1 }
43
44 /**
45 * 读一个字节
46 */
47 uint8 read_ds1302_byte(void)
48 {
49 1 uint8 i, dat=0;
50 1
51 1 for (i=0;i<8;i++)
52 1 {
53 2 dat >>= 1;
54 2 if (SDA)
55 2 dat |= 0x80;
C51 COMPILER V7.08 MAIN 01/03/2011 21:01:17 PAGE 2
56 2 SCK = 1;
57 2 SCK = 0;
58 2 }
59 1
60 1 return dat;
61 1 }
62
63 void reset_ds1302(void)
64 {
65 1 RST = 0;
66 1 SCK = 0;
67 1 RST = 1;
68 1 }
69
70 /**
71 * 清除写保护
72 */
73 void clear_ds1302_WP(void)
74 {
75 1 reset_ds1302();
76 1 RST = 1;
77 1 write_ds1302_byte(0x8E);
78 1 write_ds1302_byte(0);
79 1 SDA = 0;
80 1 RST = 0;
81 1 }
82
83 /**
84 * 设置写保护
85 */
86 void set_ds1302_WP(void)
87 {
88 1 reset_ds1302();
89 1 RST = 1;
90 1 write_ds1302_byte(0x8E);
91 1 write_ds1302_byte(0x80);
92 1 SDA = 0;
93 1 RST = 0;
94 1 }
95
96
97 /**
98 * 设定时钟数据
99 */
100 void set_time(uint8 *timedata)
101 {
102 1 uint8 i, tmp;
103 1
104 1 for (i=0; i<7; i++) // 转化为BCD格式
105 1 {
106 2 tmp = timedata[i] / 10;
107 2 timedata[i] = timedata[i] % 10;
108 2 timedata[i] = timedata[i] + tmp*16;
109 2 }
110 1
111 1 clear_ds1302_WP();
112 1
113 1 reset_ds1302();
114 1 RST = 1;
115 1 write_ds1302_byte(DS1302_W_ADDR);
116 1 for (i=0; i<7; i++)
117 1 {
C51 COMPILER V7.08 MAIN 01/03/2011 21:01:17 PAGE 3
118 2 write_ds1302_byte(timedata[i]);
119 2 delay(10);
120 2 }
121 1 write_ds1302_byte(0);
122 1 SDA = 0;
123 1 RST = 0;
124 1
125 1 set_ds1302_WP();
126 1 }
127
128 /**
129 * 读时钟数据(BCD格式)
130 */
131 void read_time(uint8 *timedata)
132 {
133 1 uint8 i;
134 1
135 1 clear_ds1302_WP();
136 1
137 1 reset_ds1302();
138 1 RST = 1;
139 1 write_ds1302_byte(DS1302_R_ADDR);
140 1 for (i=0; i<7; i++)
141 1 {
142 2 timedata[i] = read_ds1302_byte();
143 2 delay(10);
144 2 }
145 1 SDA = 0;
146 1 RST = 0;
147 1
148 1 set_ds1302_WP();
149 1 }
150
151
152
153 /**
154 * UART初始化
155 * 波特率:9600
156 */
157 void uart_init(void)
158 {
159 1 TMOD = 0x21; // 定时器1工作在方式2(自动重装)
160 1 SCON = 0x50; // 10位uart,允许串行接受
161 1
162 1 TH1 = 0xFD;
163 1 TL1 = 0xFD;
164 1
165 1 TR1 = 1;
166 1 }
167
168 /**
169 * UART发送一字节
170 */
171 void UART_Send_Byte(uint8 dat)
172 {
173 1 SBUF = dat;
174 1 while (TI == 0);
175 1 TI = 0;
176 1 }
177
178 /**
179 * 将数据转换成ASC码并通过UART发送出去
C51 COMPILER V7.08 MAIN 01/03/2011 21:01:17 PAGE 4
180 */
181 void UART_Send_Dat(uint8 dat)
182 {
183 1 UART_Send_Byte(dat/16 + '0');
184 1 UART_Send_Byte(dat%16 + '0');
185 1 }
186
187 main()
188 {
189 1
190 1 uart_init();
191 1 set_time(&time); //设定时间值
192 1
193 1 while(1)
194 1 {
195 2
196 2 read_time(&time); //秒分时日月周年
197 2
198 2 UART_Send_Dat(time[6]);
199 2 UART_Send_Byte('-');
200 2 UART_Send_Dat(time[4]);
201 2 UART_Send_Byte('-');
202 2 UART_Send_Dat(time[3]);
203 2 UART_Send_Byte(' ');
204 2
205 2 UART_Send_Dat(time[2]);
206 2 UART_Send_Byte(':');
207 2 UART_Send_Dat(time[1]);
208 2 UART_Send_Byte(':');
209 2 UART_Send_Dat(time[0]);
210 2 UART_Send_Byte('\r');
211 2 UART_Send_Byte('\n');
212 2
213 2 delay(10000);
214 2 }
215 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 411 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 7 6
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -