DS1307 & i2c

shamu

Member
Hi all.

Has PE6 changed the way it deals with i2c code?
I thought I had it cracked last year but have just come to use some code I wrote back then and now it won't work!
I have changed to 'Read' & 'Write' commands to HI2COUT & HI2CIN but to no avail.

Thanks in advance.






Code:
#picaxe 40X2
#no_data
#no_table

let dirsB = %11111111

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, %1101000, i2cslow,i2cbyte
'The last bit of the DS1307 is 0 for write and 1 for read.

' Test the B port.

let outpinsB=%11111111
pause 500
let outpinsB=%00000000

main:
 
  let seconds = %00010010
  let mins = %01011001
  let hours = %00100011
  let day = %00000111 'Day of week. 1-7.
  
  let date = %00110000 'Day of month. 1-31.
  
  let month = %00010001 ' Month N?. 1-12.

  let year = %00010011
  let control = %10010000
   
  'writei2c $0,(seconds,mins,hours,day,date,month,year,control)
  HI2COUT $0,(seconds,mins,hours,day,date,month,year,control)
     
  'This block reads the time.
  do
    HI2CIN $0,(seconds,mins,hours,day,date,month,year,control)
    
    seconds = BcdToBin seconds
    mins = BcdToBin mins
    hours = BcdToBin hours
    day = BcdToBin day
    date = BcdToBin date
    month = BcdToBin month
        
    let outpinsB = seconds
		         
  loop

goto main
 

hippy

Technical Support
Staff member
HI2CSETUP i2cmaster, %1101000, i2cslow,i2cbyte
I believe you are lacking a zero on the right of your I2C Device Address, you only have seven binary digits, should normally be eight for PICAXE ...

%11010000 = $D0
 

shamu

Member
Simple as that, thanks!
Though I must say the literature that comes with the DS1307 kind of says that the 8th digit is kind of optional!

I have another problem though now, when I include the line:
setfreq em16
to activate a 16Mhz resonator the clock runs very slow.
Does the setfreq command affect the i2c bus speed?

Thanks.
 

inglewoodpete

Senior Member
I think you may be a little confused with using an external resonator.

First, which PICAXE chip are you using? If it is a 28X2 or 40X2, these chips have a phase-locked-loop which multiplies the resonator frequency by 4, resulting in the PICAXE running at 64MHz! Have a close read of the command descriptions for Setfreq and hi2csetup for the changes you need to make when you increase the clock speed.
 

Technical

Technical Support
Staff member
With the 40X2 you are using a 16MHz external will give 64MHz operation, so you require a 'setfreq em64' command.

Then you also need

HI2CSETUP i2cmaster, %11010000, i2cslow_64, i2cbyte
 

hippy

Technical Support
Staff member
The operating speed of the PICAXE should not affect the DS1307 time keeping.

My only guess is you could be writing to the DS1307, resetting it to a time in the past and thus it appears to be running slow. Perhaps post the code you are using.
 

westaust55

Moderator
Hi.
The code is over 300 lines long in total, is it feasiable to post something that long?
As an attachment in .bas file format is should be possible.

There is a 10,000 character limit for information/test within a single post.

Maybe first clarify the hardware you have - 40X2 with 4 MHz resonator for operation at 16 MHz or a 16 MHz resonator for operation at 64 MHz.
 

nbw

Senior Member
Hmm, I've just tried this code on my 40x2 (b.1 firmware).

Even though I don't have the setfreq em64 command in my code, the code seems to pass (that's what it says in the console window), but an error message is displayed over the code:

Firmware >= B3 Setfreq em64 not supported by this firmware version

Puzzled??!!?
 

hippy

Technical Support
Staff member
Firmware B.1 indicates a "40X2-5V" PICAXE which has a more limited range of operating frequencies than the later universal voltage "40X2" chips ( firmware B.3 ).

The compilers will accept the code, pass Syntax Check and Simulate, because it is valid for latest chips but identifies that you are attempting to do something the chip cannot do on downloaded.

Basically, you can't use a 16MHz resonator and EM64 with an older 40X2-5V.
 

nbw

Senior Member
oh dammit. That might also put paid to my cunning plan of using a 40x2 with 16Mhz resonator to direct-read a DHT11 humidity sensor without any external ICs (555 etc).
thanks for the info though!

cheers
Barney
 

nbw

Senior Member
Odd that I don't have the setfreq em64 line in the code, and hi2cxxx uses ic2slow, not i2cslow16/64 ... but I still get the error. That would be the 16MHz resonator connected, yes?
 

hippy

Technical Support
Staff member
Not sure what is going on if you aren't specifying a SETFREQ EM64. The compiler would seem to have noted something which requires a 40X2 rather than a 40X2-5V. Perhaps post your code so we can test it and see what results we get.
 

nbw

Senior Member
OK, that's weird. I shut down the Picaxe editor and re-started, now I don't get the error. Here's the code I was using, in any event:
Code:
''-- set DS1307 time for orchid controller
#picaxe 40x2
symbol seconds = b0
symbol mins = b1
symbol hours = b2
symbol day = b3
symbol date = b4
symbol month = b5
symbol year = b6
symbol control = b7

pause 2000
'set the 40x2 as master
HI2CSETUP i2cmaster, %11010000, i2cslow, i2cbyte

pause 2000
'set time etc here
  let seconds = $33
  let mins = $22
  let hours = $11
  let day = $1 'Day of week. 1-7.
  let date = $17 'Day of month. 1-31.
  let month = $4 ' Month N?. 1-12.
  let year = $16
  let control = %10010000
pause 500

    'write it to the DS1307
    ' make sure there is a Bat in! or set Vbat to ground
  HI2COUT 0,(seconds,mins,hours,day,date,month,year,b7)

test_loop:
seconds = BcdToBin seconds
    mins = BcdToBin mins
    hours = BcdToBin hours
   day = BcdToBin day
    date = BcdToBin date
    month = BcdToBin month
    year = Bcdtobin year
    
    debug
    pause 500
    goto test_loop
 
Last edited by a moderator:

nbw

Senior Member
Oh, - there was one (HI2CIN $0,(seconds,mins,hours,day,date,month,year)) in there; not sure how it fell out.. I'll blame late night c'n'paste........
 
Top