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

📄 timer.c

📁 嵌入式系统
💻 C
字号:
/*-----------------------------------------------------------------------------
@@
@@ (Summary)    : LH7953x series Timer Device Driver Source File
@@
@@ (Comment)    : Source codes of routines available for Timer
@@
@@ (History)    :
@@
@@ (RCS ID) :
@@
-----------------------------------------------------------------------------*/

#define TIMER_C

/*+include files*************************************************************/
/*                                                                          */
/*                                                                          */
/**************************************************************************-*/
#include "timer.h"
#ifndef INTC_H
#include "intc.h"
#endif
/******************************************************************************
@@
@@ [Name]       : apd_TIMERInit
@@
@@ [Summary]    : This function initializes the given timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@                timer_init: specify the initial setting for the timer
@@
@@                interval: specify the timer/counter interrupt interval
@@
@@ [Return]     : None
@@
@@ [Desc]       : Initializes the timer
@@
@@ [Note]       : APD_TIMER 3 only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/
/*
    Control Register Default:
    31       8   7     6      5      4     32     1            0
     -----------------------------------------------------------
    |Reserved |  E  |  M  |  CLR  |  X  |  PS  | CI  | Reserved |
    |         |  0  |  0  |  0    |  0  |  00  |  0  |          |
     -----------------------------------------------------------
    E   - Counter/Timer disabled
    M   - Free-running mode
    CLR - Normal operation
    X   - Internal System clock
    PS  - Clock divided by 4
    CI  - Disable Carry
*/

void apd_TIMERInit(APD_TIMER_NUM timer_num, APD_TIMER_INIT_TYPE timer_init, unsigned short interval)
{
    unsigned long control_reg;
    unsigned long load_reg;

    load_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERLOAD_OFST;
    *(volatile unsigned short *)load_reg = interval;

    control_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERCONTROL_OFST;
    *(volatile unsigned char *)control_reg =    timer_init.enablebit   |
                                            timer_init.modebit     |
                                            timer_init.clearbit    |
                                            timer_init.clockbit    |
                                            timer_init.prescalebit |
                                            timer_init.carrybit;
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERClrControlRegBit
@@
@@ [Summary]    : This function clears bits of the control register to 0 for the selected timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533)
@@
@@                bdata: specify which bits to be cleared to 1,
@@                For example, 0x05 will clear bit 0 and 2.
@@
@@ [Return]     : None
@@
@@ [Desc]       : Clear the specified bits of the selected timer's control register to 0
@@
@@ [Note]       : APD_TIMER3 is only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/

void apd_TIMERClrControlRegBit(APD_TIMER_NUM timer_num, unsigned char bdata)
{
    unsigned long control_reg;

    control_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERCONTROL_OFST;

    *(volatile unsigned char *)control_reg &= ~bdata;
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERSetControlRegBit
@@
@@ [Summary]    : This function sets bits of the control register to 1 for the selected timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533)
@@
@@                bdata:    specify which bits to be set to 1,
@@                For example, 0x05 will set bit 0 and 2.
@@
@@ [Return]     : None
@@
@@ [Desc]       : Set the specified bits of the selected timer's control register to 1
@@
@@ [Note]       : APD_TIMER 3 is only avalible for LH79532 and LH79533.
@@
@@ [History]    :
@@
@@ [END]
******************************************************************************/

void apd_TIMERSetControlRegBit(APD_TIMER_NUM timer_num, unsigned char bdata)
{
    unsigned long control_reg;

    control_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERCONTROL_OFST;

    *(volatile unsigned char *)control_reg |= bdata;
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERSetInterval
@@
@@ [Summary]    : This function sets the interval of the given timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@                interval: specify the timer/counter interrupt interval
@@
@@ [Return]     : None
@@
@@ [Desc]       : Stores the interval settings for the given timer.
@@
@@ [Note]       : APD_TIMER3 only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/

void apd_TIMERSetInterval(APD_TIMER_NUM timer_num, unsigned short interval)
{
    unsigned long load_reg;

    load_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERLOAD_OFST;

    *(volatile unsigned short *)load_reg = interval;
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERGetInterval
@@
@@ [Summary]    : This function returns the interval of the given timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@ [Return]     : Load register of selected timer
@@
@@ [Desc]       : Returns the interval value for the given timer
@@
@@ [Note]       : APD_TIMER3 only avalible for LH79532 and LH79533.
@@
@@ [History]    :
@@
@@ [END]
******************************************************************************/

unsigned short apd_TIMERGetInterval(APD_TIMER_NUM timer_num)
{
    unsigned long load_reg;

    load_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERLOAD_OFST;

    return (*(volatile unsigned short *)load_reg);
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERGetCounter
@@
@@ [Summary]    : This function returns the current counter value of the given timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@ [Return]     : Current timer value of the selected timer
@@
@@ [Desc]       : Returns the current counter value for the given timer
@@
@@ [Note]       : APD_TIMER3 only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/

unsigned short apd_TIMERGetCounter(APD_TIMER_NUM timer_num)
{
    unsigned long value_reg;

    value_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERVALUE_OFST;

    return (*(volatile unsigned short *)value_reg);
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERClrIntr
@@
@@ [Summary]    : This function clears the selected timer interrupt.
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@ [Return]     : None
@@
@@ [Desc]       : Clears the selected timer interrupt.
@@
@@ [Note]       : APD_TIMER3 only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/

void apd_TIMERClrIntr(APD_TIMER_NUM timer_num)
{
    unsigned long clear_reg;

    clear_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERCLEAR_OFST;

    *(volatile unsigned long *)clear_reg = 0x0000;
}

/******************************************************************************
@@
@@ [Name]       : apd_TIMERSetPrescale
@@
@@ [Summary]    : This function sets the prescale bits of the given timer
@@
@@ [Argument]   : timer_num:    specify which timer/counter
@@                APD_TIMER0 for timer 0
@@                APD_TIMER1 for timer 1
@@                APD_TIMER2 for timer 2
@@                APD_TIMER3 for timer 3 (For LH79532 and LH79533 only)
@@
@@ [Return]     : None
@@
@@ [Desc]       : Sets the prescale bits of the given timer
@@
@@ [Note]       : APD_TIMER3 only avalible for LH79532 and LH79533.
@@
@@ [History]    : 
@@
@@ [END]
******************************************************************************/

void apd_TIMERSetPrescale(APD_TIMER_NUM timer_num, APD_TIMER_PRESCALE_TYPE prescale)
{
    unsigned long control_reg;

    control_reg = APD_TIMERBASE + (timer_num * APD_TIMERCH_OFST) + APD_TIMERCONTROL_OFST;

    *(volatile unsigned char *)control_reg &= ~APD_TIMER_PRESCALE_MASK;
    *(volatile unsigned char *)control_reg |= prescale;
}

/*************************************************************************
	start timer
*************************************************************************/
void apd_StartTimer(APD_TIMER_NUM timer_num,unsigned long int_src)
{
	apd_TIMEREnable(timer_num);
	apd_INTCEnableIRQ(int_src);
}

/*************************************************************************
	stop timer
*************************************************************************/
void apd_StopTimer(APD_TIMER_NUM timer_num,unsigned long int_src)
{
	apd_TIMERDisable(timer_num);
	apd_INTCDisableIRQ(int_src);
}

⌨️ 快捷键说明

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