Real time clock (RTC) using Cortex m4

This example describes how to use the internal RTC module in lpc4088 cortex m4. we have used cortex m4 development board from pantech.

Steps for implementing RTC using Cortex m4

  1. Power: In the PCONP register, set bit PCRTC.
  2. Select the Clock from RTC oscillator.
  3. Set current time and date
  4. Clear Interrupts (Just to ensure there is no interrupts pending)
  5. Get time and date and display its value in UART0

RTC using Cortex m4 -C source code

/*Inbuilt RTC*/
#include <LPC407x_8x_177x_8x.h>
#include "uart0.h"

unsigned char welstring[]="Inbuilt RTC demo:\r\n",comp=0;

void init_rtc(void);

int main(void)
{
  init_serial();  
  init_rtc();
  UART0_puts(welstring);
  while(1)
  {
     //(Optional) Just to send one set of data per second    
     //Else it will send data continuosly
     while(comp == LPC_RTC->SEC);      //Delete this line if u need continuos data  
   
     //5.Get time and date and display its value in UART0
     UART0_Txmt(LPC_RTC->DOM/10 + '0'); 
     UART0_Txmt(LPC_RTC->DOM%10 + '0'); 
     UART0_Txmt('/');
     UART0_Txmt(LPC_RTC->MONTH/10 + '0');  //Separates 10th digit and adds ascii '0' to make it ascii
     UART0_Txmt(LPC_RTC->MONTH%10 + '0');  //Separates single digit
     UART0_Txmt('/');
     UART0_Txmt(LPC_RTC->YEAR/10 + '0');
     UART0_Txmt(LPC_RTC->YEAR%10 + '0');
     UART0_Txmt(' ');
    
     UART0_Txmt(LPC_RTC->HOUR/10 + '0');
     UART0_Txmt(LPC_RTC->HOUR%10 + '0'); 
     UART0_Txmt(':');
     UART0_Txmt(LPC_RTC->MIN/10 + '0');
     UART0_Txmt(LPC_RTC->MIN%10 + '0');
     UART0_Txmt(':');
     UART0_Txmt(LPC_RTC->SEC/10 + '0');
     UART0_Txmt(LPC_RTC->SEC%10 + '0');
     UART0_Txmt('\r');
     comp = LPC_RTC->SEC;          //Update the current value (optional)
  }
}

void init_rtc(void)
{
 //1. set bit PCRTC
 LPC_SC->PCONP |= (1 << 9);       /* enable power to RTC*/ 
 //2. Clock Reset
 LPC_RTC->CCR  = 0x13;  
 //2. Enable Clock
 LPC_RTC->CCR  = 0x11;         
 
 //3. Set Current date and time
 LPC_RTC->SEC  = 0x00;         
 LPC_RTC->HOUR = 0x00;
 LPC_RTC->MIN  = 0x00; 
 LPC_RTC->DOM  = 0;
 LPC_RTC->MONTH= 0;
 LPC_RTC->YEAR = 0;
 
 //4. Clear RTC Interrupt
 LPC_RTC->ILR  = 0x01;
}



Pantech:
Leave a Comment

This website uses cookies.