RX210のRTC機能を使えるようにプログラムしてみました。時刻と日付はGPSより取得するようにしました。
- /*
- * RX Family RTC Module Using Firmware Integrated Technology
- * http://japan.renesas.com/support/downloads/download_results/C1000000-C9999999/mpumcu/rx/an_r01an1817eu_rx_rtc.jsp
- */
-
- #include <stdio.h>
- #include <machine.h>
- #include "iodefine.h"
- #include <string.h>
- #include "gps.h"
- /*
- * RTCの初期化
- */
- void initRTC(void)
- {
- /* Set subclock drive capacity */
- RTC.RCR3.BIT.RTCDV = 0x06; //標準CL用ドライブ能力
- while(RTC.RCR3.BIT.RTCDV != 0x06); //Confirm that it has changed, it's slow.
- /* Enable the subclock for RTC */
- RTC.RCR3.BIT.RTCEN = 1; // enable subclock
- while(RTC.RCR3.BIT.RTCEN != 1);
- /* Disable RTC interrupts */
- RTC.RCR1.BYTE = 0x00u;
- /* Stop RTC counter */
- RTC.RCR2.BIT.START = 0;
- while(RTC.RCR2.BIT.RESET != 0) ;
- /*RCR2レジスタのb7を“0”にする*/
- RTC.RCR2.BYTE &= 0x7F;
- /* RTCは24時間モードで動作 */
- RTC.RCR2.BIT.HR24 = 1;
- /* Clear alarms, capture regs, adjustment regs, and output enable */
- RTC.RCR2.BIT.RESET = 1;
- /* Wait for reset to happen before continuing.*/
- while(RTC.RCR2.BIT.RESET != 0) ;
- /* Insure ICU RTC interrupts disabled */
- IEN(RTC,PRD) = 0;
- IEN(RTC,ALM) = 0;
- IEN(RTC,CUP) = 0;
- /* Enable RTC interrupts (PIE, CIE and AIE), not ICU yet */
- RTC.RCR1.BYTE = 0x07u;
- while (RTC.RCR1.BYTE != 0x07u) ;
- }
- /*
- * BCDコードへの変換
- */
- unsigned char dec2bcd(unsigned char data)
- {
- unsigned char ans = 0;
- ans = ((data / 10) & 0x0F) << 4 | ((data % 10) & 0x0F);
- return ans;
- }
- /*
- * BINコードへの変換
- */
- unsigned char bcd2dec(unsigned char data)
- {
- unsigned char ans = 0;
- ans = ((data >> 4) & 0x0F) * 10 + (data & 0x0F);
- return ans;
- }
- /*
- * 曜日の計算
- */
- int Zeller(int y,int mo, int d)
- {
- int C,Y,G;
- C=y/100;
- Y= y % 100;
- G=5*C+C/4;
- return (d+((26*(mo+1))/10)+Y+Y/4+G + 6) % 7;
- }
- /*
- * RTCへの時間の設定
- */
- void RTCsetTime (int *y,int *mo,int *d,int *w,int *h,int *m,int *s)
- {
- /* Stop RTC counter */
- RTC.RCR2.BIT.START = 0;
- while(RTC.RCR2.BIT.START != 0);
- /* Set time */
- /* Set seconds. (0-59) */
- RTC.RSECCNT.BYTE = dec2bcd(*s);
- /* Set minutes (0-59) */
- RTC.RMINCNT.BYTE = dec2bcd(*m);
- /* Set hours. (0-23) */
- RTC.RHRCNT.BYTE = dec2bcd(*h);
- /* Set the date */
- /* Day of the week (0-6, 0=Sunday) */
- RTC.RWKCNT.BYTE = dec2bcd(*w);
- /* Day of the month (1-31) */
- RTC.RDAYCNT.BYTE = dec2bcd(*d);
- /* Month. (1-12, 1=January) */
- RTC.RMONCNT.BYTE = dec2bcd(*mo);
- /* Year. (00-99) */
- RTC.RYRCNT.WORD = (unsigned short)(dec2bcd(*y));
- /* start RTC counter */
- RTC.RCR2.BIT.START = 1;
- while(RTC.RCR2.BIT.START != 1);
- }
- /*
- * RTCのデータを読み出す
- */
- void RTCread(int *y,int *mo,int *d,int *w,int *h,int *m,int *s)
- {
- unsigned short bcd_years; // Used for converting year.
- do
- {
- /* Clear carry flag in ICU */
- IR(RTC,CUP) = 0;
- /* Read and convert RTC registers; mask off unknown bits and hour am/pm. */
- /* Seconds. (0-59) */
- *s = bcd2dec((RTC.RSECCNT.BYTE & 0x7fu));
- /* Minutes. (0-59) */
- *m = bcd2dec((RTC.RMINCNT.BYTE & 0x7fu));
- /* Hours. (0-23) */
- *h = bcd2dec((RTC.RHRCNT.BYTE & 0x3fu));
- /* Day of the month (1-31) */
- *d = bcd2dec(RTC.RDAYCNT.BYTE);
- /* Months since January (0-11) */
- *mo = bcd2dec(RTC.RMONCNT.BYTE);
- /* Years since 2000 */
- bcd_years = (unsigned short)RTC.RYRCNT.WORD;
- /* years years since 1900 */
- *y = bcd2dec((bcd_years & 0xFF));
- /* Days since Sunday (0-6) */
- *w = RTC.RWKCNT.BYTE & 0x07u;
- }
- while (IR(RTC,CUP) == 1); //Reread if carry occurs during read
- }
- void main(void)
- {
- int h,m,s,d,mo,y,w,flag=0;
- volatile unsigned int i;
- change_oscillation_PLL(); //クロックソースPLL
- SCI6_Init (9600); // SCI6-->GPSユニット
- initRTC();
- setpsw_i(); // 割込み許可 clrpsw_i()割込み禁止
- printf("\nhello world!\n");
- while(flag==0) {
- if(getJST(&y,&mo,&d,&h,&m,&s)) {
- printf("\n\n\n\ny=%d m=%d d=%d h=%d m=%d s=%d\n",y,mo,d,h,m,s);
- w = Zeller(y,mo,d);
- RTCsetTime (&y,&mo,&d,&w,&h,&m,&s);
- flag =1;
- }
- }
- while(1) {
- RTCread(&y,&mo,&d,&w,&h,&m,&s);
- printf("y=%d m=%d d=%d h=%d m=%d s=%d w=%d\n",y,mo,d,h,m,s,w);
- for(i=0;i<9000000;i++)nop();
- }
- //printf("> %s\n",buf);
- }