This implements ICP (input capture) mode for timer1. The old, incomplete and non-functional ICP code has been reworked.
Signed-off-by: Onno Kortmann <[email protected]> --- src/at4433.cpp | 2 +- src/at8515.cpp | 2 +- src/hwtimer.cpp | 40 +++++++++++++++++++++------------------- src/hwtimer.h | 6 +++--- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/at4433.cpp b/src/at4433.cpp index 25656d0..d317c34 100644 --- a/src/at4433.cpp +++ b/src/at4433.cpp @@ -66,7 +66,7 @@ AvrDevice(64, 128, 0, 4*1024) { wado= new HWWado(this); prescaler = new HWPrescaler(this); timer0= new HWTimer0(this, prescaler, timer01irq, PinAtPort(portd, 4)); - timer1= new HWTimer1(this, prescaler, timer01irq, PinAtPort(portd, 5), PinAtPort(portb, 1), PinAtPort(portx, 0)); + timer1= new HWTimer1(this, prescaler, timer01irq, PinAtPort(portd, 5), PinAtPort(portb, 1), PinAtPort(portx, 0), PinAtPort(portx, 0)); extirq= new HWExtIrq( this, irqSystem, PinAtPort(portd, 2), PinAtPort(portd, 3), 1,2); mcucr= new HWMcucr(this); //, irqSystem, PinAtPort(portd, 2), PinAtPort(portd, 3)); diff --git a/src/at8515.cpp b/src/at8515.cpp index b319666..d78def8 100644 --- a/src/at8515.cpp +++ b/src/at8515.cpp @@ -53,7 +53,7 @@ AvrDevice(64, 512, 0xfda0, 8192) { wado= new HWWado(this); prescaler = new HWPrescaler(this); timer0= new HWTimer0(this, prescaler, timer01irq, PinAtPort(portb, 0)); - timer1= new HWTimer1(this, prescaler, timer01irq, PinAtPort(portb, 1), PinAtPort(portd, 5), PinAtPort(portx, 0)); + timer1= new HWTimer1(this, prescaler, timer01irq, PinAtPort(portb, 1), PinAtPort(portd, 5), PinAtPort(portx, 0), PinAtPort(portx, 0)); extirq= new HWExtIrq( this, irqSystem, PinAtPort(portd, 2), PinAtPort(portd, 3), 1,2); mcucr= new HWMcucr(this); //, irqSystem, PinAtPort(portd, 2), PinAtPort(portd, 3)); diff --git a/src/hwtimer.cpp b/src/hwtimer.cpp index 9ff3ca8..5fad5ef 100644 --- a/src/hwtimer.cpp +++ b/src/hwtimer.cpp @@ -116,8 +116,9 @@ unsigned int HWTimer0::CpuCycle(){ } -HWTimer1::HWTimer1(AvrDevice *c, HWPrescaler *p, HWTimer01Irq *s, PinAtPort t1, PinAtPort oca, PinAtPort ocb): -Hardware(c), core(c), pin_t1(t1), pin_oc1a(oca), pin_oc1b(ocb) { +HWTimer1::HWTimer1(AvrDevice *c, HWPrescaler *p, HWTimer01Irq *s, PinAtPort t1, PinAtPort oca, PinAtPort ocb, + PinAtPort icp): + Hardware(c), core(c), pin_t1(t1), pin_oc1a(oca), pin_oc1b(ocb), pin_icp(icp) { //c->AddToCycleList(this); cntDir=1; //start with upcounting prescaler=p, @@ -329,23 +330,24 @@ unsigned int HWTimer1::CpuCycle(){ } //input capture register - // - if ( icp!=icp_old) { //pin change for capture detected - inputCaptureNoiseCnt++; - if ( ((tccr1b&0x80)==0) || (inputCaptureNoiseCnt>=4)) { - inputCaptureNoiseCnt=0; - icp_old=icp; - - //Edge detection - if (( icp ^ (tccr1b&0x40)>>6)==1) { - //edge OK - timer01irq->AddFlagToTifr(0x08); //set ICF1 in TIFR - icr1=tcnt1; //Capture - - - } //end of edge detection - } // end of noise canceler - } // end of pin change + + // edge detection + if ((pin_icp != icp_old) && (pin_icp == ((tccr1b&0x40)==0x40))) + inputCaptureNoiseCnt=1; + // and noise cancelling + else if (inputCaptureNoiseCnt && (pin_icp == ((tccr1b&0x40)==0x40))) + inputCaptureNoiseCnt+=1; + else + inputCaptureNoiseCnt=0; + + if (inputCaptureNoiseCnt>3*((tccr1b&0x80)==0x80)) { + // captured!! + inputCaptureNoiseCnt=0; + timer01irq->AddFlagToTifr(0x08); //set ICF1 in TIFR + icr1=tcnt1; //Capture + } + + icp_old=pin_icp; return 0; } diff --git a/src/hwtimer.h b/src/hwtimer.h index ff8cb0e..c6843cf 100644 --- a/src/hwtimer.h +++ b/src/hwtimer.h @@ -120,10 +120,10 @@ class HWTimer1 : public Hardware { PinAtPort pin_t1; PinAtPort pin_oc1a; PinAtPort pin_oc1b; + PinAtPort pin_icp; - bool icp; //input capture bool icp_old; - + bool last_ocr1a; bool last_ocr1b; unsigned char inputCaptureNoiseCnt; //count for 4 cycles if set in ICNC1 @@ -145,7 +145,7 @@ class HWTimer1 : public Hardware { void TimerCompareAfterCount(); public: - HWTimer1(AvrDevice *core, HWPrescaler *p, HWTimer01Irq *s, PinAtPort t1, PinAtPort oca, PinAtPort ocb); + HWTimer1(AvrDevice *core, HWPrescaler *p, HWTimer01Irq *s, PinAtPort t1, PinAtPort oca, PinAtPort ocb, PinAtPort pin_icp); void Reset() { -- 1.5.6.5 _______________________________________________ Simulavr-devel mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/simulavr-devel
