The DS1307 is a real time clock capable to provide information of date and time. In this article we will see how to interface this chip with a 8051 based MCU and get Date Time information out of this chip and into our MCU which can be sent for display later on.
To communicate with DS1307 we need to use the I2C protocol. The memory layout of this chip is explained in detail in its datasheet and so I will omit that part here. Some important have to kept in mind beforehand though:
1. The data read from the DS1307 will be in Packed BCD format.
2. We need to write a time (hr:min:sec) and date (day:date:month:year) before we start reading. As per the memory layout we need to write from location 0x00 to 0x06 (please refer the data sheet).
3. Optionally a square wave output (at specified frequencies) can be obtained from Pin 7 of the chip, and to do so we need to set the control register located at 0x07.
Steps to Write Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode which is '0'. Hence send the byte 0xD0.
3. Send the starting address from where the data will be written in the DS1307. In our case we will send the address 0x00, since we want to write all the information of date & time.
4. Next we send the bytes for sec, min, hour, day, date, month, year one after the other. These bytes has to be converted into packed BCD, otherwise erroneous result might be obtained. After each byte write, the address gets incremented by 1, and so we need not write the next address every time. Only the above sequence has to be maintained. After writing each byte we need to get the acknowledgement from the DS1307 to verify that the data transmission was a success or not. SUCCESS = 0
5. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
6. Provide some delay.
After doing the above steps, the RTC DS1307 will be up and running, and now if we read it we will get actual date time data.
Steps to Read Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode first which is '0', since we have to write the address first and then provide the read mode option. Hence send the byte 0xD0 first.
3. Send the starting address from where the data will be read from the DS1307. In our case we will send the address 0x00, since we want to read all the information of date & time.
4. Generate another I2C start condition.
5. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for read mode which is '1'. Hence send the byte 0xD1.
6. Now read the data byte by byte. The data will be sent from address 0x00 to 0x06. After reception of each byte, an acknowledgement has to be send to the DS1307. ACK = 0 for one clock pulse.
7. After reading the last byte a master non-acknowledgement has to be sent to denote end of read operation. ACK = 1 for one pulse.
8. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
9. Provide some delay.
To communicate with DS1307 we need to use the I2C protocol. The memory layout of this chip is explained in detail in its datasheet and so I will omit that part here. Some important have to kept in mind beforehand though:
1. The data read from the DS1307 will be in Packed BCD format.
2. We need to write a time (hr:min:sec) and date (day:date:month:year) before we start reading. As per the memory layout we need to write from location 0x00 to 0x06 (please refer the data sheet).
3. Optionally a square wave output (at specified frequencies) can be obtained from Pin 7 of the chip, and to do so we need to set the control register located at 0x07.
Steps to Write Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode which is '0'. Hence send the byte 0xD0.
3. Send the starting address from where the data will be written in the DS1307. In our case we will send the address 0x00, since we want to write all the information of date & time.
4. Next we send the bytes for sec, min, hour, day, date, month, year one after the other. These bytes has to be converted into packed BCD, otherwise erroneous result might be obtained. After each byte write, the address gets incremented by 1, and so we need not write the next address every time. Only the above sequence has to be maintained. After writing each byte we need to get the acknowledgement from the DS1307 to verify that the data transmission was a success or not. SUCCESS = 0
5. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
6. Provide some delay.
After doing the above steps, the RTC DS1307 will be up and running, and now if we read it we will get actual date time data.
Steps to Read Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode first which is '0', since we have to write the address first and then provide the read mode option. Hence send the byte 0xD0 first.
3. Send the starting address from where the data will be read from the DS1307. In our case we will send the address 0x00, since we want to read all the information of date & time.
4. Generate another I2C start condition.
5. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for read mode which is '1'. Hence send the byte 0xD1.
6. Now read the data byte by byte. The data will be sent from address 0x00 to 0x06. After reception of each byte, an acknowledgement has to be send to the DS1307. ACK = 0 for one clock pulse.
7. After reading the last byte a master non-acknowledgement has to be sent to denote end of read operation. ACK = 1 for one pulse.
8. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
9. Provide some delay.
No comments:
Post a Comment