Hi everybody,
I followed Hitex's I2C example for LPC2129 and had no effect. Here is the
code I wrote for writing at a 24cxx memory at address 0, showing also what
results gave the debugger step-by-step...
//initializations made
I2CONCLR = 0x000000FF; //Clear all I2C settings (i thought that we
//should not write 1s at reserved bits)
I2CONSET = 0x00000040; //Enable the I2C interface
I2CONSET = 0x00000020; //Start condition
LPC2129 elementary I2C code NOT working
Started by ●February 5, 2008
Reply by ●February 5, 20082008-02-05
I accidentaly sent it before it was completed, sorry about that, a new and
completed message was sent...
Reply by ●February 5, 20082008-02-05
I don't know how much help it will be to you as my current LPC2000 projects
are
all polled (the simple design did not warrant an event/interupt driven system),
however, I have attached two device drivers I've written below, they have both
been tested with an LPC2119, it may be wise, to add a timeout to some of the loops, but in my case, without I2C the system is a brick anyway so I have not made time for these improvements yet....
Good luck
############################## DS1307 #################################
# Segments of this code are loosley based on code supplied by ETT
#
#include // LPC2119 MPU Register
#include
//Prototypes
void InitialiseDS1307();
char ReadDS1307(unsigned char Addr);
void WriteDS1307(unsigned char Addr, unsigned char Data);
void InitialiseDS1307(){
PCB_PINSEL0 &= 0xFFFFFF0F; // Reset GPIO0[3:2] Status
PCB_PINSEL0 |= 0x00000010; // GPIO0.2 = SCL
PCB_PINSEL0 |= 0x00000040; // GPIO0.3 = SDA
// Initial I2C Data Rate = 100KHz(VPB).4912MHz)
// I2C Data Rate = 29.4912MHz / 294
// = 100.31KHz
I2C_I2SCLH = 0x00000093; // Clock High = 147
I2C_I2SCLL = 0x00000093; // Clock Low = 147
}
char ReadDS1307(unsigned char Addr){
//Set the register of ineterst
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
//Write Device address + write
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0; // Send DS1307+Write
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
//Write destination address
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){} // Wait Slave Address+W sent, ACK recived
I2C_I2DAT = Addr; // Send target address Data DS1307
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//close the transaction
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait Until data byte sent, ACK received
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//read register of ineterst
char data=0;
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0 + 1; // Send Address +[Read]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x40){} // Wait Slave Address+R, ACK
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x58){} // Wait Slave byte received, NOT ACK
data = I2C_I2DAT;
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
return data;
}
void WriteDS1307(unsigned char Addr, unsigned char Data){
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
//Write Device address + write
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0; // Send DS1307+Write
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
//Write destination address
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){} // Wait Slave Address+W sent, ACK recived
I2C_I2DAT = Addr; // Send target address Data DS1307
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//Write actual Data
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait until Byte sent, ACK received
I2C_I2DAT = Data; // Send Output Data to PCF8574A
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//close the transaction
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait Until data byte sent, ACK received
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
}
########################## PCF8574AP #################################
# Segments of this code are loosley based on code supplied by ETT
#
#include // LPC2119 MPU Register
#include
void WritePCF8574(unsigned char Addr,unsigned char Data);
char ReadPCF8574(unsigned char Addr);
void WritePCF8574(unsigned char Addr,unsigned char Data){ // Write Output
PCF8574A
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){;;} // Wait Start Condition Complete
I2C_I2DAT = Addr; // Send PCF8574A+[Read/Write]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){;;} // Wait Slave Address+W, ACK
I2C_I2DAT = Data; // Send Output Data to PCF8574A
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){;;} // Wait Slave Address+W, ACK
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
}
char ReadPCF8574(unsigned char Addr){ // Write Output PCF8574A
char data;
WritePCF8574(Addr, 0xFF);
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){;;} // Wait Start Condition Complete
I2C_I2DAT = Addr + 1; // Send PCF8574A+[Read/Write]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x40){;;} // Wait Slave Address+R, ACK
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x58){;;} // Wait Slave byte received, NOT ACK
data = I2C_I2DAT;
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
return data;
}
On 2/5/08, t...@gmail.com wrote:
Hi everybody,
I followed Hitex's I2C example for LPC2129 and had no effect. Here is the code I wrote for writing at a 24cxx memory at address 0, showing also what results gave the debugger step-by-step...
//initializations made
I2CONCLR = 0x000000FF; //Clear all I2C settings (i thought that we //should not write 1s at reserved bits)
I2CONSET = 0x00000040; //Enable the I2C interface
I2CONSET = 0x00000020; //Start condition
all polled (the simple design did not warrant an event/interupt driven system),
however, I have attached two device drivers I've written below, they have both
been tested with an LPC2119, it may be wise, to add a timeout to some of the loops, but in my case, without I2C the system is a brick anyway so I have not made time for these improvements yet....
Good luck
############################## DS1307 #################################
# Segments of this code are loosley based on code supplied by ETT
#
#include // LPC2119 MPU Register
#include
//Prototypes
void InitialiseDS1307();
char ReadDS1307(unsigned char Addr);
void WriteDS1307(unsigned char Addr, unsigned char Data);
void InitialiseDS1307(){
PCB_PINSEL0 &= 0xFFFFFF0F; // Reset GPIO0[3:2] Status
PCB_PINSEL0 |= 0x00000010; // GPIO0.2 = SCL
PCB_PINSEL0 |= 0x00000040; // GPIO0.3 = SDA
// Initial I2C Data Rate = 100KHz(VPB).4912MHz)
// I2C Data Rate = 29.4912MHz / 294
// = 100.31KHz
I2C_I2SCLH = 0x00000093; // Clock High = 147
I2C_I2SCLL = 0x00000093; // Clock Low = 147
}
char ReadDS1307(unsigned char Addr){
//Set the register of ineterst
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
//Write Device address + write
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0; // Send DS1307+Write
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
//Write destination address
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){} // Wait Slave Address+W sent, ACK recived
I2C_I2DAT = Addr; // Send target address Data DS1307
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//close the transaction
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait Until data byte sent, ACK received
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//read register of ineterst
char data=0;
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0 + 1; // Send Address +[Read]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x40){} // Wait Slave Address+R, ACK
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x58){} // Wait Slave byte received, NOT ACK
data = I2C_I2DAT;
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
return data;
}
void WriteDS1307(unsigned char Addr, unsigned char Data){
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
//Write Device address + write
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){} // Wait Start Condition Complete
I2C_I2DAT = 0xD0; // Send DS1307+Write
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
//Write destination address
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){} // Wait Slave Address+W sent, ACK recived
I2C_I2DAT = Addr; // Send target address Data DS1307
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//Write actual Data
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait until Byte sent, ACK received
I2C_I2DAT = Data; // Send Output Data to PCF8574A
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
//close the transaction
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){} // Wait Until data byte sent, ACK received
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
}
########################## PCF8574AP #################################
# Segments of this code are loosley based on code supplied by ETT
#
#include // LPC2119 MPU Register
#include
void WritePCF8574(unsigned char Addr,unsigned char Data);
char ReadPCF8574(unsigned char Addr);
void WritePCF8574(unsigned char Addr,unsigned char Data){ // Write Output
PCF8574A
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){;;} // Wait Start Condition Complete
I2C_I2DAT = Addr; // Send PCF8574A+[Read/Write]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x18){;;} // Wait Slave Address+W, ACK
I2C_I2DAT = Data; // Send Output Data to PCF8574A
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x28){;;} // Wait Slave Address+W, ACK
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
}
char ReadPCF8574(unsigned char Addr){ // Write Output PCF8574A
char data;
WritePCF8574(Addr, 0xFF);
I2C_I2CONCLR = 0x6C; // Reset all I2C Status
I2C_I2CONSET |= 0x40; // Enable I2C Interface
I2C_I2CONSET |= 0x20; // Send Start Condition
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x08){;;} // Wait Start Condition Complete
I2C_I2DAT = Addr + 1; // Send PCF8574A+[Read/Write]
I2C_I2CONCLR = 0x28; // Clear Start Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x40){;;} // Wait Slave Address+R, ACK
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
// Wait I2C Status Return
while((I2C_I2STAT)!= 0x58){;;} // Wait Slave byte received, NOT ACK
data = I2C_I2DAT;
I2C_I2CONSET |= 0x10; // Send Stop Condition
I2C_I2CONCLR = 0x0C; // Clear Acknowledge Bit + Interrupt Flag
return data;
}
On 2/5/08, t...@gmail.com wrote:
Hi everybody,
I followed Hitex's I2C example for LPC2129 and had no effect. Here is the code I wrote for writing at a 24cxx memory at address 0, showing also what results gave the debugger step-by-step...
//initializations made
I2CONCLR = 0x000000FF; //Clear all I2C settings (i thought that we //should not write 1s at reserved bits)
I2CONSET = 0x00000040; //Enable the I2C interface
I2CONSET = 0x00000020; //Start condition