Hello,
I want to interface DS1307 with TIVA C LM4C123GH6PM. I write this code but if i am read second,hour,min,year all have the same value=255,i can,t not understand what is the problem with it.
-darshan
#include "utils/ustdlib.h" #include "inc/hw_i2c.h" #include "driverlib/i2c.h" #include <string.h> #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/fpu.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/rom.h" #include "driverlib/sysctl.h" #include "driverlib/systick.h" #include "grlib/grlib.h" #include "utils/cmdline.h" //#include "utils/uartstdio.h" #include "fatfs/src/ff.h" #include "fatfs/src/diskio.h" #include "drivers/cfal96x64x16.h" #include "driverlib/timer.h" #include "driverlib/debug.h" #include "driverlib/adc.h" #include "driverlib/pin_map.h" #include "driverlib/uart.h" #define BYTE unsigned char #define SLAVE_ADDRESS 0xD0 extern void InitConsole(void); extern unsigned char dec2bcd(unsigned char val) ; extern unsigned char bcd2dec(unsigned char val) ; unsigned long tsec,tmin,thour,tday,tdate,tmonth,tyear; unsigned char dec2bcd(unsigned char val) { return (((val / 10) << 4) | (val % 10)); //return ((val/0xA*0x10)+(val%0xA)); } // convert BCD to binary unsigned char bcd2dec(unsigned char val) { return (((val & 0xF0) >> 4) * 10) + (val & 0x0F); //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 =21,hour = 4,day = 7,date = 20,month = 4,year = 14; unsigned char Last_Value = 1; SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //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); GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); //GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false); I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS , false); I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); I2CMasterDataPut(I2C0_MASTER_BASE, 0x00); //while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SCSR_RREQ)); 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; }*/ } }