BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Jan 23, 2018 8:49 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: issue with DS1307 code or hardware?
PostPosted: Fri Jan 10, 2014 8:52 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Hi all,

hope someone can help me see the wood for the trees. I'm trying to decide whether i have an issue with my code or hardware. As i go forward on my code i make copy's and call them different version numbers.

I am pretty sure i had it working and was ok. So Ive gone back and strangely have the same issues with the others. Below is a basic version of the code that all it does is allow me to adjust and save the time on a DS1307. The clock runs fine if started at 00:00 for 24hrs. The problem is while trying to adjust the time. If i set the clock to 10min all is ok, the moment i set it to 16min, exit that menu via the set and re-enter to adjust the time again, its jumped to 22min, them 34min, then 52min then 82min and so on. When set to 22min and i return to the main display i have noticed, sometimes the seconds are above 60.
I hope someone will see an obvious error or try the code out so i can eliminate an fault with this Chinese made board. thanks

Code:
   PAUSE 1000
   lcdinit p0\p1\p7\p6\p5\p4,p2

Self_test:
   'displays self test lcd message first line
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr,twoline,"    v1.00 "]

'   ;*** Pin Assignments/Constants ***
   RegSel con P0   ; LCD R/S pin
   ClkPin con P1   ; LCD enable pin
   SCL con P11      ; DS1307 clock pin
   SDA con P12      ; DS1307 data pin
   Ctrl1307 con %11010000  ; I2C control value for DS1307
'
'   ;*** Variable Initialization ***
   secs var byte
   mins var sbyte
   hrs var byte  ; hours
   flag var byte
'
   CLEAR  ; set all variables to zero
   
standby_loop:   
   HIGH P3  ; Turn on LCD backlight
'
'   Enter initial DS1307 Time/Date values. 19:30 (7:30PM),
'
   secs = bin2bcd 00  ; might as well start with zero seconds
   mins = bin2bcd 10
   hrs = bin2bcd 00
   flag = $AA  ; no conversion with this
'
'   ; Write values to DS1307 with next line. Comment this out after programming DS1307 for first time
   'I2COUT SDA, SCL, ctrl1307,[0,secs,mins,hrs,flag]
   
MAIN_clock
   PAUSE 1000  ; Wait one second for DS1307 to start up
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear] 'clears display before main so clock does not flicker
   

   I2COUT SDA, SCL, ctrl1307, [0] ; set start register to 0
   I2CIN SDA, SCL, ctrl1307, [secs, mins, hrs]  ; "day" value is unused here
'
   ; Convert values from BCD back to normal
   secs = bcd2bin secs
   mins = bcd2bin mins
   hrs  = bcd2bin hrs

   ; print date/time to LCD
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [homelcd,scr,twoline, dec hrs\2, ":", dec mins\2, ":", dec secs\2,scrram+64,"press any button"]
      
   PAUSE 330 ; Read DS1307 three times a second
   
    worksw1 var byte
   worksw2 var byte
   worksw3 var byte

standby_button
   button p8,0,100,20,worksw1,1,setup_menu   'listens for button press sw1 and jumps to setup menu
   button p9,0,100,20,worksw2,1,setup_menu   'listens for button press sw2 and jumps to setup menu
   button p10,0,80,40,worksw3,1,setup_menu   'listens for button press sw3 and jumps to setup menu
   'goto standby_button   
GOTO MAIN_clock   
   
setup_menu
   'displays setup screen
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr,twoline,"      Setup Exit"] 'first line
   lcdwrite p0\p1\p7\p6\p5\p4,[cgram+0,0x00,0x04,0x04,0x04,0x15,0x0E,0x04,0x00,scrram+0]
   lcdwrite p0\p1\p7\p6\p5\p4,[scr,scrram+65,0,scrram+72,0,scrram+78,0]

setup_menu_btn
   button p9,0,100,20,worksw2,1,set_clock_time   'listens for button press sw3 and jumps to set clock time
   button p10,0,100,20,worksw3,1,main_clock   'listens for button press sw3 and jumps to standby loop
   goto setup_menu_btn
   
set_clock_time
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr,twoline,"Set clock time?"] 'first line
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [scrram+64,"      Next   Yes"]                'second line menu options

set_clock_time_btn
   button p9,0,100,20,worksw3,1,setup_menu      'listens for button press sw3 and jumps to setup menu
   button p10,0,100,20,worksw3,1,Time_adjust   'listens for button press sw3 and jumps to time adjust
   goto set_clock_time_btn   
   
Time_adjust
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr,twoline,scrram+5,     dec hrs\2, ":", dec mins\2     ] 'first line displays time
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [scrram+64, "Set   Up   Down"]    'second line menu options
   
clockadjust_button
   button p8,0,100,20,worksw1,1,clock_time_set       'listens for button press sw1 and jumps to setup menu
   button p9,0,80,40,worksw2,1,adjup             'listens for button press sw2 and moves clock digits up
   button p10,0,80,40,worksw3,1,adjdown         'listens for button press sw3 and moves clock digits down
   goto clockadjust_button   
   
adjup
  mins=mins+1
  IF mins>59 THEN
    hrs=hrs+1
    mins=0
  ENDIF
  IF hrs>23 THEN
    hrs=0
  ENDIF
  goto time_adjust

adjdown
  mins=mins-1
  IF mins<0 THEN
    mins=59
    hrs=hrs-1
  ENDIF
  IF hrs = 255 THEN
   hrs = 23
  ENDIF
 
goto time_adjust
   
clock_time_set   
;Convert values from BCD back to normal
   secs = bin2bcd secs
   mins = bin2bcd mins
   hrs = bin2bcd hrs
   
   'flag = $AA  ; no conversion with this   
   I2COUT SDA, SCL, ctrl1307,[0,secs,mins,hrs,flag]   
goto set_clock_time


Top
 Profile  
 
 Post subject: Re: issue with DS1307 code or hardware?
PostPosted: Sat Jan 11, 2014 6:20 am 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
If you set the seconds in binary = 16 when you change to hex it is 22. Why you use binary? try using 16 = hex like 0x10.
I think the register must be in hex. Try hex and see what you get.


Top
 Profile  
 
 Post subject: Re: issue with DS1307 code or hardware?
PostPosted: Sat Jan 11, 2014 12:52 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
i understand but i cant see where that is happening. i do the bcd2bin at the start and back at the end. Which works for the main display. thanks again


Top
 Profile  
 
 Post subject: Re: issue with DS1307 code or hardware?
PostPosted: Sat Jan 11, 2014 5:18 pm 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
Also remembered that the counter will keep counting. If you load 16 and by the time it takes to read it back the counter it is already incremented. I do not know if your are taking this in consideration.


Top
 Profile  
 
 Post subject: Re: issue with DS1307 code or hardware?
PostPosted: Sat Jan 11, 2014 5:22 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Please remember that im new to this, you may have to be a bit more specific. what counter? thanks


Top
 Profile  
 
 Post subject: Re: issue with DS1307 code or hardware?
PostPosted: Sat Jan 11, 2014 10:13 pm 
Offline
Master

Joined: Wed Oct 19, 2011 2:28 am
Posts: 123
Hi,
I do not see any problem. The only way you will learn it is by try and error.

What I mean is that your are working with a clock. If you load the clock register the clock will start counting from where your loaded. The only way that the 16 your are loading in the seconds will match it is by stopping the clock. If the clock it is running then the clock will keep counting from the value in the register. Try stopping the clock, load the 16 in the register for the seconds and read it back and it will match. "Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled"

By the way there it is an application topic number 6 " App Note: Using the DS1307 RTC IC with Atoms and Nanos EDIT!" Did you check it out. The person explained how to use and how to program the ds1307.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO