Monday, 23 July 2018

Interface a RTC with PIC Microcontroller (PIC18F4520) using I2C Protocol

Aim:

The Real time clock DS1307 IC basically is stand alone time clock. Well, basically we can use a micrcontroller to keep time, but the value would go off as soon as it is powered off.
ds1307-rtc-chip-photo
The RTC DS1307 is a handy solution to keep time all the way, when it is powered by a coin cell.

Description:

Real Time Clocks, as the name suggests are clock modules. They are available as integrated circuits (ICs) and manages timing like a clock. Some RTC ICs also manages date like a calendar. The main advantage is that they have a system of battery backup which keeps the clock/ca lender running even in case of power failure. A very small current is required for keeping the RTC alive. This in most case is provided by a miniature 3V lithium coin cell. So even if the embedded system with RTC is powered off the RTC module is up and running by the backup cell. This same technique is used in PC timing also. If you have opened your computer case you will notice a small coin cell in the mother board. The DS1307 Serial Real-Time Clock is a low-power; full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially via a 2-wire, bi-directional bus(TWI Communication Protocol). The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.

In this project, we will learn How to interface a DS1307 Real Time Clock(RTC) with PIC18F4520 microcontroller and LCD Display. Here, we will read time(Minute and Second) from the DS1307 RTC and we will display the minute and second values in a 16X2 alphanumeric LCD. The data communication between DS1307 RTC and PIC18F4520 microcontroller takes place using TWI(Two Wire Interface) communication protocol. But before reading the DS1307 RTC, the DS1307 RTC needs to be initialized with second and minute values. The DS1307 RTC is initialized only once. We will initialize both the minute and second values of the DS1307 RTC with 0(Clock will start with 0 minute and 0 second). You can change the initialization values according to your need by changing the initialization values in the DS1307 write section of the code. After initialization of DS1307 RTC, the PIC18F4520 microcontroller will read the minute and second values from the DS1307 continuously through two wire interface communication and it will display those minute and second values in the 16X2 alphanumeric LCD.

Block Diagram

rtc

Schematic


Code

// ****************************************************** 
// Project: RTC interfacinig using PIC18F4520 
// Author: Hack Projects India 
// Module description: Operate DS1307 RTC IC
// ******************************************************

#include <p18f4520.h>
#include <delays.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
unsigned char rs,en,rw;
unsigned char s1[10],s2[10],s3[10];
unsigned char p3,q3,r3,p4,q4,r4;
#define LCDWriteStringXY(x,y,msg) {\
 LCDGotoXY(x,y);\
 LCDWriteString(msg);\
}

#define LCDWriteIntXY(x,y,val,fl) {\
 LCDGotoXY(x,y);\
 LCDWriteInt(val,fl);\
}

#define lcd PORTD

void delay(unsigned short long x)
{
while(x>0)
{
x--;
}
return;
}
void LCDCmd(unsigned char z)
{
  rs=0x00;//for command
  rw=0x00;
  en=0x08;//en high
  lcd=((z)&0xf0)|en|rs;
  delay(100);
  en=0x00;//en low
  lcd=((z)&0xf0)|en|rs;
  en=0x08;//en high
  lcd=((z<<4)&0xf0)|en|rs;
  delay(100);
  en=0x00;//en low
  lcd=((z<<4)&0xf0)|en|rs;
}
void LCDData(unsigned char z)
{
     rs=0x04 ;//for data
     rw=0x00;
     en=0x08;//en high
     lcd=((z)&0xf0)|en|rs;
     delay(100);
     en=0x00;//en low
     lcd=((z)&0xf0)|en|rs;
     en=0x08;//en high
     lcd=((z<<4)&0xf0)|en|rs;
     delay(100);
     en=0x00;//en low
     lcd=((z<<4)&0xf0)|en|rs;
}
void LCDInit()
{
     LCDCmd(0x02);
     LCDCmd(0x06);//entry mode
     LCDCmd(0x0c);//diply on cr off
     LCDCmd(0x28);//4 bit
}

void LCDGotoXY(unsigned char x,unsigned char y)
{
 if(x<40)
 {
  if(y)
  x|=0b01000000;
  x|=0b10000000;
  LCDCmd(x);
  }
}
void LCDWriteInt(int val,unsigned int field_length)
{
 char str[5]={0,0,0,0,0};
 int i=4,j=0;
 while(val)
 {
 str[i]=val%10;
 val=val/10;
 i--;
 }
 j=5-field_length;
 if(val<0) LCDData(' ');
 for(i=j;i<5;i++)
 {
 LCDData(48+str[i]);
 }
}

void LCDWriteString(unsigned char *str)
{
 while((*str)!='\0')
 {
  LCDData(*str);
  str++;
 }
}

void i2c_Init(void)
{
SSPSTAT |= 0x80;
SSPCON1 = 0x28;
SSPADD = 0x28;

}

void i2c_Wait(void)
{
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
}

void i2c_Start(void)
{

SSPCON2bits.SEN=1;

while(SSPCON2bits.SEN);
}

void i2c_Restart(void)
{

SSPCON2bits.RSEN=1;
while(SSPCON2bits.RSEN);
}

void i2c_Stop(void)
{

SSPCON2bits.PEN=1;
while(SSPCON2bits.PEN);
}

void i2c_Write(unsigned char data)
{
SSPBUF = data;
while(SSPSTATbits.BF );
i2c_Wait();
}

unsigned char i2c_read(unsigned char ack)
{
unsigned char data;

i2c_Wait();
SSPCON2bits.RCEN = 1;
while( SSPCON2bits.RCEN );
data = SSPBUF;
SSPCON2bits.ACKDT = ~ack;
SSPCON2bits.ACKEN = 1;
while( SSPCON2bits.ACKEN );
return data;
}

void I2CAck()
{
SSPCON2bits.ACKDT = 0;
SSPCON2bits.ACKEN = 1;
while(SSPCON2bits.ACKEN);
}

void I2CNak()
{
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(SSPCON2bits.ACKEN);
}
void convert(unsigned int val)
{
 LCDData('0'+(val>>4));
 LCDData('0'+(val&0x0f));
}

void display(unsigned char i,unsigned char j,unsigned char k)
{
 convert(i);
 s1[0]=((i/16)+48);
 s1[1]=((i%16)+48);
 LCDData('-');
 convert(j);
 s2[0]=((j/16)+48);
 s2[1]=((j%16)+48);
 LCDData('-');
 convert(k);
 s3[0]=((k/16)+48);
 s3[1]=((k%16)+48);
}

void rtc_init()
{
 i2c_Init();
        i2c_Wait();
 i2c_Start();
 i2c_Write(0xd0);
 i2c_Write(0x07);
 i2c_Write(0x00);
 i2c_Stop();
}

void rtc_time(unsigned char i,unsigned char j,unsigned char k)
{
        i2c_Wait();
 i2c_Start();
 i2c_Write(0xd0);
 i2c_Write(0);
 i2c_Write(i);
 i2c_Write(j);
 i2c_Write(k);
 i2c_Stop();
}

void rtc_read_time()
{
        i2c_Wait();
 i2c_Start();
 i2c_Write(0xd0);
 i2c_Write(0);
 i2c_Restart();
 i2c_Write(0xd1);
 p3=i2c_read(1);
 q3=i2c_read(1);
 r3=i2c_read(0);
        display(r3,q3,p3);
 i2c_Stop();
}

void rtc_date(unsigned char i,unsigned char j,unsigned char k)
{
        i2c_Wait();
 i2c_Start();
 i2c_Write(0xD0);
 i2c_Write(0x04);
 i2c_Write(i);
 i2c_Write(j);
 i2c_Write(k);
 i2c_Stop();
}

void rtc_read_date()
{
        i2c_Wait();
 i2c_Start();
 i2c_Write(0xD0);
 i2c_Write(0x04);
        i2c_Restart();
 i2c_Write(0xd1);
 p4=i2c_read(1);
 q4=i2c_read(1);
 r4=i2c_read(0);
        display(p4,q4,r4);
 i2c_Stop();
}

void rtc_time_date(unsigned char p1,unsigned char q1,unsigned char r1,unsigned char p2,unsigned char q2,unsigned char r2)
{
 rtc_time(p1,q1,r1);
 rtc_date(p2,q2,r2);
}

void main()
{
 TRISD=0;  // Configure Port d as output port
 LCDInit();
 rtc_init();
 rtc_time_date(0x55,0x59,0x23,0x31,0x12,0x12);
 while(1)
 {
            rtc_read_time();
            LCDCmd(0xc0);
            rtc_read_date();
            LCDCmd(0x80);
 }
}


Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment