In this tutorial we will interface the RTC ( Real Time Clock ) with Atmega128 Breakout. We need the following Breakout boards and Display for interfacing.

Basics

In this example we are using DS1307 serial Real Time Clock. DS1307 which communicate with Atmega128 through I2C ( Inter-Integrated-Circuit) protocol. Befor we continue please refer the Basics of Real Time Clock with AVR. In RTC Breakout SCL and SDA pins are broken out which are needs to be connected to Atmega128 controller I2C lines. LCD data lines (Lower 4 bit D4 to D7) are connected to Port B4 to Port B7 of Atmega128, and similarly LCD control lines(RS,R/W and Enable) are connected to Port B0 to Port B2 of Atmega128.

Hook Up

Interfacing RTC with Atmega128 Breakout bb.png


Code

#include "rtc.h"
#include "lcd.h"
int main()
{
rtc_t rtc;
/*Connect RS->PB0, RW->PB1, EN->PB2 and data bus to PORTB.4 to PORTB.7*/
LCD_SetUp(PB_0,PB_1,PB_2,P_NC,P_NC,P_NC,P_NC,PB_4,PB_5,PB_6,PB_7);
LCD_Init(2,16);
/*Connect SCL->PC0, SDA->PC1*/
RTC_Init();
rtc.hour = 0x10; // 10:40:20 am
rtc.min = 0x40;
rtc.sec = 0x00;
rtc.date = 0x01; //1st Jan 2016
rtc.month = 0x01;
rtc.year = 0x16;
rtc.weekDay = 5; // Friday: 5th day of week considering monday as first day.
/*##### Set the time and Date only once. Once the Time and Date is set, comment these lines
and reflash the code. Else the time will be set every time the controller is reset*/
RTC_SetDateTime(&rtc); // 10:40:20 am, 1st Jan 2016
/* Display the Time and Date continuously */
while(1)
{
RTC_GetDateTime(&rtc);
LCD_GoToLine(0);
LCD_Printf("time:%2x:%2x:%2x \nDate:%2x/%2x/%2x",(uint16_t)rtc.hour,(uint16_t)rtc.min,(uint16_t)rtc.sec,(uint16_t)rtc.date,(uint16_t)rtc.month,(uint16_t)rtc.year);
}
return (0);
}

Demo

00Interface RTC with Atmega128.gif

Video Tutorial

For those of you, who would like to watch instead of read we have made a video with all the gyan.



Downloads

Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATMega-128-Break-Out-BoardDVB-12007/archive/master.zip


Have a opinion, suggestion , question or feedback about the article let it out here!