0% found this document useful (0 votes)
40 views

#Include #Include #Include #Define Data7 P2 #Define Sca7 P3

This document contains the C source code for a real-time clock (RTC) application. It defines functions for initializing the RTC, reading and writing time values from the RTC registers in BCD format, converting between BCD and decimal representations, displaying the time on 7-segment LEDs, and handling buttons to set, increment, decrement and blink the time display. It also contains an interrupt service routine that triggers every 50ms to read the RTC, blink an LED and check buttons for alarms, manual time setting or adjustment.

Uploaded by

phamtandatx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

#Include #Include #Include #Define Data7 P2 #Define Sca7 P3

This document contains the C source code for a real-time clock (RTC) application. It defines functions for initializing the RTC, reading and writing time values from the RTC registers in BCD format, converting between BCD and decimal representations, displaying the time on 7-segment LEDs, and handling buttons to set, increment, decrement and blink the time display. It also contains an interrupt service routine that triggers every 50ms to read the RTC, blink an LED and check buttons for alarms, manual time setting or adjustment.

Uploaded by

phamtandatx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

G:\RTC\rtc.

Page 1 of 8
date:2/24/2012, time:4:00:22 PM

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:

//Device : 89s52
// Crysltal: 12MHZ
#include <regx51.h>
#include <stdio.h>
#include <intrins.h>
#define data7 P2
#define sca7 P3
sbit sda = P1^0;
sbit scl = P1^1;
sbit bset = P1^2;
sbit bup = P1^3;
sbit bdown = P1^4;
sbit balarm = P1^5;
bit chon=0,alarm_bit=1;
sbit bmanual = P1^6;
sbit alarm_led = P0^0;
sbit alarm_sound = P1^7;
sbit led_blink =P0^1;
unsigned char mangT[4];
unsigned char led[10] =
{
0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90
};
unsigned char phut,gio,gio_t,phut_t,count=0;
//================================
//Chuong trinh con
//================================
//==================================
void delay1 ()
{
unsigned char i;
for (i=0;i<20;i++)
_nop_();
}
//================================
void delay2(unsigned int t)
{
TH1=(-t/256);
TL1=(-t%256);
TR1=1;
while (!TF1);
TR1=TF1=0;
}
//================================
void start_rtc ()
{
sda=1;
scl=1;
delay1();
sda=0;
delay1();
scl=0;
}

Printed by C-Free

1/8

G:\RTC\rtc.c

Page 2 of 8
date:2/24/2012, time:4:00:22 PM

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:

//================================
void stop_rtc()
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
}
//================================
void ack_rtc()
{
sda=0; //ack=0;
delay1();
scl=1;
delay1();
scl=0;
}
//================================
void nack_rtc()
{
sda=1; //ack=1
delay1();
scl=1;
delay1();
scl=0;
}
//================================
void write_i2c(unsigned char dl)
{
unsigned char i;
for(i=0;i<8;i++)
{
if (((dl<<i) & 0x80)==0x80)
sda=1;
else
sda=0;
delay1();
scl=1;
delay1();
scl=0;
delay1();
}
//ack tu ds1307
sda=1;
delay1();
scl=1;
delay1();
scl=0;
}
//================================
void write_rtc(unsigned char dc,unsigned char dl)
{
start_rtc();
write_i2c(0xd0);
write_i2c(dc);
write_i2c(dl);
stop_rtc();
}

Printed by C-Free

2/8

G:\RTC\rtc.c

Page 3 of 8
date:2/24/2012, time:4:00:22 PM

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:

//================================
unsigned char read_i2c()
{
unsigned char i,dl=0;
sda=1;
for(i=0;i<8;i++)
{
delay1();
scl=1;
delay1();
dl<<=1;
dl=dl|sda;
scl=0;
}
return dl;
}
//================================
unsigned char read_byte_rtc(unsigned char dc)
{
unsigned char dl;
sda=1;
scl=1;
delay1();
start_rtc ();
write_i2c(0xd0);
write_i2c(dc);
start_rtc (); //start again
write_i2c(0xd1);
read_i2c ();
nack_rtc ();
stop_rtc ();
return dl; //Tra ve
}
//================================
void read_rtc()
{
start_rtc ();
write_i2c (0xd0);
write_i2c (0x01);
start_rtc();
//Start again
write_i2c (0xd1);
phut=read_i2c ();
ack_rtc();
gio=read_i2c();
nack_rtc ();
stop_rtc ();
}
//================================
void bcd2led7 ()
{
mangT[0]=led[gio>>4];
mangT[1]=led[gio&0X0F];
mangT[2]=led[phut>>4];
mangT[3]=led[phut&0X0F];
}

Printed by C-Free

3/8

G:\RTC\rtc.c

Page 4 of 8
date:2/24/2012, time:4:00:22 PM

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:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:

//================================
void hienthi()
{
unsigned char i;
bcd2led7();
for(i=0;i<4;i++)
{
sca7=255; //chong lem
data7=mangT[i];
sca7=~(1<<i);
delay2(1000);
}
}
//================================
unsigned char bcd2dec (unsigned char dl)
{
unsigned char kq;
kq=((dl>>4)*10)+(dl&0x0f);
return kq;
}
//================================
unsigned char dec2bcd (unsigned char dl)
{
unsigned char temp,kq;
temp=dl/10;
kq=(temp<<4);
temp=dl%10;
kq=kq|temp;
return kq;
}
//================================
void tang (chon)
{
if (!chon)
{
gio_t=bcd2dec(gio);
gio_t++;
if (gio_t==24)
gio_t=0;
write_rtc (0x02,dec2bcd(gio_t));
}
else
{
phut_t=bcd2dec (phut);
phut_t++;
if (phut_t==60)
phut_t=0;
write_rtc (0x01,dec2bcd(phut_t));
}
}
//================================
void giam (chon)
{
if (!chon)
{
gio_t=bcd2dec (gio);

Printed by C-Free

4/8

G:\RTC\rtc.c

Page 5 of 8
date:2/24/2012, time:4:00:22 PM

241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:

gio_t--;
if (gio_t==255)
gio_t=23;
write_rtc (0x02,dec2bcd(gio_t));
}
else
{
phut_t=bcd2dec(phut);
phut_t--;
if (phut_t==255)
phut_t=59;
write_rtc (0x01,dec2bcd(phut_t));
}
}
//================================
void check_ud (chon)
{
unsigned char i,j;
bup=bdown=1;
if (!bup)
{
j=0;
do
{
for(i=0;i<10;i++)
hienthi(); //chong doi
j++;
}
while ((j<20)&&(!bup));
if (j==20)
while(!bup)
{
tang(chon);
for(i=0;i<20;i++) hienthi(); //tang lien tuc
}
else
tang(chon); //tang 1 don vi
} //end if (!bup)
//------------------------------if (!bdown)
{
j=0;
do
{
for (i=0;i<5;i++) hienthi(); //chong doi
j++;
}
while ((j<20) && (!bdown));
if (j==20)
while (!bdown)
{
giam(chon);
for (i=0;i<10;i++) hienthi(); //giam lien tuc
}
else giam(chon); //giam 1 don vi
}
}
//================================
void blink (chon)
{

Printed by C-Free

5/8

G:\RTC\rtc.c

Page 6 of 8
date:2/24/2012, time:4:00:22 PM

301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:

unsigned char i,j;


for (i=0;i<80;i++)
{
bcd2led7 ();
for(j=0;j<2;j++)
{
sca7=255; //chong lem
if ((i>40)||(chon))
data7=mangT[j];
else
data7=255; //tat led7seg
sca7=~(1<<j);
delay2(1000);
}
for(j=2;j<4;j++)
{
sca7=255; //chong lem
if ((i>40)||(!chon))
data7=mangT[j];
else
data7=255; //tat led7seg
sca7=~(1<<j);
delay2(1000);
}
check_ud (chon);
if (!bset) break;
} //end for
} //end blink
//================================
void jset()
{
unsigned char i;
do
{
for(i=0;i<2;i++) hienthi(); //chong doi
}
while (!bset);
//
while (bset) //blink gio
blink (0);
//
do
{
for (i=0;i<2;i++) hienthi(); //chong doi
}
while (!bset);
//
while (bset) //blink phut
blink (1);
//
do
{
//chuan bi thoat khoi blink
for (i=0;i<2;i++) hienthi(); //chong doi
}
while (!bset);
write_rtc (0x00,0x00); //reset giay
} //end jset

Printed by C-Free

6/8

G:\RTC\rtc.c

Page 7 of 8
date:2/24/2012, time:4:00:22 PM

361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:

//================================
void jalarm()
{
unsigned char i;
alarm_bit =alarm_led=~alarm_bit;
do
{
for (i=0;i<10;i++) hienthi();
}
while (!balarm);
}
//================================
void jmanual ()
{
unsigned char i;
for (i=0;i<5;i++) hienthi(); //chong doi
while ((!alarm_bit)&&(!bmanual))
{
alarm_sound=0;
hienthi();
}
alarm_sound=1;
}
//================================
void main()
{
TMOD=0X11;
IE=0X82;
TF0=1;
while (1)
{
hienthi();
if (!bset) jset();
if (!balarm) jalarm();
if (!bmanual) jmanual();
}
}
//================================
//Trinh phuc vu ngat********* ******** ********** ********
void ngatT0() interrupt 1
{
TR0=0;
TH0=(-50000)/256;
TL0=(-50000)%256;
//Doc rtc
read_rtc();
//LED BLINK
count++;
if(count==20)
count=0;
if (count<6) led_blink=1;
else led_blink=0;
TR0=1;

Printed by C-Free

7/8

G:\RTC\rtc.c

Page 8 of 8
date:2/24/2012, time:4:00:22 PM

421: }
422:
423: //Het chuong trinh

Printed by C-Free

8/8

You might also like