i2C library freezes the controller

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
farhan
Posts: 2
Joined: 04 Nov 2021 07:03

i2C library freezes the controller

#1 Post by farhan » 18 Nov 2021 07:51

Hello everyone, i'm working on stm32f407 and want to interface ds3231 rtc using i2c. But the code freezes as it reaches the i2c_read() function.
I've searched on the internet but couldn't get the solution. Hoping for a swift solution here on the official forum, thanks.

Code: Select all

#define RTC_Address 0x68
#define Start_Reg 0x00


int set[7];
int get[7];
int buffer[30];

int sec, min, hr, dow, dom, mon, yr;

typedef struct {
    int seconds;
    int minutes;
    int hour;
    int dayofweek;
    int dayofmonth;
    int month;
    int year;
} TIME;

TIME time;

int decToBcd(int val)     // Convert normal decimal numbers to binary coded decimal
{
  return (int)( (val/10*16) + (val%10) );
}

int bcdToDec(int val)     // Convert binary coded decimal to normal decimal numbers
{
  return (int)( (val/16*10) + (val%16) );
}


void Get_Time (void)
{
    I2C1_Read(RTC_Address, get, 7, END_MODE_RESTART);
 //   Delay_ms(5);
	
    time.seconds = bcdToDec(get[0]);
    time.minutes = bcdToDec(get[1]);
    time.hour = bcdToDec(get[2]);
    time.dayofweek = bcdToDec(get[3]);
    time.dayofmonth = bcdToDec(get[4]);
    time.month = bcdToDec(get[5]);
    time.year = bcdToDec(get[6]);
}

void main()
{
    GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_13);
    I2C1_Init_Advanced(100000, &_GPIO_MODULE_I2C1_PB67);
    Delay_ms(100);
    UART2_Init(9600);    //For Debugging       PA2/PA3
    Delay_ms(100);

    while(1)
    {
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);
      GPIOD_ODR.B13 = ~GPIOD_ODR.B13;
      Delay_ms(500);

      Get_Time();

      sprintf (buffer, "%02d:%02d:%02d", time.hour, time.minutes, time.seconds);
      UART2_Write_Text(buffer);
      UART2_Write_Text("\t");

      sprintf (buffer, "%02d-%02d-20%02d", time.dayofmonth, time.month, time.year);
      UART2_Write_Text(buffer);
      UART2_Write_Text("\r");
    }
}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: i2C library freezes the controller

#2 Post by filip » 19 Nov 2021 09:35

Hi,

Can you try to toggle I2C pins before I2C initialization, like this :

Code: Select all

  // If bus is busy wait SDA high before initializing I2C module
  GPIO_Digital_Output(&GPIOF_BASE, _GPIO_PINMASK_1);
  GPIO_Digital_Input(&GPIOF_BASE, _GPIO_PINMASK_0);
  GPIOF_ODR.B1 = 1;
  while (GPIOF_IDR.B0 == 0) {
    GPIOF_ODR.B1 = 0;
    Delay_us(10);
    GPIOF_ODR.B1 = 1;
    Delay_us(10);
  }
  I2C2_Init_Advanced(400000, &_GPIO_MODULE_I2C2_PF01);
Regards,
Filip.

Post Reply

Return to “mikroC PRO for ARM General”