Sunday 22 November 2015

HOW TO INTERFACE RTC USING PIC MICROCONTROLLER

RTC Programming and Interfacing

RTC  interfacing with 8051 microcontroller is similar to all other kinds of  “Real Time Clocks” interfaced to it. So let us look on simple RTC interfacing with 8051 microcontroller and programming procedure involving in it.

Step1: Select RTC Device

The various kinds of RTC chips are available in the real time embedded world, which are classified based on various criteria such as package type, supply voltage and pin configuration etc.  A few types of RTC devices are;
  • Two-Wire Serial Interface (I2C Bus)
  • Three-Wire Serial Interface (USB BUS)
  • Four-wire Serial interface (SPI BUS)
First, we need to select type of RTC device by category based on requirement like I2C Bus RTC or SPI Bus RTC or other, which suitsfor interfacingwith respective microcontroller. Then we can select features of RTC device depending on requirement of application such as battery life, suitable package and clock frequency. Let us consider two-wire interfacing RTC with 8051 microcontroller such as DS1307.

Step2: Internal Register and Address of the RTC Device

RTC stands for real time clock  which provides years, months, weeks, days, hours, minutes and seconds based on crystal frequency. RTC consists of inbuilt RAM memory for data storage. A battery backup will be provided in case of failure of main power supply by connecting a battery to RTC device.





CODING
void idle()
{
while(RW==1);
}

void wait()
{
while(SSPIF=0);
SSPIF=0;

}

void I2C_Start()
{
SEN=1;while(SEN==1);idle();
}

void I2C_Stop()
{
PEN=1;
while(PEN==1);idle();
}

void I2C_Send(unsigned char send)
{
ACKSTAT=1;
SSPBUF=send;
while(ACKSTAT==1);idle();
}

unsigned char dec2bcd(unsigned int time)
{
unsigned char x,y;
y=time;
x=(y/10) << 4;
x+=(y%10);
time=x;
return(time);
}

void I2C_Init()
{
SMP=1;
CKE=1;
SSPCON=0x28;
SSPCON2=0X00;
SSPADD =10;
PSPIF=0;     
BCLIF=0;
}

void time_write()
{
I2C_Start();
I2C_Send(0xD0);
I2C_Send(0x00);
I2C_Send(0x00);
I2C_Send(0x00);
I2C_Send(0x00);
I2C_Send(0x03);
I2C_Send(0x00);
I2C_Send(0x01);
I2C_Send(0x13);
I2C_Stop();
}

void I2C_Restart()
{
RSEN=1;
while(RSEN==1);idle();
}

unsigned char I2C_read(char ack)
{
unsigned char s;
RCEN=1;
while(RCEN==1);
s=SSPBUF;
SSPIF=0;
SSPOV=0;
if(ack==6)
ACKDT=1;
else
ACKDT=0;
ACKEN=1;
return s;
}

void I2C_set(unsigned char min, unsigned char hr,unsigned char day, unsigned char date,unsigned char month)
{
I2C_Start();
I2C_Send(0xD0);
I2C_Send(0x00);
I2C_Send(0x00);
I2C_Send(min);
I2C_Send(hr);
I2C_Send(day);
I2C_Send(date);
I2C_Send(month);
I2C_Send(0x12);
I2C_Stop();
}
 





No comments:

Post a Comment