?? funclock.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 + -