how to configure a I2C in MCB1700 with LPC1758?
I have a MCB1700 board with LPC1758, I am trying to use the I2C on it as a master to run a ultrasonic sensor srf08.
How should I configure the I2C setting on the LPC1758?
Hi Alban,
Thanks for the suggestion.
I have used the sample code from the links you gave.
The area that I do not understand is the i2cmst.c file where is the main function.
I find it so different with the step that i have learn from the website here I2C Tutorial.
therefore I dont know to make a main function to make the I2C write and read from the SRF08 sensor.
I have also ask some similar question on the LPCware forum. But still not answered my question.
The difference in the example code is that they use an array (for command/data) and constant (for slave address) instead of passing the values directly to the functions.
When the webpage says:
As an example, you have an SRF08 at the factory default address of 0xE0. To start the SRF08 ranging you would write 0x51 to the command register at 0x00 like this: 1. Send a start sequence 2. Send 0xE0 ( I2C address of the SRF08 with the R/W bit low (even address) 3. Send 0x00 (Internal address of the command register) 4. Send 0x51 (The command to start the SRF08 ranging) 5. Send the stop sequence.
As an example, you have an SRF08 at the factory default address of 0xE0. To start the SRF08 ranging you would write 0x51 to the command register at 0x00 like this:
1. Send a start sequence
2. Send 0xE0 ( I2C address of the SRF08 with the R/W bit low (even address)
3. Send 0x00 (Internal address of the command register)
4. Send 0x51 (The command to start the SRF08 ranging)
5. Send the stop sequence.
Then it translates to using the Master routine and replace the PCF8594_ADDR with 0xE0, the address of the SRF08 if you use the factory default.
Also, you adapt the length of transmission as you only want to send 0x00 and 0x51.
The example functions take care of the start and stop sequences, you only need to fill-in the variable parts.
Good luck.
Thank you for your suggestion, I had a try to test the sensor by write to the sensor address and the read a byte from the sensor.
I just want to test is the sensor connect to the I2C instead of asking sensor to give me reading of the distance.
I think my main code can preform that (please correct me if it is wrong) the i2cslavebuffer should have the data record from sensor.
Also another task is I want to show the result into the LCD screen on my board as well, how can I do it?
int main (void)
{
uint32_t i;
SystemClockUpdate();
I2C1Init( ); /* initialize I2c1 */
e.g. Start, DevAddr(W), WRByte1...WRByteN, Repeated-Start, DevAddr(R),
RDByte1...RDByteN Stop. The content of the reading will be filled
after (I2CWriteLength + two devaddr) bytes. */
I2CWriteLength[PORT_USED] = 2;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = sensor_addr;
I2CMasterBuffer[PORT_USED][1] = 0x00;
I2CMasterBuffer[PORT_USED][2] = 0x51;
I2CEngine( PORT_USED );
for ( i = 0; i < 0x200000; i++ ); /* Delay after write */
for ( i = 0; i < BUFSIZE; i++ )
I2CSlaveBuffer[PORT_USED][i] = 0x00;
}
/* Write SLA(W), address, SLA(R), and read one byte back. */
I2CWriteLength[PORT_USED] = 1;
I2CReadLength[PORT_USED] = 1;
while ( 1 );
To read, you would need to use the same function with a proper read length, I think.
The difference between the master and the slave is not sending versus receiving. The master is the one clocking and scheduling. The slave is the one responding/acting. I don't think your sensor would be a master.
I think it would be useful for you to understand the comment text just above the example text. It describes what you need to do to Write, Read and Read/Write:
/* In order to start the I2CEngine, the all the parameters must be set in advance, including I2CWriteLength, I2CReadLength, I2CCmd, and the I2cMasterBuffer which contains the stream command/data to the I2c slave device. (2) If it's a I2C read only, the number of bytes to be read is I2CReadLength, I2CWriteLength is 0, the read value will be filled in the I2CMasterBuffer.
/* In order to start the I2CEngine, the all the parameters must be set in advance, including I2CWriteLength, I2CReadLength, I2CCmd, and the I2cMasterBuffer which contains the stream command/data to the I2c slave device.
(2) If it's a I2C read only, the number of bytes to be read is I2CReadLength, I2CWriteLength is 0, the read value will be filled in the I2CMasterBuffer.
For the LCD question that's totally different: a new subject = a new thread, otherwise the subject line doesn't match the discussion and that makes it very difficult for everyone else find content.
Sorry I am a bit confuse right now.
For the I2C write to Sensor
I2CWriteLength[PORT_USED] = 3; //writelength=3 because 1 for sensor_addr(0xE8), 1 for 0x00 and 1 for0x51 right?
I2CReadLength[PORT_USED] = 0; //readlength=0 because it is write only
I2CMasterBuffer[PORT_USED][0] = sensor_addr; //first port is sensor addr telling the i2c what the sensor addr is
I2CMasterBuffer[PORT_USED][1] = 0x00; //second port is command register
I2CMasterBuffer[PORT_USED][2] = 0x51; //command for sensor start in range cm
I2CEngine( PORT_USED ); //I2CEngine do the scheduling
Do I need a delay after write and then do a read 1 byte from sensor?
I2CWriteLength[PORT_USED] = 0; //writelength=0 because read only
I2CReadLength[PORT_USED] = 1; //read 1 byte only
I2CMasterBuffer[PORT_USED][0] = sensor_addr; //telling the i2c what the sensor addr is
while ( 1 ); //loop forever
So the byte read from the sensor will be in the masterbuffer?
Sorry for having so many question, it is because this is my first to write a program and learn a I2C!
And thank you for you being patient to me
I was away from computers for a while.
Have you tried the software you wrote? That would be the best way to see if it works or not...
You can also add an oscilloscope on the line and see what is being sent.
I have used I²C a lot but I haven't used this NXP I²C module or that driver.