I2C using a DS1307 and a 20M2 chip

jackbec18

New Member
Hello, I'm using a LCD screen, which is connected to a DS1307 real time clock chip (version two of the LCD kit: http://www.picaxe.com/docs/axe033.pdf) and I'm having some difficultly in actually programming the circuit in order to have shown on the LCD screen an accurate time. I've been able to program the circuit in order to show simple texts such as hello for instance, as shown in the picaxe help manual; however I'm struggling to understand how to actually program and display the circuit to show an accurate time! I think it's to do with assigning a variable value to each specific quantity; i.e. B.0 could be a for the seconds value, B.1 could be for minutes, however I'm unsure as to how to assign each value to each quantity, and whether it must comply with any specific outputs etc?
Any help would be greatly appreciated
Thank you!
 

papaof2

Senior Member
I use a DS1307 with a 20X2 and a large LCD with unique line select commands. Just ignore the "upper" and "lower" commands. Here's the code:

Code:
#picaxe 20x2
#no_data
#no_table

setfreq m8

'serial LCD is large 4 line (from Ebay) using two 2-line display on same board. uses  4800-N-8-1 with jumper S3 removed (otherwise odd parity)

symbol UpperControl	= b8
b8 = 17
symbol LowerControl	= b9
b9 = 21
symbol UpperData		= b10
b10 = 18
symbol LowerData		= b11
b11 = 22

; Example of how to use DS1307 Time Clock (i2c device)
; Note the data is sent/received in BCD format.

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

	serout c.5, n4800, (LowerControl, 1)

	pause 100
	
	serout c.5, n4800, (UpperControl, 1)
	
	serout c.5, n4800, (UpperControl, 128, UpperData, "DS1307 ") 'start of first line	

' set DS1307 slave address
	i2cslave %11010000, i2cslow, i2cbyte


' uncomment next line to set or update the clock
' goto start_clock 


' read time and date and display on serial LCD module

main:
	readi2c 0,(seconds,mins,hour,day,date,month,year)
		
	serout c.5, n4800, (LowerControl, 128, LowerData, " ") 'start of first line	

	let temp = date & %00110000 / 16
	serout c.5, n4800,(#temp)
	let temp = date & %00001111
	serout c.5, n4800,(#temp,"/")

	let temp = month & %00010000 / 16
	serout c.5, n4800,(#temp)
	let temp = month & %00001111
	serout c.5, n4800,(#temp,"/")

	let temp = year & %11110000 / 16
	serout c.5, n4800,(#temp)
	let temp = year & %00001111
	serout c.5, n4800,(#temp," ")
	
serout c.5, n4800, (LowerControl, 192, LowerData, " ")

	let temp = hour & %00110000 / 16
	serout c.5, n4800,(#temp)
	let temp = hour & %00001111
	serout c.5, n4800,(#temp,":")

	let temp = mins & %01110000 / 16
	serout c.5, n4800,(#temp)
	let temp = mins & %00001111
	serout c.5, n4800,(#temp,":")

	let temp = seconds & %01110000 / 16
	serout c.5, n4800,(#temp)
	let temp = seconds & %00001111
	serout c.5, n4800,(#temp)

	pause 1000
	goto main



'write time and date e.g. to 16:18:00 on Sun 10 Mar 2013
start_clock:
	let seconds = $00	' 00 Note all BCD format
	let mins    = $38	' 18 Note all BCD format
	let hour    = $16	' 16 Note all BCD format
	let day     = $04	' 01 Note all BCD format
	let date    = $27	' 10 Note all BCD format
	let month   = $11	' 03 Note all BCD format
	let year    = $13	' 13 Note all BCD format
 	let control = %00010000 ' Enable output at 1Hz

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

goto main
	end
This code has been running for months, so it appears to be bulletproof ;-)
 

jackbec18

New Member
Ahh that makes sense now, when I'd tried to do it I'd not assigned the variable correctly, and I think I'd used the wrong command!
Thank you for your help!
 
Top