BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Jul 17, 2018 6:42 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: trying to set the clock on a ds1307 and a nano28
PostPosted: Sat Nov 02, 2013 3:26 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
hi again, playing around with a nano28 a ds1307 and 3 switches. Im trying to set the clock time via an up and down switch by cutting and pasting snippets of code from other programs and trying to understand why they function as they do. Ive had some success with the moving the time up part but the time adjust down has me confused so i need some help please. When the clock gets to 00:00 and i press one more time it goes to down to 00:255, why? what errors can you see? will doing this adjustment set the time in the main clock? Please forgive all the apostrophed comments its were im trying snippets to see what changes they make. The intention is to make an alarm clock so any advise or sample code would be great thanks in advance.

Code:
'dS1307 RTC clock program
'
'   ;*** A brief pause to allow the ATOM and LCD to initialize after power up ***
   PAUSE 1000
'
'   ;*** 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 byte
   hrs var byte  ; hours
   flag var byte
'
   CLEAR  ; set all variables to zero
   
   lcdinit p0\p1\p7\p6\p5\p4,p2   ' Initialize LCD
   
   Lcdwrite p0\p1\p7\p6\p5\p4,p2, [lcdclear,homelcd,scr,twoline,"  v1.00 "]
   
   
   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 30
   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]
'
   PAUSE 1000  ; Wait one second for DS1307 to start up
'   
'   ; MAIN loop
MAIN
   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

'clockadjustloop

   worksw1 var byte
   worksw2 var byte
   worksw3 var byte

Time_adjust
   'displays time adjust screen
   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
   'reads & adjusts rtc

clockadjust_button
   'button p8,0,100,20,worksw1,1,set_clock_time   '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
     'hrs=23
  ENDIF
   
'IF hrs<0 THEN
    'hrs=23
IF hrs = 255 THEN
   hrs = 23
IF mins = 255 THEN
   mins = 59   
     'mins=59
  'ENDIF
  endif
  endif

'IF hrs > 23 THEN hrs = 0

'IF mins > 59 THEN mins = 0

'adjup
'mins = mins -1
'if mins = 60 then
' mins = 0
'hrs = hrs + 1
'if hrs = 13 then
' hrs = 1
'if hrs = 24 then
' hrs = 0
'endif
'endif
'endif

'PAUSE 330 ; Read DS1307 three times a second
   goto time_adjust
   'GOTO MAIN
   
'let hrs = hrs + 1
'if hrs == 13 then
'let hrs = 1
'endif

'let min = min + 1
'if min == 60 then
'let min = 0
'endif

'IF hrs = 255 THEN hrs = 23
'IF hrs > 23 THEN hrs = 0
'IF mins = 255 THEN mins = 59
'IF mins > 59 THEN mins = 0


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Mon Nov 04, 2013 2:41 am 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
HI,

Well looking at the code for the adjdown routine you can follow the flow using the debugger mode of the Basic Micro IDE and if you set mins to 0 and then enter the routine, mins is an unsigned byte variable so in the first line of the routine you say mins=mins-1 so mins starts at 0 and now its been decremented and becomes 255 as its an 8 bit(byte) variable so on the next line where you say IF mins<0 THEN this will never get called as mins is now 255 and thats greater then 0 with an unsigned number,

Code:
adjdown
mins=mins-1
IF mins<0 THEN
mins=59
hrs=hrs-1
'hrs=23
ENDIF


To get around this you can either define these variables as SIGNED bytes using the SBYTE rather than BYTE in the variable set up routine, if you do that then when you take mins being a zero and then subtract 1 you will get -127 and so the test for mins<0 will now work fine, if the signed variable will not cause you any other problems its a simple fix for this problem,

The other way to do it if you want to keep the variables as BYTE type is to PRE TEST the value in the adjdown routine,

First you check mins BEFORE you decrement,

Code:
adjdown
IF mins=0 THEN
mins=59
hrs=hrs-1                         ; Pre Test hrs here BEFORE subtracting
else
mins-mins-1                         
endif


You may want to pre check hours here BEFORE you decrement it and then set that to 11 or 23 and handling any AM/PM flags etc depending on the clock type your using,

I hope this helps,

Many thanks
Dave

BasicMicroUK
http://www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Mon Nov 04, 2013 2:40 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Thanks, used the Sbyte option and it worked great. Now have to work out how to insert the adjusted time into the main clock. I can see the time changing up or down but when I return to the main program the clock is on the original time and not the adjusted. Thanks again


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Mon Nov 04, 2013 3:57 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
HI Again

YES once you have set up your local hrs and mins via the clock set routines you will have to write them back to the DS1307 RTC BEFORE you return to the main program as in Main the program it reads the RTC IC and updates them back to the original values,

You could have a SET button that takes the current hrs and mins and outputs them to the RTC IC using the line below and then returns to the main program,


Code:
; 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]


If the new values remain then you will know the RTC IC accepted the data OK, if the values are not stored then you have a problem with the write data to DS1307 routine,

I hope this helps,

Many thanks
Dave

BasicMicroUK
http://www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Tue Nov 05, 2013 12:35 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Hi again that worked great. Its what i thought, just was having one of those days and looked past cutting and pasting that line away from the main clock and putting it further down. The issue is now when time is adjusted and set to eg: 10.30, when you return to the main clock screen it displays 10.24. The high the set time the more loss or 10.00 and it goes back to 9.58. Any ideas? thanks again
Code:
'basicmicro nano28 on breadboard

'wiring
'p0      lcd RS
'p1      lcd enable
'p2      lcd r/w
'p3      BL on/off
'p4      lcd data 4
'p5      lcd data 5
'p6      lcd data 6
'p7      lcd data 7
'p8      switch 1 left
'p9      switch 2 centre
'p10   switch 3 right
'p11   serial clock for rtc DS1307 & AT24C32
'p12   serial data in from rtc DS1307 & AT24C32
'p13   serial data for external DS18B20 serial temp probe in cooler
'p14   stepper motor1
'p15   stepper motor2
'p16   stepper motor3
'p17   stepper motor4


'initialize lcd display
   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 "]


standby_loop:
'   ;*** 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
   
   HIGH P3  ; Turn on LCD backlight
'
'   ;  Enter initial DS1307 Time/Date values. 19:30 (7:30PM), 5 April  2010 was used for example
'
   secs = bin2bcd 00  ; might as well start with zero seconds
   mins = bin2bcd 30
   hrs = bin2bcd 19
   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]
'
   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
'   
MAIN_clock
   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 1000

   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
PAUSE 660 ; Read DS1307 three times a second   
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,standby_loop   'listens for button press sw3 and jumps to standby loop
   goto setup_menu_btn

set_clock_time
   'displays set clock screen
   'reads rtc & displays 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
   'displays time adjust screen
   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
   'reads & adjusts rtc

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
'   ; 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]
goto set_clock_time
   


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Thu Nov 07, 2013 4:21 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
Hi, on playing around ive noticed that if i set the clock to 15.00 then exit it stays at 15.00, but if set to 16.00 and exit it reverts back to 10.00. hope this helps someone. thanks


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Fri Nov 08, 2013 10:53 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
Hi Again,

Well this is a simple one and its just down to the way the clock works,

When you read the clock you are reading all the raw values for hours, mins etc but these in are in Binary coded decimal format and your then converting them from BCD into Decimal to use in the program and that all works fine BUT when your writing the values back to the clock IC you are writing it back as decimal and NOT BCD that the clock needs hence the change in the munbers so you need to add code to convert decimal to BCD BEFORE the writeclock command in the routine to correct the values like this :-


Code:
 bcdsecs = bin2bcd secs  ; might as well start with zero seconds
   bcdmins = bin2bcd mins
   bcdhrs = bin2bcd hrs
   flag = $AA  ; no conversion with this
   I2COUT SDA, SCL, ctrl1307,[0,bcdsecs,bcdmins,bcdhrs,flag]


You will see that to save confusion I have set up a BCD version of each variable and so you will need to define these in the program at the start where the original hrs, mins and secs are being set-up, to do this for example for the bcdsecs variable you would add "bcdsecs var byte" to the program and do the same for the others bcdmins and bcdhrs,

I hope this helps get you up and running,

Many thanks
Dave

BasicMicroUK
http://www.basicmicro.co.uk


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Mon Nov 11, 2013 12:59 pm 
Offline
Guru

Joined: Sat Mar 30, 2013 10:19 pm
Posts: 51
great that makes sense and has fixed it. i have got it working with the code below and not adding the extra vars
.
Code:
clock_time_set
'Convert values from normal back to BCD
   secs = bin2bcd secs
   mins = bin2bcd mins
   hrs = bin2bcd hrs

'   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]
goto set_clock_time


my question now is, what is the reason behind you making a bcdsecs var byte at the beginning as it works for me without this. Am i going to get into problems later by just using the original secs, mins, and hrs var bytes? thanks


Top
 Profile  
 
 Post subject: Re: trying to set the clock on a ds1307 and a nano28
PostPosted: Mon Nov 11, 2013 2:12 pm 
Offline
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
Hi,

The reason for using a dedicated variable was that if you were going to still use the original hrs, mins etc in the main program and try and print them out they would no longer be in DECIMAL as your program expects so it would not work again and would show the wrong values BUT if after writing to the clock you always go back and read the clock again it will work as after you have read the clock you convert the BCD into DECIMAL,

Its just good practise to keep the number bases separate so I would always use the BCD versions of them for reading and writing to the RTC IC and the decimal versions for working with within the program and then you would never get them mixed up and the program would be clear to read in 6 months time when you maybe come to make some changes,

I hope this helps,

Many thanks,

Dave
BasicMicroUK

http://www.basicmicro.co.uk


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 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