Shop OBEX P1 Docs P2 Docs Learn Events
I2C Examples — Parallax Forums

I2C Examples

Steven TronSteven Tron Posts: 13
edited 2004-08-25 17:17 in General Discussion
Hi,

I'm new to the Javlin and to java. I am at the moment trying to write a simple program to interface to DS1307 RTC device using the I2C class·with little success.

The program attempts to just set the clocks seconds register

··· ioBus.start();
··· ioBus.write(208);
··· ioBus.write(8);
··· ioBus.write(5);
··· ioBus.stop();

Then read the seconds back.

··· ioBus.start();
··· ioBus.write(208); // Reset reg pointer;
··· ioBus.write(8);
··· ioBus.write(209); // Clock ID with write bit unset;
··· ioBus.read(nak);
··· ioBus.stop();

When debuging I positive acks back from the DS1307 for each write but when reading the seconds register I get 255 each time.

Regards

Steve

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-08-23 12:18
    Why are you using 8 as the seconds register address? -- it should be 0. On you read, you need to generate a second start after setting the address. Try this:

    Reset seconds:

    ioBus.start(); 
    ioBus.write(0xD0); 
    ioBus.write(0); 
    ioBus.write(0); 
    ioBus.stop();
    


    Read back seconds:

    ioBus.start(); 
    ioBus.write(0xD0); // Reset reg pointer; 
    ioBus.write(0); 
    ioBus.start(); 
    ioBus.write(0xD1); // Clock ID with write bit unset; 
    ioBus.read(nak); 
    ioBus.stop();
    


    I haven't written Javelin code for the DS1307, but you can find a BS2 sample in the BASIC Stamp forum.· You can find it in this thread:

    http://forums.parallax.com/showthread.php?p=466264

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-08-23 15:40
    You will find a complete DS1307 class here:
    http://groups.yahoo.com/group/javelinstamp/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/rtc/

    together with date/time conversion classes.

    The DS1307 class does require my I2Cdevice classes here
    http://groups.yahoo.com/group/javelinstamp/files/Javelin%20Stamp%20IDE/lib/stamp/protocol/

    The purpose of the I2Cdevice class is to not bother with start/stop
    sequences on the mainline code. So far it worked with all I2C devices
    I ever used. All it requires is a declaration line in your main class.


    regards peter

    ·
  • Steven TronSteven Tron Posts: 13
    edited 2004-08-23 19:32
    I did use 0 as the register address to begin with but when I did'nt get the results I was expecting·I re-read the device data sheet and thought I had to use the RAM addresses. But I guess not being from a electronics background making mistakes is all part of the learning curve. smile.gif

    Thanks for the pointers to where I was going wrong Jon its much appreciated, Peter your class looks great, but I think I need to read through it a couple of times to fully understand it. Being relativley new to Java as well as electronics looks like that curve just got steeper

    Thanks Guys

    Regards

    Steve
  • Steven TronSteven Tron Posts: 13
    edited 2004-08-24 16:28
    Hi,

    Well here is my first attempt at a class for the DS1307 RTC chip. I'm new to both the Javlin and to Java so please feel free to suggest improvments that might make the code smaller and more efficent.

    The class is about 704 Bytes in size excluding other library classes and has the following methods:

    setclock(hour,min,sec)·sets the time
    setdate(days,month,year)·sets the date, year does not inc century ie yy
    setday(day) sets day of the week 1=sun, 2=mon, etc
    getreg(reg) get the contents of DS1307 reg see data sheet for IC
    gettime() returns current time in hh[noparse]:mm:[/noparse]ss format
    getdate() returns date in dd/mm/yyyy format
    getday() returns day of the week in Sun, Mon etc format

    All input var's are int
  • Steven TronSteven Tron Posts: 13
    edited 2004-08-25 17:17
    Hi All,

    For anyone that needs it here is an improved version of my simple DS1307 class. Rather than define and use an I2C bus object internally. You now can pass a pointer to an I2C bus object. So this class should be more efficient if you are utilising multiple I2C devices as it allows a single instance of a I2C bus object to be used.

    the constructor method is DS1307(I2C) where I2C is a variable pointing to an I2C bus object

    As before if anyone would like to suggest any improvements that would be great.

    Regards

    Steve
Sign In or Register to comment.