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");
}
}