Checkout Registers

After wiring up the DS1337 RTC with its respective crystal to my Arduino, I needed to learn how to communicate with slave devices over I2C – which is a serial bus designed by Phillips to allow a master device (Arduino) to communicate with a multitude of slave devices with only two wires. The benefit of this protocol is the preservation of I/O pins, both analogue and digital, to communicate with up to 100+ devices while only occupying two pins on the master device, either the Arduino or other micro controller. Another added benefit to the I2C protocol is the ability to a) hot swap devices, and b) add in more components without having to re-wire or change the code already being used.

For this project I wanted to skip over the RTClib library which simplifies the use of the DS1337 and other similar RTC’s for two reasons: First, it saves space in the Arduino memory when compiled and uploaded; Secondly, it forces me to learn how to use the I2C protocol for future projects. The most beneficial aspect of not including the library is to force myself to learn. This way, if, and when, I start a project that uses an I2C device, I will know how to communicate without hoping I’ll find a library that has already been written for it.

My first step in learning how to communicate to the DS1337 was reading the Data Sheet – one of the most important pieces of information that comes with an electronics component. The Data Sheet tells you, literally, everything you need to know about that component. For the DS1337 Data Sheet, they have included a nice table that spells out which registers do what and what bits in that register refer to.

The second step in understanding the DS1337 and the I2C bus was finding some example code for the arduino that strictly uses the Wire.h library only – a library needed to communicate over I2C. This is where the Chronodot comes into play. The Chronodot is an RTC breakout for the big brother of the DS1337, the DS3231. The website for this board has some example code that, after studying for a while, got me through the basics of communicating with the DS1337.

To write data to a register, lets say the “Hours” on the DS1337, which is register 0x02, all you need to do is send the register number, either in hexidecimal or binary, to tell the DS1337 where to write data, and based on what features you want (12 hour or 24 hour), you send the 7 bit data which includes the “Hour”.


Wire.beginTransmission(0x68); //Begin transmission to device with address 0x68

Wire.write(0x02);  //Select register to write to

Wire.write(0b01101010); //write data to register

wire.endTransmission(); //end transmission

Line 1 addresses the device with address 0x68 – the RTC. Different devices will have different addresses.

Line 3 selects the register and line 5 writes the data to that register.

Line 7 ends the transmission.

From the data sheet, we know that register 0x02 will let us specify if we want 12 or 24 hour mode, if we choose 12 hour mode, we can tell it if we want to start with AM or PM, and then what hour to set.

Byte 6 will specify 12 or 24 hour. If we leave that byte as 0, the RTC will count the hours in 24 hour format, so we change that to a 1. Doing this, we also need to specify if we are writing the time as AM or PM, which byte 5 lets us do, 0 for AM and 1 for PM. Bytes 3, 2, and 1 allow us to specify the hour. In this example, we set the hour to 10, we send the binary for 10, which is 1010. All together the 7 bit binary looks like this: 01101010.

Once I played around with what I had learned so far, communicating over I2C has been a breeze. Making changes to the registers – either data or configuration settings is so much easier than I had anticipated. Learning this also will make it easier for me to have the Python program send either binary or hexadecimal data through to the Arduino to update the clock. This will be useful for the end user so I won’t have to program a time, 12/24 mode, etc. for anybody – they can configure it themselves.

The DS1337 also has an alarm configuration which I may implement to trigger an interrupt on the Arduino for periodical data storage – 1, 5, 10 minute intervals based on the accurate RTC and not the somewhat-accurate Arduino delay() function.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: