rtc_sample.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 498 行 · 第 1/2 页
C
498 行
/*************************************************************************************/
/* */
/* Copyright (C) 2005 Oki Electric Industry Co., LTD. */
/* */
/* System Name : ML674051/ML67Q4061 */
/* Module Name : RTC sample program */
/* File Name : rtc_sample.c */
/* Revision : 1.00 */
/* Date : 2005/02/14 */
/* Initial version */
/* */
/*************************************************************************************/
#include "ML674061.h"
#include "common.h"
#include "irq.h"
#ifdef __IAR__
#include "intrinsics.h"
#endif
/* constants */
/* functions */
int main(void); /* main routine */
static void reg_irq_handler(void); /* registration of IRQ handler */
void init_uart(void) ; /* initialize uart */
void write_uart(char *buf) ; /* writing to uart */
int get_binary_data(void) ; /* get binary data from uart */
void receive_data(void) ; /* receive RTC setting data from PC */
void init_rtc(void) ; /* initialize RTC */
void send_rtc_data(void) ; /* send RTC data to UART */
void exint1_handler(void) ; /* exint1 handler */
int get_char(void) ; /* get 1byte character from UART */
/* global variables */
UHWORD int_flg ; /* cap interrupt count flag */
UHWORD data_y10 = 0 ; /* rtc data Y10 */
UHWORD data_y1 = 0 ; /* rtc data Y1 */
UHWORD data_mo10 = 0 ; /* rtc data MO10 */
UHWORD data_mo1 = 0 ; /* rtc data MO1 */
UHWORD data_d10 = 0 ; /* rtc data D10 */
UHWORD data_d1 = 0 ; /* rtc data D1 */
UHWORD data_h10 = 0 ; /* rtc data H10 */
UHWORD data_h1 = 0 ; /* rtc data H1 */
UHWORD data_m10 = 0 ; /* rtc data M10 */
UHWORD data_m1 = 0 ; /* rtc data M1 */
UHWORD data_24or12 = 0 ; /* rtc data 24/12 */
UHWORD data_s10 = 0 ; /* rtc data S10 */
UHWORD data_s1 = 0 ; /* rtc data S1 */
BYTE rtc_data_str[25] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
UHWORD rtc_data_bin[25] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
/* LED lighting pattern table */
UHWORD LED_TABLE [18] = {LED_0,LED_1,LED_2,LED_3, /* "0","1","2","3" */
LED_4,LED_5,LED_6,LED_7, /* "4","5","6","7" */
LED_8,LED_9,LED_A,LED_b, /* "8","9","A","b" */
LED_C,LED_d,LED_E,LED_F, /* "C","d","E","F" */
LED_all,LED_off}; /* all on ,all off */
/****************************************************************************/
/* Entry point */
/* Function : main */
/* Parameters */
/* Input : Nothing */
/* Output : 0 */
/****************************************************************************/
int main(void)
{
/* initialize IRQ */
init_irq();
/* registration of IRQ handler */
reg_irq_handler();
/* initialize LED */
init_led(); /* set output mode */
/* light LED start pattern */
led_on(LED_START_PATTERN);
/* initialize UART */
init_uart() ;
/* message 1 output */
write_uart("Please send setting data in the following format.\n") ;
write_uart("Year-Month-Day-Hour-Minute-1 or 0 (24hour = 1 or 12hour with PM/AM = 0)\n") ;
write_uart("For example...\n") ;
write_uart("05-01-31-14-30-1\n") ;
write_uart("\n") ;
put_value(UARTFCR0, (UARTFCR_RFCLR|UARTFCR_TFCLR)); /* receiver/transmitter FIFO clear */
/* receive setting data from PC */
receive_data() ;
/* initialize RTC */
init_rtc() ;
/* get RTC data and send to UART */
send_rtc_data() ;
/* enable IRQ */
#ifdef __IAR__
__enable_interrupt();
#else
irq_en();
#endif
/* message 2 output */
write_uart("Please push EXINT1 button on CPU board to display RTC data.\n") ;
/* infinite loop */
for(;;)
{
if(int_flg == 1)
{
int_flg = 0 ;
send_rtc_data() ;
}
}
// return 0 ;
}
/****************************************************************************/
/* Registration of IRQ Handler */
/* Function : reg_irq_handler */
/* Parameters */
/* Input : Nothing */
/* Output : Nothing */
/* Note : Initialize of IRQ needs to be performed before this process. */
/****************************************************************************/
void reg_irq_handler(void)
{
/* register IRQ handlers into handler table */
IRQ_HANDLER_TABLE[INT_EXINT1] = exint1_handler;
/* setup interrupt level */
set_wbit(EXILCB, ILC_ILC34 & ILC_INT_LV1); /* exint1(nIRQ[34]) -> level1 */
set_wbit(EXIRQB, IRQB_IRQ34); /* dummy write to init the edge detect circuit */
set_wbit(EXIDMA, (IDM_IDM34 | IDM_IDMP34) ); /* Set Ext Interrupt0 to edge detect*/
return;
}
/*****************************************************************************/
/* External 1 handler */
/* Function : exint1_handler */
/* Parameters */
/* Input : Nothing */
/* Output : Nothing */
/*****************************************************************************/
void exint1_handler(void)
{
#ifdef __IAR__
__disable_interrupt();
#else
irq_dis();
#endif
int_flg = 1 ;
set_wbit(EXIRQB, IRQB_IRQ34); /* Clear external interrupt1 */
#ifdef __IAR__
__enable_interrupt();
#else
irq_en();
#endif
return;
}
/*****************************************************************************/
/* Receive RTC setting data from PC */
/* Function : receive_data */
/* Parameters */
/* Input : Nothing */
/* Output : Nothing */
/*****************************************************************************/
void receive_data(void)
{
UHWORD h_data = 0 ;
UHWORD b_data = 0 ;
UHWORD set_data ;
UHWORD data_counter = 0 ;
for(; data_counter < 15; data_counter++)
{
/* get 1byte binary data from uart */
set_data = (UHWORD)get_binary_data() ;
switch(data_counter)
{
case 0 :
data_y10 = set_data ;
break ;
case 1 :
data_y1 = set_data ;
break ;
case 3 :
data_mo10 = set_data ;
break ;
case 4 :
data_mo1 = set_data ;
break ;
case 6 :
data_d10 = set_data ;
break ;
case 7 :
data_d1 = set_data ;
break ;
case 9 :
data_h10 = set_data ;
break ;
case 10 :
data_h1 = set_data ;
break ;
case 12 :
data_m10 = set_data ;
break ;
case 13 :
data_m1 = set_data ;
break ;
default :
break ;
}
}
h_data = (UHWORD)get_char() ;
if(('0' <= h_data) && (h_data <= '9'))
{
b_data = (h_data - '0');
}
else if(('A' <= h_data) && (h_data <= 'F'))
{
b_data = (h_data - 'A' + 10);
}
else
b_data = h_data ;
data_24or12 = b_data ;
return ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?