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

📄 main.c

📁 利用定时器中断
💻 C
字号:
/*""FILE COMMENT""*************************************************************
*  System Name : This program is only for education.
*  File Name   : main.c
*  Contents    : Practice 4-A time management by timer ---sample---
*  Model       : OAKS16-LCD Board
*  CPU         : M16C/60 Serials
*  Compiler    : M16C/60 NC30 COMPILER V.4.00 Release 2 - Entry
*  OS          : NON
*  Programer   : RENESAS Semiconductor Training Center
*  Note        : For OAKS16-M30620FCAFP(M16C/62group,16MHz)
*******************************************************************************
* COPYRIGHT(C) 2004 RENESAS TECHNOLOGY CORPORATION ALL RIGHTS RESERVED 
*               AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
*******************************************************************************
* History      : ---
*""FILE COMMENT END""*********************************************************/

/*===== symbol definition for M16C/62 group =====*/
union byte_def{                 /* use to access bit/byte        */
    struct{                 
        unsigned char b0:1;     /* bit0(LSB)                     */
        unsigned char b1:1;     /* bit1                          */
        unsigned char b2:1;     /* bit2                          */
        unsigned char b3:1;     /* bit3                          */
        unsigned char b4:1;     /* bit4                          */
        unsigned char b5:1;     /* bit5                          */
        unsigned char b6:1;     /* bit6                          */
        unsigned char b7:1;     /* bit7(MSB)                     */
    }bit;                       /* use to access bit             */
    unsigned char   byte;       /* use to access byte            */
};
#define     p2          (*(volatile unsigned char *)0x003e4)
                                            /* Port P2 register             */
#define     pd2         (*(volatile unsigned char *)0x003e6)
                                            /* Port P2 direction register         */
#define     ta0         (*(volatile unsigned short *)0x00386) 
                                            /* timer A0 register             */
#define     ta0mr       (*(volatile unsigned char *)0x00396)
                                            /* timer A0 mode register       */
#define     ir_ta0ic    ((*(volatile union byte_def *)0x00055).bit.b3)
                                            /* timer A0 interrupt request bit   */
#define     ta0s        ((*(volatile union byte_def *)0x00380).bit.b0)
                                            /* count start flag of timer A0  */


/*===== prototype declaration =====*/
void main(void);                        
static void init_ta0(void);             /* initialization of timer A0       */
static char count_up_0to255(char Count);  /* count up (0乣9)      */


/*===== variable declaration =====*/


/*""FUNC COMMENT""*************************************************************
* ID             : ---
* Function name  : void main(void)
* Feature        : repeatly count up from 0(initial value) to 9 every 1s
*                : and display the count value in SingleLED as BCD code 
*                : every time.
*                : timer is started cyclically (polling). 
* Argument       : NON
* Return value   : NON
* Called function: init_ta0()           ; initialization of timer A0
*                : count_up_0to9()      ; count up from 0 to 9 
* Causion        : NON
* History        : ---
*""FUNC COMMENT END""*********************************************************/
#define SET     1               /* set the symbol SET to 1                 */
#define CLEAR   0               /* set the symbol CLEAR to 0               */
#define TRUE    1               /* when condition is satisfied             */
#define FALSE   0               /* when condition is not satisfied          */
#define CYCLICTIME  5          /* to count the set time(100ms)   */
                                /* 100ms亊5times = 500ms = 0.5s                */
void main(void)
{
    char TimeFor1s = CYCLICTIME;    /* 1s counter (100ms亊10=1000ms)    */
    char Count = 0;                 /* the data conted up every 1s  */
    char DispLED = 0x00;            /* output data to SingleLED        */

    /* Port initialization */
    p2   = 0xff;                    /* set SingleLED to off          */
    pd2  = 0xff;                    /* set Port P7 to output           */
    /* initialization of timer A0 */
    init_ta0();
    
    while(1){
        /* judgment for passing 100ms */
        if( ir_ta0ic == SET ){      /* as soon as passed 100ms, timer A0 interrupt   */
                                    /* request bit (bit3) is set. */
            ir_ta0ic = CLEAR;       /* clearing timer A0 interrupt request bit  */
            
            /* judgment for passing 1s */
            TimeFor1s--;
            if( TimeFor1s == 0 ){           /* passed 1s?             */
                TimeFor1s = CYCLICTIME;     /* initialize 1s counter    */
                
                /* updata the count data (0乣9) */
                Count = count_up_0to255( Count );
                /* updata display data */
                DispLED = Count;
            }
            /* output to SingleLED */
                p2 = DispLED ^ 0xff;
        }
    }
}


/*""FUNC COMMENT""*************************************************************
* ID               : ---
* Function name    : static void init_ta0(void)
* Feature          : initialization of timer A0 
* Argument         : NON
* Return value     : NON
* Called function  : NON
* Causion          : NON
* History          : ---
*""FUNC COMMENT END""*********************************************************/
static void init_ta0(void)
{
    ta0mr = 0x80;                   /* setting timer A0 mode register         */
                                    /* 10000000                             */
                                    /* ||||||++---- timer  mode            */
                                    /* |||||+------ pulse is not output        */
                                    /* |||++------- gate function not availble  */
                                    /* ||+--------- timer mode       */
                                    /* ++----------  count source is f32       */
                                    /*              (f(XIN)=16MHz:2兪s))    */
    ta0 = 74999;                     /* setting timer A0 register (20ms)         */
                                    /* 49999 = (100ms / 2兪s) -1              */
    ir_ta0ic = CLEAR;               /* clearign timer A0 interrupt request  bit     */
    ta0s = SET;                     /* setting timer A0 count to start           */
}


/*""FUNC COMMENT""*************************************************************
* ID                : ---
* Function name     : static char count_up_0to9(char Count)
* Feature           : repeatly count up during 0乣9
* Argument          : char Count;  count up data
* Return value      : char;       count up data after 1 s
* Called function   : NON
* Causion           : return to 0 if the count data is graeter than 9
* History           : ---
*""FUNC COMMENT END""*********************************************************/
static char count_up_0to255(char Count)
{
    if(255 < ++Count){
        Count = 0;
    }
    return Count;
}


/******************************************************************************
    end of file
******************************************************************************/

⌨️ 快捷键说明

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