help to resolve DS1307 - Page 1
Forum section: electronics / general

author: alizare368
author_rank: Regular Contributor
timestamp: July 20, 2019, 02:09:17 pm
content: Hi everyoneI 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: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;char txt_buf;int main(void){MX_GPIO_Init();MX_DMA_Init();MX_I2C1_Init();/* USER CODE BEGIN 2 */uint8_t ds_tbuf={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);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 */

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 20, 2019, 04:34:51 pm
content: Hmm. If you're going to launch it, how is it ever going to come back?

author: alizare368
author_rank: Regular Contributor
timestamp: July 21, 2019, 02:13:16 pm
content: I don't understand.

author: TK
author_rank: Super Contributor
timestamp: July 21, 2019, 02:17:12 pm
content: 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?

author: alizare368
author_rank: Regular Contributor
timestamp: July 21, 2019, 02:29:33 pm
content: I pull-up the SDA and SCL line with a 10K resistor to 3.3v.

author: TK
author_rank: Super Contributor
timestamp: July 21, 2019, 02:32:01 pm
content: 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)

author: alizare368
author_rank: Regular Contributor
timestamp: July 21, 2019, 02:37:52 pm
content: I connected VCC to 5v.

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 21, 2019, 02:42:00 pm
content: 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.

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 21, 2019, 02:44:07 pm
content: Quote from: alizare368 on July 21, 2019, 02:37:52 pmWhich VCC? Supply the DS1307 with a 5V supply, yes. But do not supply the MCU with a 5V supply, you will fry it.