I2C Examples
Steven Tron
Posts: 13
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
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
Reset seconds:
Read back seconds:
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
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
·
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
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
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