Skip to content

Instantly share code, notes, and snippets.

@Embedded-linux
Created September 4, 2013 13:58
Show Gist options
  • Select an option

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

Select an option

Save Embedded-linux/6437304 to your computer and use it in GitHub Desktop.
Note:
the operation of this experiment RTC module cannot operate
continuous without supply voltage. Because the program set to RTC clock use
same souce clock of CPU (CLKSRC bit in CCR register is ‘0’). RTC module will operate
when apply supply voltage to CPU only
//————————————————————————————————————————————————————————————————————————//
// Program : Example for Real time clock
// Description : -
// Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz
// Filename : rtc_int.c
// C compiler : Keil CARM Compiler
//————————————————————————————————————————————————————————————————————————//
#include “lpc214x.h” // Header file for Phillips LPC2148 controller
#include “sound.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
#define LED1_ON FIO0CLR = 0x00400000 // Red led 0.22 on
#define LED1_OFF FIO0SET = 0x00400000 // Red led 0.22 off
char alarm = 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_rtc(void) __irq
{
if(ILR & 0x01) // Check Interrupt block generate
{
LED1_ON; // LED on
delay_ms(100); // Delay for Blink LED
LED1_OFF; // LED off
ILR = 0x01; // Clear interrupt flag
}
if(ILR & 0x02)
{
alarm = 1; // Set flag alarm for generate sound beep
ILR = 0x02; // Clear interrupt flag
}
//————————————————— Main Program ————————————————————————//
void main()
{
char i=0;
init(); // Initialize the system
SCS = 0x03; // select the “fast” version of the I/O ports
FIO0DIR |= 0x00400000; // Config. pin P0.22 as output
uart1_init(9600);
PREINT = 0x00000392; // Set RTC Prescaler for PCLK 30 MHz
PREFRAC = 0x00004380;
CIIR = 0x00000001; // Enable seconds counter interrupt
ALSEC = 0x00000003; // Set alarm register for 3 seconds
// (match when xx:xx:03)
AMR = 0x000000FE; // Enable seconds alarm
CCR = 0x00000001; // Start RTC
VICVectAddr13 = (unsigned)isr_rtc;
VICVectCntl13 = 0x20 | 13;
VICIntEnable |= (1<<13); // Enable RTC Interrupt
while (1) // Infinite loop
{
if(alarm==1) // Check seconds alarm match
{
beep(); // Sound beep 1 time
i++; // Increment counter for sound
if(i>10) // Over 10 time?
{
i=0; // Clear counter
alarm = 0; // Clear alarm flag
}
}
printf(“TIME: %d:%d:%d \r “,HOUR,MIN,SEC);
// Display time format hh:mm:ss
delay_ms(100); // Delay for display
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment