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

📄 drvtimer.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 5 页
字号:
			NVIC_DisableIRQ((IRQn_Type)((uint32_t)TMR0_IRQn + (uint32_t)ch)); 
            return E_SUCCESS ;
        }

        default:
        {
            return E_DRVTIMER_CHANNEL ; 
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_GetIntFlag                                                                    */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */   
/*                  iIntStatus                              0:No interrupt / 1:Interrupt occurred          */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/* Description:                                                                                            */
/*                  Get the interrupt flag status from the specified timer channel.                        */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_GetIntFlag(E_TIMER_CHANNEL ch)
{
	if (ch == E_TMR0 )
		return TIMER0->TISR.TIF;
	else if(ch == E_TMR1 )									 
		return TIMER1->TISR.TIF;
	else if(ch == E_TMR2 )
		return TIMER2->TISR.TIF;
	else if(ch == E_TMR3 )
		return TIMER3->TISR.TIF;
	else 
    	return E_DRVTIMER_CHANNEL;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_ClearIntFlag                                                                  */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */   
/*                  E_SUCCESS                               Operation successful                           */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/* Description:                                                                                            */
/*                  Clear the interrupt flag of the specified timer channel.                               */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_ClearIntFlag(E_TIMER_CHANNEL ch)
{
    if (ch == E_TMR0 )
        TIMER0->TISR.TIF = 1;
    else if(ch == E_TMR1 )                                   
        TIMER1->TISR.TIF = 1;
    else if(ch == E_TMR2 )
        TIMER2->TISR.TIF = 1;
    else if(ch == E_TMR3 )
        TIMER3->TISR.TIF = 1;
    else 
        return E_DRVTIMER_CHANNEL;

    return E_SUCCESS;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_Start                                                                         */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */
/*                  E_SUCCESS                               Operation successful                           */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/* Description:                                                                                            */
/*                  Start to count the specified timer channel.                                            */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_Start(E_TIMER_CHANNEL ch)
{
	TIMER_T * tTMR;

   	switch (ch)
    {
        case E_TMR0:
        case E_TMR1:
        case E_TMR2:
		case E_TMR3:
        {
        	tTMR = (TIMER_T *)((uint32_t)TIMER0 + CH_OFFSET[ch]);         
            tTMR->TCSR.CEN = 1;
            return E_SUCCESS ;
        }

        default:
        {
            return E_DRVTIMER_CHANNEL ; 
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_GetIntTicks                                                                   */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */
/*                  uTimerTick                              Return the interrupt ticks                     */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/* Description:                                                                                            */
/*                  This function is used to get the number of interrupt occurred                          */
/*                  after the timer interrupt function is enabled.                          .              */
/*                  Thus DrvTIMER_EnableInt(ch) must been called in advance.                               */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvTIMER_GetIntTicks(E_TIMER_CHANNEL ch)
{
    switch (ch)
    {
        case E_TMR0:
        {
            return uTimer0Tick;  
        }

        case E_TMR1:
        {
            return uTimer1Tick;
        }   

        case E_TMR2:
        {
            return uTimer2Tick;
        }

        case E_TMR3:
        {
            return uTimer3Tick;
        }

        default:
        {
            return E_DRVTIMER_CHANNEL;
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_ResetIntTicks                                                                 */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/* Returns:                                                                                                */
/*                  E_SUCCESS                               Operation successful                           */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/* Description:                                                                                            */
/*                  This function is used to clear interrupt ticks to 0.                                   */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_ResetIntTicks(E_TIMER_CHANNEL ch)
{
    switch (ch)
    {
        case E_TMR0:
        {
            uTimer0Tick = 0;
            break;
        }

        case E_TMR1:
        {
            uTimer1Tick = 0;
            break;
        }

        case E_TMR2:
        {
            uTimer2Tick = 0;
            break;
        }

        case E_TMR3:
        {
            uTimer3Tick = 0;
            break;
        }

        default:
        {
            return E_DRVTIMER_CHANNEL;
        }
    }

    return E_SUCCESS;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_Delay                                                                         */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/*                  uIntTicks - [in]                                                                       */
/*                      The delay ticks                                                                    */
/* Returns:                                                                                                */
/*                  None                                                                                   */
/* Description:                                                                                            */
/*                  This function is used to add a delay loop by specified interrupt ticks                 */
/*                  of the timer channel.                                                                  */
/*---------------------------------------------------------------------------------------------------------*/
void DrvTIMER_Delay(E_TIMER_CHANNEL ch, uint32_t uIntTicks)
{
    uint32_t volatile btime;

    btime = DrvTIMER_GetIntTicks(ch);
    
    while (1)
    {
        if ((DrvTIMER_GetIntTicks(ch) - btime) > uIntTicks)
        {
            break;
        }
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:        DrvTIMER_OpenCounter                                                                   */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*                  ch - [in]                                                                              */
/*                      E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3                           */   
/*                  uCounterBoundary - [in]                                                                */
/*                      The parameter is used to determine how many counts occurred will                   */
/*                      toggle once timer interrupt.                                                       */
/*                  op_mode - [in]                                                                         */
/*                      E_TIMER_OPMODE, E_ONESHOT_MODE/E_PERIODIC_MODE/E_CONTINUOUS_MODE                   */
/* Returns:                                                                                                */
/*                  E_SUCCESS                               Operation successful                           */
/*                  E_DRVTIMER_CHANNEL                      Invalid Timer channel                          */
/*                  E_DRVTIMER_EIO                          Timer has not been initialized                 */
/* Description:                                                                                            */
/*                  This function is used to open the timer channel with the specified operation mode.     */
/*                  And the counting source of timer is from the external event/counter.                   */
/*                  The TIMER clock source should be set as HCLK.                                          */
/*                  Only NuMicro NUC1x0xxxBx and NUC1x0xxxCx series support this function,                 */  
/*                  ex:NUC140RD2BN, NUC140VE3CN.                                                           */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvTIMER_OpenCounter(E_TIMER_CHANNEL ch, uint32_t uCounterBoundary, E_TIMER_OPMODE op_mode)
{
    uint32_t i;
	TIMER_T * tTMR;

    if ((bIsTimer0Initial == FALSE) || (bIsTimer1Initial == FALSE) || 
        (bIsTimer2Initial == FALSE) || (bIsTimer3Initial == FALSE))
    {
        return E_DRVTIMER_EIO;
    }

	if (op_mode == E_TOGGLE_MODE)
        return E_DRVTIMER_CHANNEL;
				    	
   	switch (ch)
    {
        case E_TMR0:
        {
            if (bIsTimer0Used != FALSE)
                return E_DRVTIMER_EIO;
            
            bIsTimer0Used = TRUE;
           
 		   	SYSCLK->APBCLK.TMR0_EN = 1;

			outpw((uint32_t)&TIMER0->TCSR ,0 );	/* Disable timer */
			

⌨️ 快捷键说明

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