Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[SOLVED] AT89S52 and DS1307 Based Clock Power off second reset problem

Status
Not open for further replies.

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
My clock using AT89S52 and DS1307 RTC and 16X2 LCD is working well. But I have a problem. When I switch off the power supply and switch on again the second digits starts counting from 0.For example, if I switch off at 4:16:24 and switch on again it shows 4:16:00!!!!!!!!!!. Please help
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,146
Reputation
4,298
Reaction score
2,087
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,097
1. Have you connected the battery to RTC.
2. when you initialize the RTC and then power OFF and comment the I2C write or init of RTC where you put the values into rtc register..
3. keep only the read routine . compile the program and burn the hex file....
4. put the controller back and power ON.. it will skip the rtc write and only read the rtc registers with updated time.........
 

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
Thanks for reply.
I have connected the battery. There are no problems with the hour and min display.

Here are the code to read:-



$mod51
ORG 0000H

;===================================================================================

SCL EQU P0.0 ;IN THIS EXAMPLE I USED PORT 2.0
SDA EQU P0.1 ;AND PORT 2.1 FOR THE I2C LINES
;YOU CAN CHANGE THEM TO WHATEVER ACCEPTABLE

;===================================================================================
;=====THE READ AND WRITE COMMANDS (0D0H AND 0D1H)

CONT_BYTE_W EQU 11010000B
CONT_BYTE_R EQU 11010001B

;===================================================================================

ORG 0060H

;===================================================================================
;=====ADD_LOW IS THE DPL, THIS IS THE ADDRESS INISDE THE DS1307
;=====DAVAVA IS THE VARIABLE TO STORE DATA WHEN IT GETS BACK FROM THE DS1307

ADD_LOW EQU 62H
DAVAVA EQU 63H

;===================================================================================
;=====VARIABLES TO STORE THE TIME IN, COULD BE USED ALSO TO STORE DATA TO WRITE ON DS1307

SEC EQU 64H
MIN EQU 65H
HOUR EQU 66H
DAY EQU 67H
DATE EQU 68H
MONTH EQU 69H
YEAR EQU 6AH
RAND EQU 6BH

ORG 0100H

;==================================================================================
;=====THIS PROCEDURE INITIATES THE DS1307, WITHOUT IT THE DS1307 WON'T START!!!!!!

BEGIN:
CLR SCL
CLR SDA
NOP
SETB SCL
SETB SDA
NOP
MOV ADD_LOW,#00H
MOV DAVAVA,#00H
LCALL WRITE_BYTE

;==================================================================================
;=====THIS IS THE MOST IMPORTANT PROCEDURE!!!! HERE YOU GET ALL THE DATA FROM THE DS1307

ACALL ONETIME_LCDINIT
ACALL DELAY_200M
Start:
MOV A,#86H
ACALL COMMAND
CALL INIT_PORT

mov A,HOUR
ACALL BCDTOASCII
MOV A,#':'
ACALL DATA_DISPLAY
mov A,MIN
ACALL BCDTOASCII
MOV A,#':'
ACALL DATA_DISPLAY
mov A,SEC
ACALL BCDTOASCII
ACALL DELAY
ACALL DELAY
SJMP START ;LOOP, YOU CAN ADD A LONGER DELAY, THEN END IT TO LCD OR ANYTHING YOU WANT

;==================================================================================
;=====PAY ATTENTION, HERE IS WHERE YOU CHOOSE WHAT DATA YOU WANT, AND WHERE TO STORE IT!!

INIT_PORT:
;==================================================READS SECONDS
MOV ADD_LOW,#00h
LCALL READ_BYTE
MOV SEC,DAVAVA
LCALL I2C_STOP
;==================================================READS MINUTES
MOV ADD_LOW,#01h
LCALL READ_BYTE
MOV MIN,DAVAVA
LCALL I2C_STOP
;==================================================READS HOURS
MOV ADD_LOW,#02h
LCALL READ_BYTE
MOV HOUR,DAVAVA
LCALL I2C_STOP
;==================================================READS DAYS
MOV ADD_LOW,#03h
LCALL READ_BYTE
MOV DAY,DAVAVA
LCALL I2C_STOP
;==================================================READS DATE OF WEEK
MOV ADD_LOW,#04h
LCALL READ_BYTE
MOV DATE,DAVAVA
LCALL I2C_STOP
;==================================================READS MONTHS
MOV ADD_LOW,#05h
LCALL READ_BYTE
MOV MONTH,DAVAVA
LCALL I2C_STOP
;==================================================READS YEARS
MOV ADD_LOW,#06H
LCALL READ_BYTE
MOV YEAR,DAVAVA
LCALL I2C_STOP
;=====
RET

;==================================================================================
;=====stop I2C communication

I2C_Stop:
CLR SDA
SETB SCL
NOP
SETB SDA
RET

;==================================================================================
;=====ANYTHING BELOW IS JUST PROCEDURES TO GET THE DATA, JUST COPY AND PASTE FROM
;=====HERE DOWN, IT'S EXPLAINED AS WELL, IF YOU INSIST. CHANGE THE PROCEDURE NAMES IF
;=====ANY CONFLICT HAPPENS WHEN YOU WRITE A LONGER PROGRAM
;=====
;*****************************************************
;* WRITE DAVAVA TO DS1307 1 BYTE *
;* INPUT : ADD_LOW *
;* : DAVAVA *
;*****************************************************
WRITE_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SCL
MOV A,DAVAVA ;send DAVAVA
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,WRITE_BYTE ;loop until busy
CLR SDA
CLR SCL
SETB SCL ;stop bit
SETB SDA
RET

;******************************************************
;* READ DAVAVA FROM DS1307 1 BYTE *
;* INPUT : ADD_HIGH *
;* : ADD_LOW *
;* OUTPUT : DAVAVA *
;******************************************************
READ_BYTE: CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_W ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
MOV A,ADD_LOW ;send address low
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL

SETB SCL
SETB SDA
CLR SDA ;start bit
CLR SCL
MOV A,#CONT_BYTE_R ;send control byte
LCALL LOOP_BYTE
SETB SDA
SETB SCL
JB SDA,READ_BYTE ;loop until busy
CLR SCL
LCALL LOOP_READ
SETB SDA
SETB SCL
CLR SCL

SETB SCL ;stop bit
SETB SDA
RET

;****************************************************
;* WRITE *
;* INPUT: ACC *
;****************************************************
LOOP_BYTE: PUSH 02H
MOV R2,#08H
LOOP_SEND: RLC A
MOV SDA,C
SETB SCL
CLR SCL
DJNZ R2,LOOP_SEND
POP 02H
RET

;*****************************************************
;* READ *
;* OUTPUT: ACC *
;*****************************************************
LOOP_READ: PUSH 02H
MOV R2,#08H
LOOP_READ1: SETB SCL
MOV C,SDA
CLR SCL
RLC A
DJNZ R2,LOOP_READ1
MOV DAVAVA,A
POP 02H
RET

;*****************************************************
;* DELAYS *
;* *
;*****************************************************
DELAY4M: PUSH DPH
PUSH DPL
MOV DPTR,#0000H
DEL4: INC DPTR
MOV A,DPL
ORL A,DPH
JNZ DEL4
POP DPL
POP DPH
RET



;----------------------------------------me------------------
BCDTOASCII:
MOV B,A
SWAP A
ANL A,#0FH
ORL A,#30H
ACALL DATA_DISPLAY
MOV A,B
ANL A,#0FH
ORL A,#30H
ACALL DATA_DISPLAY
RET
ONETIME_LCDINIT:
MOV A,#38H
ACALL COMMAND
MOV A,#0EH
ACALL COMMAND
MOV A,#01H
ACALL COMMAND
MOV A,#06H
ACALL COMMAND
MOV A,#86H
ACALL COMMAND
RET
COMMAND: ACALL READY
MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
CLR P2.2
RET
DATA_DISPLAY:
ACALL READY
MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELAY
CLR P2.2
RET
READY: SETB P1.7
CLR P2.0
SETB P2.1
BACK: CLR P2.2
ACALL DELAY
SETB P2.2
JB P1.7,BACK
RET

DELAY_200M:
DELAY_FORLOOP:
MOV R7,#5H
L1_DELAY:
MOV TMOD,#10H
MOV TH0,#00ADH
MOV TL0,#00F8H
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R7,L1_DELAY
RET
DELAY: MOV R3,#50
HERE3: MOV R4,#255
HERE2: DJNZ R4,HERE2
DJNZ R3,HERE3
RET

DELAY1:
MOV R7,#250
D1: DJNZ R7,D1
RET

END
 

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
I am using a different program to write for the first time. The above program is for reading the RTC and to display on the LCD.
 

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
My clock using AT89S52 and DS1307 RTC and 16X2 LCD is working well. But I have a problem. When I switch off the power supply and switch on again the digits showing seconds starts counting from 0. For example, if I switch off at 4:16:24 and switch on again it shows 4:16:00!!!!!!!!!!. The Ds1307 chip stores and updates min data and hrs data while on backup battery but fails to store "sec" data. Why. I am using the above code (post reply dated 09-05-11 19:36 scroll above) to read the RTC chip. Please help
 

thunderboympm

Full Member level 5
Joined
Sep 17, 2007
Messages
244
Helped
32
Reputation
64
Reaction score
29
Trophy points
1,308
Location
Malappuram, India
Activity points
2,467
when an initialisation of rtc done, the time will be like this. do the setting to rtc first . Then in main program only reading want to be done.
Check this techniq
I guess the problem will be rectified.
 

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
Does that mean I have to arrange a battery backup for the whole clock(including a coin type 3V battery for DS1307)? In this case everything is working fine. Thanks.
 

papunblg

Advanced Member level 3
Joined
Oct 22, 2010
Messages
726
Helped
172
Reputation
344
Reaction score
165
Trophy points
1,343
Location
Kolkata India
Activity points
6,439
No I mean to say the 3v coin type battery will be attached to the RTC chip and a seperate 5V battery back up supply(from 6V 4Ah battery using 7805) for the microcontroller circuit. In this arrangement I am not facing the problem I mentioned above.
So when the AC mains is not available, the whole thing runs on battery. No power off no reset problem as mention by thunderboympm above
 
Last edited:

hafizimran18

Member level 4
Joined
Aug 10, 2012
Messages
69
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,779
please provide me code, compiler name, simulation on proteus 7.2 or earlier and complete circuit diagram
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top