This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi there
I would like to read a register from the TI temperature sensor TMP117 with my microcontroller. The picture below shows the desired behavior:
Here is my init()-function. As I see some behavior on the I2C-Lines, I guess it works.
void I2C_init(void) { // Enable the I2C0 peripheral MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); I2CMasterEnable(I2C1_BASE); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1); // Wait for the I2C0 module to be ready. while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C1)) { } MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Configure the GPIO Pin Mux for PA6 for I2C1SCL MAP_GPIOPinConfigure(GPIO_PA6_I2C1SCL); MAP_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); // Configure the GPIO Pin Mux for PA7 for I2C1SDA MAP_GPIOPinConfigure(GPIO_PA7_I2C1SDA); MAP_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); // Initialize Master and Slave I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false); while(I2CMasterBusBusy(I2C1_BASE)) { } }
With the following code i want to:
uint32_t I2C_readThisRegister() { uint8_t slaveAddress = 0b10010010; uint32_t registerToRead = 0x00001111; //------WRITE-Section---------------------------------------- I2CMasterSlaveAddrSet(I2C1_BASE, slaveAddress, false); I2CMasterDataPut(I2C1_BASE, registerToRead); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START); while(I2CMasterBusBusy(I2C1_BASE)) { } //------READ-Section---------------------------------------- I2CMasterSlaveAddrSet(I2C1_BASE, slaveAddress, true); // set slave address //read first Byte I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); while(I2CMasterBusBusy(I2C1_BASE)) { } uint32_t msb=I2CMasterDataGet(I2C1_BASE); //read second Byte I2CMasterControl( I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while(I2CMasterBusBusy(I2C1_BASE)) { } uint32_t lsb=I2CMasterDataGet(I2C1_BASE); return ((msb << 8)| lsb); }
The oscilloscope shows that only the WRITE part is executed. The clock is not working in the READ part. It looks like the READ part is ignored.
I have already tried different variants, but I can't get the values to be read in.
PS: The addresses in the example are not especially useful. As soon as the I2C interface works properly, I will add the code so that it drives the sensor correctly.
Thank you for your help! It is much appreciated!
Best regards
Patrik
Hi
The clock is not working in the READ part. It looks like the READ part is ignored.
Can you show your scope cap for the entire operation? What do you mean the clock is not working?
Please refer to the below example that may help.
C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c123gxl-boostxl-senshub\humidity_sht21_simple
Dear Charles
Thanks for your recommendation. I must have copied the wrong command. they look similar. Beside this the i2c address was 8 bit long instead of 7bits. I post the working code below in case someone else has a similar problem.
init()
uint32_t I2C_init(void) { // Enable the I2C0 peripheral MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1); I2CMasterEnable(I2C1_BASE); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1); // Wait for the I2C0 module to be ready. while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C1)) { } MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Configure the GPIO Pin Mux for PA6 for I2C1SCL MAP_GPIOPinConfigure(GPIO_PA6_I2C1SCL); MAP_GPIOPinTypeI2CSCL(GPIO_PORTA_BASE, GPIO_PIN_6); // Configure the GPIO Pin Mux for PA7 for I2C1SDA MAP_GPIOPinConfigure(GPIO_PA7_I2C1SDA); MAP_GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_7); // Initialize Master and Slave I2CMasterInitExpClk(I2C1_BASE, SysCtlClockGet(), false); while(I2CMasterBusBusy(I2C1_BASE)) { } return 0; }
function to write 16Bit to a Register:
uint32_t I2C_write16BitRegister(uint8_t slaveAddress, uint32_t addressPointer, uint8_t firstByte, uint8_t secondByte) { I2CMasterSlaveAddrSet(I2C1_BASE, slaveAddress, false); I2CMasterDataPut(I2C1_BASE, addressPointer); // pointer auf Unlock register I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_START); while(I2CMasterBusy(I2C1_BASE)) { } I2CMasterDataPut(I2C1_BASE, firstByte); // Bit 15 setzen I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); while(I2CMasterBusy(I2C1_BASE)) { } I2CMasterDataPut(I2C1_BASE, secondByte); I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); while(I2CMasterBusy(I2C1_BASE)) { } return 0; }
function to write address pointer and then read two bytes:
uint32_t I2C_read16BitRegister(uint8_t slaveAddress, uint8_t registerAddress) { /* * Einzelne Schreibaktion spricht den Sensor an und * stellt den Address-Pointer des Sensors auf jenes Register, * welches im zweiten Teil gelesen werden soll. */ I2CMasterSlaveAddrSet(I2C1_BASE, slaveAddress, false); // set slave address I2CMasterDataPut(I2C1_BASE, registerAddress); // set register to be read I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND); while(I2CMasterBusy(I2C1_BASE)) { } /*---------------------------------READ-Section---------------------------------------- * Hier zwei Bytes im Burstmode auslesen und zusammensetzen */ I2CMasterSlaveAddrSet(I2C1_BASE, slaveAddress, true); // set slave address //read first Byte I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); while(I2CMasterBusy(I2C1_BASE)) { } uint32_t msb=I2CMasterDataGet(I2C1_BASE); //read second Byte I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); while(I2CMasterBusy(I2C1_BASE)) { } uint32_t lsb=I2CMasterDataGet(I2C1_BASE); return ((msb << 8) | lsb); }
Thanks for your help.
Best regards