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

📄 incap.c

📁 Position PID algorithm control motor by dsPIC
💻 C
字号:
#include "incap.h"
#include "incap_define.h"
#include "timer2.h"
#include "IO.h"
//unsigned int time_per_um=MAX_TIME_PER_2UM;
//unsigned int last_time_per_um_in=MAX_TIME_PER_2UM;
//extern unsigned int timer3_overflow;
unsigned long cap_value=MAX_LONG;//max long value
extern unsigned int T2_overflow;
//unsigned int last_cap_value=0;

/********************************************************************
*    Function Name:  CloseCapture2                                  *
*    Description:    This routine disable the inputcapture and its  *
*                    interrupt bits.                                *
*    Parameters:     None                                           *
*    Return Value:   None                                           *
********************************************************************/
void CloseCapture2()
{
    _IC2IE = 0; /* disable the interrupt */
    IC2CONbits.ICM = 0; /* Input Capture x(ic_no) Off */
    _IC2IF = 0; /* disable the interrupt flag */
}

/********************************************************************
*    Function Name:  ConfigIntCapture2                              *
*    Description:    Set the Enable Interrupts and Interrupt        *
*                    Priorites  to the Interrupt Control and        *
*                    Interrupt Priority Register respectively       *
*    Parameters:     unsigned int config                            *
*    Return Value:   None                                           *
********************************************************************/
void ConfigIntCapture2(unsigned int config)
{
    _IC2IF = 0;                     /* Clear IF bit */
    _IC2IP = (config &0x0007);      /* assigning Interrupt Priority
                                             to IPC Control REgister */
    _IC2IE = (config &0x0008) >> 3; /* assiging InputCapture Enable/
                                              Disable bit of IEC Register*/
}

/********************************************************************
*    Function Name:  OpenCapture2                                   *
*    Description:    This routine include configuring clock ,       *
*                    number of capture per interrupt ,capture mode  *
*    Parameters:     unsigned int config                            *
*    Return Value:   None                                           *
********************************************************************/
void OpenCapture2(unsigned int config)
{
    /* Config contains Clock source, number of Captures per interuppt
                                    and Capture Mode */
    IC2CON = config;  
}
/********************************************************************
*    Function Name:  ReadCapture2                                   *
*    Description:    This routine reads all pending InputCapture    *
*                    Buffers and stores in the locations specified  *
*    Parameters:     address of locations where buffer data to be   *
*                    stored                                         *
*    Return Value:   None                                           *
********************************************************************/
void ReadCapture2(unsigned int * buffer)
{
	while (IC2CONbits.ICBNE)
	{
		*buffer++ = IC2BUF; /* reads the input capture buffer */
	}
}

void cap2_init(void)
{
CAP2_ENABLE_DIR=INPUT;
CloseCapture2();
ConfigIntCapture2(IC_INT_PRIOR_2 & IC_INT_ON);
timer2_init(); 
OpenCapture2(IC_IDLE_STOP & IC_TIMER2_SRC & IC_INT_1CAPTURE & IC_EVERY_EDGE);
}


void __attribute__((__interrupt__, __no_auto_psv__)) _IC2Interrupt(void) 
{
cap_value = (unsigned long) (T2_overflow * MAX_TIMER_2);
cap_value+= IC2BUF; 
T2_overflow=0;
TMR2=0;
_IC2IF = 0;
} 

⌨️ 快捷键说明

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