#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); void GetRTCRegister(uint8_t nReg); void DisplayCurrentRTC(); void RTCInit(); void RTC_2_WRITE(); void delay(int num); static uint8_t timedate[] = "20yy:mm:dd - hh:mm:ss"; const uint8_t ascii[] = "0123456789ABCD"; 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() { GetRTCRegister(SEC_REG); GetRTCRegister(MIN_REG); GetRTCRegister(HOUR_REG); GetRTCRegister(DATE_REG); GetRTCRegister(MONTH_REG); GetRTCRegister(YEAR_REG); UARTprintf("%s\n",&timedate); } 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; } void GetRTCRegister(uint8_t nReg) { uint8_t nVal = 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_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); switch(nReg) { case SEC_REG: timedate[19] = ascii[(nVal & 0x70) >> 4]; timedate[20] = ascii[nVal & 0x0F]; break; case MIN_REG: timedate[16] = ascii[(nVal & 0x70) >> 4]; timedate[17] = ascii[nVal & 0x0F]; break; case HOUR_REG: timedate[13] = ascii[(nVal & 0x30) >> 4]; timedate[14] = ascii[nVal & 0x0F]; break; case DATE_REG: timedate[8] = ascii[(nVal & 0x30) >> 4]; timedate[9] = ascii[nVal & 0x0F]; break; case MONTH_REG: timedate[5] = ascii[(nVal & 0x10) >> 4]; timedate[6] = ascii[nVal & 0x0F]; break; case YEAR_REG: timedate[2] = ascii[(nVal & 0xF0) >> 4]; timedate[3] = ascii[nVal & 0x0F]; break; default: UARTprintf("Wrong RTC register selected"); } return ; } void RTCInit() { SetRTCRegister(SEC_REG,0x10); // 0x02 SetRTCRegister(MIN_REG,0x45); // 0x10 SetRTCRegister(HOUR_REG,0x09); //18 // 0x11 SetRTCRegister(DATE_REG,0x01); // 0x06 SetRTCRegister(MONTH_REG,0x06); // 0x12 SetRTCRegister(YEAR_REG,0x14); } void InitConsole(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // UART0 POARTA GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); //Using!6mhz internal clock source GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0,115200,16000000); } int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); I2C_Init(); // RTCInit(); ROM_IntMasterEnable(); InitConsole(); // DisplayCurrentRTC(); while(1) { delay(500); delay(500); delay(500); RTC_FLAG++; if(RTC_FLAG > 2000000) { for(i = 0;i < 2000000;i++); if(RTC_FLAG1 > 2) { RTC_FLAG1 = 0; DisplayCurrentRTC(); } for(i = 0;i < 2000000;i++); RTC_FLAG = 0; RTC_FLAG1++; } delay(500); delay(500); delay(500); } }