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

📄 funqep.c

📁 dsp2407的函数库
💻 C
字号:
/****************************************************************
*	funqep.c					See funlib.h for instuctions	*
*	Don Luebbe                  about using this file.          *
*	10/12/02                                                    *
*																*
*	This file defines two QEP functions: initQEP() and getQEP().*
*	initQEP() initializes a QEP circuit with either timer 2		*
*	(pins QEP1 and QEP2) or timer 4 (pins QEP3 and QEP4) and the*
*	initial count for the timer.  Verify that there is no 		*
*	conflict for these timers by another function.  It should be*
*	called before attempting to use the QEP and should be called*
*	only once per QEP circuit.  getQEP() reads the desired timer*
*	and returns the corresponding count.						*
*																*
*	Example:	main()                                          *
*				{                                               *
*					initQEP(4,1000);                       		*
*					...                                         *
*					while(1)                                    *
*					{                                           *
*						position = getQEP(4);                   *
*					}                                           *
*				}                                               *
*				This code sets up timer 4 to read an encoder	*
*				with pins QEP3 and QEP4.  The corresponding		*
*				value is placed in variable position.			*
****************************************************************/

#include        "f2407_c.h"
#include        "funlib.h"
#include		<math.h>

int initQEP(int qeptimer, int initvalue)
{   
    if ((qeptimer < 2) || (qeptimer == 3) || (qeptimer > 4))
    {
    	return -1;
    }
    	
	switch (qeptimer)
	{   
		case 2:	*MCRA |= 0x0018;        /* set pins for QEP function */
				*T2CNT = initvalue;		/* clear timer counter */
    			*T2PR = 0xFFFF;         /* set timer period */
    			*T2CON = 0xD870;        /* configure T2CON register */
				/*     
				bit 15-14     11:     stop immediately on emulator suspend
				bit 13        0:      reserved
 				bit 12-11     11:     directional up-down count mode
 				bit 10-8      000:    x/1 prescaler
 				bit 7         0:      use own TENABLE bit
 				bit 6         1:      enable timer
 				bit 5-4       11:     QEP enable
 				bit 3-2       00:     00 = reload compare reg on underflow
 				bit 1         0:      disable timer compare
 				bit 0         0:      use own period register
				*/

			    *CAPCONA = 0;
				/*     
 				bit 15        0:      capture reset
 				bit 14-13     11:     enable QEP and disable capture 1 and 2
 				bit 12        0:      disable CAP 3
 				bit 11        0:      reserved
 				bit 10        0:      GPT selection for CAP 3
 				bit 9         00:     GPT selection for CAPs 1 and 2(don't care)
 				bit 8         0:      disallow CAP 3 to start ADC
 				bit 7-6       00:     edge detection for CAP 1(don't care)
 				bit 5-4       00:     edge detection for CAP 2(don't care)
 				bit 3-2       00:     no edge detection for CAP 3
 				bit 1-0       00:     reserved
				*/
				break;

		case 4: *MCRC |= 0x0180;
				*T4CNT = initvalue;
    			*T4PR = 0xFFFF;
    			*T4CON = 0xD870;
    			*CAPCONB = 0;
    			break;
    }
	return 0;
}

int getQEP(int qeptimer)
{   
	int count = 0;
	
	if ((qeptimer < 2) || (qeptimer == 3) || (qeptimer > 4))
    {
    	return -1;
    }

    switch (qeptimer)				/* Reads count from corresponding timer */
	{   
		case 2:	count = *T2CNT;
				break;
		case 4: count = *T4CNT;
				break;
	}
	
	return (count);
}

⌨️ 快捷键说明

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