This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Interfacing RTC DS1307 with tiva using I2c



Hi,

I want to interface RTC ds1307 with Tiva using I2C.I have gone through the working of I2c module in Tiva ,as it has been given in an example of Tiva codes.

I have interfaced the RTC module in PIC using register level programming ,but finding it difficult to implement in Tiva.

Following is my code, I have connected SCL to PB2 and SDA to PB3,along with that I have connected 4.7k pullup reistors with SCL and SDA .I am trying to print the output on my serial monitor through UART but I am not getting any response over there.Please help me debug my code!!!!

  • board-Tm4C123G Launchpad
  • crystal frequency-32.768 Khz




#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_i2c.h"
#include "i2c.h"
#include "sysctl.h"
#include "gpio.h"
#include "uartstdio.h"
#include "systick.h"
#include "uart.h"
//#include "Debug1.h"
#include "ustdlib.h"

#define BYTE unsigned char
#define SLAVE_ADDRESS 0x68
extern void InitConsole(void);
extern unsigned char dec2bcd(unsigned char val) ;
extern unsigned char bcd2dec(unsigned char val) ;



unsigned char dec2bcd(unsigned char val)
{
return ((val/0xA*0x10)+(val%0xA));
}

// convert BCD to binary
unsigned char bcd2dec(unsigned char val)
{
return ((val/0x10*0xA)+(val%0x10));
}
void InitConsole(void)
{

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

UARTStdioInit(0);
}

int main()
{
unsigned long sec=3,min =59,hour = 12,day = 4,date = 3,month = 12,year = 10,tsec,tmin,thour,tday,tdate,tmonth,tyear;
unsigned char Last_Value = 1;
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);
InitConsole();

SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysTickPeriodSet(SysCtlClockGet());
SysTickEnable();

GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);

I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS , false);

I2CMasterDataPut(I2C0_MASTER_BASE, 0);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(sec));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(min));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(hour));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(day));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(date));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(month));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterDataPut(I2C0_MASTER_BASE, dec2bcd(year));
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C0_MASTER_BASE));

while(1)
{
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS , false);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);

I2CMasterDataPut(I2C0_MASTER_BASE, 0x00);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C0_MASTER_BASE));

I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS , true);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C0_MASTER_BASE));
sec = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
min = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
hour = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
day = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
date = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
month = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
year = I2CMasterDataGet(I2C0_MASTER_BASE);

I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusy(I2C0_MASTER_BASE));

tsec = bcd2dec(sec)&0x7f;
tmin = bcd2dec(min);
thour = bcd2dec(hour)&0x3f;
tday = bcd2dec(day);
tdate = bcd2dec(date);
tmonth = bcd2dec(month);
tyear = bcd2dec(year);
if(Last_Value != tsec)
{

UARTprintf("%02d:%02d:%02d \n%02d %02d/%02d/%02d\n",thour,tmin,tsec,tday,tdate,tmonth,tyear);
Last_Value = tsec;
tsec = 0;
}

}


}