DS1307 RTC
Posted: Tue Jul 05, 2011 9:04 pm
DS 1307 Real Time Clock Tutorial
The new double summer issue (2011) of “Elektor “ as always has a good selection of small interesting circuits.
One that caught my eye was for a real time clock that can be linked to a Microcontroller.
A couple of IC’s were ordered along with the relevant crystal, and the it was time to study the data sheet.
The datasheet , like most datasheets can be somewhat cryptic, so a search of the Matrix forums for more information and ideas was begun.
A lot of information was found to be available, but somewhat spread over many postings.
This tutorial is the consolidated results of my finding.
Hardware needed :
EB006 Multi-programmer - Standard configuration with a PIC 16F877A
EB005 LCD Display
EB016 Protoboard
DS1307 RTC
Crystal 32.768 kHz - For the RTC
Jumper Wires
10k-ohm resistors (2)
100nF Capacitor
CR2032 Lithium Battery & Holder
I will present this tutorial in 2 parts. Programming the data, and reading the data back.
PART 1 Programming the RTC
The flowchart posted by DAN81 is the initial basis of my experiments, as I found this program worked correctly and after some study I understood what it was trying to do.
This is a link to Dan81’s original
http://www.matrixmultimedia.com/mmforum ... rtc#p18528
This is a link to the DS1307 Data Sheet
http://www.maxim-ic.com/datasheet/index.mvp/id/2688
A similar circuit to the elektor one can be found in the data sheet.
The DS1307RTC operates using the I2C protocol.
I2C Bus Connection.
DS 1307 Pin 5 = SDA To PIC PORT C bit 4
DS 1307 Pin 6 = SCL T0 PIC PORT C bit 3
Note both these lines need a pull up resistor connected to 5V
Battery Backup.
A 3V lithium button cell is connected between Pin 3 V-Bat and ground
If battery backup is not needed Pin 3 V-Bat must be connected to ground.
Flowcode Programming.
The I2C protocol is built into Flowcode, so the following will show how to setup the device to program with the current time and date.
The calculation box in the flowchart
RTC = 0x68 Base address
RTC_W = ( RTC << 1 ) + 0 This adds the direction bit to set to write. = 0xD0
This sets up the variables. The numbers can be changed to suit.
Hour = 0x19
Minute = 0x25
Second = 0x00
Day = 0x03
Date = 0x05
Month = 0x07
Year = 0x11
This sets up the day letter shown on the display. These changed automatically once the clock passes through midnight.
Weekday[1] = 'S'
Weekday[2] = 'M'
Weekday[3] = 'T'
Weekday[4] = 'W'
Weekday[5] = 'T'
Weekday[6] = 'F'
Weekday[7] = 'S'
The days can be moved round so Monday is day 1 if preferred, all the DS1307 sees is the number. The letter is added by the PIC program.
First The I2C component needs to be added to the form.
The first macro needs to be MI2C Init - This is placed in MAIN
This will initialise the I2C bus.
Place this early on near the start of the flowchart before any loops etc.
The rest of the commands are in the Setup_Clock macro.
First we have to set up the section of the flowchart that will perform the actual write operations.
The MI2C Start Needs to be the first macro in the list.
The address is calculated as follow
The 7bit base address of the DS1307 is
Binary = 1101000
Hex = 0x68
This needs to be transmitted followed by the direction bit (0 for write 1 for read)
This makes the overall address
Binary = 11010000
Hex = 0xD0
MI2C Transmit Byte This sets the address of the DS1307 and puts it in to write mode
The program uses the variable already setup RTC_W, though 0xD0 could also be used.
MI2C Transmit Byte This sets the first timekeeper register
Data(Byte) = 0
We then enter each parameter in the order sec/min/hour/day number/date/month/year
By using the following method.
MI2C Transmit Byte
Data(Byte) = seconds
MI2C Transmit Byte
Data(Byte) = minutes
And so on as once each parameter has been programmed the next one is selected.
The final parameter being the year value
MI2C Transmit Byte
Data(Byte) = year
Finally we send the stop command
MI2C Stop
This will program the clock with the following
Time = 19:25:00
Date = T 05:07:11
It is possible to program the device to use 12 hour clock and AM/PM indicators. This is slightly more advanced could be looked at later.
Please comment if points are not clear and I will try to improve on it.
Note no backup battery as the CR2032 holder has not arrived yet
PART 2 Will cover reading the data back from the RTC
The new double summer issue (2011) of “Elektor “ as always has a good selection of small interesting circuits.
One that caught my eye was for a real time clock that can be linked to a Microcontroller.
A couple of IC’s were ordered along with the relevant crystal, and the it was time to study the data sheet.
The datasheet , like most datasheets can be somewhat cryptic, so a search of the Matrix forums for more information and ideas was begun.
A lot of information was found to be available, but somewhat spread over many postings.
This tutorial is the consolidated results of my finding.
Hardware needed :
EB006 Multi-programmer - Standard configuration with a PIC 16F877A
EB005 LCD Display
EB016 Protoboard
DS1307 RTC
Crystal 32.768 kHz - For the RTC
Jumper Wires
10k-ohm resistors (2)
100nF Capacitor
CR2032 Lithium Battery & Holder
I will present this tutorial in 2 parts. Programming the data, and reading the data back.
PART 1 Programming the RTC
The flowchart posted by DAN81 is the initial basis of my experiments, as I found this program worked correctly and after some study I understood what it was trying to do.
This is a link to Dan81’s original
http://www.matrixmultimedia.com/mmforum ... rtc#p18528
This is a link to the DS1307 Data Sheet
http://www.maxim-ic.com/datasheet/index.mvp/id/2688
A similar circuit to the elektor one can be found in the data sheet.
The DS1307RTC operates using the I2C protocol.
I2C Bus Connection.
DS 1307 Pin 5 = SDA To PIC PORT C bit 4
DS 1307 Pin 6 = SCL T0 PIC PORT C bit 3
Note both these lines need a pull up resistor connected to 5V
Battery Backup.
A 3V lithium button cell is connected between Pin 3 V-Bat and ground
If battery backup is not needed Pin 3 V-Bat must be connected to ground.
Flowcode Programming.
The I2C protocol is built into Flowcode, so the following will show how to setup the device to program with the current time and date.
The calculation box in the flowchart
RTC = 0x68 Base address
RTC_W = ( RTC << 1 ) + 0 This adds the direction bit to set to write. = 0xD0
This sets up the variables. The numbers can be changed to suit.
Hour = 0x19
Minute = 0x25
Second = 0x00
Day = 0x03
Date = 0x05
Month = 0x07
Year = 0x11
This sets up the day letter shown on the display. These changed automatically once the clock passes through midnight.
Weekday[1] = 'S'
Weekday[2] = 'M'
Weekday[3] = 'T'
Weekday[4] = 'W'
Weekday[5] = 'T'
Weekday[6] = 'F'
Weekday[7] = 'S'
The days can be moved round so Monday is day 1 if preferred, all the DS1307 sees is the number. The letter is added by the PIC program.
First The I2C component needs to be added to the form.
The first macro needs to be MI2C Init - This is placed in MAIN
This will initialise the I2C bus.
Place this early on near the start of the flowchart before any loops etc.
The rest of the commands are in the Setup_Clock macro.
First we have to set up the section of the flowchart that will perform the actual write operations.
The MI2C Start Needs to be the first macro in the list.
The address is calculated as follow
The 7bit base address of the DS1307 is
Binary = 1101000
Hex = 0x68
This needs to be transmitted followed by the direction bit (0 for write 1 for read)
This makes the overall address
Binary = 11010000
Hex = 0xD0
MI2C Transmit Byte This sets the address of the DS1307 and puts it in to write mode
The program uses the variable already setup RTC_W, though 0xD0 could also be used.
MI2C Transmit Byte This sets the first timekeeper register
Data(Byte) = 0
We then enter each parameter in the order sec/min/hour/day number/date/month/year
By using the following method.
MI2C Transmit Byte
Data(Byte) = seconds
MI2C Transmit Byte
Data(Byte) = minutes
And so on as once each parameter has been programmed the next one is selected.
The final parameter being the year value
MI2C Transmit Byte
Data(Byte) = year
Finally we send the stop command
MI2C Stop
This will program the clock with the following
Time = 19:25:00
Date = T 05:07:11
It is possible to program the device to use 12 hour clock and AM/PM indicators. This is slightly more advanced could be looked at later.
Please comment if points are not clear and I will try to improve on it.
Note no backup battery as the CR2032 holder has not arrived yet
PART 2 Will cover reading the data back from the RTC