⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_timer_sample.c

📁 oki67500系列arm工程例程源代码
💻 C
字号:
/**********************************************************************************/
/*                                                                                */
/*    Copyright (C) 2003 Oki Electric Industry Co., LTD.                          */
/*                                                                                */
/*    System Name    :  ML675001 series                                           */
/*    Module Name    :  auto reload timer sample program                          */
/*    File   Name    :  auto_reload_timer_sample.c                                */
/*    Revision       :  01.00                                                     */
/*    Date           :  2003/03/09 initial version                                */
/*                                                                                */
/*    Variable 'COUNTER[0-5]' is incremented by timer0-5 interrupt.               */
/**********************************************************************************/
#include "ML675001.h"
#include "common.h"
#include "irq.h"
#include "cache.h"

/* constants */
#define MHz     (1000000L)
#define TMRCYC0 (5)    /* interval of TIMER0 interrupt (ms) */
#define TMRCYC1 (10)    /* interval of TIMER1 interrupt (ms) */
#define TMRCYC2 (15)    /* interval of TIMER2 interrupt (ms) */
#define TMRCYC3 (20)    /* interval of TIMER3 interrupt (ms) */
#define TMRCYC4 (25)    /* interval of TIMER4 interrupt (ms) */
#define TMRCYC5 (30)    /* interval of TIMER5 interrupt (ms) */
#define CCLK    (60*MHz)    /* frequency of CCLK (Hz) */
#define VALUE_OF_TIMECMP(N)   /* compare value of TIMER_N */\
                ((TMRCYC##N) * (CCLK) / (32 * 1000))

/* check VALUE_OF_TIMECMP */
#if ((VALUE_OF_TIMECMP(0)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(0)))
#error Invalid value : VALUE_OF_TIMECMP(0)
#elsif ((VALUE_OF_TIMECMP(1)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(1)))
#error Invalid value : VALUE_OF_TIMECMP(1)
#elsif ((VALUE_OF_TIMECMP(2)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(2)))
#error Invalid value : VALUE_OF_TIMECMP(2)
#elsif ((VALUE_OF_TIMECMP(3)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(3)))
#error Invalid value : VALUE_OF_TIMECMP(3)
#elsif ((VALUE_OF_TIMECMP(4)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(4)))
#error Invalid value : VALUE_OF_TIMECMP(4)
#elsif ((VALUE_OF_TIMECMP(5)) < 0 || 0x10000 <= (VALUE_OF_TIMECMP(5)))
#error Invalid value : VALUE_OF_TIMECMP(5)
#endif

/* functions */
int main(void);                     /* main routine */
static void reg_irq_handler(void);  /* registration of IRQ handler */
static void set_timer(void);        /* setup auto reload timer */
//static void led_on(UHWORD);           /* LED on */
static void timer0_handler(void);   /* TIMER0 handler */
static void timer1_handler(void);   /* TIMER1 handler */
static void timer2_handler(void);   /* TIMER2 handler */
static void timer3_handler(void);   /* TIMER3 handler */
static void timer4_handler(void);   /* TIMER4 handler */
static void timer5_handler(void);   /* TIMER5 handler */

/* global variables */
static volatile int COUNTER[6];     /* 10-60ms cycle counter */

/****************************************************************************/
/*  Entry point                                                             */
/*  Function : main                                                         */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   0                                                   */
/****************************************************************************/
int main(void)
{
    int i;
    

    init_cache();  /* Initialize CACHE memory */
    cache_on(CACHE_BANK0);  /* Bank0 : Cache enable */

    init_irq(); /* initialize IRQ */

    reg_irq_handler();  /* registration of IRQ handler */

    set_timer();    /* setup TIMER */
    
    /* initialize 10-60ms cycle counter */
    for(i=0; i<6; i++)
        COUNTER[i] = 0;
    
    irq_en();   /* enable IRQ */

	/* light LED */
    init_led();   /* output mode */
    led_on(LED_START_PATTERN);
    
    /* start timer */
    set_hbit(TIMECNTL0, TIMECNTL_START);    /* start TIMER0 */
    set_hbit(TIMECNTL1, TIMECNTL_START);    /* start TIMER1 */
    set_hbit(TIMECNTL2, TIMECNTL_START);    /* start TIMER2 */
    set_hbit(TIMECNTL3, TIMECNTL_START);    /* start TIMER3 */
    set_hbit(TIMECNTL4, TIMECNTL_START);    /* start TIMER4 */
    set_hbit(TIMECNTL5, TIMECNTL_START);    /* start TIMER5 */

    /* infinite loop */
    for(;;)
        ;

    return 0;
}

/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handlers into IRQ handler table */
    IRQ_HANDLER_TABLE[INT_TIMER0] = timer0_handler; /* TIMER0 */
    IRQ_HANDLER_TABLE[INT_TIMER1] = timer1_handler; /* TIMER1 */
    IRQ_HANDLER_TABLE[INT_TIMER2] = timer2_handler; /* TIMER2 */
    IRQ_HANDLER_TABLE[INT_TIMER3] = timer3_handler; /* TIMER3 */
    IRQ_HANDLER_TABLE[INT_TIMER4] = timer4_handler; /* TIMER4 */
    IRQ_HANDLER_TABLE[INT_TIMER5] = timer5_handler; /* TIMER5 */

    /* setup interrupt level */
    set_wbit(ILC, ILC_ILC16 & ILC_INT_LV3); /* timer0(nIR[16]),timer1(nIR[17]) -> 3 */
    set_wbit(ILC, ILC_ILC18 & ILC_INT_LV2); /* timer2(nIR[18]),timer3(nIR[19]) -> 2 */
    set_wbit(ILC, ILC_ILC20 & ILC_INT_LV1); /* timer4(nIR[20]),timer5(nIR[21]) -> 1 */

    return;
}

/****************************************************************************/
/*  Setup of timer                                                          */
/*  Function : set_timer                                                    */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_timer(void)
{
    /* stop timer */
    put_hvalue(TIMECNTL0, 0);   /* TIMER0 */
    put_hvalue(TIMECNTL1, 0);   /* TIMER1 */
    put_hvalue(TIMECNTL2, 0);   /* TIMER2 */
    put_hvalue(TIMECNTL3, 0);   /* TIMER3 */
    put_hvalue(TIMECNTL4, 0);   /* TIMER4 */
    put_hvalue(TIMECNTL5, 0);   /* TIMER5 */

    /* clear timer status register */
    put_hvalue(TIMESTAT0, TIMESTAT_STATUS); /* TIMER0 */
    put_hvalue(TIMESTAT1, TIMESTAT_STATUS); /* TIMER1 */
    put_hvalue(TIMESTAT2, TIMESTAT_STATUS); /* TIMER2 */
    put_hvalue(TIMESTAT3, TIMESTAT_STATUS); /* TIMER3 */
    put_hvalue(TIMESTAT4, TIMESTAT_STATUS); /* TIMER4 */
    put_hvalue(TIMESTAT5, TIMESTAT_STATUS); /* TIMER5 */

    /* setup timer control register */
    put_hvalue(TIMECNTL0,       /* TIMER0 */
               TIMECNTL_CLK32   /* clock of timer = CCLK/32 */
               |TIMECNTL_IE     /* enable interrupt */
               |TIMECNTL_INT);  /* interval timer */
    put_hvalue(TIMECNTL1, TIMECNTL_CLK32|TIMECNTL_IE|TIMECNTL_INT); /* TIMER1 */
    put_hvalue(TIMECNTL2, TIMECNTL_CLK32|TIMECNTL_IE|TIMECNTL_INT); /* TIMER2 */
    put_hvalue(TIMECNTL3, TIMECNTL_CLK32|TIMECNTL_IE|TIMECNTL_INT); /* TIMER3 */
    put_hvalue(TIMECNTL4, TIMECNTL_CLK32|TIMECNTL_IE|TIMECNTL_INT); /* TIMER4 */
    put_hvalue(TIMECNTL5, TIMECNTL_CLK32|TIMECNTL_IE|TIMECNTL_INT); /* TIMER5 */

    /* setup timer base register */
    put_hvalue(TIMEBASE0, 0x0000);  /* TIMER0 */
    put_hvalue(TIMEBASE1, 0x0000);  /* TIMER1 */
    put_hvalue(TIMEBASE2, 0x0000);  /* TIMER2 */
    put_hvalue(TIMEBASE3, 0x0000);  /* TIMER3 */
    put_hvalue(TIMEBASE4, 0x0000);  /* TIMER4 */
    put_hvalue(TIMEBASE5, 0x0000);  /* TIMER5 */

    /* setup timer compare register */
    put_hvalue(TIMECMP0, (UHWORD)VALUE_OF_TIMECMP(0));  /* TIMER0 */
    put_hvalue(TIMECMP1, (UHWORD)VALUE_OF_TIMECMP(1));  /* TIMER1 */
    put_hvalue(TIMECMP2, (UHWORD)VALUE_OF_TIMECMP(2));  /* TIMER2 */
    put_hvalue(TIMECMP3, (UHWORD)VALUE_OF_TIMECMP(3));  /* TIMER3 */
    put_hvalue(TIMECMP4, (UHWORD)VALUE_OF_TIMECMP(4));  /* TIMER4 */
    put_hvalue(TIMECMP5, (UHWORD)VALUE_OF_TIMECMP(5));  /* TIMER5 */

    return;
}

/****************************************************************************/
/*  Timer0 handler                                                          */
/*  Function : timer0_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer0_handler(void)
{
    COUNTER[0]++;  /* increment 10ms cycle counter */
    put_hvalue(TIMESTAT0, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

/****************************************************************************/
/*  Timer1 handler                                                          */
/*  Function : timer1_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer1_handler(void)
{
    COUNTER[1]++;  /* increment 20ms cycle counter */
    put_hvalue(TIMESTAT1, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

/****************************************************************************/
/*  Timer2 handler                                                          */
/*  Function : timer2_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer2_handler(void)
{
    COUNTER[2]++;  /* increment 30ms cycle counter */
    put_hvalue(TIMESTAT2, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

/****************************************************************************/
/*  Timer3 handler                                                          */
/*  Function : timer3_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer3_handler(void)
{
    COUNTER[3]++;  /* increment 40ms cycle counter */
    put_hvalue(TIMESTAT3, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

/****************************************************************************/
/*  Timer4 handler                                                          */
/*  Function : timer4_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer4_handler(void)
{
    COUNTER[4]++;  /* increment 50ms cycle counter */
    put_hvalue(TIMESTAT4, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

/****************************************************************************/
/*  Timer5 handler                                                          */
/*  Function : timer5_handler                                               */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void timer5_handler(void)
{
    COUNTER[5]++;  /* increment 60ms cycle counter */
    put_hvalue(TIMESTAT5, TIMESTAT_STATUS); /* clear timer status register */

    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -