Skip to content

Instantly share code, notes, and snippets.

@Embedded-linux
Last active December 22, 2015 07:19
Show Gist options
  • Select an option

  • Save Embedded-linux/6437250 to your computer and use it in GitHub Desktop.

Select an option

Save Embedded-linux/6437250 to your computer and use it in GitHub Desktop.
Setting Time To RTC for LPC2148
//————————————————————————————————————————————————————————————————————————//
// Program : Example for Real time clock
// Description : Example for display via Terminal program time format hh:mm:ss
// : and user can setup new time by press key ‘*’ on keyboard
// Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz
// Filename : rtc_setup.c
// C compiler : Keil CARM Compiler
//————————————————————————————————————————————————————————————————————————//
#include “lpc214x.h” // Header file for Phillips LPC2148 controller
#include “stdio.h” // Library for use puts function(For UART1)
#include “uart.h” // Library for use module UART0,UART1
#include “ctype.h” // Library for isdigit function
#include “stdlib.h” // Library for atoi function
char key = 0; // Variable for status sound alarm
//—————————————— Function for Initial system clock ————————————————//
void init()
{
PLL0CFG=0x24; // MSEL = 4,PSEL = 2
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
PLL0CON=0x1;
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked
PLL0CON=0x3; // Connect the PLL as the clock source
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash
// memory fetch (4 cclks in this case)
MAMTIM=0x4;
VPBDIV=0x02; // PCLK at 30 MHz
}
//————————————————— Function delay ———————————————————————//
void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz
{
long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 6659; j++ );
}
//—————————————— Interrupt service routine for UART1 ———————————————//
void isr_uart1(void) __irq
{
char msg;
if(((msg = U1IIR) & 0x01) == 0) // Check status flag communication
{
switch (msg & 0x0E) // Filter message
{
case 0x04: while(!(U1LSR & 0x20)); // Receive Data Available
key = U1RBR;
break;
case 0x02: break; // THRE Interrupt
default: break; // Other
}
}
VICVectAddr = 0; // Acknowledge Interrupt
}
//—————————————— Function for setup date/time for Real time clock —————————//
void rtc_uart1_setup(char *s)
{
unsigned char tm; // Buffer for keep date/time setup value
char i=0; // Variable for loop counter
for(i=0;i<2;i++) // Loop for keep value 2 byte
{
while(!isdigit(key)); // Wait for key ‘0’-’9' only
if(i==0)
tm = 10*atoi(&key); // Keep ‘1 value
if(i==1)
tm = tm+atoi(&key); // Keep ‘2 value
putchar(key); // Display key on Terminal program
key = 0; // Clear old key for next key
}
*s = tm; // Load setup new value
}
//————————————————— Main Program ————————————————————————//
void main()
{
init(); // Initialize the system
SCS = 0x03; // select the “fast” version of the I/O ports
uart1_init(9600); // Initial UART1 @9600 bps,8 bit data,1 stop bit,
// no parity bit
U1IER = 3; // Enable rx/tx interrupt for UART1
PINSEL0 |= (1<<18); // Enable RXD1(from UART1) at P0.9
VICVectAddr0 = (unsigned)isr_uart1; // Register Interrupt service routine name
VICVectCntl0 = 0x20 | 7; // UART1 Interrupt
VICIntEnable |= 1 << 7; // Enable UART1 Interrupt
CCR = 0x00000011; // Start RTC used 32.768 kHz crystal for RTCX1/RTCX2 pin
while (1) // Infinite loop
{
printf(“TIME: %d:%d:%d \r “,HOUR,MIN,SEC);
// Display time format hh:mm:ss
delay_ms(100); // Delay for display
if(key==’*’) // Check key for setup time?
{
key = 0; // Clear old key for next key
printf(“\nSet Time:”); // Display message for setup time at new
line
rtc_uart1_setup(&HOUR); // Wait until user insert new value of HOUR
uart1_putc(‘:’); // Display ‘:’ at terminal program
rtc_uart1_setup(&MIN); // Wait until user insert new value of MIN
uart1_putc(‘:’); // Display ‘:’ at terminal program
rtc_uart1_setup(&SEC); // Wait until user insert new value of SEC
printf(“\nTIME: %d:%d:%d \r “,HOUR,MIN,SEC);
// Display new time for setup at new line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment