(1)i2c.
c
/
*************************************************************************************
*********
File
: I2C.c
Date
: Jan 2013
Target
: LPC2148
Description : Functions related to the I2C
*************************************************************************************
**********/
#include <LPC214X.H>
#include <Stdio.h>
#include "Type.h"
#include "I2C.h"
#include "TIMER.h"
/***************************************************
Contains all EEPROM related functions.
Writing to and reading from the EEPROM fuctions.
All the user data, settings and logged data
will goes into EEPROM.
I2C0 is used for interfacing.
****************************************************
*/
/*
Initialises the I2C protocol and port pins.
*/
void I2C_Init (void)
{
// Power on I2C0 peripheral
PCONP
|= 0x00000080;
// Define port pin as SDA and SCL
PINSEL0
|= 0x00000050 ;
I2C0CONCLR = 0x6C;
// clear all I2C config bits
I2C0CONSET = 0x40;
// set I2EN
// I2C Clock Duty Cycle (high and low)
I2C0SCLH
= PERIFERAL_OPERATING_FREQUENCY_IN_HZ/
(2*EEPROM_OPERATING_FREQUENCY_IN_HZ);
I2C0SCLL
= PERIFERAL_OPERATING_FREQUENCY_IN_HZ/
(2*EEPROM_OPERATING_FREQUENCY_IN_HZ);
}
/*
Waits until given status occured.
Return:
True on status occured and
False on time out
*/
BOOL I2C_WaitStatus (uint8 u8status)
{
TIMER0_RESET();
TIMER0_ENABLE();
while (T0TC < EEPROM_WAIT_TIME_OUT)
{
if (I2C0CONSET & 8) // poll SI bit
{
if (I2C0STAT == u8status)
{
TIMER0_DISABLE();
return TRUE;
}
}
}
TIMER0_DISABLE();
return FALSE;
}
/*
Reads data from EEPROM.
Return:
True on valid data and
False on time out or any error with device
*/
BOOL I2C_ReadFromEEPROM (uint32 u32startAddr, uint8 *u8ptr2arr, uint32 u32len)
{
uint32u32i;
// Check for upper limit
if (u32startAddr + u32len > EEPROM_SIZE)
return FALSE;
for (u32i=0;u32i<u32len;u32i++)
{
I2C0CONSET = 0x20;
if (!I2C_WaitStatus(0x08))
// Start set
// 0x08: ready for device address
return FALSE;
I2C0DAT
= EEPROM_DEVADDR;// addr[0]=0 means I2C write
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x18))
// clear all except I2EN
// 0x18: ready for data byte
return FALSE;
// Transmit start address - Dummy byte write
I2C0DAT
#ifndef
EEPROM_24C02
= (u32startAddr & 0x000000FF) ;
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
// clear all except I2EN
// 0x28: ACK has been received
return FALSE;
I2C0CONCLR = 0x08;
I2C0DAT
// clear SI flag
= ((u32startAddr & 0x0000FF00)>>8) &0xFF;
#endif
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
// clear all except I2EN
// 0x28: ACK has been received
return FALSE;
I2C0CONCLR = 0x08;
// clear SI flag
I2C0CONSET = 0x10;
// generate stop condition
// Read data - Sequential mode.
I2C0CONSET = 0x20;
if (!I2C_WaitStatus(0x08))
// Start set
// 0x08: ready for device address
return FALSE;
I2C0DAT
= EEPROM_DEVADDR|0x01;
// addr[0]=1
means I2C read
I2C0CONCLR = 0x28;
if (!I2C_WaitStatus(0x40))
// clear all except I2EN and AA
// 0x40: ready for data byte
return FALSE;
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x58))
// clear all except I2EN
// 0x58: data byte received return ACK
return FALSE;
u8ptr2arr[u32i]
= (uint8)I2C0DAT ;
u32startAddr++;
I2C0CONSET = 0x10;
I2C0CONCLR = 0x2C;
}
return TRUE;
}
// generate stop condition
/*
Writes data to EEPROM.
Return:
True on successful write and
False on time out or any error with device
*/
BOOL I2C_WriteToEEPROM (uint32 u32startAddr, uint8 *u8ptr2arr, uint32 u32len)
{
uint32u32i,u32j;
// Check for upper limit
if (u32startAddr + u32len > EEPROM_SIZE)
return FALSE;
// write data byte wise
for (u32i = 0; u32i < u32len; u32i++)
{
I2C0CONSET = 0x20;
if (!I2C_WaitStatus(0x08))
// Start set
// 0x08: ready for device address
return FALSE;
I2C0DAT
= EEPROM_DEVADDR;// addr[0]=0 means I2C write
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x18))
// clear all except I2EN
// 0x18: ready for data byte
return FALSE;
// Transmit start address
I2C0DAT
#ifndef
= (u32startAddr & 0x000000FF);
EEPROM_24C02
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
// clear all except I2EN
// 0x28: ACK has been received
return FALSE;
//I2C0CONCLR
= 0x08;
// clear SI flag
I2C0DAT
= ((u32startAddr & 0x0000FF00)>>8) &0xFF;
#endif
I2C0CONCLR = 0x2C;
// clear all except I2EN
if (!I2C_WaitStatus(0x28))
// 0x28: ACK has been received
return FALSE;
I2C0DAT
= (u8ptr2arr[u32i])&0x000000FF;
u32startAddr++;
I2C0CONCLR = 0x2C;
// clear all except I2EN
if (!I2C_WaitStatus(0x28))
// 0x28: ACK has been received
return FALSE;
I2C0CONSET = 0x10;
// generate stop condition
I2C0CONCLR = 0x2C;
// Poll for write done
for (u32j=20;u32j>0;u32j--)
{
I2C0CONSET = 0x20;
// Start set
if (!I2C_WaitStatus(0x08)) // 0x08: ready for device address
return FALSE;
I2C0DAT
= EEPROM_DEVADDR;// addr[0]=0 means
I2C write
I2C0CONCLR = 0x2C;
// clear all except I2EN
if (I2C_WaitStatus(0x18)) // 0x18: ready for data byte
break;
else
{
I2C0CONCLR = 0x2C;
I2C0CONSET = 0x10;
// generate stop condition
I2C0CONCLR = 0x2C;
}
}
I2C0CONSET = 0x10;
// generate stop condition
I2C0CONCLR = 0x2C;
}
return TRUE;
}
(2)main.c
/
******************************************************************
*********************************************
Project
: I2CLPC2148
File
Date
Target
: main.c
: May 2016
: LPC2148
Description : Functions related to the I2C EEPROM. The program
reads and write the data in EEPROM through I2C
******************************************************************
**********************************************/
#include <LPC214X.H>
#include <Stdio.h>
#include "Type.h"
#include "uart.h"
#include "I2C.h"
#include "TIMER.h"
/***************************************************
Main function.
***************************************************/
int main(void)
{
uint8 write_buffer[20] = {'B', 'I', 'N', 'A', 'R', 'Y',0};
uint8 read_buffer[20];
uint32 delay;
PINSEL0 |= 0x00000005 ;
UART0_Init(); // Initialize UART0
I2C_Init();
// Initialize I2C0
TIMER_Init();
// Initialize Timer
UART0_Write_Text("********* LPC2148 ARM7 I2C EEPROM
Demo **********\n\n\r");
UART0_Write_Text("Initialization done. \n\r\n");
while(1)
{
if (!I2C_WriteToEEPROM(0, write_buffer, 20))
write into EEPROM
UART0_Write_Text("\nMemory write error.");
if (!I2C_ReadFromEEPROM(0, read_buffer, 20))
// read from EEPROM
UART0_Write_Text("\nMemory Read error..");
UART0_Write_Text("\n\r The Read Data are: \t");
UART0_Write_Text((char *)read_buffer);
// display data on serial port
UART0_Write_Text("\n\r");
for(delay=0;delay<=5000000;delay++);
}
//return 0 ;
}
//
(3)timer.c
/
******************************************************************
****************************
File
Date
Target
: Timer.c
: Jan 2013
: LPC2148
Description : Functions related to the Timer
******************************************************************
*****************************/
#include <LPC214X.H>
#include "Type.h"
#include "Timer.h"
/**************************************************
Timer0 is used for delay in micro sec
Timer1 is used for time out delay
Timer is initialized to simply count at a specified
frequency(Configuration.h).
Count can be read from register TC.
****************************************************/
/*Initialises the timers*/
void TIMER_Init (void)
{
// Power ON timer peripheral
PCONP
|= 0x00000006;
// TPC: Timer Prescaler counter
// the counter is incremented once every TPC+1 cycles of
PCLK
T0PR= PRESCALER0;
//T1PR
= PRESCALER1;
// TCR: Timer Control Register
// 2: reset counters (both timer and prescaler)
// 1: enable counting
T0TCR=2;
//T1TCR=2;
}
/*waits for next tick in timer*/
void TIMER_WaitForNextTick (void)
{
uint32 start=T0TC;
while (T0TC==start) {}
// wait until timer counter
changes, then leave
}
(4)uart.c
/
******************************************************************
****************************
File
Date
Target
: UART.c
: Jan 2013
: LPC2148
Description : Functions related to the UART)
******************************************************************
*****************************/
#include <LPC21xx.h>
#include "TYPE.h"
#include "UART.h"
/***************UART-0 Functions**************/
void UART0_Init(void)
{
PINSEL0 |= 0x00000005;
//P0.0 as TX0 and P0.1 as
RX0
U0LCR = 0x83;
//Enable
access to Divisor Latches
//and Set 8 bit Character Length with 1 Stop bit and Parity
Disabled
//Access to Divisor Latches is Enabled, in order to write Baud
Rate Generator Registers
//Values to be written in Baud Rate Registers U0DLM and
U0LL
/*
Formula is
Baud_Rate = PCLK*MulVal /
[(16*(256*U0DLM+U0DLL)*(MulVal + DivAddVal))]
Example:MulVal = 1;
DivAddVal = 0;
Baud_Rate = 9600;
PCLK = 15MHz
U0DLM = 0;
Hence,
U0DLL = 15000000/(9600*16) = 97.65625 = 98
U0DLL = 98 = 0x62
*/
U0DLM = 0x00;
U0DLL = 0x62;
//Baud Rate of 9600
U0LCR = 0x03;
//Disable Access to Divisor Latches
}
void UART0_Write(unsigned char value)
{
/*
THRE bit can be extracted by this U0LSR & 0x20
THRE = 0 means data is present.
THRE = 1 means register is empty.
In order to transmit data, we have to wait will the THRE = 1,
then only we can transmit data.
*/
while(!(U0LSR&0x20));
0 stay here
U0THR = value;
}
//THRE =
void UART0_Write_Text(char * msg)
{
while(*msg)
{
UART0_Write(*msg);
msg++;
}
}
unsigned char UART0_Read(void)
{
/*
Receiver Data Ready = U0LSR.0 bit
RDR bit can be extracted by this U0LSR & 0x01
RDR = 0 means no Data is Received in U0RBR
RDR = 1 means that Data is present in U0RBR
*/
while(!(U0LSR & 0x01));
stay here
return (U0RBR);
}
/***************UART-1 Functions**************/
void UART1_Init(void)
{
//RDR = 0
PINSEL0 |= 0x00050000;
//P0.8 as TX1 and P0.9 as
RX1
U1LCR = 0x83;
//Enable
access to Divisor Latches
//and Set 8 bit Character Length with 1 Stop bit and Parity
Disabled
//Access to Divisor Latches is Enabled, in order to write Baud
Rate Generator Registers
//Values to be written in Baud Rate Registers U0DLM and
U0LL
/*
Formula is
Baud_Rate = PCLK*MulVal /
[(16*(256*U0DLM+U0DLL)*(MulVal + DivAddVal))]
Example:MulVal = 1;
DivAddVal = 0;
Baud_Rate = 9600;
PCLK = 15MHz
U0DLM = 0;
Hence,
U0DLL = 15000000/(9600*16) = 97.65625 = 98
U0DLL = 98 = 0x62
*/
U1DLM = 0x00;
U1DLL = 0x62;
//Baud Rate of 9600
U1LCR = 0x03;
//Disable Access to Divisor Latches
}
void UART1_Write(unsigned char value)
{
/*
THRE bit can be extracted by this U0LSR & 0x20
THRE = 0 means data is present.
THRE = 1 means register is empty.
In order to transmit data, we have to wait will the THRE = 1,
then only we can transmit data.
*/
while(!(U1LSR&0x20));
0 stay here
U1THR = value;
}
void UART1_Write_Text(unsigned char * msg)
{
while(*msg)
//THRE =
{
UART1_Write(*msg);
msg++;
}
}
unsigned char UART1_Read(void)
{
/*
Receiver Data Ready = U0LSR.0 bit
RDR bit can be extracted by this U0LSR & 0x01
RDR = 0 means no Data is Received in U0RBR
RDR = 1 means that Data is present in U0RBR
*/
while(!(U1LSR & 0x01));
//RDR = 0
stay here
return (U1RBR);
}
(5)i2c.h
#ifndef I2C_H
#define I2C_H
/* Settings */
#define
PERIFERAL_OPERATING_FREQUENCY_IN_HZ
#define
EEPROM_OPERATING_FREQUENCY_IN_HZ50000
#define EEPROM_DEVADDR
14745600
0xA0
#define
EEPROM_24C512
// EEPROM IC used
#define
EEPROM_SIZE
0xFFFF
// Size
#define
EEPROM_WAIT_TIME_OUT
//EEPROM_24C02
size 0x0000FF
//EEPROM_24C04
size 0x0001FF
//EEPROM_24C08
size 0x0003FF
//EEPROM_24C16
size 0x0007FF
//EEPROM_24C32
size 0x000FFF
//EEPROM_24C64
size 0x001FFF
5000 // 1msec
//EEPROM_24C128 size 0x003FFF
//EEPROM_24C256 size 0x007FFF
//EEPROM_24C512 size 0x00FFFF
/* Function Definations */
extern
void I2C_Init (void);
extern
BOOL
I2C_WaitStatus (uint8 u8status);
extern
BOOL
I2C_WriteToEEPROM (uint32 u32startAddr,
uint8 *u8ptr2arr, uint32 u32len);
extern
BOOL
I2C_ReadFromEEPROM (uint32 u32startAddr,
uint8 *u8ptr2arr, uint32 u32len);
#endif //I2C_H
(6)timer.h
/
******************************************************************
****************************
File
: Timer.h
Date
: Jan 2013
Target
: LPC2148
Description : Functions related to the Timer
******************************************************************
*****************************/
#ifndef TIMER_H
#define TIMER_H
/* Prescaler */
#define PERIFERAL_OPERATING_FREQUENCY_IN_HZ
14745600
#define DESIRED_COUNT_FREQ0
1000000
//1MHz
#define PRESCALER0
((PERIFERAL_OPERATING_FREQUENCY_IN_HZ/DESIRED_COUNT_FR
EQ0)-1)
//#define PRESCALER1
((PERIFERAL_OPERATING_FREQUENCY_IN_HZ/DESIRED_COUNT_FR
EQ1)-1)
#define
TIMER0_RESET()
T0TCR=2
#define
TIMER0_ENABLE()
T0TCR=1
#define
TIMER0_DISABLE()
T0TCR=0
/* Function declrations */
extern void TIMER_Init (void);
extern void TIMER_WaitForNextTick (void);
#endif /* TIMER_H */
(7) type.h
#ifndef TYPE_H
#define TYPE_H
#ifndef NULL
#define NULL
(0)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE
(1)
#endif
/* Arm data types */
typedef unsigned char
uint8;
typedef unsigned short int
uint16;
typedef unsigned int
uint32;
typedef char
int8;
typedef short int
int16;
typedef int
int32;
typedef unsigned char
BOOL;
/*
Data Type
Actual Cdata type Bytes allocated.
uint8
unsigned char
int8
char
1
1
uint16
unsigned short
int16
short
uint32
unsigned long
int32
long
float32
32 bit float
float64
double
*/
/*
Data type Short form
int8
i8
uint8
u8
int16
i16
uint16
u16
int32
i32
uint32
u32
4
8
float32
fl32
float64
fl64
pointer
ptr
bool
*/
#endif // TYPE_H
(8)uart.h
/
******************************************************************
****************************
File
Date
: UART.h
: Jan 2013
Target
: LPC2148
Description : Functions related to the UART)
******************************************************************
*****************************/
#ifndef UART_H
#define UART_H
void UART0_Init(void);
void UART0_Write(unsigned char value);
void UART0_Write_Text(char *msg);
unsigned char UART0_Read(void);
void UART1_Init(void);
void UART1_Write(unsigned char value);
void UART1_Write_Text(unsigned char *msg);
unsigned char UART1_Read(void);
#endif /* UART0_H */