RTC Interfacing with 8051


RTC-DS1307 Interfacing with 8051

The Real-time clock DS1307 IC basically is stand-alone time clock with following features.
·         Real-time clock (RTC) counts seconds, minutes, hours, date of the month, month, the day of the week, and year with leap-year compensation valid up to 2100.
·         The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.
·         56-byte, battery-backed, non-volatile (NV) RAM for data storage
·         Two-wire(I2C) serial interface
·         Programmable squarewave output signal
·         Automatic power-fail detect and switch circuitry
·         Consumes less than 500nA in battery backup mode with oscillator running
·         Optional industrial temperature range: -40°C to +85°C

DS1307 Pins

Below image shows the pin diagram and the recommended connections for DS1307.



VCC, GND: These pins are used to provide the power to the chip. When 5V is applied within normal limits, the device is fully accessible and data can be written and read. When a 3V battery is connected to the device and VCC is below 1.25 x VBAT, reads and writes are inhibited. However, the timekeeping function continues unaffected by the lower input voltage. As VCC falls below VBAT the RAM and timekeeper are switched over to the external power supply (nominal 3.0V DC) at VBAT.
X1-X2:Pins to connect the external 32.768kHz oscillator that provides the clock source to the chip.
Vbat: A 3.3v Lithium battery can be connected to this pin in order to provide the power source when the external supply voltage is not available. Battery voltage must be held between 2.0V and 3.5V for proper operation.
SCL: This pin must be connected to SCL pin of the I2C Bus/Master.
SDA: This pin must be connected to SDA pin of the I2C Bus/Master.
SQW/OUT: When enabled, the SQWE bit set to 1, the SQW/OUT pin outputs one of four square wave frequencies (1Hz, 4kHz, 8kHz, 32kHz).
·         Note: The SCL,SDA, and SQW are open drain and must be pulled up with appropriate pull-up resistors as shown in the image.

DS1307 Memory

The RTC keeps the date and time arranged in it's memory as shown below:



Control Register
7
6
5
4
3
2
1
0
OUT
0
0
SQWE
0
0
RS1
RS0
OUT: This bit controls the output level of the SQW/OUT pin when the square-wave output is disabled. If SQWE = 0, the logic level on the SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0. By default this pin will be 0.
SQWE:This bit, when set to logic 1, enables the oscillator output. The frequency of the square-wave output depends on the value of the RS0 and RS1 bits. 
RS1-RS0:These bits control the frequency of the square-wave output when the squarewave output has been enabled.

DS1307 ID

Ds1307 ID
7
6
5
4
3
2
1
0
1
1
0
1
0
0
0
R/W
·         R/W is 0 then data is Written to RTC
·         R/W is 1 then data is Read from RTC

Steps to initialize DS1307
1.     Start the I2C communication.
2.     Send the DS1307 address and select write operation
3.     Send the Address of Control Register for sending the command.
4.     Send the Command to Disable the SQW Out.
5.     Stop the Communication.

Steps to Write Date and Time


1.     Start the I2C communication.
2.     Send the DS1307 address and select write operation
3.     Send the Address of SECOND Register for writing the second value.
4.     Write the Sec,min,hour,weekDay,date,month,year one by one.
5.     Stop the Communication.


Steps to Read Date and Time

1.     Start the I2C communication.
2.     Send the DS1307 address and select write operation
3.     Send the Address of SECOND Register for reading the second value.
4.     Stop the Communication.
5.     Send the DS1307 address and select Read operation
6.     Read the Sec,min,hour,weekDay,date and month one by one and send positive acknowledgement.
7.     Read the Year and send the Neg/No Acknowledgement.
8.     Stop the Communication.
Note: The date and time read from Ds1307 will be of BCD format, like:
·         0x12,0x39,0x26 for 12hr,39min and 26sec.
·         0x15,0x08,0x47 for 15th day,8th month and 47th year
#include "rtc.h"
#include "lcd.h"
int main()
{
    rtc_t rtc;
    /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
    LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
    LCD_Init(2,16);
      
    /*Connect SCL->P0.6, SDA->P0.7*/   
    RTC_Init();
    rtc.hour = 0x10; //  10:40:20 am
    rtc.min =  0x40;
    rtc.sec =  0x00;
    rtc.date = 0x01; //1st Jan 2016
    rtc.month = 0x01;
    rtc.year = 0x16;
    rtc.weekDay = 5; // Friday: 5th day of week considering monday as first day.
    /*##### Set the time and Date only once. Once the Time and Date is set, comment these lines
         and reflash the code. Else the time will be set every time the controller is reset*/
    RTC_SetDateTime(&rtc);  //  10:40:20 am, 1st Jan 2016
    /* Display the Time and Date continuously */
    while(1)
    {
        RTC_GetDateTime(&rtc);
        LCD_GoToLine(0);
        LCD_Printf("time:%2x:%2x:%2x  \nDate:%2x/%2x/%2x",(uint16_t)rtc.hour,(uint16_t)rtc.min,(uint16_t)rtc.sec,(uint16_t)rtc.date,(uint16_t)rtc.month,(uint16_t)rtc.year);
    }
    return (0);
}


Comments

Popular posts from this blog

8051 INTERRUPT

Serial Communication With 8051 Microcontroller