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

📄 funclock.c

📁 dsp2407的函数库
💻 C
字号:
/****************************************************************
*	funclock.c 					See funlib.h for instuctions	*
*	Don Luebbe					about using this file.			*
*	10/12/02													*
*																*
*	This file defines two clock functions: initClock() and 		*
*	updateClock().  initClock() sets up the global variable 	*
*	clocktime to contain the time in seconds since the clock 	*
*	began or multiple thereof based on the prescaler.  It is 	*
*	possible possible to use any timer as the time base for the *
*	clock; however, it must not conflict with any other 		*
*	functions or code.  It need be called only once.  However,  *
*	updateClock() should be called before the designated timer. *
*	As most programs go through the while loop section several	*
*	times before the timer rolls over, it is adequate to call	*
*	updateClock() immediately after entering the while loop.	*
*	        													*
*	Example:	main()											*
*				{     											*
*					...                                         *
*					initClock(4,0);                             *
*					while(1)                                    *
*					{                                           *
*						updateClock();                          *
*						...                                     *
*					}                                           *
*				}												*
****************************************************************/
               
#include        "f2407_c.h"
#include        "funlib.h"
#include		<math.h>

int timerasclock = 0;
int prescale = 0;
int clockprescale = 0;
long coarsecount = 0;
int finecount = 0;
int finecountlast = 0;
double secsperfine = 0;
double secspercoarse = 0;
double clocktime = 0;
int timer = 0;

int initClock(timer, prescale)
{
	if(timer<1 || timer>4)
    	return -1;
  	if(prescale<0 || prescale>7)
    	return -1;
    
  	*GPTCONA = 0x0000;  /* disable ADC start from timer, disable timer compare output */
  	*GPTCONB = 0x0000;

  	switch(timer)
  	{
  		case 1:	*T1CON = (0x1040|(prescale<<8));
    			*T1CNT = 0x0000;
    			*T1PR = 0xFFFF;
    			timerasclock = 1;
    			break;

  		case 2: *T2CON = (0x1040|(prescale<<8));
    			*T2CNT = 0x0000;
    			*T2PR = 0xFFFF;
    			timerasclock = 2;
    			break;

 		case 3: *T3CON = (0x1040|(prescale<<8));
    			*T3CNT = 0x0000;
    			*T3PR = 0xFFFF;
    			timerasclock = 3;
    			break;

  		case 4: *T4CON = (0x1040|(prescale<<8));
    			*T4CNT = 0x0000;
    			*T4PR = 0xFFFF;
    			timerasclock = 4;
    			break;

  		default:
    			return -1; /* return -1 if not a valid timer */
 	}
  	clockprescale = ldexp(1,prescale);
  	secsperfine = (double)clockprescale/(double)CPUCLOCKFREQ;
  	secspercoarse = secsperfine * 0xFFFF;
  	coarsecount = 0;
  	updateClock();
  
  	return 0;
}
  
int updateClock(void)
{       
	switch(timerasclock)
  	{
  		case 1:	finecount = *T1CNT;
  				break;

		case 2: finecount = *T2CNT;
  				break;

  		case 3: finecount = *T3CNT;
  				break;

		case 4: finecount = *T4CNT;
				break;

  		default:
  				return -1;
	}
  	if(finecount < finecountlast)
  	{
    	coarsecount++;
	}
  	clocktime = (coarsecount * secspercoarse) + (finecount * secsperfine);
  	finecountlast = finecount;
}

⌨️ 快捷键说明

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