gpio_sample.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 155 行

C
155
字号
/*************************************************************************************/
/*                                                                                   */
/*      Copyright (C) 2005 Oki Electric Industry Co., LTD.                           */
/*                                                                                   */
/*      System Name     :  ML675050                                                  */
/*      Module Name     :  Parallel Functions                                        */
/*      File   Name     :  gpio_sample.c                                             */
/*      Revision        :  1.00                                                      */
/*      Date            :  2005/02/10                                                */
/*                          Initial version                                          */
/*                                                                                   */
/*************************************************************************************/
#include "ML675050.h"
#include "common.h"
#include "irq.h"
#include "cache.h"

#ifdef __IAR__
#include <intrinsics.h>
#endif

/* constants */
#define MHz      (1000000L)
#define TMRCYC   (2)           /* interval of timer interrupt (ms) */
#define CLKGEAR  (1)           /* clock gear */
#define SYSCLK	(64)	       /* SYSCLK (MHz)*/
#define VALUE_OF_TMRLR         /* reload value of timer */\
                (65536 - (TMRCYC * SYSCLK * 1000) / 16)

#if ((VALUE_OF_TMRLR) < 0 || 0x10000 <= (VALUE_OF_TMRLR))
#error "Invalid value : VALUE_OF_TMRLR"
#endif

/* functions */
int main(void);                     /* main routine */
static void reg_irq_handler(void);  /* registration of IRQ handler */
static void set_timer(void);        /* setup of timer */
static void timer_handler(void);    /* timer handler */

/* global variables */
static volatile int counter;
static volatile unsigned int led_count;

/* LED lighting pattern table */
UHWORD LED_TABLE [4] = {LED_0,LED_1,LED_2,LED_3,   /* "0","1","2","3" */
                         };

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

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

    /* initialize IRQ */
    init_irq();

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

    /* setup of timer */
    set_timer();

    /* initialize LED */
    init_led();   /* set output mode */

    /* light LED start pattern */
    led_on(LED_START_PATTERN);

    /* initialize variable */
    led_count = 0;
    counter = 0;

    //put_wvalue(CLKCNT, 0x000f00a8);

    /* enable IRQ */
      __enable_interrupt();

    /* timer start */
    put_wvalue(TMEN, 0x01); /* enable timer (write '1' in TMEN[0]) */

    while(1){
        if(counter >= 20){    /* timer interrupt occur? */

            counter = 0;

            /* light LED */
            led_on((UBYTE)LED_TABLE[led_count]);

            /* update led_count */
            led_count++;

            if(led_count >= 4)
                led_count = 0;

        }
    }
    //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_SYSTEM_TIMER] = timer_handler;

    /* setup interrupt level */
    set_wbit(ILC0, ILC0_ILR0 & ILC0_INT_LV1);  /* system timer(nIRQ[0]) -> level1 */

    return;
}
/****************************************************************************/
/*  Setup of timer                                                          */
/*  Function : set_timer                                                    */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_timer(void)
{
    put_wvalue(TMEN,  0x0);     /* disable timer (write '0' in TMEN[0])*/
    put_wvalue(TMOVF, 0x01);    /* clear overflow register (write '1' in TMOVF[0])*/

    put_wvalue(TMRLR, VALUE_OF_TMRLR);  /* set TMRLR */

    return;
}
/*****************************************************************************/
/*  System Timer handler                                                     */
/*  Function : timer_handler                                                 */
/*     Parameters                                                            */
/*          Input   :   Nothing                                              */
/*          Output  :   Nothing                                              */
/*****************************************************************************/
void timer_handler(void)
{
    counter++;

    put_wvalue(TMOVF, 0x01);    /* clear TMOVF register (write '1' in TMOVF[0]) */
    return;
}

⌨️ 快捷键说明

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