real time clock using pic16f877 and DS1307

Status
Not open for further replies.

manarose

Newbie level 4
Joined
Dec 4, 2007
Messages
6
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,283
Location
Amman- Jordan
Activity points
1,347
ds1307 mikroc

hi everybody
i'm a new member and i need your help in my project.
i want to use DS1307 with pic16f877a to design a real time clock but i don't know how to use the DS1307 and how to set the time for first time

help me please, and thank you anyway.
 

pic ds1307 mikroc

manarose
DS1307 is real time IC with I2C protocol
hi,I haven't used PIC to control it yet.But I have use AT89Sxx to control it. here are my code. hope it helpfull to you
Code:
#include "REG52.H"	
#include "REG52.H"
sbit SDA = P1^0;
sbit SCL = P1^1;
//declare function 
void delay(void);
void SCL_high(void);
void SCL_low(void);
void I2C_Start(void);
void I2C_Stop(void);
bit I2C_Write(unsigned char dat);
unsigned char I2C_Read(bit ack);
//RTC DS1307
void rtc_write(unsigned char add , unsigned char dataa);
unsigned char rtc_read(unsigned char add);



//--------------------ham chinh----------------------------

void main(void)
{
unsigned char hour, minute, second, date, month, year;
rtc_write(0x07, 10); // set control register 00010000; 1Hz

//......................
// ghi thoi gian vao RTC
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
// rtc_write(0x03, day_of_week);
rtc_write(0x04, date);
rtc_write(0x05, month);
rtc_write(0x06, year);
while(1)
     {
     // doc thoi gian RTC
     second = rtc_read(0x00);
     minute = rtc_read(0x01);
     hour = rtc_read(0x02);
     date = rtc_read(0x04);
     month = rtc_read(0x05);
     year = rtc_read(0x06);
	 P2=255-second;
     }
}

//===============cac function================
//===============delay=======================
void delay(void)
{
unsigned char i;
for (i = 0; i < 20; i++)
{;}
}

//---------------SCL high--------------------------
void SCL_high(void)
{
SCL = 1;
delay();
}

//---------------SCL low---------------------------
void SCL_low(void)
{
SCL = 0;
delay();
}

//-----------------START Condition-----------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
SDA = 0;
delay();
SCL = 0;
SDA = 1;
}

//------------------STOP Condition--------------------------
void I2C_Stop(void)
{
SDA = 0;
SCL_high();
SDA = 1;
}

//-------------------I2C Write---------------------------
bit I2C_Write(unsigned char dat)
{
unsigned char i;
bit outbit;
for (i = 1; i <= 8; i++)
   {
    outbit=dat&0x80;
    SDA = outbit;
    dat = dat << 1;
    SCL_high();
    SCL_low();
   }
SDA = 1; // set SDA to receive Acknowledge
SCL_high();
outbit = SDA; // check busy or not
SCL_low();
return(outbit); // if outbit=1 ,A=1: error // if outbit=0 ,A=0: ok
}

//------------------I2C Read----------------------------------
unsigned char I2C_Read(bit ack)
{
unsigned char i, dat;
bit inbit;

dat = 0;
for(i=1;i<=8;i++) {
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack) SDA = 0; // set SDA = 0 (ACK)
else SDA = 1; // set Non ACK
SCL_high();
SCL = 0;
SDA = 1; // Set SDA = 1 for next read
delay();
return(dat);
}
//-------------------------------------------------------------
// ghi data vao cac thang ghi co dia chi add cua RTC
//---------------------RTC wtie----------------------------------------
void rtc_write(unsigned char add , unsigned char dataa)
{
I2C_Start();
I2C_Write(0xd0); // dia chi cua DS1307 o che do  ghi
I2C_Write(add); // dia chi thanh ghi
I2C_Write(((dataa/10)<<4)|(dataa%10)); // chuyen tahnh so thap phan
I2C_Stop();
}

//---------------------------------------------------------------
// doc du lieu tai thanh ghi cho dia chi add cua RTC
//---------------------------------------------------------------
unsigned char rtc_read(unsigned char add)
{
unsigned char dataa ;
I2C_Start();
I2C_Write(0xd0); // dia chi cua DS1307 o che do ghi
I2C_Write(add); // dia chi thanh ghi can doc
I2C_Start(); // restart
I2C_Write(0xd1); //  dia chi DS1307 o che do doc
dataa = I2C_Read(0);
I2C_Stop();
dataa = (dataa & 0x0f) + (dataa>>4)*10; // chuyenthanh so thap phan
return (dataa);
}
 

rtc_read pic

Attached is the assembly code routines for 16F877 to interface with DS1307.

Cheers

Ravi
 
i2c_read(0)+what mean+ds1307

ravimarcus said:
Attached is the assembly code routines for 16F877 to interface with DS1307.

Cheers

Ravi
thank you very much i realy pretuiate your help but aunestly i did not understand this code very well so i tried to simulate it but i get stucked in an infinite loop in the iiportcin routine.
thanks again
 

    V

    Points: 2
    Helpful Answer Positive Rating
ds1307 with pic16f8xx

manarose said:
thank you very much i realy pretuiate your help but aunestly i did not understand this code very well so i tried to simulate it but i get stucked in an infinite loop in the iiportcin routine.
It will not work as is. You will have to implement it in your code. I have just given you all the routines individually.

Cheers

Ravi
 

tai lieu realtime pic16f877a

I made a program in C for PIC18F452 for control of DS1307.
the used compiler is the mikroC

simplicio
 

ds1307 clock using pic

It will not work as is. You will have to implement it in your code. I have just given you all the routines individually.

Cheers

Ravi[/quote]

thanks again i realy pretiate your help but can you help me in some way else?
i read about ds1307 and i knew that it works as a slave using i2c mode. can you help me in learning about this topic? and the steps in details to intrface pic16f877 with ds1307 using this protocol .we just ablut USART.

thank you
 

cach set time cho ds1307

Use CCS C then implement I2C.
 

interfacing pic16f877a and ds1307

Use any 2 I/O line to interface to DS1307. You do not have to use the internal IIC engine to communicate with DS1307. For the SDA line, have a pull up resistor of 1K.

Any thing you want to know, let me know, I will try to help you out.

Cheers

Ravi
 

pic16f877 sleep interrupt

ravimarcus said:
Use any 2 I/O line to interface to DS1307. You do not have to use the internal IIC engine to communicate with DS1307.


Ravi

hello Ravi;
thank you for supporting me in my project work, but i did not understand how i can use any I/O lines to communicate with ds1307, if so how can i set the time for first time and how can i reseive the clock and the date data all the time to display them on a LCD? can i use interrupt in this case?

thank you million times.

manar
 

time setting alarm clock using pic16f877


Use "bit banging" technique.... meaning simulate the function of SDA and SCL lines using any I/O lines.

Setting the time.... you have to write the time.... then you read the time back. If you use SQW/OUT, then you can enable the interrupt and read every second.

Cheers

Ravi
 

16f877a+ds1307+code

Hello Ravi;

to put you in the picture i will describe my project so that i can ask you (and believe me i will) alot of questions if you do not mind .....

i want to design a real time clock that displays the time and the date on LCD all the time and i will use a 3V lithium battery to keep the time when the power supply is turned off.
and i will add a timer that waits the user to enter a certain time by the keypad and after this time a buzzer will give an alarm sound.

I am planing to use the i2c mode since i do not know how to use (bit banging technique). and for the timer i want to build a routine that gives 1second dellay using TIMER0 interrupt and i will repeat it number of times equals to the number that the user enterd.

now my questions is
will this timer job effect the process of reading the time evey one second since both depend on passing one second to response?

and another question:
is it right that i have to acsess to the ram memory of the DS1307 to set the seconds,min,hours,.....etc?
if yes how can i do this?

Thanks Alot

manar
 
pic16f877 sleep mode code

There are 2 ways to do your project.

1. Use 16F877A and DS1307. In my experience with DS1307, it looses time due to the 32768 Hz crystal. Will you be able to get a very stable crystal?

You can write to the time registers and RAM of the DS1307 through IIC.

2. Use 16F877A and a 32768 Hz crystal in TMR1. Configure it to give you interrupt every second. Monitor the AC power, if it fails, go to sleep mode. The TMR1 interrupt will wake up the 16F877A and update the clock. If the time matches the set time, sound a buzzer, else go back to sleep. This method will not need DS1307.

Cheers

Ravi
 

16f877a ds1307

I can help you with PICBasic code to setup the clock, first you have to write a control byte to the control register 07H to put the DS1307 in setup mode what the byte is you will have to look it up in the data sheet, then you send the date and time to the individial registers that is also one byte each starting with the seconds at address 00H, minutes at 01H and so on then to end the setup place the DS1307 back to transmit mode by sending the control byte to 07H this code is as is and not tested or debugged but you will get the idea and you don't need a large 40 pin device a PIC16F627 will do the trick, LCD display connected in 4bit mode only 6 wires to the display
Code:
a VAR byte[7]    'an array variable
'I2CWRITE DataPin,ClockPin,Control,{Address,}[Value{,Value...}]{,Label}

a[0] = 34 'seconds
a[1] = 16 'minutes
a[2] = 14 'hours
a[3] = 4  'day of the week 1 to 7
a[4] = 13 'date
a[5] = 12 'month
a[6] = 07 'year 
I2CWRITE PORTC.4,PORTC.3,$a0,07,["the control byte as in data sheet"] 'set the control register
I2CWRITE PORTC.4,PORTC.3,$a0,0,[STR a\7]       'set the time on the ds1307
I2CWRITE PORTC.4,PORTC.3,$a0,$07,["the control byte as in data sheet"]  'reset the control register
Main:
I2CREAD  PORTC.4,PORTC.3,$a0,0,[STR a\7]
LCDOUT $FE, $C0,STR a\7          'Clear the display and print the time and date
PAUSE 100
GOTO Main
end
 
can we generate a digital clock from PIC16F877a and will not be using ds1307? does it more difficult? thanks.
 
Olá colegas, estou com dificuldades, nem sei como explicar fiz diversas tentativas, enfim...
Gostaria de saber se alguém tem o código para usar o ds1307 com o pic16f876, de tal maneira que me permita ainda, usar as interrupções do TMR0 e TMR2.

Se alguém puder ajudar em alguma coisa eu lhes agradeço.

///

Hello colleagues, I'm struggling, I did not know how to explain several attempts, finally ...
I wonder if anyone has the code to use the DS1307 with PIC16F876, so allow me to still use the TMR0 and TMR2 interrupts.

If anyone can help with anything I thank you.
 
Reactions: patee

    patee

    Points: 2
    Helpful Answer Positive Rating
Hello


See this site

Real Time clock ith Ds1307 , PIC16F877A and CCS C
**broken link removed**
 

Hi I am very very new to this can this be implemented on a 16f690

Please let me know
 

please i need programe and schematics for 16f877 with DS1307 0N SEVEN SEGMENT DISPLAY
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…