The DS1302 Real Time Clock and PIC16F628A microcontroller on PIC-01 Development Board.

27 January, 2007 (10:31) | Microchip PIC, Projects | By: glogin

1. Introduction.

This is an example of the Real Time Clock (RTC) application. In this project we used the DS1302 from Dallas Semiconductor(Maxim). This project was tested using the PIC16F628A microcontroller and the PIC-01 Development Board. All routines were written in the CCS C.

2. Circuit.

The DS1302 to its work needs only the 32.768kHz crystal. It communicates with the microcontroller through 3-wire serial interface. These lines are connected to the three of the port B pins of the microcontroller instaled on the PIC Development Board.

Drawing 1. (large view)
DS1302 circuit

3. Listing.

All of the routines were written in the CCS C. The program uses standard ds1302 library of the CCS compiler. Date and time are set with the RB0 and RA4 push-buttons.

//----------------------------------------------------------
//
// ds1302.c
//
//----------------------------------------------------------
#include<16f628a.h> //Device
#fuses xt,nowdt,protect,noput,nolvp //Fuses
#use delay (clock=4000000) //4.00 MHz
#use rs232 (Baud=9600) //RS-232 parametres
//(only for printf function)
#include"llcd_new.h" //LCD driver
#include "ds1302.h" //ds1302 driver
int day,mth,year,dow,h,m,s;
int a,b,c,d,e,x,y,z,v;

set_h() //set hour
{
while(!input(pin_b0)) //wait till button released
delay_ms(5);
while(1)
{
if(!input(pin_a4)&input(pin_b0)) //if button pressed
if(++d>23) //increase hours
d=0; //till < 24
delay_ms(200);
printf(lcd_putc,"\f%02u-%02u-20%02u",a,b,c);
printf(lcd_putc,"\n%02u:%02u:00",d,e);

rtc_set_datetime(a,b,c,0,d,e); //set date and time
delay_ms(50); //0-24h mode; 1-12h mode
x=1;
if(!input(pin_b0)) //return if button pressed
{delay_ms(100);
return(0);}
}
}

set_m() //set minute
{
while(!input(pin_b0))
delay_ms(5);
while(1)
{
if(!input(pin_a4)&input(pin_b0))
if(++e>59)
e=0;
delay_ms(200);
printf(lcd_putc,"\f%02u-%02u-20%02u",a,b,c);
printf(lcd_putc,"\n%02u:%02u:00",d,e);

rtc_set_datetime(a,b,c,0,d,e);
delay_ms(50);
y=1;
if(!input(pin_b0))
{delay_ms(100);
return(0);}
}
}

set_d() //set day
{
while(!input(pin_b0))
delay_ms(5);
while(1)
{
if(!input(pin_a4)&input(pin_b0))
if(++a>31)
a=0;
delay_ms(200);
printf(lcd_putc,"\f%02u-%02u-20%02u",a,b,c);
printf(lcd_putc,"\n%02u:%02u:00",d,e);

rtc_set_datetime(a,b,c,0,d,e);
delay_ms(50);
z=1;
if(!input(pin_b0))
{delay_ms(100);
return(0);}
}
}

set_mo() //set month
{
while(!input(pin_b0))
delay_ms(5);
while(1)
{
if(!input(pin_a4)&input(pin_b0))
if(++b>12)
b=0;
delay_ms(200);
printf(lcd_putc,"\f%02u-%02u-20%02u",a,b,c);
printf(lcd_putc,"\n%02u:%02u:00",d,e);

rtc_set_datetime(a,b,c,0,d,e);
delay_ms(50);
v=1;
if(!input(pin_b0))
{delay_ms(100);
return(0);}
}
}

set_y() //set year
{
while(!input(pin_b0))
delay_ms(5);
while(1)
{
if(!input(pin_a4)&input(pin_b0))
if(++c>99)
c=0;
delay_ms(200);
printf(lcd_putc,"\f%02u-%02u-20%02u",a,b,c);
printf(lcd_putc,"\n%02u:%02u:00",d,e);

rtc_set_datetime(a,b,c,0,d,e);
delay_ms(50);
v=1;
if(!input(pin_b0))
{delay_ms(100);
return(0);}
}
}

main()
{
a=0,b=0,c=0,d=0,e=0,x=0,y=0,z=0,v=0;

rtc_init(); //start ds1302
rtc_set_datetime(a,b,c,0,d,e); //set date and time

while(1)
{
rtc_get_time(h,m,s); //read time
rtc_get_date(day,mth,year,dow); //read date

printf(lcd_putc,"\f%02u-%02u-20%02u",day,mth,year);
printf(lcd_putc,"\n%02u:%02u:%02u",h,m,s);
delay_ms(100);

if(!input(pin_b0))
set_h(); //set hour
if(!input(pin_b0)&x)
{while(!input(pin_b0)) //wait till button released
delay_ms(5);
set_m();} //set minute
if(!input(pin_b0)&y)
{while(!input(pin_b0))
delay_ms(5);
set_d();} //set day
if(!input(pin_b0)&z)
{while(!input(pin_b0))
delay_ms(5);
set_mo();} //set month
if(!input(pin_b0)&v)
{while(!input(pin_b0))
delay_ms(5);
set_y();} //set year
}
}

Time and date is displayed on the PIC-01 Development Board's LCD in formats "XX-XX-20XX" and "XX:XX:XX" (24h mode).

All files are available here:
ds1302.c (.zip)
ds1302.h (.zip)
The DS1302 Real Time Clock data scheet.

Bad Behavior has blocked 58 access attempts in the last 7 days.