DS1307

shamu

Member
Hi all.

I am trying to use a DS1307 RTC with an 18M2 picaxe.
I've read and re-read the data sheet and tutorial but must be missing something fundamental as my code appears not to work.
Here it is:

Code:
#picaxe 18M2
#no_data

symbol seconds = b0
symbol mins = b1
symbol hours = b2
symbol day = b3
symbol date = b4
symbol month = b5
symbol year = b6
symbol control = b7

HI2CSETUP i2cmaster, %11010000, i2cslow,i2cbyte

main:
  if pinb.2=1 then
 
    let seconds = $00
    let mins = $59
    let hours = $8
    let day = $3
    let date = $25
    let month = $12
    let year = $03
    let control = $0001000

    hi2cout 0,(seconds,mins,hours,day,date,month,year,control)

    high b.3
    pause 2000
    low b.3

endif 

goto main
This is my first foray into the world of the i2c bus and as yet I have no way of checking that the time is correct, what I hope to achieve is pin 7 of the DS1307 flashing an LED at 1Hz.
I'm sure that the program uploads and runs at least partially as pin b.3 goes high when the button connected to pin b.2 is pressed.
I must admit to being being slightly confussed by the different addresses and what must and must note be stored in BCD.

Any help would be much appreciated.
Thanks.
 
Last edited by a moderator:

nick12ab

Senior Member
It doesn't work because you put
Code:
 let control = $0001000
instead of
Code:
 let control = $00010000
 

shamu

Member
Hi.
I've tried that, putting $00010000 generates and error message saying maximum value allowed is 65,0000 ?
 

AllyCat

Senior Member
Hi,

You probably should have used % (binary) not $ (Hexadecimal), but I don't know that chip well enough to say if there should be 3 or 4 trailing 0s.

Cheers, Alan.
 

westaust55

Moderator
CONTROL REGISTER
The DS1307 control register is used to control the operation of the SQW/OUT pin.
Code:
BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
OUT   0    0  SQWE    0    0  RS1   RS0
%00010000 is fairly normal and will give a 1 Hz signal on the SQW pin .

In terms of actual operation, the value for the control register is fairly irrelevant unless you intend to use the square wave output.

The code as posted is only to set the DS1307. ideally instead of GOTO Main, a DO : LOOP rather than keep resetting the time is better.

A separate routine is required to read the time back from the DS1307.
 

shamu

Member
Hi all.

I have learnt some more about my problem with the DS1307.
Here goes, firstly the 18M2 has been changed to a 28M2, the DS1307 stores seconds as two separate 4 bit values, individual seconds, value 0-9 and tens of seconds value, 1-5.

Thus, nine seconds would be displayed as: 00001001 ten seconds would be displayed as: 00010000 because the left four bits represent the tens.

Is there a way of adding the two ‘Nibbles’ together to create one byte that would display ten as: 00001010.
Thanks.

#picaxe 28X2
#no_data

let dirsB = %11111111
'let dirsC = %00000000

symbol seconds = b0
symbol mins = b1
symbol hours = b2
symbol day = b3
symbol date = b4
symbol month = b5
symbol year = b6
symbol control = b7

HI2CSETUP i2cmaster, %11010000, i2cslow,i2cbyte
'The last bit of the a DS1307 is 0 for write and 1 for read.

main:

let seconds = %00000001
let mins = %00111011
let hours = %00001000
let day = %00000011
let date = %00011001
let month = %00001100
let year = %00000011
let control = %10010000

writei2c $0,(seconds,mins,hours,day,date,month,year,control)

HI2CSETUP i2cmaster, %11010001, i2cslow,i2cbyte

'This block reads the time.
do
readi2c $0,(seconds,mins,hours,day,date,month,year,control)
let outpinsB = seconds
loop

goto main
 

Attachments

hippy

Technical Support
Staff member
Thus, nine seconds would be displayed as: 00001001 ten seconds would be displayed as: 00010000 because the left four bits represent the tens.
That's correct, and this is known as 8-bit BCD representation ( binary coded decimal ).

Is there a way of adding the two ‘Nibbles’ together to create one byte that would display ten as: 00001010.
There are, and a number of solutions. The X2's have an easy to use function -

b0 = BcdToBin b0

For other PICAXE, or to not use the function, the most common way of doing this would be -

b1 = b0 / 16 * 10
b0 = b0 & 15 | b1

or, without an extra variable required -

b0 = b0 / 16 * $FFFA + b0

http://www.picaxeforum.co.uk/showthread.php?21286-Converting-BCD-byte-values-to-decimal
 

Captain Haddock

Senior Member
Heres another one to play with, needs C.4 changing for use on 18m2, no credit for me as I 'borrowed' it from the forum in the first place.
Code:
#picaxe 20x2
init:
	i2cslave %11010000, i2cslow, i2cbyte
	;writei2c 0, ($00, $21, $17, $04, $02, $05, $12, $00) ;set time
	serout C.4,N2400,(254,1)

main: readi2c 0, (b0,b1,b2,b3,b4,b5,b6)
	pause 100
	bcdtoascii b0,b19,b20		'Secs Convert to ASCII
	bcdtoascii b1,b8,b9		'Mins 
	bcdtoascii b2,b10,b11		'Hours
	bcdtoascii b3,b21,b22         'DayOfWeek;	
	bcdtoascii b4,b12,b13		'Date
	bcdtoascii b5,b14,b15		'Month
	bcdtoascii b6,b16,b17		'Year
	
	serout C.4,N2400,(254,132)
	serout C.4,N2400,(b10,b11, ":",b8,b9,":",b19,b20)
	serout C.4,N2400,(254,200)
	serout C.4,N2400,(b12,b13,"/",b14,b15,"/",b16,b17)
	If b22="1" then serout C.4,N2400,(254,192,"Sun ") 'convert Ascii 1-7 Day of Week number to Text
	elseif b22="2" then serout C.4,N2400,(254,192,"Mon ")
	elseif b22="3" then serout C.4,N2400,(254,192,"Tue ")
	elseif b22="4" then serout C.4,N2400,(254,192,"Wed ")
	elseif b22="5" then serout C.4,N2400,(254,192,"Thur")
	elseif b22="6" then serout C.4,N2400,(254,192,"Fri ")
	elseif b22="7" then serout C.4,N2400,(254,192,"Sat ")
	endIf
	
	pause 100
	goto main
 

shamu

Member
Hi all.
I'm still working on this DS1307 project and appear to have hit another problem.
The clock function seems to work correctly, returning the expected values, however when the 3V back-up battery is connected the whole thing freezes.
I'm slightly confussed about the two supply voltages, the picaxe is running at 4.5V but the back-up battery is only 3V surely this will cause some conflict, I've tried de-coupling caps across both chips but this made no difference.
Any advice would be greatly appreciated, thanks.
 

Attachments

westaust55

Moderator
Please check that the 3 Volt back up battery connections to the DS1307 and to ground are good.
If one of these connections is broken so there is no voltage to the DS1307 Bat pin the RTC will not function.
When there is no battery the Bat pin must be tied to ground. If the RTC is working with no battery, have you got a wire from the Bat pin to ground that needs to be removed?


I'm slightly confused about the two supply voltages, the picaxe is running at 4.5V but the back-up battery is only 3V surely this will cause some conflict
No there is no conflict. In fact the normal supply voltage must be higher than 1.25 time Vbatt.
It's all part of the voltage detection circuit. If the supply falls too low the battery back-up kicks in and internally the time is kept but there is no output.

What is the actual measured battery voltage?
What is the actual measured normal supply voltage?

If Vbat is 3 Volts and the normal supply falls below 3.75 Volts you cannot read the RTC data registers.

4.5 Volts is the minimum normal operating voltage for the DS1307. It works better at 5 V and a max of 5.5 Volts.
 

nick12ab

Senior Member
There isn't a pull-up resistor on the reset pin.

Although it might not be causing the problem and it's just luck that the PICAXE isn't constantly resetting, if wires for the 3V battery are connected near to the reset pin then capacitance could be causing the reset pin to become low only when the battery is present.
 

shamu

Member
As simple as that! Tried new batteries and the problem is solved, thanks for the help.

One more question, I believe the picaxe can work at upto 5V, how can this voltage be obtained from 1.5v cells.
Can a 5v regulator be used with say 6v?

Thanks again.
 

westaust55

Moderator
Great to see/read that the problem is resolved.

Some Low drop out voltage regulators such as the LP2590/LP2591 can operate with an input to output voltage differential as low as 0.38 Volts but are limited to 100 mA current ratings.
There are other low drop out voltage regulator types/parts if you care to search.

A conventional 78L05 (100mA) or 7805 (1.0 Amp – with heat sinking) linear voltage regulator required the input to output voltage differential to be around 2 volts so the input must be at least 7 Vdc and ideally the input voltage should be around 8 or 9 Vdc for better output voltage regulation tolerance over the rated current range.
 

shamu

Member
Hi all.

I'm current using:

b0 = BcdToBin b0

to convert values from a DS1307, is there an opostire command I can use to put BCD values into the DS1307 ?

Thanks.
 

westaust55

Moderator
For the X1 and X2 parts there is the BINTOBCD command. See PICAXE manual2 page 25.

Otherwise you need to create a small portion of program
;for b0 = 00 to 99
b1 = b0/10*16
b0 = b0//10+b1
 
Top