EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: alizare368 on July 20, 2019, 02:09:17 pm

Title: help to resolve DS1307
Post by: alizare368 on July 20, 2019, 02:09:17 pm
Hi everyone
I want to launch a DS1307 with stm32f0 ,and I used this code's , but it doesn't work.

Someone know what the problem it??
Code: [Select]
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C1_Init(void);

static uint8_t ds_rbuf[8];
char txt_buf[10];

int main(void)
{
MX_GPIO_Init();
  MX_DMA_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
uint8_t ds_tbuf[8]={0};



HAL_I2C_Master_Transmit(&hi2c1,0xd0,ds_tbuf,8,100);
lcd_initilize();


HAL_I2C_Master_Receive_DMA(&hi2c1,0xd1,ds_rbuf,8);

  /* USER CODE END 2 */

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
lcd_gotoxy(0,0);
sprintf(txt_buf,"%02d",ds_rbuf[0]);
lcd_puts(txt_buf);

  }
}

/* USER CODE BEGIN 4 */
 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hi2c);

  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
   */
HAL_I2C_Master_Receive_DMA(&hi2c1,0xd1,ds_rbuf,8);


}



/* USER CODE END 4 */
Title: Re: help to resolve DS1307
Post by: SiliconWizard on July 20, 2019, 04:34:51 pm
Hmm. If you're going to launch it, how is it ever going to come back?
 ;D
Title: Re: help to resolve DS1307
Post by: alizare368 on July 21, 2019, 02:13:16 pm
I don't understand. :-[
Title: Re: help to resolve DS1307
Post by: TK on July 21, 2019, 02:17:12 pm
DS1307 is a 5V device, STM32 is 3.3V.  How are you interfacing both ICs?  Do you have the required pull-up resistors on the i2c lines?
Title: Re: help to resolve DS1307
Post by: alizare368 on July 21, 2019, 02:29:33 pm
I pull-up the  SDA and SCL line with a 10K resistor to 3.3v.
Title: Re: help to resolve DS1307
Post by: TK on July 21, 2019, 02:32:01 pm
You need to use 5V on the DS1307 and add level shifting logic between the 2 ICs... low voltage side pulled up to 3.3V (STM32), high voltage side pulled to 5V (DS1307)
Title: Re: help to resolve DS1307
Post by: alizare368 on July 21, 2019, 02:37:52 pm
I connected VCC to 5v.
Title: Re: help to resolve DS1307
Post by: SiliconWizard on July 21, 2019, 02:42:00 pm
Yes, jokes aside, this is a 5V device. So first make sure you power it with a 5V supply.

Since it's I2C and since the DS1307's datasheet states a min voltage of 2.2V for the "1" input level, it may not need an additional level shifter. Just pulling up SDA and SCL to 3.3V should reasonably work I guess?

Now there is also the software side. Make sure you initialized the I2C peripheral correctly. Have you looked at SCL and SDA with a scope? Is there any activity to be seen on this I2C bus?
Also, your code looks fishy regarding the use of DMA.

You're issuing a "HAL_I2C_Master_Receive_DMA(&hi2c1,0xd1,ds_rbuf,8);", and then issuing the same again in the HAL_I2C_MasterRxCpltCallback() function, which doesn't seem to make sense as it is.

First try with blocking calls only. If everything is well, you can always move on to use DMA.
Title: Re: help to resolve DS1307
Post by: SiliconWizard on July 21, 2019, 02:44:07 pm
I connected VCC to 5v.

Which VCC? Supply the DS1307 with a 5V supply, yes. But do not supply the MCU with a 5V supply, you will fry it.