General area when it fits no where else

Moderator: Mmiscool

User avatar
By PhilTilson
#66669 The documentation on I2C functions is sparse, to say the least! I am interfacing a DS1307 chip with the ESP12 and can successfully read the time from the RTC. However, it's a day out and, whatever I try, I cannot seem to write to the chip.

Previous experience with other microcontrollers and I2C suggests that I first need to do an I2C.begin with the address of the RTC (&H68, or 104), then do a write to the byte that I want to modify - I2C.write(4) which is the date field, and finally the value I want in that field - I2C.write(3) and an I2C.end command. The question is, do I need an I2C.end between the two writes? Do I need to do another I2C.begin between the writes, with or without the I2C.end?

This is part of the code I have been trying:

Code: Select alladdress = 104

i2c.setup(13,12)   'set I2C pins on ESP
i2c.begin(address)   'start I2C conversation
i2c.write(4)   'point to the date address location
'i2c.end()   'finish this conversation
'i2c.begin(address)   'start another conversation
i2c.write(3)   'the value to go in the date field
i2c.end   'finish this conversation

but whatever combination of commenting in or out of the two lines in the middle, the value never gets changed.

I know the basic I2C connection is working because I can read the time and date from the RTC with no problems. Where am I going wrong? :(
User avatar
By CiR
#66679 I think you want to get rid of the end and begin in the middle of the conversation.
According to the data sheet, the first byte after the start bit is treated as the register address, so your just selecting address 4, stopping, then selecting register 3 without setting anything in either.
User avatar
By heckler
#66683 Hi Phil,

Here is working code that is written in PICBASIC for the PIC microcontrollers.
You may be able to learn something from how I did it in PICBASIC for the ds1307

SDA Var PORTB.4
SCL Var PORTB.6
Ctrl con %11010000
Adrs con $00
'---------------------------------------------------
' Set initial time by manually entering the time values below
RTCYear = $14 '%00010100
RTCMonth = $01 '%00000001
RTCDate = $09 '%00001001
RTCDay = $16 '%00000110
RTCHour = $10 '%00010010
RTCMin = $58 '%00011001
RTCSec = $00
RTCCtrl = $00
'*******************************************************
' Subroutine to read time from RTC
gettime:
I2CRead SDA, SCL, Ctrl, Adrs, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return

' Subroutine to write time to RTC
settime:
I2CWrite SDA, SCL, Ctrl, Adrs, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
Return
'*******************************************************


I have not yet tried to connect the ds1307 I2C clock to my esp module but I want to. If I get a chance I will and report back here.

Hope this helps
dwight
User avatar
By PhilTilson
#66710
I think you want to get rid of the end and begin in the middle of the conversation.

I quite agree! However, it fails to work with or without either or both of those lines! :(

Here is working code that is written in PICBASIC for the PIC microcontrollers.

Appreciate the help, but I, too, have got it working with a PIC! As you see from your code, the I2C commands are much more structured here which makes life easy. I have not come across any ESPbasic examples that seek to write I2C except this from TrackerJ:

Code: Select all     [I2C_Write]
     print "Writing to I2C address " & i2c_address

     stwl = len(stw)
     print "String : " & stw
     print "String Lenght : " & stwl

     adrh = eeprom_address >> 8
     adrl = eeprom_address and 255

     i2c.begin(i2c_address)
     i2c.write(adrh)
     i2c.write(adrl)

     i = 1
     do
         stru = mid(stw,i,1)
         print  "Value to Write: " & asc(stru)
         i2c.write(asc(stru))
         i = i + 1
     loop while i < stwl+1
     i = 0
     i2c.end()
     print "Write complete"
    wait


but here he is writing to an EEPROM with 10-bit addressing, and describes the code as "just a proof of concept", so I am not sure if he ever actually tested it.

Seems I am stuck here unless anyone else has managed to write time and date to a DS1307 from an ESP?

Phil