Wednesday, August 27, 2008

PIC Clock - with DS1307

After so many sleepless nights I managed to run a clock using a PIC, this time using the Maxim RTC chip, DS1307. This time I didn't use multiplexing to drive the four seven segments, instead I used another Maxim product, MAX7221 display driver. It has SPI interface and can drive upto 8 seven segments with display brightness control and that is cool... Since I'm using the PIC16F628 at the moment, I had to use Sotware SPI to drive the display. It worked without giving much problems but later came the most difficult part, DS1307. Oh boy, what a mess... It gave me thousand and one problems and took nearly a month to get it worked. But truly speaking I leaned a lot and mastered the DS1307. This fellow is really great and extremely easy to communicate via the I2C interface. I had problems because I used Software I2C routines in MikroC. I think SOFT_I2C has a problem with PORTA and after so many trial and error attempts it worked when I moved the DS1307 to PORTB. Now both DS1307 (I2C) and MAX7221 (SPI) are in PORTB and work without any trouble. DS1307 needs an external crystal (32.768kHz) to operate and again Maxim has another great product DS32KHZ, a temperature compensated 32.768kHz crystal which I found extremely accurate. So far the clock is running smoothly and accurately, which needs lot of other things, a way to set time, view calendar, set alarm etc. And I'm thinking of attaching a Temperature sensor too. Pictures, videos, schematic and code segments will appear shortly.

4 comments:

suding said...

Hi there,
I'm trying to program DS1302 now with PIC18F452. Any code from yours for DS1302??
I had tried few days, but with my shallow knowledge on programming, it's hard for me to go far.
I'm an engineering student, doing mini project.
If you do have, kindly email me. thanks!

Chandana said...

Hi,
What is language you use? I normally use MikroC for my projects. Therefore the first place I look for help is the MikroE forum.
(http://www.mikroe.com/forum/)

I remember there were several code samples in the forum for DS1302. Just search there, you'll find what you want.

Unknown said...

hello there,

would like to ask you about DS1307. I coundn't understand the datasheet for it. I would like to know how to set the Hour for DS1307 at 8am (how to set the bit)? and the read_ds1307(2) is in binary?

thanks for the prompt reply..

Chandana said...

Hello Tang,
I use the following two procedures to read and write to DS1307

unsigned short read_ds1307(unsigned short address)
{
Soft_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xd1);
data=BCD2DEC(Soft_I2C_Read(0));
Soft_I2C_Stop();
return(data);
}

void Write_DS1307(unsigned short Address, unsigned short _Data) {

Soft_I2C_Start(); // start a serial transfer
Soft_I2C_Write(0xD0); // which device, 1101000 = DS1307, + direction bit (R=1,W=0)
// thus 11010000 = 0xD0 ie write to DS1307
// need to tell the DS1307 which address is to be written to
Soft_I2C_Write(Address); // send the address to be written to the DS1307
Soft_I2C_Write(_Data); // send actual data to be written into Address on the DS1307
Soft_I2C_Stop(); // finished serial transfer (and release the I2C bus)
}

You may set the time to 8.00 AM as follows
Write_DS1307(2,dec2bcd(8)) //hours
Write_DS1307(1,dec2bcd(0)) //minutes
Write_DS1307(0,dec2bcd(0))//seconds

To read from DS1307
_secs=read_ds1307(0);
_mins=read_ds1307(1);
_hours=read_ds1307(2);

Hope this will help you. Good Luck!