简体   繁体   中英

I2C Communication Problem using STM32 HAL

I'm trying to connect an STM32 Board with an Arduino over I2C.

First I checked my I2C Connection is working with the following Sample: https://www.digikey.be/en/maker/projects/getting-started-with-stm32-i2c-example/ba8c2bfef2024654b5dd10012425fa23

That works, the Terminal prints the Temperature of the Sensor.

Second I wrote a small Sketch for the Arduino who display the received I2C Message and blinks a LED:

#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
  Serial.begin(115200);
}
void receiveEvent(int bytes) {
  Serial.println("Data");
  x = Wire.read();    // read one character from the I2C
}
void loop() {
  Serial.println(x);
  //If value received is 0 blink LED for 200 ms
  if (x == 1) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 4) {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }

Here is my modified Code for STM:

buf[0] = REG_TEMP;
        ret = HAL_I2C_Master_Transmit(&hi2c1, TMP102_ADDR, buf, 1,
                HAL_MAX_DELAY);
        if (ret != HAL_OK) {
            strcpy((char*) buf, "Error Tx\r\n");
        } else {

            // Read 2 bytes from the temperature register
            ret = HAL_I2C_Master_Receive(&hi2c1, TMP102_ADDR, buf, 2,
                    HAL_MAX_DELAY);
            if (ret != HAL_OK) {
                strcpy((char*) buf, "Error Rx\r\n");
            } else {

                //Combine the bytes
                val = ((int16_t) buf[0] << 4) | (buf[1] >> 4);

                // Convert to 2's complement, since temperature can be negative
                if (val > 0x7FF) {
                    val |= 0xF000;
                }

                // Convert to float temperature value (Celsius)
                temp_c = val * 0.0625;

                // Convert temperature to decimal format
                temp_c *= 100;
                sprintf((char*) buf, "%u.%u C\r\n",
                        ((unsigned int) temp_c / 100),
                        ((unsigned int) temp_c % 100));
            }
        }

        // Send out buffer (temperature or error message)
        HAL_UART_Transmit(&huart1, buf, strlen((char*) buf), HAL_MAX_DELAY);
        HAL_Delay(200);
        //Test an Ardu

        if (HAL_I2C_Master_Transmit(&hi2c1, (9 << 1), (uint8_t*) 0x02, 1,
        HAL_MAX_DELAY) != HAL_OK) {
            strcpy((char*) buf, "Error Ardu Tx\r\n");
        } else {

            //strcpy((char*) buf, "Error Ardu Tx\r\n");
        }
        HAL_UART_Transmit(&huart1, buf, strlen((char*) buf), HAL_MAX_DELAY);

        // Wait
        HAL_Delay(500);

The related Part is that Code:

if (HAL_I2C_Master_Transmit(&hi2c1, (9 << 1), (uint8_t*) 0x02, 1,
        HAL_MAX_DELAY) != HAL_OK) {
            strcpy((char*) buf, "Error Ardu Tx\r\n");
        } else {

            //strcpy((char*) buf, "Error Ardu Tx\r\n");
        }
        HAL_UART_Transmit(&huart1, buf, strlen((char*) buf), HAL_MAX_DELAY);

The terminal prints "Error Ardu Tx" if the Arduino is not Connected to I2C so looks like the STM found the Slave Adress 0x09.

Strange Thing is, anyway what I send (currently 0x02) the Ardu Terminal prints strange Things. Almost a "84" but never "Data".

Not sure whats wrong there.

Maybe some of you can see an Error. Think on STM Side its correct.

Although I have not looked into your described issue in detail yet, I've spotted a pointer mistake in the last code block. You're type casting 0x02 to a uint8_t * type (a pointer to a uint8_t). So, on the STM32 it will effectively be used as an 32-bit address (ie 0x00000002). This might be the reason why you receive a wrong data byte on the Arduino side. The code could be modified to something like this:

    uint16_t addr = 0x09;
    uint8_t data[] = { 0x02 };
    if (HAL_I2C_Master_Transmit(&hi2c1, (addr << 1), data, sizeof(data),
        HAL_MAX_DELAY) != HAL_OK) {
        /* ... */
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM