Tutorial Microcontroller 8051
 
 
 
 
 
   

Micro-C Library Extension for DS1202 Real-time Clock
W. Sirichote

An 8-pin RTC chip, DS1202 provides an accurate digital clock for real-time applications. Example circuit shown above requires 3-pin for interfacing with 8051 chip, P1.3, P1.4 and P1.5. For the DS1202 chip, backup supply, +V requires a signal diode, say 1N914 to switch between normal supply and +3V lithium battery. Newer version DS1302 has an internal switching diode, +3V battery supply can then be connected directly. My friend, Santi Hankla, wrote me the library extension for DS1202, DS1202.ASM. The library extension is used with Micro-C Compiler written in Assembly. To use the extension, you have to add the name and label of the DS1202.ASM in the TINY.LIB. He also provides me the example C program, REALTIME.C, sending real-time clock to terminal every second.

/*
DS1202/1302 RTC example program
send reading every 1000 ms
original source program was written by S.HANKRA
require DS1202 Library:
wrds1202(int) write data to DS1202
char rdds1202(char) read data from DS1202
/RST P1.3
I/O P1.4
SCLK P1.5
*/

#include c:\mc\8051io.h
#include c:\mc\8051reg.h


#define rdsec 0x81
#define rdmin 0x83
#define rdhour 0x85
#define rddate 0x87
#define rdmonth 0x89
#define rdyear 0x8d
#define secu 0x8e
#define wrdate 0x86
#define wrmonth 0x88
#define wryear 0x8c
#define wrhour 0x84
#define wrmin 0x82
#define wrsec 0x80
#define unlock 0
#define lock 0x80
/* ---------------------------------------------------- */
writertc(a,b)
char a,b;
{
int i;
i = a;
i <<= 8;
i |= b;
wrds1202(i);
}
/* ------------------------------------------------------- */
printdate()
{
printf("%02x/%02x/%02x\n",rdds1202(rddate),
rdds1202(rdmonth),rdds1202(rdyear));
}
/* ----------------------------------------------------------- */
printtime()
{
printf("%02x:%02x:%02x\n",rdds1202(rdhour),
rdds1202(rdmin),rdds1202(rdsec));
}
/* ----------------------------------------------------------- */
setdate()
{
writertc(secu,unlock);
writertc(wrdate,0x08);
writertc(wrmonth,0x12);
writertc(wryear,0x99);
writertc(secu,lock);
}
/* ----------------------------------------------------------- */
settime()
{
writertc(secu,unlock);
writertc(wrhour,0x12);
writertc(wrmin,0);
writertc(wrsec,0);
writertc(secu,lock);
}
/* ----------------------------------------------------------- */

main()
{
setdate();
settime();
serinit(9600);
while(1){
printdate();
printtime();
delay(1000);
}
}

);
while(1){
printdate();
printtime();
delay(1000);
}
}

Best Link

 

BACK

 

Lesson 1:
T o o l
1.1. Programmer
1.2.
Edsim 51
1.3. MIDE-51
1.4. ATMEL ISP

Lesson 2:
Input Output
2.1.LED
2.2.Swicht
2.3.7 Segmen
2.4.LCD Character
2.5.ADC
2.6.DAC
2.7.Motor Stepper
2.8.Keypad

Lesson 3:

Timer Counter

3.1.Basic
3.2.Mode 0
3.3.Mode 1
3.4.Mode 2
3.5.Mode 3

Lesson 4:

Serial Comm.

4.1.Basic
4.2.LED
4.3.Rotate LED
4.2 ADC
4.3.LCD

Lesson 5:
Interuption

5.1.Basic
5.2.Timer
5.2.External