1 |
/****************************************************/ |
---|
2 |
/* Examples Program For "CP-JR ARM7 LPC2368" */ |
---|
3 |
/* Target MCU : Philips ARM7-LPC2368 */ |
---|
4 |
/* : X-TAL : 12.00 MHz */ |
---|
5 |
/* : PLL Setup = MSEL(12),NSEL(1) */ |
---|
6 |
/* Keil Editor : uVision3 V3.53a */ |
---|
7 |
/* Compiler : Keil Realview MDK V3.20 */ |
---|
8 |
/* Create By : Eakachai Makarn (WWW.ETT.CO.TH) */ |
---|
9 |
/* Last Update : 12/November/2007 */ |
---|
10 |
/* Function : Example Read RTC + Display on UART0*/ |
---|
11 |
/****************************************************/ |
---|
12 |
// Read/Write Internal RTC of LPC2368 |
---|
13 |
// Display Result on UART0(115200,N,8,1) |
---|
14 |
|
---|
15 |
#include "LPC23xx.H" // LPC2368 MPU Register |
---|
16 |
#include <stdio.h> // For Used Function printf |
---|
17 |
|
---|
18 |
#define MASKSEC 0x3F // Second 00..59 00000000:00000000:00xxxxxx |
---|
19 |
#define MASKMIN 0x3F00 // Minute 00..59 00000000:00xxxxxx:00000000 |
---|
20 |
#define MASKHR 0x1F0000 // Hour 00..23 000xxxxx:00000000:00000000 |
---|
21 |
|
---|
22 |
// UART Buffer |
---|
23 |
char uart0_buf[]; // "sprint" UART[0] Buffer |
---|
24 |
|
---|
25 |
/* pototype section */ |
---|
26 |
void init_serial0 (void); // Initil UART-0 |
---|
27 |
void putchar0(char ch); // Put Char To UART-0 |
---|
28 |
char getchar0(void); // Get Char From UART-0 |
---|
29 |
void print_uart0(void); // Print String to UART0 |
---|
30 |
|
---|
31 |
int main(void) |
---|
32 |
{ |
---|
33 |
unsigned char Hour,Minute,Second,Last_Second; // RTC Buffer Data |
---|
34 |
init_serial0(); // Initilial UART0 = 9600,N,8,1 |
---|
35 |
sprintf(uart0_buf,"\n\n\nCP-JR ARM7 LPC236..TEST RTC\n"); |
---|
36 |
print_uart0(); |
---|
37 |
|
---|
38 |
// Initial Internal RTC Function |
---|
39 |
// Initial RTC Function |
---|
40 |
RTC_CCR = 0; // Reset All Bit Control |
---|
41 |
RTC_CCR |= 0x00000010; // CLKSRC = 1 = Used EXT 32.768 KHz |
---|
42 |
RTC_CCR |= 0x00000002; // Reset Clock |
---|
43 |
RTC_CCR &= 0xFFFFFFFD; // Release Reset |
---|
44 |
RTC_CCR |= 0x00000001; // Start RTC Clock |
---|
45 |
|
---|
46 |
//HOUR = 0x00; |
---|
47 |
//MIN = 0x00; |
---|
48 |
//SEC = 0x00; |
---|
49 |
Last_Second = 0x00; |
---|
50 |
|
---|
51 |
// Start Test Read RTC and Display on UART0 // |
---|
52 |
while(1) |
---|
53 |
{ |
---|
54 |
do // Repeat Get Second until Second Change |
---|
55 |
{ |
---|
56 |
Hour = (RTC_CTIME0 & MASKHR)>>16; // Read Hour |
---|
57 |
Minute = (RTC_CTIME0 & MASKMIN)>>8; // Read Minute |
---|
58 |
Second = RTC_CTIME0 & MASKSEC; // Read Second |
---|
59 |
} |
---|
60 |
while(Last_Second == Second); // Repeat if Second Not Change |
---|
61 |
|
---|
62 |
Last_Second = Second; // Update Current Second |
---|
63 |
|
---|
64 |
//************************************// |
---|
65 |
// Display Clock = Hour:Minute:Second // |
---|
66 |
//************************************// |
---|
67 |
sprintf(uart0_buf,"\rReal Time Clock = "); // Print Message String |
---|
68 |
print_uart0(); |
---|
69 |
sprintf(uart0_buf," %2d : %2d : %2d",Hour,Minute,Second); |
---|
70 |
print_uart0(); |
---|
71 |
} |
---|
72 |
} |
---|
73 |
|
---|
74 |
/********************************/ |
---|
75 |
/* Initial UART0 = 115200,N,8,1 */ |
---|
76 |
/********************************/ |
---|
77 |
void init_serial0 (void) |
---|
78 |
{ |
---|
79 |
PINSEL0 &= 0xFFFFFF0F; // Reset P0.2,P0.3 Pin Config |
---|
80 |
PINSEL0 |= 0x00000010; // Select P0.2 = TxD(UART0) |
---|
81 |
PINSEL0 |= 0x00000040; // Select P0.3 = RxD(UART0) |
---|
82 |
U0LCR = 0x83; // 8 bits, no Parity, 1 Stop bit |
---|
83 |
U0DLL = 3; // Baud 115200BPS for 12MHz PCLK Clock |
---|
84 |
U0FDR = 0x67; // Fractional Divider |
---|
85 |
U0LCR = 0x03; // DLAB = 0 |
---|
86 |
} |
---|
87 |
|
---|
88 |
/******************************/ |
---|
89 |
/* Write Character To UART[0] */ |
---|
90 |
/******************************/ |
---|
91 |
void putchar0 (char ch) |
---|
92 |
{ |
---|
93 |
if (ch == '\n') |
---|
94 |
{ |
---|
95 |
while (!(U0LSR & 0x20)); // Wait TXD Buffer Empty |
---|
96 |
U0THR = 0x0D; // Write CR |
---|
97 |
} |
---|
98 |
|
---|
99 |
while (!(U0LSR & 0x20)); // Wait TXD Buffer Empty |
---|
100 |
U0THR = ch; |
---|
101 |
} |
---|
102 |
|
---|
103 |
/******************************/ |
---|
104 |
/* Get character From UART[0] */ |
---|
105 |
/******************************/ |
---|
106 |
char getchar0() |
---|
107 |
{ |
---|
108 |
while (!(U0LSR & 0x01)); // Wait RXD Receive Data Ready |
---|
109 |
return (U0RBR); // Get Receice Data & Return |
---|
110 |
} |
---|
111 |
|
---|
112 |
/***************************/ |
---|
113 |
/* Print String to UART[0] */ |
---|
114 |
/***************************/ |
---|
115 |
void print_uart0(void) |
---|
116 |
{ |
---|
117 |
char *p; // Pointer Buffer |
---|
118 |
p = uart0_buf; // UART Buffer |
---|
119 |
|
---|
120 |
do // Get char & Print Until null |
---|
121 |
{ |
---|
122 |
putchar0(*p); // Write char to UART |
---|
123 |
p++; // Next char |
---|
124 |
} |
---|
125 |
while(*p != '\0'); // End of ASCII (null) |
---|
126 |
|
---|
127 |
return; |
---|
128 |
} |
---|