How to Interface I2C-RTC with AVR Primer

Interfacing I2C-RTC with AVR Primer

Tags: Interfacing I2C – RTC, Interfacing I2C – RTC with AVR, Circuit Diagram to Interface I2C–RTC with AVR, C Program to interface I2C – RTC with AVR atmesga8/16, Testing the I2C – RTC with AVR Primer, source code for I2CRTC,
Overall rating
interface-i2c-rtc-with-avr-primer
AVR Primer Board

The AVR Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports In-system programming (ISP) which is done through Parallel/Printer port.


ATMEL’s AVR (Atmega8535), AVR Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.


I2C (Inter Integrated Circuit)

The I2C (Inter-IC) bus is a bi-directional two-wire serial bus that provides a communication link between integrated circuits (ICs).I2C is a synchronous protocol that allows a master device to initiate communication with a slave device. Data is exchanged between these devices.


RTC (Real Time Clock)

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. 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.


Interfacing I2C - RTC

Fig. 1 shows how to interface the RTC with microcontroller through I2C. I2C is a Master-Slave protocol. I2C has a clock pulse along with the data. The master device controls the clock line, SCL. This line dictates the timing of all transfers on the I2C bus. No data will be transferred unless the clock is manipulated.


I2c bus supports many devices, each device is recognized by a unique address—whether it’s a micro-controller, LCD Driver, memory or keyboard interface and can operate as transmitter or receiver based on the functioning of the device. The controller designed controls the RTC ds1307 device through I2C protocol. The I2C Controller here acts as a master device and controls RTC ds1307 which acts as a slave. The read operation is accomplished by sending a set of control signals including the address and/or data bits. The control signals must be accompanied with proper clock signals.




interfacing-i2c---rtc-to-microcontroller

Fig. 1 Interfacing I2C - RTC to Microcontroller



Interfacing I2C – RTC with AVR

We now want to read date & time by using I2C - RTC in AVR Primer Board. Wiring up an I2C based RTC to the I2C port is relatively simple. The RTC also makes the software easier as it takes care of all calendar functions; accounting for leap years etc. The DS1307 (RTC) Real Time Clock IC (an I2C real time clock) is an 8 pin device using an I2C interface.


In AVR Primer Kit, 2 nos. of RTC lines are controlled by I2C Enabled drivers. I2C Lines serial clock of CLK (PORTC.1), serial data of DATA (PORTC.0), connected to the I2C based serial RTC ds1307 IC. The date & times are read in AVR Primer Kit by using these DATA & CLK I2C lines.


Pin Assignment with AVR

HARDWARE PIN OUT

CONNECTIONS

OUTPUT

CLK

PORTC.1

Turn ON RTC Pin of

CONFIG-II switch SW6.

Connect Serial cable between USART Section in the Board and PC.

Date and Time will be dis-played in HyperTerminal.

Seconds will be incrementing.

DATA

PORTC.0

 

Circuit Diagram to Interface I2C–RTC with AVR



circuit-diagram-to-i2c-rtc-with-avr-primer



Source Code

The Interfacing I2C – RTC with AVR program is very simple and straight forward that read date & time in RTC by using I2C & the value is displayed in serial port. A delay is occurring in every single data read from RTC. The delay depends on compiler how it optimizes the loops as soon as you make changes in the options the delay changes.


C Program to interface I2C – RTC with AVR

***************************************************************************************


Title : Program to read date & time from I2C - RTC


***************************************************************************************


 

#include<mega8535.h>

#include<delay.h>      

#include<stdio.h>

 

#define TXIF       UCSRA.5

#define BAUD       100000L       //TWI frequency   100Khz

#define FOSC       16000000L                          

//Oscillator Frequency   16Mhz 

#define Prescale   4          //Prescalar set to 1:1

#define I2C_Baud   ((FOSC / BAUD-16)/(2*Prescale))

 

#define SLA_W      0xd0       //EEPROM write and read

#define SLA_R      0xd1

#define REP_START  TWCR = 0xa4  //Following are TWI commands

#define START      TWCR = 0xa4     

#define STOP       TWCR = 0x94

#define CLR_TWINT  TWCR = 0x84     

#define Wait()     while((TWCR & 0x80)==0)

#define DATA       TWDR 

    

#define USART_Baud  9600                                                

#define BaudH  ((unsigned int)(((FOSC/16/USART_Baud)-1) >> 8))

#define BaudL  ((unsigned int)(((FOSC/16/USART_Baud)-1) & 0xFF))

 

unsigned char sec,min,hour,day,date,month,year;

unsigned char data[7]={0x45,0x59,0x71,0x04,0x05,0x10,0x06};

unsigned char i=0;

void DS1307Write(unsigned char, unsigned char);

unsigned char DS1307Read(unsigned char);

void I2c_init(void);

void Serial_init(void);    

 

 

void main()

{  

    delay_ms(10);

    I2c_init();

    Serial_init(); 

    delay_ms(20);

    printf("\033[2J");

    printf("DS1307 Demo Program: \r\n"); 

   

    for(i=0;i<7;i++)

    DS1307Write(i,data[i]);     //call write routines      

    delay_ms(100);

   

    while(1)

    {

            sec  =  DS1307Read(0);  // read second

            min  =  DS1307Read(1);  // read minute

            hour =  DS1307Read(2);  // read hour

            day  =  DS1307Read(3);  // read day

            date =  DS1307Read(4);  // read date

            month=  DS1307Read(5);  // read month

            year =  DS1307Read(6);  // read year                    

         printf("Time: %x : %x : %x   ",(hour&0x1f),min,sec);  

         printf("Date: %x / %x / %x   \r", date, month,  year);

         delay_ms(100);

    }

}

            

void DS1307Write(unsigned char addr, unsigned char data)

{                

        START;     //send start pulse START=0xa4         

        Wait();     //wait untill the TWINT bit set,if TWINT bit

//is ==1 process completed     

        

        DATA  = SLA_W; //send SLave Address + write bit

        CLR_TWINT;     //clear TWINT bit by writing 1 into it

        Wait();      //wait  untill the TWINT bit set

            

        DATA  = addr; //Send the starting address where we are

//going to write out data

        CLR_TWINT;

        Wait();

                     

        DATA=data;

        CLR_TWINT;  

        Wait();              

        STOP;

       

        delay_ms(10);

}

 

   

unsigned char DS1307Read(unsigned char addr)

{

        unsigned char RecDat=0;

             

        START;

        Wait();

          

        DATA  = SLA_W;

        CLR_TWINT;

        Wait();           

                     

        DATA = addr;

        CLR_TWINT;

        Wait();            

       

        REP_START;

        Wait();

       

        DATA  = SLA_R;

        CLR_TWINT;

        Wait();

                

         TWCR = 0xc4;

        CLR_TWINT;

        Wait();  

        RecDat=DATA;

       

        return RecDat;     

            

 

 

void I2c_init()

{     

        TWCR = 0x04;

        TWSR = 0x01;

        TWBR = I2C_Baud;  

}

 

void Serial_init(void)

{

        UCSRA = 0x00;  //Status Register

        UCSRB = 0x18;   //Transmit Enable,Receive

                        //enable,interrupts disabled   

        UCSRC = 0x86;   //8 databits,no parity,single stop   

        UBRRH = BaudH;

        UBRRL = BaudL;        

}

 

#define _ALTERNATE_PUTCHAR_       

 

void putchar(unsigned char Data)

   while(TXIF==0);

   UDR = Data;     

}

 


To compile the above C code you need the CodeVision AVR software. The software has it’s own IDE and built-in AVR gcc- Compiler. They must be properly installed and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.


In CodeVision AVR software, you can develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without microcontroller Board.


The AVR Code Blaster software is used to download the hex file into your microcontroller through Parallel port.


Testing the I2C – RTC with AVR

Give +12V power supply to AVRPrimer Board; the RTC Battery device is connected with the AVR Primer Board. First check the entire Battery device fixed properly. A serial cable is connected between the microcontroller and PC.


In PC, open the Hyper Terminal for displaying the values from RTC.


Now, the Hyper Terminal shows the received data from RTC Battery through I2C.


If the Hyper Terminal is working but it is not reading any value from AVR Primer Board, then you just check the configuration switch connections. Change the Battery & ds1307 device.


If you are not receiving any data in Hyper Terminal, then you just check the serial cable is working or not. Otherwise you just check the code with debugging mode in CodeVisionAVR.


General Information

  • For proper working use the components of exact values as shown in Circuit file. Wherever possible use new components.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints. Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,
       -  User Manual of AVR Primer Board.
       -  How to create & Debug a Project in CodeVision AVR.

Join the World's Largest Technical Community

we respect your privacy.