Real Time Clock

Thread Starter

RG23

Joined Dec 6, 2010
304
How to implement a RTC without using any timer in PIC 16F887 ?

How to use DS1307 to achieve RTC for PIC?​
 

beenthere

Joined Apr 20, 2004
15,819
Did you get that data sheet? - http://www.maxim-ic.com/datasheet/index.mvp/id/2688

The data sheet give the details about the RTC chip:
The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I²C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
i tried with that datasheet.


But somehow could not display Hours, min , Sec on LCD display

Please help me out.
 

tom66

Joined May 9, 2009
2,595
i tried with that datasheet.


But somehow could not display Hours, min , Sec on LCD display

Please help me out.
What have you done so far? Have you got an LCD display? What type of LCD display, dot-matrix, or individual segments? Is it a fully graphical LCD or is it segmented? Does it have an integrated controller? What is the interface of the controller, if any?
 

hgmjr

Joined Jan 28, 2005
9,027
How to implement a RTC without using any timer in PIC 16F887 ?

How to use DS1307 to achieve RTC for PIC?
You have not as yet described your problem. However, I believe I can tell you why your DS1307 is not working. Assuming that you have implemented the SPI data read and write functions correctly in software then most likely you have not enabled the 32,768Hz oscillator. Until you set the CH bit in register 00 high, the 32,768Hz oscillator will not oscillate and you will not be able to read the time. The chip comes from the manufacturer with this bit set to 0. YOu will get nothing from teh RTC until you write a "1" to the CH bit. This suggestion comes from personal experience.

hgmjr
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I just want to read the time from RTC and display it on LCD in Hours: MIn: Sec format.

I tried the following code bt it did not work. Please help me out.

Rich (BB code):
RTC_rd:
call I2C_STRT
movlw 0XD0 ; slave address + write
movwf I2C_Value
call I2C_SEND
movlw 0X00 ; set word address to seconds register
movwf I2C_Value
call I2C_SEND
call I2C_STRT
movlw 0XD1 ; slave address + read
movwf I2C_Value 
call I2C_SEND
call read_RTC ; read the seconds data
movwf SECS ; save it
call ack

movlw 0X01 ; set word address to seconds register
movwf I2C_Value
call I2C_SEND
call I2C_STRT
movlw 0XD1 ; slave address + read
movwf I2C_Value 
call I2C_SEND
call read_RTC ; read the seconds data
movwf MINS
call ack

movlw 0X02 ; set word address to seconds register
movwf I2C_Value
call I2C_SEND
call I2C_STRT
movlw 0XD1 ; slave address + read
movwf I2C_Value 
call I2C_SEND
call read_RTC ; read the seconds data
movwf SECS 
call nack
call I2C_STP
return

read_RTC:

bsf I2C_DTA ; set SDA for input
movlw 0x08 ; send 8 bits
movwf COUNTER5

bcf I2C_CLOCK ; SCL low (output)
clrf I2C_Value ; clear var
;rlf I2C_Value, 1 ; rotate carry in
;clrf I2C_Value ; clear var again

I2C_read_loop:
rlf I2C_Value, 1

bsf I2C_CLOCK ; SCL high (input)
btfsc PORTC,4
bsf I2C_Value, 0 ; if data out = 1, set bit

;bcf I2C_CLOCK 

bcf I2C_CLOCK ; SCL low (output)

decfsz COUNTER5, 1
goto I2C_read_loop

movf I2C_Value, 0

return
 
Last edited by a moderator:

hgmjr

Joined Jan 28, 2005
9,027
Try writing the hex value 0x80 to the seconds register. The MSB in this data value is the CH bit that I was referring to. By writing a "1" to that bit location in the seconds register you enable the 32,768Hz oscillator.

hgmjr
 

retched

Joined Dec 5, 2009
5,207
Have you tested you LCD to check if it is operational?
Is it broken?

You should try to send a few characters to the LCD to see if it works first.
 

bertus

Joined Apr 5, 2008
22,278
Hello,

At the moment you only talked about the hardware and the pic program.
A schematic how everything is connected would help a lot to get the overview.
Can you make the schematic and post it?

Bertus
 

Thread Starter

RG23

Joined Dec 6, 2010
304
The LCD is working fine

The clock frequency is the same as the crystal frequency

By setting the CH bit in register 00 the oscillator should have been enabled but it doesn't
 

Thread Starter

RG23

Joined Dec 6, 2010
304
i dont have the schematic

But I just want to read the sec,min , hrs from RTC and display it on LCD

I still haven't solved that problem

I appreciate your help

Thank you
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I had one more question

Do I need to call the RTC subroutine in the main or in the ISR

I have posted my code earlier

Please have a look and let me know any flaws

Thank you
 
Top