Interface I2C-RTC with LPC2129

I2C RTC Interfacing With ARM7 Tyro

Tags: ARM7 LPC2129 Tyro Board, Interfacing I2C RTC, Interfacing I2C RTC to Microcontroller, Interfacing I2C – RTC with LPC2129, Circuit Diagram to Interface I2C–RTC with LPC2129, C Program to interface I2C – RTC with LPC2129,,
Overall rating
interfacing-i2c-rtc--with-lpc2129
ARM7 LPC2129 Tyro Board

The ARM7 LPC2129 Tyro board is specifically designed to help students to master the required skills in the area of embedded systems. This 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 serial port.


NXP’s ARM7 (LPC2129), ARM Tyro Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 32-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 EEPROM 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 LPC2129

We now want to read date & time by using I2C - RTC in LPC2129 Tyro 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 LPC2129 TYRO Kit, 2 nos. of RTC lines are controlled by I2C Enabled drivers. I2C Lines serial clock SCL (P0.2), serial data SDA (P0.3) connected to the I2C based serial RTC ds1307 IC. The date & times are read in LPC2129 Tyro Kit by using these SDA & SCL I2C lines.


Pin Assignment with LPC2129

 

I2C RTC

LPC2129 Lines

Real Time Clock

DS1307

SCL

SCL1 -  (P0.11)

  real-time-clock-for-arm

SDA

SDA1 -  (P0.14) 

 

Circuit Diagram to Interface I2C–RTC with LPC2129



circuit-diagram-to-interface-i2crtc-with-lpc2129


Source Code

The Interfacing I2C – RTC with LPC2129 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 LPC2129

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


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


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


 

#include <LPC21xx.h>

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#include <stdlib.h>

#include "UART.h"

 

#define   MAX     8

#define   AA      2

#define   SI      3

#define   STO     4

#define   STA     5

#define   I2EN    6

#define   ESC     0x1B

#define   SW3     1<<24

#define   SW4     1<<25

#define   SW5     1<<26

 

void I2C_ISR(void)__irq;

void UART0_ISR (void)__irq;

void Wait (unsigned int);

void I2C_Init (void);

int I2C_Start (void);

int I2C_Write (unsigned char *Buff, unsigned int Count);

 

char Buff[MAX] = {0x59,0x58,0x65,0x04,0x04,0x11,0x09};

unsigned char Day[7][10] = {"Sunday", "Monday",

  "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"};

unsigned char Rec[MAX]   =   {"NO-DATA"};

unsigned char index      =   0;

unsigned char flag = 0, Ready=0;

int ii=0;char Key;

 

 

void I2C_ISR(void) __irq

{

   if (I2CONSET & 0x08)

   {

      switch (I2STAT)

      {

         Case (0x08): // A START cond has been TXed  

 

            if (flag == 'W')

            {

              I2CONCLR   =   1 << STO;

              I2CONCLR   =   1 << STA; // Clear START Bit

              I2CONSET   =   1 << AA;

              I2DAT      =   0xD0;     // Slave Addr + W

              I2CONCLR   =   1 << SI;

              //   U0THR   =   'S';

            }

            else if (flag == 'R')

            {

              I2DAT      =   0xD0;    // Slave Addr + R

              I2CONSET   =   1 << STA;

              I2CONCLR   =   1 << SI;

              //   U0THR   =   's';

            }

            index   =   0;

 

         break;

  

         case (0x10): // Repeated START cond has been TXed

 

            I2CONCLR   =   1 << STA;  

            if (flag == 'W')

            {

               I2DAT   =   0xD0;    // Slave Addr + W

            }

            else if (flag == 'R')

            {

               I2DAT      =   0xD1; // Slave Addr + R

               I2CONCLR   =   1 << STO;  

               I2CONSET   =   1 << AA;

 

 

               I2CONCLR   =   1 << SI;

               index = 0;

            }

                    

         break;

  

         case (0x18): // SLA+W TXed; ACK has been RXed

 

            if (flag == 'W')

            {

               I2DAT      =   0x00;

               index=0;

               I2CONCLR   =   1 << STA; // Clear START Bit

               I2CONCLR   =   1 << STO; // Clear all flags

            }

            else

            {

               I2DAT   =   0x00;

               index    =   0;

               I2CONCLR   =   1 << STA; // Clear START Bit

               I2CONCLR   =   1 << STO; // Clear all flags

               I2CONSET   =   1 << AA;

            }

            I2CONCLR   =   1 << SI;

 

         break;

  

         case (0x20): // SLA+W TXed; NOT ACK RXed

 

            if (flag == 'W')

            {

               I2DAT      &=   0x7F;

               index=0;

               I2CONCLR   =   1 << STA; // Clear START Bit

               I2CONCLR   =   1 << STO; // Clear all flags

            }

            else

            {

               I2DAT   =   0xD1;

               index    =   0;

 

 

               I2CONCLR   =   1 << STA; // Clear START Bit

               I2CONCLR   =   1 << STO; // Clear all flags

               I2CONSET   =   1 << AA;

            }

            I2CONCLR   =   1 << SI;    

               

         break;

  

         case (0x28): // Data byte in I2DAT TXed; ACK RXed

 

            if (index < MAX && flag == 'W')

            {

               I2DAT      =   Buff[index];

               I2CONCLR   =   0x20; // Clear START Bit

               I2CONCLR   =   0x38; // Clear all flags

               index++;

            }

            else if (flag == 'R') // Repeated Start to MST/REC

            {

               I2CONCLR   =   1 << STO;

               I2CONSET   =   1 << STA;

               I2CONCLR   =   1 << SI;

            }

            else // Stop TXn to EEPROM. Data Count Reset..

            {

               index = 0;

               flag = 'R';

               I2CONSET   =   1 << STO; // Send STOP Bit

               I2CONCLR   =   1 << AA;

               I2CONSET   =   1 << STA;

               I2CONCLR   =   1 << SI;

            }

 

         break;

  

         case (0x30)   : // Byte in I2DAT TXed; NOT ACK RXed

 

            if (index < MAX && flag == 'W')

            {

               I2DAT      =   Buff[index];

 

 

               I2CONCLR   =   0x20; // Clear START Bit

               I2CONCLR   =   0x38; // Clear all flags

               index++;

            }

            else if (flag == 'R') // Repeated Start to MST/REC

            {

               I2CONCLR   =   1 << STO;

               I2CONSET   =   1 << STA;

               I2CONCLR   =   1 << SI;

            }

            else // Stop TXn to EEPROM. Data Count Reset

            {

               index = 0;

               flag = 'R';

               I2CONSET   =   1 << STO;      //Send STOP Bit

               I2CONCLR   =   1 << AA;

               I2CONSET   =   1 << STA;

               I2CONCLR   =   1 << SI;

            }

 

         break;

  

         case (0x38): // Arbitration lost in SLA+R/W

 

            I2CONSET   =   0x20;

 

         break;

 

         case (0x40): // SLA+R TXed; ACK has been RXed

 

            if (index < MAX)

            {

               Rec [index]   =   I2DAT;

               index++;

               I2CONSET   =   1 << AA;

               I2CONCLR   =   1 << STA;

               I2CONCLR   =   1 << STO;

            }

            else

            {

 

 

               index = 0;

               I2CONCLR   =   1 << AA;

               I2CONSET   =   1 << STO;

               I2CONCLR   =   1 << STA;

            }

            I2CONCLR   =   1 << SI;

 

         break;

 

         case (0x48): // SLA+R TXed; NOT ACK RXed

 

            if (index < MAX)

            {

               Rec [index]   =   I2DAT;

               index++;

               I2CONSET   =   1 << AA;

 

 

               I2CONCLR   =   1 << STA;

               I2CONCLR   =   1 << STO;

            }

            else

            {

               index = 0;

               I2CONCLR   =   1 << AA;

               I2CONSET   =   1 << STO;

               I2CONCLR   =   1 << STA;

            }

            I2CONCLR   =   1 << SI;

 

         break;

 

         case (0x50): // byte RXed; ACK returned

 

            I2CONSET   =   1 << AA;  

            if (index < MAX)

            {

               Rec [index]   =   I2DAT;

               I2CONSET   =   1 << AA;

               I2CONCLR   =   1 << STA;

 

 

               I2CONCLR   =   1 << STO;

               index++;

            }

            else

            {

               I2CONCLR   =   1 << STO;

               I2CONCLR   =   1 << AA;

               I2CONCLR   =   1 << SI;

               index = 0;

               Ready   =   'T';

            }

 

         break;

 

         case (0x58): // Data byte RXed; NOT ACK returned

 

            I2CONSET   =   1 << STO;

            I2CONSET   =   1 << STA;

            I2CONSET   =   1 << AA;

            I2CONCLR   =   1 << SI;

            flag = 'R';

            index = 0;

            Ready   =   'T';

 

         break;

                    

      }

   }

   I2CONCLR   =   1 << SI;

   VICVectAddr   =   0x00;

 

}

 

void UART0_ISR (void)__irq

{

   char Msg;

 

   if(((Msg = U0IIR) & 0x01) == 0) // Flag Status of RX Int

   {

      switch   (Msg & 0x0E)        // Filter Msg

 

 

      {

         case 0x04: while (!(U0LSR & 0x20)); // RX Data

 

              Key = U0RBR;

                 

         case 0x02:break;

 

         default    :   break;

      }

   }

   VICVectAddr = 6;

 

}

 

int main()

{

   unsigned char i, AP;

   VPBDIV   =   0x02;

   PINSEL0   =   0x00000005; // P0.3 - SDA0 & P0.2 - SCL0

   PINSEL0   |=   0x30C00000;

   IODIR1   =   0x00 << SW3;

   serial_init ();

   U0IER   =   3;

 

   VICIntSelect   = 0<<6;      // UART0 ('0' - irq   '1'-fiq)

   VICVectCntl6   = 0x020 | 6; // VIC slot enabled

   VICVectAddr6   = (unsigned long)UART0_ISR;

   VICIntEnable   = 1 << 6;    // Enable  UART0 Interrupt

 

   VICIntSelect   = 0<<19;

   VICVectCntl0    = 0x020 | 19 ;

   VICVectAddr0    = (unsigned long)I2C_ISR;

   VICIntEnable    = 1<<19;

 

   /*   Before the master transmitter mode can be entered,

        the I2CONSET register must be initialized  */

   printf("HI");

 

   I2C_Init();

 

   UART0_PutS ("\n\r********* ARM Tyro LPC2148 I2C

                 RTC Demo **********\n\n\r");

   UART0_PutS ("\r--------------------------------

                 -----------\n");

   UART0_PutS("\n\rVersion Release v1.0 25/05/09\n");

   UART0_PutS("\rResearch & Development Divison\n");

 

   UART0_PutS("\r(c) Pantech Solutions

               Pvt Ltd.,\nwww.pantechsolutions.net\n");

 

   UART0_PutS("\rChennai - India\n");

   UART0_PutS ("\n\rDS1307 Ext RTC\n\n\r");

 

   UART0_PutS ("\n\r_______________________________

                    ____________________\n\n\r");  

   Wait (5000);        

   flag   =   'W';

   I2C_Start ();

 

   for (i=0;i<30;i++)   Wait(10000);

 

   I2CONCLR   =   1 << SI;

   printf ("\n\n\rTime");

   printf ("\n\r------\n\n\r");

   Delay();

 

   while (1)

   {

 

       Wait (5000);Wait (5000);

       for (i=0;i<250;i++)   Wait(10000);

       if (Ready == 'T')

       {

          AP = (Rec[3] & 0x40)?'P':'A';           

          ii = 1;

          Delay();

          printf ("%02x : %02x : %02x %cM | ",

                 (Rec[3] & 0x1F), Rec[2], Rec[1], AP );

          Delay();Delay();

 

          printf ("%s, %02x / %02x / %02x \r",

                  Day[Rec[4]-1], Rec[5], Rec[6], Rec[7]);

 

          Delay();Delay();

          Ready = 'F';

       }

   } 

}

 

void Delay(void)

{

   unsigned int i,j;

  

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

 

      for(j=0;j<900;j++);

}

 

void Wait (unsigned int Delay)

{

   while(Delay--);

}

 

void I2C_Init (void)

{

   I2SCLH = 150;// 50%dutycycle.I2C Freq-100KHz,PCLK = 30MHz.

   I2SCLL = 150;

 

   I2CONSET = 1 << I2EN;   //Enable I2C 0

}

 

int I2C_Start (void)

{

   I2CONCLR   =   1 << STO;

   I2CONSET   =   1 << AA;

 

   I2CONSET   =   1 << STA;

   return 0;

}

 

int I2C_Write (unsigned char *Buff, unsigned int Count)

{

 

   while(Count--)

   {

      I2DAT      =   *Buff++;

   }

   return 0;

}

 

void RTC_Setup(char *Buff)

{

 

   unsigned char TimE;

 

   char i=0;

 

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

   {

 

      while(!isdigit(Key));  // Wait till Key = 0 to 9

 

      if (i==0)

      {

         TimE   =   Key - '0';

         TimE    <<=   4;

      }

 

      if (i==1)

      {

         TimE   |=   Key - '0';

      }

 

      Key    =   0;

   }

 

   *Buff   =   TimE;        // Load Setup New Value

}

 

unsigned char BCD2HEX (unsigned char BCD)

{

   unsigned char HEX;

   HEX   =   (BCD >> 4)*10 | (BCD & 0x0F);

   return HEX;

}

 

void serial_init(void)

{

  PINSEL0 |= 0x00000005; // Enable RxD0 and TxD0 

  U0LCR = 0x83;          // 8 bits, no Parity, 1 Stop bit

 

  U0DLL = 195;           // 9600 Baud Rate @ 12MHz VPB Clock

  U0LCR = 0x03;          // DLAB = 0 

}

 

 


To compile the above C code you need the KEIL software. They must be properly set up 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 Keil, you want to 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 LPC2129 Tyro Board.


The Flash Magic software is used to download the hex file into your microcontroller IC LPC2129 through UART0.


Testing the I2C – RTC with LPC2129

Give +3.3V power supply to LPC2129 Tyro Board; the RTC Battery device is connected with the Tyro 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 you want to change the time value from RTC Battery then you just Turn ON the switch, sw3. Now, you can write any new time values into the RTC by using Hyper Terminal. Then Turn OFF switch, sw3. Now the RTC Battery start the new time value & it display in Hyper Terminal.


The Hyper Terminal is working but it is not reading any value from LPC2129 Tyro Board, then you just check the jumper connections. Change the Battery & ds1307 device.


If any data is not coming in Hyper Terminal, then you just check the serial cable is working or not.


Otherwise you just check the code with debugging mode in Keil. If you want to see more details about debugging just see the videos in below link.



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 LPC2129 Tyro Board.
       - Tutorial of how to create & Debug a project in Keil.

Join the World's Largest Technical Community

we respect your privacy.