cancel
Showing results for 
Search instead for 
Did you mean: 

hello i'm trying to make a project whit stm32f401ve and Digital Thermometer DS1621 using I2C protocol, and I have a problem whit i2c receive. here is my source code and photos of datasheet and simulation any help will be appreciated

SEKLAMIN
Associate II

0693W00000Y9DtmQAF.png 

static const uint16_t TEMP_ADRESS = 0x48<<1;
int main(void)
{
HAL_StatusTypeDef RETURN ;
uint8_t buffer[1];
uint8_t buffer2[2];
uint16_t Val ;
float temp_c ;
 
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_I2C1_Init();
 
 buffer[0]=0xEE;
 RETURN=  HAL_I2C_Master_Transmit(&hi2c1, TEMP_ADRESS, buffer, 1,  HAL_MAX_DELAY);
               if(RETURN!=HAL_OK)
                  {
                   Lcd_cursor(&lcd, 0,0);
                   Lcd_string(&lcd, "step 1  ERUUR");
                   HAL_Delay (2000);
                  }
                  else
                  {
                   Lcd_cursor(&lcd, 0,0);
                   Lcd_string(&lcd, "step 1 DON ");
                   HAL_Delay (2000);
                   Lcd_clear(&lcd);
                  }
while (1)
  {
  buffer[0]=0xAA; 
  RETURN = HAL_I2C_Master_Transmit(&hi2c1, TEMP_ADRESS, buffer, 1,  HAL_MAX_DELAY);
           if(RETURN != HAL_OK)
           {
             Lcd_cursor(&lcd, 0,0);
             Lcd_string(&lcd, "* step 2 ERRUR*");
             HAL_Delay (2000);
             Lcd_clear(&lcd);
           }
           else
           {
             Lcd_cursor(&lcd, 0,0);
             Lcd_string(&lcd, "step 2 DON");
             HAL_Delay (2000);
             Lcd_clear(&lcd);
RETURN =  HAL_I2C_Master_Receive(&hi2c1, TEMP_ADRESS,buffer2, 2, 10000);
          if(RETURN != HAL_OK)
                 {
                   Lcd_cursor(&lcd, 0,0);
                   Lcd_string(&lcd, "*step 3 ERRUR*");
                   HAL_Delay (2000);
                   Lcd_clear(&lcd);
                 }
                 else
                 {
                   Lcd_cursor(&lcd, 0,0);
                   Lcd_string(&lcd, " step 3 DON");
                   HAL_Delay (2000);
                   Lcd_clear(&lcd);
Val = ((int16_t)buffer2[0]<<4) | (buffer2[1]>>4);
                               if(Val>0x7FF)
                               {
                                 Val|=0xF000;
                               }
                               temp_c= Val*0.0625;
                               temp_c*=100;
  sprintf((char*)buffer2,"Temp = %u.%u",((unsigned int)temp_c/100),((unsigned int)temp_c% 100));
                               Lcd_cursor(&lcd, 0,0);
                               Lcd_string(&lcd, buffer2);
                               HAL_Delay (2000);
                               Lcd_clear(&lcd);
                 }
           }

0693W00000Y9DvTQAV.png0693W00000Y9DvnQAF.png0693W00000Y9DvxQAF.png

2 REPLIES 2
S.Ma
Principal

Well, how about starting a board bringup with I2C by bit banging?

Check I2C_Master.c here

in the /B_ANALOG_ENV add-on board environment sensors folder, you'll find STTS751, Bosch, etc... slave example using the I2C Master single API.

The code deals with I2C stuck situation (SDA remaining low when Start bit generation is required).

The handy function to sweep all 7 bit slaves addresses is in brisk.c file (board bringup speedy test)

The base code was built 20 years ago on 8 bit MCU and evolved to deal with STM32C0 and other targets.

S.Ma
Principal

The temp sensor is a normal 1 byte subaddress memory device with auto increment to read/write.

Nothing peculiar.