Mark A. - Thank you for the excellent response. I am going to leave the i2cinit, after rest ok but maybe it is cleanup in another place if reused, makes me think of it.
Here is my final code(I hope) for this test exercise. Worked first time. Better when you understand it.... :) Thanks all who contributed.
Next step will be to sync the RTC to the SNTP and when the time is off by a definable parameter read from the SD, it will correct the RTCs time. That way I am not writing to the RTC as often. Say a correction will occur if >3 seconds off. That will be a different thread. I did see some SNTP strangeness during testing. Need to revisit, validate and create a test case. Function locked in the SNTP call during testing, Ethernet cable was unplugged.
Sorry for the formatting of the comments, not a fixed width font here... :(
I realize in the main loop I could have just done a print of Date$ and Time$ but wanted to try some other functions. In my app I will only use the system vars and the ISR will signal to the main process to read the RTC value only in that place Reading form SRAM is faster than the RTC's I2C.
[code:1:74d6f0688f]
'-------------------------------------------------------------------------------
' DS1307.BAS
' Shows how to use the ds1307 clock on the STK500/STK501. A timer is configured
' at a 1 second interval. The ISR will set a flag to allow the DS1307's
' getdatetime routine to be executed.
'
' With a 14.7456MHZ clock it takes ~965uS to get the date/time.
'
' Mark L. Marlette 01/29/2014
' www.cloud9tech.com
'-------------------------------------------------------------------------------
$regfile = "m128def.dat"
$hwstack = 80
$swstack = 80
$framesize = 160
$crystal = 14745600 ' External Xtal, full swing mode
$baud = 115200 ' force Local_SetBaudRate routine to set proper baud rate
'$programmer=9 ' STK500 extended
'$prog $ff,$ce,$c9,$ff ' Lock Bits, Fuse Bits Low, Fuse Bits High, Fuse Bits Extended
'
Const False = 0
Const True = 1
'
'Declare Subroutines / Functions
Declare Sub GetDateTime
Declare Sub SetDate
Declare Sub SetTime
'
$lib "i2c_twi.lbx" ' using hardware I2C/TWI
Config Sda = Portd.1 ' define pin names, scl and sda pins
Config Scl = Portd.0
Config Twi = 100000 ' CONFIG TWI will ENABLE the TWI master interface, 100KHZ
i2cinit ' initialize the I2C bus
Config Date = mdy , Separator = / ' configure the date format
config clock = user ' define USER clock, it dims date / time vars
' The USER must have the calls of settime, setdate, getdatetime
' to talk to the USER's hardware
'address of ds1307
Const Ds1307w = &HD0 ' Addresses of Ds1307 clock
Const Ds1307r = &HD1
'
'-----[ Output Timer ISR for Oscilloscope Check on Porta.1 ]-----------------
'Config PortA.0 = output ' used for scop to capture time to get date/time
Config Porta.1 = Output '
Porta.1 = 1 ' LED off, active lo.
TickBit Alias Porta.1
'-----[ Setup Timer1 for Tick ISR off system XTAL]--------------------------------
' Timer1 is used for Tick counter at 1Hz
Config Timer1 = Timer , Prescale = 1024 , Compare A = Disconnect , Clear Timer = 1
' crytal / prescale / x Hz = Compare1a val needed
' 16000000 / 1024 / 1(Hz) = 15625
' 14745600 / 1024 / 1(Hz) = 14400
Compare1a = 14400
On Compare1a T1_TickCounter_ISR ' Define ISR Entry Point
Enable Timer1
Enable Compare1a
'-----[Serial Port(s)]--------------------------------------------------------
Config Com1 = 115200, Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "com1:" For Binary As #1
Waitms 5
'
Enable Interrupts ' Using a timer to generate 1 second ISR interval
'dim other needed variables
Dim Weekday As Byte
Dim lngTick as long ' one second tick timer counter
Dim booRTCupdate as boolean ' Flag to allow event to occur
Dim strDate As String * 8
Dim strTime As String * 8
Print #1, "DS1307"
'
' Clear Vars
'
lngTick = 0 ' Reset already, but can't help it......
booRTCupdate = false
'
Do ' Main Loop
if booRTCupdate = true then
booRTCupdate = false
call GetDateTime ' read from RTC
strTime = Time(_sec) ' use system vars to create time string
strDate = Date(_day) ' use system vars to create date string
Print #1, "Date & Time : " ; strDate ; " & " ; strTime
endif
Loop
'------[ Timer ISR routine ]----------------------------------------------------
T1_TickCounter_ISR:
Incr lngTick ' inc event counter
Toggle TickBit ' allow visual on scope
booRTCupdate = true ' every tick, allow DS1307 RTC to read date/time
Return
'------------------------------------------------------------------------------
'called from CONFIG CLOCK = USER / DATE$/TIME$
Getdatetime:
'set PORTA.0
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Ds1307r ' send address
I2crbyte _sec , Ack
I2crbyte _min , Ack ' MINUTES
I2crbyte _hour , Ack ' Hours
I2crbyte Weekday , Ack ' Day of Week
I2crbyte _day , Ack ' Day of Month
I2crbyte _month , Ack ' Month of Year
I2crbyte _year , Nack ' Year
I2cstop
_sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
_day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
'reset PORTA.0
Return
Setdate:
_day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 4 ' starting address in 1307
I2cwbyte _day ' Send Data to SECONDS
I2cwbyte _month ' MINUTES
I2cwbyte _year ' Hours
I2cstop
Return
Settime:
_sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
I2cstart ' Generate start code
I2cwbyte Ds1307w ' send address
I2cwbyte 0 ' starting address in 1307
I2cwbyte _sec ' Send Data to SECONDS
I2cwbyte _min ' MINUTES
I2cwbyte _hour ' Hours
I2cstop
Return
[/code:1:74d6f0688f]
↧