Hi everyone
I am santosh prasad new to tiva c series controllers. I am using TM4C129encpdt controller and interfaced with BQ2000. I2C is the protocol that is used to communicate with RTC BQ32000. I am able to read and write to the RTC register only once ,when i make the second write the read it back there is no change in the data and if i try to read the data from rtc continously after a single write i get default values i.e date register shows 00 month 00 year 2000 hrs 00 min 00 sec 00. Please help me on this how to read and write to the rtc repeatedly.
i am placing the code below
Thanks in advance
santosh prasad s
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/rom.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include <stdint.h>
//#include "inc/tm4c129xnczad.h"
#define RTC_SLAVE_ADDR ( 0xD0 >> 1)
#define SEC_REG 0x00
#define MIN_REG 0x01
#define HOUR_REG 0x02
#define DAY_REG 0x03 //MON, TUE, WED,..
#define DATE_REG 0x04
#define MONTH_REG 0x05
#define YEAR_REG 0x06
uint8_t GetRTC( uint8_t nReg);
bool SetRTCRegister( uint8_t nReg, uint8_t nVal);
void RTCTest(void);
void RTCTest()
{
uint8_t hr,min,date,month,year,sec;
sec = GetRTC(0x00);
min = GetRTC(0x01);
hr = GetRTC(0x02);
date = GetRTC(0x04);
month = GetRTC(0x05);
year = GetRTC(0x06);
UARTprintf("**Date= %02x-%02x-20%02x*** Hour:Min:Sec = %x:%x:%x**",date,month,year,hr,min,sec);
}
bool SetRTCRegister(uint8_t nReg,uint8_t nVal)
{
I2CMasterDataPut(I2C0_BASE,nReg);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_START);//I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_BASE)){}
I2CMasterDataPut(I2C0_BASE,nVal);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);//I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_SEND);
return true;
}
uint8_t GetRTC(uint8_t nReg)
{
uint8_t nVal;
nVal = I2CMasterDataGet(I2C0_BASE);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusy(I2C0_BASE)){}
switch(nReg)
{
case MIN_REG:
nVal &= 0x7F;
break;
case HOUR_REG:
nVal &= 0x3F;
break;
case DATE_REG:
nVal &= 0x3F;
break;
case MONTH_REG:
nVal &= 0x1F;
break;
case YEAR_REG:
case SEC_REG:
nVal &= 0xFF;
break;
default:
UARTprintf("Wrong RTC register selected\n");
}
return nVal;
}
void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // UART0 POARTA
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX)
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); //Enabling the UART
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); //Using !6mhz internal clock sourc
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 115200, 16000000); //Initialising the UART
}
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
I2CMasterEnable(I2C0_BASE);
I2CMasterInitExpClk(I2C0_BASE,160000000,true);
InitConsole();
SysCtlDelay(50000);
SysCtlDelay(50000);
SysCtlDelay(50000);
I2CMasterSlaveAddrSet(I2C0_BASE,RTC_SLAVE_ADDR,false);
SetRTCRegister(0x00,0x00);
SetRTCRegister(0x01,0x02);
SetRTCRegister(0x02,0x10);
SetRTCRegister(0x04,0x11);
SetRTCRegister(0x05,0x06);
SetRTCRegister(0x06,0x12);
I2CMasterSlaveAddrSet(I2C0_BASE,RTC_SLAVE_ADDR,true);
RTCTest();
while(1)
{
}
}