3) ARM7 LPC2148 interfacing with I2C-RTC

advertisement
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 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 serial port.
NXP’s ARM7 (LPC2148), ARM Primer 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 2wire, 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 12hour 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.
Fig. 1 Interfacing I2C - RTC to Microcontroller
Interfacing I2C – RTC with LPC2148
Read date & time by using I2C - RTC in LPC2148 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 LPC2148 Primer 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 LPC2148 Primer Kit by using these SDA & SCL I2C lines.
Pin Assignment with LPC2148
I2C RTC
LPC2148 Lines
SCL
SCL1 - (P0.11)
DS1307
SDA
LPC2148
Source Code
SDA1 - (P0.14)
Real Time Clock
Circuit
Diagram to
Interface I2C–
RTC with
The Interfacing I2C – RTC with LPC2148 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 LPC2148
******************************************************************************
*********
Title : Program to read date & time of I2C - RTC
******************************************************************************
*********
#include <LPC214x.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "UART.h"
#define MAX
#define AA
8
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 (I2C1CONSET & 0x08)
{
switch (I2C1STAT)
{
case (0x08):
if (flag == 'W')
{
I2C1CONCLR = 1 << STO;
I2C1CONCLR = 1 << STA;
I2C1CONSET = 1 << AA;
I2C1DAT
= 0xD0;
I2C1CONCLR = 1 << SI;
}
else if (flag == 'R')
{
I2C1DAT
= 0xD0;
I2C1CONSET = 1 << STA;
I2C1CONCLR = 1 << SI;
}
index = 0;
break;
case (0x10):
I2C1CONCLR = 1 << STA;
if (flag == 'W')
{
I2C1DAT = 0xD0;
}
else if (flag == 'R')
{
I2C1DAT
= 0xD1;
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << AA;
I2C1CONCLR = 1 << SI;
index = 0;
}
break;
case (0x18):
if (flag == 'W')
{
I2C1DAT
= 0x00;
index=0;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
}
else
{
I2C1DAT = 0x00;
index
= 0;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << AA;
}
I2C1CONCLR = 1 << SI;
break;
case (0x20):
if (flag == 'W')
{
I2C1DAT
&= 0x7F;
index=0;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
}
else
{
I2C1DAT = 0xD1;
index
= 0;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << AA;
}
I2C1CONCLR = 1 << SI;
break;
case (0x28):
if (index < MAX && flag == 'W')
{
I2C1DAT
= Buff[index];
I2C1CONCLR = 0x20;
I2C1CONCLR = 0x38;
index++;
}
else if (flag == 'R'
{
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << STA;
I2C1CONCLR = 1 << SI;
}
else
{
index = 0;
flag = 'R';
I2C1CONSET = 1 << STO;
I2C1CONCLR = 1 << AA;
I2C1CONSET = 1 << STA;
I2C1CONCLR = 1 << SI;
}
break;
case (0x30):
if (index < MAX && flag == 'W')
{
I2C1DAT
= Buff[index];
I2C1CONCLR = 0x20;
//Clear START Bit
I2C1CONCLR = 0x38;
index++;
}
else if (flag == 'R')
{
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << STA;
I2C1CONCLR = 1 << SI;
}
else
{
index = 0;
flag = 'R';
I2C1CONSET = 1 << STO;
I2C1CONCLR = 1 << AA;
I2C1CONSET = 1 << STA;
I2C1CONCLR = 1 << SI;
}
break;
case (0x38):
I2C1CONSET = 0x20;
break;
case (0x40:
if (index < MAX)
{
Rec [index] = I2C1DAT;
index++;
I2C1CONSET = 1 << AA;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
}
else
{
index = 0;
I2C1CONCLR = 1 << AA;
I2C1CONSET = 1 << STO;
I2C1CONCLR = 1 << STA;
}
I2C1CONCLR = 1 << SI;
break;
case (0x48):
if (index < MAX)
{
Rec [index] = I2C1DAT;
index++;
I2C1CONSET = 1 << AA;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
}
else
{
index = 0;
I2C1CONCLR = 1 << AA;
I2C1CONSET = 1 << STO;
I2C1CONCLR = 1 << STA;
}
I2C1CONCLR = 1 << SI;
break;
case (0x50):
I2C1CONSET = 1 << AA;
if (index < MAX)
{
Rec [index] = I2C1DAT;
I2C1CONSET = 1 << AA;
I2C1CONCLR = 1 << STA;
I2C1CONCLR = 1 << STO;
index++;
}
else
{
I2C1CONCLR = 1 << STO;
I2C1CONCLR = 1 << AA;
I2C1CONCLR = 1 << SI;
index = 0;
Ready = 'T';
}
break;
case (0x58):
I2C1CONSET = 1 << STO;
I2C1CONSET = 1 << STA;
I2C1CONSET = 1 << AA;
I2C1CONCLR = 1 << SI;
flag = 'R';
index = 0;
Ready = 'T';
break;
}
}
I2C1CONCLR = 1 << SI;
VICVectAddr = 0x00;
}
void UART0_ISR (void)__irq
{
char Msg;
if(((Msg = U0IIR) & 0x01) == 0)
{
switch (Msg & 0x0E)
{
case 0x04: while (!(U0LSR & 0x20));
Key = U0RBR;
case 0x02: break;
default
: break;
}
}
VICVectAddr = 6;
}
int main()
{
unsigned char i, AP;
VPBDIV = 0x02;
PINSEL0 = 0x00000005;
PINSEL0 |= 0x30C00000;
IODIR1 = 0x00 << SW3;
serial_init ();
U0IER = 3;
VICIntSelect = 0<<6;
VICVectCntl6 = 0x020 | 6;
VICVectAddr6 = (unsigned long)UART0_ISR;
VICIntEnable = 1 << 6;
VICIntSelect = 0<<19;
VICVectCntl0
= 0x020 | 19 ;
VICVectAddr0
VICIntEnable
= (unsigned long)I2C_ISR;
= 1<<19;
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 (">
Turn SW3 ON and Check
Hyperteminal to Set Time...\n\r");
UART0_PutS ("\n\r_____________________________________
______________\n\n\r");
Wait (5000);
flag = 'W';
I2C_Start ();
for (i=0;i<30;i++) Wait(10000);
I2C1CONCLR = 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)
{
I2C1SCLH = 150;
I2C1SCLL = 150;
I2C1CONSET = 1 << I2EN; //Enable I2C 0
}
int I2C_Start (void)
{
I2C1CONCLR = 1 << STO;
I2C1CONSET = 1 << AA;
I2C1CONSET = 1 << STA;
return 0;
}
int I2C_Write (unsigned char *Buff, unsigned int Count)
{
while(Count--)
{
I2C1DAT
= *Buff++;
}
return 0;
}
void RTC_Setup(char *Buff)
{
unsigned char TimE;
char i=0;
for(i=0;i<2;i++)
{
while(!isdigit(Key));
if (i==0)
{
TimE = Key - '0';
TimE
<<= 4;
}
if (i==1)
{
TimE |= Key - '0';
}
//putchar(Key);
Key
= 0;
}
*Buff = TimE;
}
unsigned char BCD2HEX (unsigned char BCD)
{
unsigned char HEX;
HEX = (BCD >> 4)*10 | (BCD & 0x0F);
return HEX;
}
void serial_init(void)
{
PINSEL0 |= 0x00000005;
U0LCR = 0x83;
U0DLL = 195;
U0LCR = 0x03;
}
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
LPC2148 Primer Board.
The Flash Magic software is used to download the hex file into your microcontroller IC
LPC2148 through UART0.
Testing the I2C – RTC with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; the RTC Battery device is connected with
the LPC2148 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 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 LPC2148 Primer Board,
then you just check the jumper connections. Change the Battery & ds1307 device.
Download