Wednesday, May 18, 2016

Interfacing PIC16F84A with DS1307 real time clock


This topic shows how to interface DS1307 real time clock chip with PIC16F84A microcontroller, how to write data to the DS1307 RTC and how to read data from the DS1307 RTC.
Related topics:
Real time clock using PIC16F877A microcontroller and DS1307 serial RTC
Real time clock with PIC18F4550 and DS1307 RTC
The DS1307 is an 8-pin integrated circuit uses I2C communication protocol to communicate with master device which is in our case the PIC16F84A microcontroller. This small chip can count seconds, minutes, hours, day, date, month and year with leap-year up to year 2100.
The DS1307 receives and transfers data (clock data and calendar data) as BCD format, so after receiving data we have to convert these data into decimal data, and before writing data to the DS1307 we have to convert this data from decimal to BCD format. For example we have the BCD number 33, converting this number into decimal gives 21.
The following image shows the DS1307 pin configurations:

DS1307 real time clock RTC pin configuration PIC16F84A
And the following circuit is typical operating circuit for the DS1307 RTC:
DS1307 real time clock RTC circuit PIC16F84A
A 3V battery can be connected between VBAT and GND as a backup supply input.
The DS1307 uses an external 32.768KHz crystal and there is no need to add any resistors or capacitors with it.
More informations are in the DS1307 RTC datasheet.
Interfacing PIC16F84A with DS1307 RTC:
The following image shows the interfacing circuit schematic diagram.

Interfacing PIC16F84A with DS1307 real time clock RTC circuit CCS PIC C
The circuit is clear and simple. The LCD is used to display the time as well as the date.
Interfacing PIC16F84A with DS1307 RTC CCS C code:
The following C code is the full code of this project.

software I2C is used at 100KHz because PIC16F84A does not have hardware I2C module.
First the microcontroller writes clock 18:35:00 and date 17/05/16 and after writing the time and date the microcontroller starts and endless loop (while(TRUE)) for reading time and date from DS1307.
If you want to understand the code read the DS1307 datasheet.
// Interfacing PIC16F84A with DS1307 real time clock CCS PIC C compiler code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections

#include <16F84A.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay(clock = 8000000)
#include <lcd.c>
#use I2C(master, sda=PIN_A1, scl=PIN_A0, FAST=100000)

char time[] = "TIME:  :  :  ";
char calendar[] = "DATE:  /  /20  ";
unsigned int8 second, second10, minute, minute10,
               hour, hour10, date, date10, month, month10,
               year, year10, day;
void ds1307_display(){
  second10  =  (second & 0x70) >> 4;
  second = second & 0x0F;
  minute10  =  (minute & 0x70) >> 4;
  minute = minute & 0x0F;
  hour10  =  (hour & 0x30) >> 4;
  hour = hour & 0x0F;
  date10  =  (date & 0x30) >> 4;
  date = date & 0x0F;
  month10  =  (month & 0x10) >> 4;
  month = month & 0x0F;
  year10  =  (year & 0xF0) >> 4;
  year = year & 0x0F;
  time[12]  = second  + 48;
  time[11]  = second10  + 48;
  time[9]  = minute  + 48;
  time[8]  = minute10  + 48;
  time[6]  = hour  + 48;
  time[5]  = hour10  + 48;
  calendar[14]  = year  + 48;
  calendar[13]  = year10  + 48;
  calendar[9]  = month + 48;
  calendar[8]  = month10 + 48;
  calendar[6]  = date + 48;
  calendar[5]  = date10 + 48;
  lcd_gotoxy(1, 1);                     // Go to column 1 row 1
  printf(lcd_putc, time);               // Display time
  lcd_gotoxy(1, 2);                     // Go to column 1 row 2
  printf(lcd_putc, calendar);           // Display calendar
}
void ds1307_write(unsigned int8 address, data_){
  i2c_start();                          // Start I2C
  i2c_write(0xD0);                      // DS1307 address
  i2c_write(address);                   // Send register address
  i2c_write(data_);                     // Write data to the selected register
  i2c_stop();                           // Stop I2C
}
void main(){
  lcd_init();                           // Initialize LCD module
  lcd_putc('\f');                       // LCD clear
  ds1307_write(1, 0x35);                // Write minutes
  ds1307_write(2, 0x18);                // Write hour
  ds1307_write(4, 0x17);                // Write date
  ds1307_write(5, 0x05);                // Write month
  ds1307_write(6, 0x16);                // Write year
  ds1307_write(0, 0);                   //Reset seconds and start oscillator
  while(TRUE){
   i2c_start();                         // Start I2C
   i2c_write(0xD0);                     // DS1307 address
   i2c_write(0);                        // Send register address
   i2c_start();                         // Restart I2C
   i2c_write(0xD1);                     // Initialize data read
   second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();                          // Stop I2C
   ds1307_display();                    // Diaplay results
   delay_ms(50);                        // Wait 50ms
  }
}


Interfacing PIC16F84A with DS1307 RTC video:
The following video shows a hardware circuit of this project with some details about the circuit.