#include #include #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 //#include "inc/tm4c129xnczad.h" #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 void I2C_Init(); bool SetRTCRegister(uint8_t nReg,uint8_t nVal); uint8_t GetRTCRegister(uint8_t nReg); void DisplayCurrentRTC(); void RTCInit(); void RTC_2_WRITE(); void delay(int num); void DelayMs(); uint32_t RTC_FLAG = 0,RTC_FLAG1 = 0,loop,loop1; uint32_t i = 0; uint8_t hr1 = 0,min1 = 0,sec1 = 0,date1 = 0,month1 = 0,year1 = 0; #define RTC_SLAVE_ADDR ( 0xD0 >> 1) //Slave address specific to Bq 32000 (Fixed) void delay(int num) { for(loop = 0;loop < num;loop++) { for(loop1 = 0;loop1 < 200000;loop1++); } } void DisplayCurrentRTC() { uint8_t hr,min,sec; uint8_t date,month,year; sec = GetRTCRegister(SEC_REG); min = GetRTCRegister(MIN_REG); hr = GetRTCRegister(HOUR_REG); date = GetRTCRegister(DATE_REG); month = GetRTCRegister(MONTH_REG); year = GetRTCRegister(YEAR_REG); // UARTprintf("******Date = %02x-%02x-20%02x******Hour:Min:Sec = %x:%x:%x********\n",date,month,year,hr,min,sec); UARTprintf("******Date = %02x-%02x-20%02x******Hour:Min:Sec = %02x:%02x:%02x********\n",date,month,year,hr,min,sec); } void I2C_Init() { 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,16000000,true); } bool SetRTCRegister(uint8_t nReg,uint8_t nVal) { //first set the RTC device address I2CMasterSlaveAddrSet(I2C0_BASE,RTC_SLAVE_ADDR,false); //Write the RTC sub address. I2CMasterDataPut(I2C0_BASE,nReg); //Continue transfer.. I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_START); //Wait until current byte has been transferred. while(I2CMasterBusy(I2C0_BASE)){} //Write the next byte to the controller I2CMasterDataPut(I2C0_BASE,nVal); //End the transfer I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH); //Give some delay-- SysCtlDelay(50000); return true; } uint8_t GetRTCRegister(uint8_t nReg) { uint8_t nVal = 0; uint8_t nVal1 = 0; uint8_t nVal2 = 0; //write the RTC device address first I2CMasterSlaveAddrSet(I2C0_BASE,RTC_SLAVE_ADDR,false); //write the sub address/register to be read from I2CMasterDataPut(I2C0_BASE,nReg); //perform a single send, write the register address only I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_START);//I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_SEND); //wait until the current byte has been transferred while(I2CMasterBusy(I2C0_BASE)){} //Put the master in receive mode now I2CMasterSlaveAddrSet(I2C0_BASE,RTC_SLAVE_ADDR,true); //start the receive I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE); //wait until the current byte has been read while(I2CMasterBusy(I2C0_BASE)){} //read the received character nVal = I2CMasterDataGet(I2C0_BASE); I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH); switch(nReg) { case MIN_REG: nVal = ((nVal & 0x70)>>4)| 0x30 ; nVal = (nVal & 0x0F)|0x30; // this gives you tens-of-minutes ('3') nVal2 = nVal | nVal1;// nVal = nVal|nVal1; //nVal &= 0x7F; break; case HOUR_REG: nVal = ((nVal & 0x30)>>4)| 0x30 ; nVal1 = (nVal & 0x0F)|0x30; //nVal &= 0x3F; nVal2 = nVal | nVal1;// nVal = nVal|nVal1; break; case DATE_REG: nVal = ((nVal & 0x30)>>4)| 0x30 ; nVal1 = (nVal & 0x0F)|0x30;//nVal &= 0x3F; nVal2 = nVal | nVal1; // nVal = nVal|nVal1; break; case MONTH_REG: nVal = ((nVal & 0x10)>>4)| 0x30 ; nVal1 = (nVal & 0x0F)|0x30;//nVal &= 0x1F; nVal2 = nVal | nVal1;// nVal = nVal|nVal1; break; case YEAR_REG: break; case SEC_REG: nVal = ((nVal & 0x70)>>4)| 0x30 ; nVal1 = (nVal & 0x0F)|0x30;//nVal &= 0x7F; //nVal &= 0x7F; as per pic example nVal2 = nVal | nVal1;// nVal = nVal|nVal1; break; default: UARTprintf("Wrong RTC register selected"); } return nVal2; } }