Monday, 16 July 2018

Interface a RTC(Real Time Clock) with AVR Microcontroller (Atmega8) 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 AVR ATmega8 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 AVR ATmega8 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 AVR ATmega8 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

rtc

Code

// ****************************************************** 
// Project: RTC interfacinig using atmega8 
// Author: Hack Projects India 
// Module description: Operate DS1307 RTC IC
// ******************************************************
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
void rtc_init();
void rtc_time(unsigned char i,unsigned char j,unsigned char k);
void rtc_read_time();
void rtc_date(unsigned char i,unsigned char j,unsigned char k);
void rtc_read_date();
void rtc_time_date(int p1,int q1,int r1,int p2,int q2,int r2);
#define       START                     0x08
#define  REPEAT_START             0x10
#define  MT_SLA_ACK               0x18
#define  MT_SLA_NACK       0x20
#define  MT_DATA_ACK       0x28
#define  MT_DATA_NACK             0x30
#define  MR_SLA_ACK               0x40
#define  MR_SLA_NACK       0x48
#define  MR_DATA_ACK       0x50
#define  MR_DATA_NACK             0x58
#define  ARB_LOST                 0x38
#define  ERROR_CODE               0x7e
#define  DS1307_W                 0xd0
#define  DS1307_R                 0xd1
#define  EEPROM_W                 0xa0
#define  EEPROM_R                 0xa1
#define Data PORTB
#define RS_en() PORTB |=(1<<PB4)
#define RS_clr() PORTB &= ~(1<<PB4)
#define E_en() PORTB |=(1<<PB5)
#define E_clr() PORTB &= ~(1<<PB5)

void InitLCD();
void LCDCmd(unsigned char);
void LCDData(unsigned char);
void LCDWriteString(const char *);
void i2c_init();
void i2c_start();
void i2c_write(unsigned char data);
unsigned char i2c_read(unsigned char a);
void i2c_stop();

unsigned char s1[10],s2[10],s3[10];
unsigned int p,q,r;

void InitLCD()

{
       LCDCmd(0x02);
       _delay_ms(2);
       LCDCmd(0X28);
       _delay_ms(2);
       LCDCmd(0X06);
       _delay_ms(2);
       LCDCmd(0X01);
       _delay_ms(2);
       LCDCmd(0X0c);
       _delay_ms(2);
       LCDCmd(0X80);
       _delay_ms(2);
}
void LCDCmd(unsigned char cmd)
{
       RS_clr();
       E_en();
       _delay_us(100);
       Data|=(cmd>>4);
       _delay_us(100);
       E_clr();
       Data&=~(cmd>>4);
       _delay_us(100);
       E_en();
       _delay_us(100);
       Data|=(cmd&0x0f);
       _delay_us(100);
       E_clr();
       Data&=~(cmd&0x0f);
}
void LCDData(unsigned char dat)
{
       RS_en();
       E_en();
       _delay_us(100);
       Data|=(dat>>4);
       _delay_us(100);
       E_clr();
       Data&=~(dat>>4);
       _delay_us(100);
       E_en();
       _delay_us(100);
       Data|=(dat&0x0f);
       _delay_us(100);
       E_clr();
       Data&=~(dat&0x0f);
       _delay_us(100);
}
void LCDWriteString(const char *str)
{
       while((*str)!='\0')
       {
              LCDData(*str);
              str++;
       }
}
void LCDGotoXY(uint8_t x,uint8_t y)
{
       if(x<40)
       {
              if(y)
              x|=0b01000000;
              x|=0b10000000;
              LCDCmd(x);
       }
}
void LCDClear()
{
       LCDCmd(0x01);
}
void rtc_init()
{
       i2c_init();
       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_start();
       i2c_write(0xd0);
       i2c_write(0);
       i2c_write(i);
       i2c_write(j);
       i2c_write(k);
       i2c_stop();
}
void rtc_read_time()
{
       i2c_start();
       i2c_write(0xd0);
       i2c_write(0);
       i2c_stop();
       _delay_ms(50);
       i2c_start();
       i2c_write(0xd1);
       p=i2c_read(1);
       q=i2c_read(1);
       r=i2c_read(0);
       i2c_stop();
}
void rtc_date(unsigned char i,unsigned char j,unsigned char k)
{
       i2c_start();
       i2c_write(0xD0);
       i2c_write(0x04);
       i2c_write(i);
       i2c_write(j);
       i2c_write(k);
       i2c_stop();
}
void rtc_read_date()
{
       i2c_start();
       i2c_write(0xD0);
       i2c_write(0x04);
       i2c_stop();
       _delay_ms(50);
       i2c_start();
       i2c_write(0xd1);
       p=i2c_read(1);
       q=i2c_read(1);
       r=i2c_read(0);
       i2c_stop();
}
void rtc_time_date(int p1,int q1,int r1,int p2,int q2,int r2)
{
       rtc_time(p1,q1,r1);
       rtc_date(p2,q2,r2);
}
//************************************************
//TWI initialize
// bit rate:18 (freq: 100Khz @16MHz)
//************************************************
void i2c_init()
{
 TWCR= 0x00;         //disable twi
 TWBR= 0x47;         //set bit rate
 TWCR = (1<<TWEN);   //set prescale
}
//*************************************************
//Function to start i2c communication
//*************************************************
void i2c_start()
{
       TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTA);
       while(!(TWCR&(1<<TWINT)));
}
//**************************************************
//Function to transmit a data byte
//*************************************************
void i2c_write(unsigned char data)
{
       TWDR = data;
       TWCR = (1<<TWINT)|(1<<TWEN);
       while(!(TWCR&(1<<TWINT)));
}
//*****************************************************
//Function to receive a data byte and send ACKnowledge
//*****************************************************
unsigned char i2c_read(unsigned char a)
{
       TWCR=(1<<TWINT)|(1<<TWEN)|(a<<TWEA);
       while(!(TWCR&(1<<TWINT)));
       return(TWDR);
}
//**************************************************
//Function to end the i2c communication
//*************************************************  
void i2c_stop()
{
       TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
void convert(unsigned int val)
{
       LCDData('0'+(val>>4));
       LCDData('0'+(val&0x0f));
}
void display_time(unsigned char i,unsigned char j,unsigned char k)
{
       convert(i);
       LCDWriteString("-");
       convert(j);
       LCDWriteString("-");
       convert(k);
}
void display_date(unsigned char i,unsigned char j,unsigned char k)
{
       convert(i);
       LCDWriteString("/");
       convert(j);
       LCDWriteString("/");
       convert(k);
}
int main(void)
{
       DDRB=0xff;
       InitLCD(0);
       rtc_init();
       _delay_ms(50);
       rtc_time_date(0x30,0x59,0x23,0x31,0x12,0x16);
       while(1)
       {
              LCDCmd(0x80);
              LCDWriteString("Time: ");
              rtc_read_time();
              display_time(r,q,p);
              LCDCmd(0xc0);
              LCDWriteString("Date: ");
              rtc_read_date();    
              display_date(p,q,r);
       }            
}

Downloads:

The code was compiled in Atmel Studio 6 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment