RTC DS 1307 Interfacing with PIC Microcontroller

///RTC DS 1307 Interfacing with PIC Microcontroller
  • RTC DS 1307 Interfacing with PIC Microcontroller

RTC DS 1307 Interfacing with PIC Microcontroller

A real-time clock (RTC ) works like a watch. They are usually included in a computer motherboard. RTC has registers which store the system description or setup values including the current time values stored by the real-time clock. A user can write into these registers for configuring the RTC. This chapter deals with the of an RTC DS 1307 interfacing with PIC microcontroller.

RTC

RTCs are widely used in embedded systems which uses controllers without a built-in RTC module. It can count leap years and knows how many days are there in a month. RTCs can be configured in both 12 hr and 24 hr mode. The day register is incremented at midnight.

The processor communicates with an externally interfaced RTC through simple serial communication protocols. DS1307 RTC is interfaced with the microcontroller using I2C interface. I2C is a serial communication protocol developed by Philips and is widely used in embedded systems because of its features which makes it simple.

Since DS1307 communicates with the microcontroller using I2C protocol it is necessary to understand the protocol in brief

I2C

I2C is a synchronous serial communication protocol which uses two lines for communication with the microcontroller. SDA line is used for transferring data and SCK is used for transferring clock information. Every device connected to an I2C bus has a unique address. I2C communication protocol involves communication between a slave and a master. The device which initiates the communication and which provides the clock is referred to as a master device. The devices which receive the clock signal and receive/transmit data according to the clock signal is termed as a slave device. Each device on the bus is accessed using its slave address.

Let’s have a look on the communication process in an I2C bus. First, the MCU will issue a START condition. The devices connected to the bus will listen to the START condition and will stay ready to begin the communication process. Then MCU will send the address of the device with which it needs to communicate. Master indicates the action to be performed with the device whether to read or write along with the address. All devices connected to the bus will receive the address and will compare it with its own address. If the addresses match with each other, the device will send back an ACKNOWLEDGEMENT signal to the master device. If they don’t match they will simply wait for the bus to be released with a STOP condition.

Once the MCU sends the address and the corresponding device acknowledges, the MCU can start transmitting or receiving data. When the data transmission or reception is complete, the MCU will stop communicating by sending a STOP condition. STOP condition indicates that the bus is released and it can be used by any other master (if any) connected to the I2C bus.

After a master generates a start condition I2C bus will solely belong to it. The bus will be freed only if the master generates a STOP condition. Any other master connected to the bus can access the bus after a STOP is identified on the bus. If the master device which uses the bus needs to communicate with a different slave it should generate a RESTART. Instead, if it tries to stop current communication and then start again it may lose access to the bus. RESTART is nothing but a start signal without a stop in the bus.

RTC Interfacing Firmware

The RTC registers are located in the address locations 00h to 07h inside DS1307. The RAM registers are located in address locations from 08h to 3Fh.

rtc-interfacing-firmware

The above table shows the different registers and its addresses. The time and calendar are set or initialized by writing to the appropriate registers.The contents of the calendar and time registers are in the BCD format so that all the data should be written in the BCD format. The seven-bit slave address for the DS1307 RTC is 1101000. Therefore 0xD0 is transmitted to access the RTC in write mode and 0xD1 is transmitted to access the slave in read mode. The least significant bit will describe the operation to be performed whether to write into the RTC or to read from RTC.

Procedure for Writing Time and Date

  • Initiate a START condition.
  • Transmit the RTC address 0xD0 with LSB 0 for write mode.
  • Transmit the address of the register to write(for example 0x00 for write to the second register). This transfer sets the register pointer inside the DS1307 RTC
  • Transmit the second’s value in BCD format. Following the procedure of writing to the second’s register, the pointer in the RTC will automatically increment. The next data will be written in the 0x01 location which is minute register.
  • Transmit until year register is written.
  • Terminate the communication with a STOP condition.

writing-time-and-date-in-rtc

Procedure for Reading the Time and Date

  • Initiate a START condition
  • Transmit RTC address with LSB 1 to access the RTC in read mode.
  • Now RTC will transmit data from its registers. It should be taken care that it will output data in the register whose address is currently stored in the pointer. If a user needs to read a particular register, first the RTC should be accessed in write mode and the desired register address should be written to a pointer. Now again RTC should be addressed in read mode after a RESTART or a STOP followed by a START.
  • Each receiving byte should be acknowledged by the master to receive the next byte.
  • After receiving the last byte master should non-acknowledge the RTC.
  • Terminate communication with a STOP condition in the bus.

reading-time-and-date-in-rtc

Writing a time and date example function,

Reading the current date and time example function,

RTC DS 1307 interfacing with PIC microcontroller

RTC DS 1307 interfacing with PIC microcontroller

RTC DS1307 interfacing circuit

I2C protocol allows multiple slave devices and master devices communicate with each other and it is used for short distance communications. It uses two signal wires to communicate with each other, SDA or data line and SCL or clock line.

SDA and SCL lines are connected to the corresponding pins in the microcontroller. I2C bus drivers are an open drain, that means they can’t drive corresponding signals high. This avoids bus contention when more than two devices communicate with each other and each device is trying a different logic level, thus avoiding a chance to damage drivers. The signal lines have pull-up resistors to restore the signal state high when no device is using the line.

DS1307 uses a 32.768 kHz quartz crystal as the oscillator. The pins X1 and X2 is used as the oscillator pins. There is a VBAT pin for connecting the battery. The battery will provide backup in the absence of power. A CR2032 battery will provide around 7 years backup.

 

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this

2 Comments

  1. Santosh June 13, 2018 at 11:34 am

    required code DS1307 display on LCD

Leave A Comment

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
close-link
25% OFF - OpenLabPro Course
Use Coupon: CRS25UL

 
PIC Microcontroller Primer
Want to learn indepth programming, peripheral interfacing and driver development?
close-link
Launched: IoT online Course - 25% Off when you complete your order today.
Checkout Now
close-image

Subscribe To our Email Newsletter

Be the first to get latest updates and exclusive content straight to your email inbox.
Send me Tutorials and Guides
close-link