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

📄 timer2.c

📁 PID control speed of motor using dspIC30f2010
💻 C
字号:
#include "timer2.h"
unsigned int T2_overflow=0;

/********************************************************************
*    Function Name:  OpenTimer2                                     *
*    Description:    This routine configures the timer control      *
*                    register and timer period register.            *
*    Parameters:     config: bit definitions to configure Timer2    *
*                    period: value to be loaded to PR reg           *
*    Return Value:   None                                           *
********************************************************************/

void OpenTimer2(unsigned int config,unsigned int period)
{
    TMR2  = 0;          /* Reset Timer2 to 0x0000 */
    PR2   = period;     /* assigning Period to Timer period register */
    T2CON = config;     /* configure control reg */
    T2CONbits.T32 = 0;
}

/********************************************************************
*    Function Name:  CloseTimer2                                    *
*    Description:    This routine disables the Timer2 and its       *
*                    interrupt and flag bits.                       *
*    Parameters:     None                                           *
*    Return Value:   None                                           *
********************************************************************/

void CloseTimer2(void)
{
    _T2IE = 0;      /* Disable the Timer2 interrupt */
    T2CONbits.TON = 0;      /* Disable timer2 */
    _T2IF = 0;      /* Clear Timer interrupt flag */
}

/*******************************************************************
*    Function Name: ConfigIntTimer2                                *
*    Description:   This function configures interrupt and sets    *
*                   Interrupt Priority                             *
*    Parameters:    unsigned int config                            *
*    Return Value:  None                                           *
*******************************************************************/

void ConfigIntTimer2(unsigned int config)
{
    _T2IF = 0;                   /* clear IF bit */
    _T2IP = (config &0x0007);    /* assigning Interrupt Priority */
    _T2IE = (config &0x0008)>>3; /* Interrupt Enable /Disable */
}

void timer2_init(void)
{
    unsigned int match_value;
	ConfigIntTimer2(T2_INT_PRIOR_4 & T2_INT_ON);//Enable Timer1 Interrupt and Priority to "1"

    //WriteTimer1(0);
	TMR2=0;
    match_value = 0xFFFF;

	OpenTimer2(T2_ON & T2_GATE_OFF & T2_IDLE_STOP & T2_PS_1_8  & T2_SOURCE_INT &
			   T2_32BIT_MODE_OFF, match_value);
}

/* Interrupt Service Routine                        */
/* Fast context save (using push.s and pop.s)       */

void __attribute__((__interrupt__, __auto_psv__, __shadow__)) _T2Interrupt(void)
	{
  	//_LATD1= ~_LATD1;
	T2_overflow++;
	IFS0bits.T2IF = 0;/* clear interrupt flag */       
	}

⌨️ 快捷键说明

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