Professional Documents
Culture Documents
c
/
*************************************************************************************
*********
File
: I2C.c
Date
: Jan 2013
Target
: LPC2148
/*
Initialises the I2C protocol and port pins.
*/
void I2C_Init (void)
{
// Power on I2C0 peripheral
PCONP
|= 0x00000080;
|= 0x00000050 ;
I2C0CONCLR = 0x6C;
I2C0CONSET = 0x40;
// set I2EN
= 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:
*/
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:
*/
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
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x18))
return FALSE;
// Transmit start address - Dummy byte write
I2C0DAT
#ifndef
EEPROM_24C02
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
return FALSE;
I2C0CONCLR = 0x08;
I2C0DAT
// clear SI flag
#endif
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
return FALSE;
I2C0CONCLR = 0x08;
// clear SI flag
I2C0CONSET = 0x10;
// Start set
// 0x08: ready for device address
return FALSE;
I2C0DAT
= EEPROM_DEVADDR|0x01;
// addr[0]=1
return FALSE;
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x58))
return FALSE;
u8ptr2arr[u32i]
= (uint8)I2C0DAT ;
u32startAddr++;
I2C0CONSET = 0x10;
I2C0CONCLR = 0x2C;
}
return TRUE;
}
/*
Writes data to EEPROM.
Return:
*/
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
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x18))
return FALSE;
EEPROM_24C02
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
return FALSE;
//I2C0CONCLR
= 0x08;
// clear SI flag
I2C0DAT
#endif
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
return FALSE;
I2C0DAT
= (u8ptr2arr[u32i])&0x000000FF;
u32startAddr++;
I2C0CONCLR = 0x2C;
if (!I2C_WaitStatus(0x28))
return FALSE;
I2C0CONSET = 0x10;
I2C0CONCLR = 0x2C;
// Poll for write done
for (u32j=20;u32j>0;u32j--)
{
I2C0CONSET = 0x20;
// Start set
I2C write
I2C0CONCLR = 0x2C;
{
I2C0CONCLR = 0x2C;
I2C0CONSET = 0x10;
I2C0CONCLR = 0x2C;
}
}
I2C0CONSET = 0x10;
I2C0CONCLR = 0x2C;
}
return TRUE;
}
(2)main.c
/
******************************************************************
*********************************************
Project
: I2CLPC2148
File
Date
Target
: main.c
: May 2016
: LPC2148
// Initialize I2C0
TIMER_Init();
// Initialize Timer
//
(3)timer.c
/
******************************************************************
****************************
File
Date
Target
: Timer.c
: Jan 2013
: LPC2148
#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.
****************************************************/
|= 0x00000006;
= PRESCALER1;
uint32 start=T0TC;
while (T0TC==start) {}
: UART.c
: Jan 2013
: LPC2148
#include "UART.h"
/***************UART-0 Functions**************/
void UART0_Init(void)
{
PINSEL0 |= 0x00000005;
RX0
U0LCR = 0x83;
//Enable
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 =
//RDR = 0
PINSEL0 |= 0x00050000;
RX1
U1LCR = 0x83;
//Enable
*/
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
/* Function Definations */
extern
extern
BOOL
extern
BOOL
BOOL
(6)timer.h
/
******************************************************************
****************************
File
: Timer.h
Date
: Jan 2013
Target
: LPC2148
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 */
uint8;
uint16;
uint32;
typedef char
int8;
int16;
typedef int
int32;
BOOL;
/*
Data Type
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