Meinst du?
Hier noch die Implementation von timGet();
Ich habe übriegens noch die Variabeln volatile deklariert. Daran liegt es jedoch nicht (nur).
Code:
// includes --------------------------------------------------------------------
#include <normlib.h>
#include <interrupt.h>
#include "tim.h"
// local variables -------------------------------------------------------------
volatile static word overrun;
// global function implementation ----------------------------------------------
void timInit(){
overrun=0;
b_setH(TCCR1B,CS10); //activate timer, no prescaling
b_setH(TIMSK,TOIE1); //activate Overflow Interrupt
}
dword timGet(){
byte sregSav=SREG;
cli();
dword tmp=(((dword)overrun<<16)&0xFFFF0000)|(((dword)TCNT1)&0x0000FFFF);
SREG=sregSav;
return tmp;
}
dword timGetDifUp(dword *t0){
dword t1=timGet();
dword res;
if(t1<(*t0))
res=-(*t0)+t1;
else
res=t1-(*t0);
*t0=t1;
return res;
}
dword timGetDifNUp(dword t0){
dword t1=timGet();
if(t1<t0)
return -t0+t1;
return t1-t0;
}
byte timReached(dword t,dword t0){
if(timGetDifNUp(t0)>=t)
return TRUE;
return FALSE;
}
// interrupt routines ----------------------------------------------------------
ISR(TIMER1_OVF_vect){
overrun++;
}
// EOF -------------------------------------------------------------------------
Code:
/// includes --------------------------------------------------------------------
#include <normlib.h>
#include <interrupt.h>
#include "avr.h"
#include "tim.h"
#include "out.h"
#include "dataTable.h"
#include "data.h"
#include "freq.h"
// global variables ------------------------------------------------------------
byte freqPulsLedFlag;
byte freqEnable;
// local definitions -----------------------------------------------------------
// local variables -------------------------------------------------------------
static word actFreq;
static dword t0;
volatile static dword tI1; //timestamp of last puls
static dword dt; //delta t
static byte signal;
static byte preload;
static dword pLedTime;
// global function implementation ----------------------------------------------
void freqInit(){
TCCR0=BV(CS02)|BV(CS00); // activate Timer 0, prescaler /1024
preload=dataGetByte(DATATABLE_AC_TCNT0); //preload of Timer0
// Enable Analog Comparator, Enable Analog Comparator Interrupt
if(dataGetByte(DATATABLE_AC_EDGE))
ACSR=BV(ACIS0)|BV(ACIS1); //rising Edge
else
ACSR=BV(ACIS1); //falling Edge
cbi(ACSR,ACD); //Enable Analog Comparator
sbi(ACSR,ACIE); //Enable Analog Comparator Interrupt
actFreq =0;
signal =FALSE;
freqPulsLedFlag=TRUE;
freqEnable =TRUE;
pLedTime =(F_CPU/1000)*dataGetWord(DATATABLE_PULS_LED_FREQ);
}
word freqGet(){
return actFreq;
}
void freqUpdate(){
if(!freqEnable)
return;
cli();
dword t1=tI1;
sei();
if(t0!=t1){
signal=TRUE;
if(t1<t0)
dt=-t0+t1;
else
dt=t1-t0;
if(dt<((dword)(10*F_CPU/0xFFFF)))
actFreq=0xFFFF;
else
actFreq=(word)((dword)(10*F_CPU)/dt);
t0=t1;
if(freqPulsLedFlag)
outClear(OUT_PULS);
}
if(!signal){
actFreq=0;
return;
}
if(t0==t1){
dword t2=timGet();
dword dtN;
if(t2<t0)
dtN=-t0+t2;
else
dtN=t2-t0;
if(dtN>dt){
actFreq=(word)((dword)(10*F_CPU)/dtN);
}
}
if(timReached((dword)F_CPU,t0))
signal=FALSE;
if(freqPulsLedFlag&&timReached(pLedTime,t0))
outSet(OUT_PULS);
}
// interrupt routines ----------------------------------------------------------
ISR(ANA_COMP_vect){
tI1=timGet();
cbi(ACSR,ACIE); //disable Analog Comparator Interrupt
TCNT0=preload; //preload Timer0
sbi(TIMSK,TOIE0);//enable Timer 0 Interrupt
}
ISR(TIMER0_OVF_vect){
sbi(ACSR,ACI); //clear Analog Comparator Interrupt Flag
sbi(ACSR,ACIE); //enable Analog Comparator Interrupt
cbi(TIMSK,TOIE0);//disable Timer 0 Overflow Interrupt
}
// EOF -------------------------------------------------------------------------
Lesezeichen