Saturday, March 14, 2015

Interfacing DS1307 Real Time Clock With PIC16F84A Microcontroller


Real time clock using ds1307 and pic16f84a microcontroller

The PIC16F84A does not have any built-in I2C module, so we are going to use a very useful library on our lover compiler which is MikroC PRO For PIC. This library is called : Software I2C Library and it can be used with any MCU. For more details about this library visit the following page: Software I2C Library. (Thanks to mikroElektronika
Circuit:
The circuit schematic diagram is shown below:
interfacing pic16f84 with ds1307 rtc mikroc

The I2C box is not real chip, it used just to run the Proteus simulation.
Picture from my camera:
soon ...
Interfacing DS1307 Real Time Clock With PIC16F84A mikroC Code:
 The code is written using MicroC PRO For PIC compiler.

// Interfacing DS1307 Real Time Clock with PIC16F84A
// Used microcontroller: PIC16F84A @ 12MHz
// Written by: BENCHEROUDA Okba
// http://www.elecnote.blogspot.com
// electronnote@gmail.com
// Use at your own risk

 // LCD module connections
 sbit LCD_RS at RB5_bit;
 sbit LCD_EN at RB4_bit;
 sbit LCD_D4 at RB3_bit;
 sbit LCD_D5 at RB2_bit;
 sbit LCD_D6 at RB1_bit;
 sbit LCD_D7 at RB0_bit;
 sbit LCD_RS_Direction at TRISB5_bit;
 sbit LCD_EN_Direction at TRISB4_bit;
 sbit LCD_D4_Direction at TRISB3_bit;
 sbit LCD_D5_Direction at TRISB2_bit;
 sbit LCD_D6_Direction at TRISB1_bit;
 sbit LCD_D7_Direction at TRISB0_bit;
 // End LCD module connections
 // Software I2C connections
 sbit Soft_I2C_Scl           at RA2_bit;
 sbit Soft_I2C_Sda           at RA3_bit;
 sbit Soft_I2C_Scl_Direction at TRISA2_bit;
 sbit Soft_I2C_Sda_Direction at TRISA3_bit;
 // End Software I2C connections
 unsigned char  second, second10, minute, minute10,
               hour, hour10, date, date10, month, month10,
               year, year10, day ;
 void display(){
  //Split data into two parts
  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;
  //Display Time
  Lcd_Chr(1, 11, second + 48);
  Lcd_Chr(1, 10, second10 + 48);
  Lcd_Chr(1, 8, minute + 48);
  Lcd_Chr(1, 7, minute10 + 48);
  Lcd_Chr(1, 5, hour + 48);
  Lcd_Chr(1, 4, hour10 + 48);
  //Display calendar
  Lcd_Chr(2, 5, date + 48);
  Lcd_Chr(2, 4, date10 + 48);
  Lcd_Chr(2, 8, month + 48);
  Lcd_Chr(2, 7, month10 + 48);
  Lcd_Chr(2, 13, year + 48);
  Lcd_Chr(2, 12, year10 + 48);
  }
 void write_value(char address, char data_){
   Soft_I2C_Start();        // Start I2C signal
   Soft_I2C_Write(0xD0);    // DS1307 address
   Soft_I2C_Write(address);
   Soft_I2C_Write(data_);
   Soft_I2C_Stop();     }
 void main() {
 Lcd_Init();                // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
 Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off
 Soft_I2C_Init();
 write_value(1, 0x18);    //Write minutes
 write_value(2, 0x21);    //Write hours
 write_value(4, 0x14);    //Write date
 write_value(5, 0x03);    //Write month
 write_value(6, 0x15);    //Write year
 write_value(7, 0x10);    //Set SQW output at 1Hz
 write_value(0, 0);   //Reset seconds and start oscillator
 Lcd_Out(1, 6, ":  :");
 Lcd_Out(2, 6, "/  /20");
 while(1){
 Soft_I2C_Start();         // Start I2C signal
 Soft_I2C_Write(0xD0);     // DS1307 address
 Soft_I2C_Write(0);        //Start from reg0
 Soft_I2C_Start();
 Soft_I2C_Write(0xD1);   // DS1307 read data address
  second = Soft_I2C_Read(1);   // Read seconds
  minute = Soft_I2C_Read(1);   // Read minutes
  hour = Soft_I2C_Read(1);     // Read hours
  day = Soft_I2C_Read(1);      // Read day
  date = Soft_I2C_Read(1);     // Read date
  month = Soft_I2C_Read(1);    // Read month
  year = Soft_I2C_Read(0);     // Read year
  Soft_I2C_Stop();             // Stop I2C signal
  display();            //Display time on LCD
 }
} // End of program
  // Free RAM 23%
  // Free ROM 14%

Interfacing DS1307 Real Time Clock With PIC16F84A video: