You are on page 1of 11

COMSATS Institute of Information

Technology Islamabad
Microprocessor and Interfacing
Lab Report 12

Submitted By:
Omer Zamir
BET-084
Mujtaba Habib
096

Submitted To:
Dr. Omer Ahmed

Submission Date:

FA13FA13-BET-

May 26, 2016


Lab Title:
Real Time Clock Interfacing using I2C Protocol
Task 1:
Complete The Given Write Function to set the time
and date on RTC
Main Code:
/
******************************************************************************
*
// Project: Lab12 Realtime Clock (DS1307) Interfacing using I2C Protocol
//
// Author: Omar Ahmad
//
// Date: 26-05-2016
//
// Module description:
//
// In this lab the students will interface DS1307 RTC with Atmega16. The
students
// will also learn to use the provided I2C library (by Peter Fleury). DS1307 RTC
// is a dual powered (VCC and Vbat) time keeping device which maintains
records
// of the time as well as calendar. Its internal registers are accessed via I2C
// and are used to configure time on the device. These registers can later be
read

// to get the current time and date. The accuracy of the RTC depends upon
the
// accuracy of the crystal oscillator it is clocked with.
//
//
Task1:
date on RTC

Complete the given write fucntions to set the time and

//
// Task2:
LCD

Read the RTC by completing the read functions and display on

******************************************************************************
**/

#include <avr/io.h>
#include <avr/interrupt.h>

#define F_CPU 1000000UL


#include <util/delay.h>
#include <string.h>
#include <math.h>
#include "i2chw/i2cmaster.h"
/****************** Definitions for LCD *********************/
#define LCD_CTRL_PORT

PORTC

// Define the Ports used for LCD

#define LCD_CTRL_DDR

DDRC

// Data and Control Commands

#define RS

PC5

#define RW

PC6

#define En

PC7

#define LCD_DATA_PORT

PORTD

#define LCD_DATA_DDR

#define LCD_CLEAR

DDRD

0x01

#define LCD_HOME

0x02

#define LCD_NEXT_LINE 0xC0

#include "LCD.h"
/***************************************************************/

/****************** Definitions for DS1307 *********************/


#define DS1307_ADDR 0xD0 // 1101 000 is the 7-bit address + RW bit to be
added

#define DATE_REG
#define MONTH_REG

0x04 // day of month is stored in this reg (1-31)


0x05

#define YEAR_REG

0x06

#define DOW_REG

0x03 // Day of Week (contains values 1-7)

#define HOUR_REG

0x02

#define MINUTE_REG 0x01


#define SECOND_REG 0x00

/********************* Globals for DS1307 **********************/

/*
unsigned char date = 26;
unsigned char month = 11;
unsigned char year = 16;

unsigned char second = 25;


unsigned char minute = 30;
unsigned char hour = 12;*/

/**************** Function Prototypes **************************/


int system_init(void);

// initializes the LCD and I2C

unsigned char dec2bcd(unsigned char);


unsigned char bcd2dec(unsigned char);

int DS1307_setDate(unsigned char date, unsigned char month, unsigned


char year);
int DS1307_getDate(unsigned char * date, unsigned char * month, unsigned
char * year);

int DS1307_getDOW(unsigned char * dow);

// get day of week

int DS1307_setTime(unsigned char hour, unsigned char minute, unsigned


char second);
int DS1307_getTime(unsigned char * hour, unsigned char * minute, unsigned
char * second);
/***************************************************************/

// Main program
//
int main(void)
{
unsigned char date = 26;
unsigned char month = 11;
unsigned char year = 16;

unsigned char second = 25;


unsigned char minute = 30;
unsigned char hour = 12;

system_init();
_delay_ms(200);
//LCD_display_str("Initializatin!!!");

DS1307_setDate(date, month, year);


DS1307_setTime(hour, minute, second);

while(1)
{
// Infinite loop; define here the

LCD_display_str("DATE ");
LCD_display_BCD(dec2bcd(date));
LCD_display_str(":");
LCD_display_BCD(dec2bcd(month));
LCD_display_str(":");
LCD_display_BCD(dec2bcd(year));

LCD_SendCommand(LCD_NEXT_LINE);
LCD_display_str("TIME ");
LCD_display_BCD(dec2bcd(hour));
LCD_display_str(":");
LCD_display_BCD(dec2bcd(minute));
LCD_display_str(":");
LCD_display_BCD(dec2bcd(second));
LCD_SendCommand(LCD_HOME);
DS1307_getDate(&date,&month,&year);
DS1307_getTime(&hour,&minute,&second);

int system_init(void)
{
LCD_Initialize();

// initiallize the LCD

i2c_init();

// intialize the TWI interface

return(0);

// successfully initiallized the system

/* Fucntion to convert from decimal to BCD */


unsigned char dec2bcd(unsigned char val)
{
return val + 6 * (val / 10);
}
/* Fucntion to convert from BCD to decimal */
unsigned char bcd2dec(unsigned char val)
{
return val - 6 * (val >> 4);
}

int DS1307_setDate(unsigned char date, unsigned char month, unsigned


char year)
{
//write date
i2c_start_wait(DS1307_ADDR | I2C_WRITE);

i2c_write(DATE_REG);
date registor
i2c_write(dec2bcd(date));
i2c_write(dec2bcd(month));

//send address of the

i2c_write(dec2bcd(year));
i2c_stop();
return(0);

}
int DS1307_getDate(unsigned char * date, unsigned char * month,
unsigned char * year)
{
//read date
i2c_start_wait(DS1307_ADDR | I2C_WRITE);
i2c_write(DATE_REG);
i2c_stop();
i2c_start_wait(DS1307_ADDR | I2C_READ);
*date = bcd2dec(i2c_readAck());
*month = bcd2dec(i2c_readAck());
*year = bcd2dec(i2c_readNak());
i2c_stop();
return(0);
}

int DS1307_setTime(unsigned char hour, unsigned char minute, unsigned


char second)
{
//write date
i2c_start_wait(DS1307_ADDR | I2C_WRITE);

i2c_write(SECOND_REG);

//send address of the

date registor
i2c_write(dec2bcd(second));
i2c_write(dec2bcd(minute));
i2c_write(dec2bcd(hour));
i2c_stop();
return(0);
}
int DS1307_getTime(unsigned char * hour, unsigned char * minute, unsigned
char * second)
{
i2c_start_wait(DS1307_ADDR | I2C_WRITE);
i2c_write(SECOND_REG);
i2c_stop();
i2c_start_wait(DS1307_ADDR | I2C_READ);
*second = bcd2dec(i2c_readAck());
*minute = bcd2dec(i2c_readAck());
*hour = bcd2dec(i2c_readNak());
i2c_stop();
return(0);
}

Proteus Simulation:

You might also like