DS1307 Real Time Clock(RTC) interfacing with PIC18F4520 using I2C Protocol

DS1307 Real Time Clock(RTC) interfacing with PIC18F4520 using I2C Protocol



/********************RTC(DS1307) interfacing with PIC18F4520*******************/
#include<p18f4520.h>
#include<string.h>
/************************* CONFIGURATION BITS SETTINGS *************************/
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF,LVP=OFF
#pragma config BOREN=OFF
#pragma config FCMEN=OFF
#pragma config IESO=OFF
/********************************** MACROS *************************************/
#define ldata PORTD
#define rs PORTCbits.RC0
#define rw PORTCbits.RC1
#define en PORTCbits.RC2
/*************************** FUNCTION DECLARATIONS *****************************/
void lcd_init(void);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void Displaystring(unsigned char *);
void msdelay(unsigned int);
void ConfigureI2C(void);
void I2C_idle(void);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_restart(void);
void I2C_write(unsigned char);
unsigned char ReadI2C(void);
unsigned char rtc_read(unsigned char);
void write_ds1307(unsigned char,unsigned char);
void BCD2UpperCh(unsigned char);
void BCD2LowerCh(unsigned char);
/*******************************************************************************/
unsigned char seconds,minutes,hours,Day,date,Month,Year;
unsigned char text[]="RTC INTERFACING";
/*******************************************************************************/
void main()
{
ADCON1=0x0F;
TRISD=0x00;
TRISCbits.TRISC0=0;
TRISCbits.TRISC1=0;
TRISCbits.TRISC2=0;
TRISCbits.TRISC3=1; //SCL pin
TRISCbits.TRISC4=1; //SDA pin
lcd_init();
Displaystring(text);
msdelay(5000);
lcdcmd(0x01);
ConfigureI2C();
I2C_idle();
write_ds1307(0x07,0x93); 
write_ds1307(0x00,0x00); //Set seconds to zero
write_ds1307(0x01,0x40); //10:40:00
write_ds1307(0x02,0x10);
write_ds1307(0x03,0x01);
write_ds1307(0x04,0x01); //1 january
write_ds1307(0x05,0x01);
write_ds1307(0x06,0x18); //2018
while(1)
{
 seconds=rtc_read(0x00);
 minutes=rtc_read(0x01);
 hours=rtc_read(0x02);
 date=rtc_read(0x04);
 Month=rtc_read(0x05);
 Year=rtc_read(0x06);
 lcdcmd(0x80);
 BCD2UpperCh(hours);
 BCD2LowerCh(hours);
 lcdcmd(0x82);
 lcddata(':');
 lcdcmd(0x83);
 BCD2UpperCh(minutes);
 BCD2LowerCh(minutes);
 lcdcmd(0x85);
 lcddata(':');
 BCD2UpperCh(seconds);
 BCD2LowerCh(seconds); 
 lcdcmd(0xC0);
 BCD2UpperCh(date);
 BCD2LowerCh(date);
 lcdcmd(0xC2);
 lcddata('/');
 lcdcmd(0xC3);
 BCD2UpperCh(Month);
 BCD2LowerCh(Month);
 lcdcmd(0xC5);
 lcddata('/');
 BCD2UpperCh(Year);
 BCD2LowerCh(Year);
}
}
void lcd_init()
{
lcdcmd(0x38); //----initialize lcd 2 lines,5x7 matrix----//
msdelay(15);
lcdcmd(0x01); //----Clear lcd----//
msdelay(15);
lcdcmd(0x0C); //----Display on,cursor off----//
msdelay(15);
lcdcmd(0x06); //----shift cursor right----//
msdelay(15);
lcdcmd(0x80); //-----force cursor to beginning of first line-----//
msdelay(15);
}
void lcdcmd(unsigned char command) //*****Function for sending commands to lcd*****//
{
 ldata=command;
rs=0;
 rw=0;
 en=1;
 msdelay(10);
 en=0;
}
//******Function for sending Data to lcd*******//
void lcddata(unsigned char dataout)
{
ldata=dataout;
rs=1;
 rw=0;
 en=1;
 msdelay(10);
 en=0;
}
void Displaystring(unsigned char *string) //*****Function for displaying string on Lcd*****//
{
 while(*string!='\0')
 {
 lcddata(*string++);
 msdelay(3000);
 }

void msdelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++);
for(j=0;j<135;j++);
}
void I2C_Start(void)
{
 SSPCON2bits.SEN=1; //Initiate Start condition on SDA and SCL pins
 while(SSPCON2bits.SEN==1); //Wait for Start condition to complete
}
void I2C_Stop(void)
{
 SSPCON2bits.PEN=1; //Initiate Stop condition on SDA and SCL pins
 while(SSPCON2bits.PEN==1);
}
void I2C_restart(void)
{
 SSPCON2bits.RSEN=1; //Initiate Repeated start condition on SDA and SCL pins
 while(SSPCON2bits.RSEN==1);
}
void I2C_idle(void)
{
 while((SSPCON2 & 0x1F)|(SSPSTATbits.R_W));

void ConfigureI2C(void)
{
 SSPADD=0x09; //I2C transmission speed is 100KHz,Crystal frequency is 4MHz
 SSPCON1=0x28;
 SSPSTATbits.SMP=1; //Slew rate control disabled for standard speed mode(100Khz & 1MHz mode)
 SSPSTATbits.CKE=0; //Disable SMBus specific inputs
 SSPCON2=0x00;
 PIR1bits.SSPIF=0;
}
unsigned char ReadI2C(void)
{
 SSPCON2bits.RCEN=1; //Enable master for 1 Byte reception
 while(!SSPSTATbits.BF); //Wait until byte is received
 return SSPBUF; //return with read byte
}
void I2C_write(unsigned char data)
{
 SSPBUF=data;
 while(SSPSTATbits.BF==1); //Wait until write cycle is completed
}
unsigned char rtc_read(unsigned char address)
{
 unsigned char var;
 I2C_idle();
 I2C_Start();
 I2C_write(0xD0);
 I2C_idle();
 I2C_write(address);
 I2C_idle();
 I2C_restart();
 I2C_write(0xD1);
 I2C_idle();
 var=ReadI2C();
 I2C_Stop();
 return var;
}
void write_ds1307(unsigned char Address,unsigned char value)
{
 I2C_idle();
 I2C_Start();
 I2C_idle();
 I2C_write(0xD0);
 I2C_idle();
 I2C_write(Address);
 I2C_idle();
I2C_write(value);
 I2C_Stop();
}
void BCD2UpperCh(unsigned char value1)
{
 unsigned char temp=value1;
 temp=temp&0xF0;
 temp=temp>>4;
 temp=temp|0x30;
 lcddata(temp);
}
void BCD2LowerCh(unsigned char value2)
{
 unsigned char temp=value2;
 temp=temp&0x0F;
 temp=temp|0x30;
 lcddata(temp);
}

--------------------------------------------------------

Contributed by our guest contributor, Abhishek Mane. Reach out to Abhishek from his LinkedIn account.

Post a Comment

0 Comments