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.

I2C receives only 0x00 from DS1307

I'm using TM4C1294, Tivaware 2.1.1.71. I'm trying to read from DS1307 but i only receive value 0x00 from all registers. Here is my code, please help me

I'm sorry if my English is not so good and thanks for your help.

void BasicConfig()
{
/*
* Sets the clocking to run at 120MHz from the PLL.
*/
SysClock = SysCtlClockFreqSet((SYSCTL_OSC_INT | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_320), 120000000);
}

void UARTConfig()
{
/*
* Enable GPIO port A which is used for UART0 pins.
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

/*
* Configure the pin muxing for UART0 functions on port A0 and A1.
* This step is not necessary if your part does not support pin muxing.
*/
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

/*
* Enable UART0 so that we can configure the clock.
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

/*
* Use the internal 16MHz oscillator as the UART clock source.
*/
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

/*
* Select the alternate (UART) function for these pins.
*/
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

/*
* Initializes the UART for console I/O.
*/
UARTStdioConfig(0, 115200, 16000000);
}

void I2CConfig()
{
/*
* Enables I2C7 peripheral.
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C7);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C7);

/*
* Enables GPIO port D to use with I2C7 module.
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

/*
* Select the I2C function for these pins. This function will also
* configure the GPIO pins pins for I2C operation, setting them to
* open-drain operation with weak pull-ups.
*/
GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1);
GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);

/*
* Configure the pin muxing for I2C7 functions on port D0 and D1.
*/
GPIOPinConfigure(GPIO_PD0_I2C7SCL);
GPIOPinConfigure(GPIO_PD1_I2C7SDA);


/*
* Enables the I2C Master block.
*/
// I2CMasterEnable(I2C7_BASE);

/*
* Initializes the I2C Master block. The master block is set up
* to transfer data at standard-mode(100 Kbps)
*/
I2CMasterInitExpClk(I2C7_BASE, SysCtlClockGet(), false);
}

void SCCB_set(uint8_t address, uint8_t reg, uint8_t data)
{
/*
* Specify that we want to communicate to device address with an intended write to bus
*/
I2CMasterSlaveAddrSet(I2C7_BASE, address, false);

/*
* Register to be read
*/
I2CMasterDataPut(I2C7_BASE, reg);

/*
* Send control byte and register address byte to slave device
*/
I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_SEND_START);

/*
* Wait for MCU to finish transaction
*/
while(I2CMasterBusy(I2C7_BASE));

/*
* Specify data to be written to the above mentioned register
*/
I2CMasterDataPut(I2C7_BASE, data);

/*
* Send data to slave
*/
I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

/*
* Wait for MCU & device to complete transaction
*/
while(I2CMasterBusy(I2C7_BASE));
}

uint8_t SCCB_get(uint8_t address, uint8_t reg)
{
I2CMasterSlaveAddrSet(I2C7_BASE, address, false);

I2CMasterDataPut(I2C7_BASE, reg);

I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_SEND_START);

while(I2CMasterBusy(I2C7_BASE));

I2CMasterSlaveAddrSet(I2C7_BASE, address, true);

I2CMasterControl(I2C7_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);

while(I2CMasterBusy(I2C7_BASE));


return(I2CMasterDataGet(I2C7_BASE));
}

void main(void)
{
BasicConfig();
UARTConfig();
I2CConfig();
SCCB_set(0x68, 0x02, 0x01); // hour
SCCB_set(0x68, 0x01, 0x01); // minute
SCCB_set(0x68, 0x00, 0x05); // second
while(1)
{
UARTprintf("\n%02d", SCCB_get(0x68, 0x02));
UARTprintf("-%02d", SCCB_get(0x68, 0x01));
UARTprintf("-%02d", SCCB_get(0x68, 0x00));
SysCtlDelay(SysCtlClockGet());
}
}

  • Hello Tuan,

    Thank you for the question. I am moving this to the TM4C micro controller forum for better support since this is related to your TM4C processor.
  • Hello Tuan,

    TM4C1294 does not use SysCtlClockGet(). Instead you must use the return value of SysCtlClockFreqSet for getting the clock frequency.

    Secondly

    SysClock = SysCtlClockFreqSet((SYSCTL_OSC_INT | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_320), 120000000);

    will not yield 120MHZ as 320/120 is not an integer. Replace 320 with 480.

    Regards
    Amit
  • I changed my code and it works, thank you. Can I ask you one more question? What is the third parameter of UARTStdioConfig used for? When I config that to 160000000, it works, but when I write SysClock to that place, it returns wrong value
  • Hello Tuan,

    The 3rd parameter is the clock source for the UART. Since you have used the API call UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC) the 16MHz clock is routed to the UART. Hence only 16000000 will work. If you instead replace it bu UART_CLOCK_SYSTEM then the SysClock value will work.

    Regards
    Amit