hello,
I am trying to use TimerB with two match registers.
match register 1 for a periodic interrupt, and match register 2 to make an accurate pulse on port PE7.
this is the code:
nodebug root interrupt timerb_isrRF()
{
#asm
;timerb_isrRF::
push af ; save registers
push hl
push de
#endasm
isrRegVal=RdPortI(TBCSR); // read the interrupt register to check wich match register invoked the interrupt.
if(((isrRegVal>>2)&0x01) == 1) // if its match register 2 (PWM interrupt)
{
if( periodicFlag == 1 )
{
WrPortI(PEDR, &PEDRShadow,0); // load output value for next match.
mrval2=240;
WrPortI(TBL2R, NULL, (char)mrval2);
WrPortI(TBM2R, NULL, (char)(mrval2>>2)); // set initial match for next time omnt match 2.
periodicFlag=0;
}
}
if(((isrRegVal>>1)&0x01) == 1) // if its match register 1 (periodic interrupt)
{
TimerBvalue=TimerBvalue+TIMER_B_VAL;
if(TimerBvalue>1024)
TimerBvalue=TimerBvalue-1024;
WrPortI(TBL1R, NULL, (char)TimerBvalue);
WrPortI(TBM1R, NULL, (char)(TimerBvalue>>2)); // set initial match for next time omnt match 2.
msec100Counter++;
if( msec100Counter >= 1000 )
{
periodicFlag=1;
WrPortI(PEDR, &PEDRShadow,0x80); // load output value for next match.
mrval2=200;
WrPortI(TBL2R, NULL, (char)mrval2);
WrPortI(TBM2R, NULL, (char)(mrval2>>2)); // set initial match for next time omnt match 2.
msec100Counter=0;
}
}
#asm
pop de ; restore registers
pop hl
pop af
ipres ; restore interrupts
ret
#endasm
}
this code should make a periodic interrupt every 1 msec. this is working ok.
in the poeriodic interrupt the code init the value of PE7 for 1 and set the match register 2 for 200. so when it will arrive 200 it will take PE7 to high.
when this happened the interrupt inviked again and set the match register 2 to 240 and init the PE7 to low so when it will arrive to 240 it will take PE7 to low.
the ports are initialized like this:
// ====== port E =============================================================
WrPortI(PEFR,&PEFRShadow,0x00); // set PE as standard mode.
WrPortI(PEDDR,&PEDDRShadow,0x80); // set PE7 as output .
// set upper nibble of port E to clock out on B1 match!
WrPortI(PECR, &PECRShadow, 0x30); // upper nibble clock transfer is timerB2
WrPortI(TBL1R, NULL, 0x00);
WrPortI(TBM1R, NULL, 0x00); // set initial match
WrPortI(PEB7R, NULL, 0x80); // set initial output value
the problem is that the port PE7 is jiterring… but it should be very accurate.
anybody can see if I am wrong here?
thanks.
if you have questions please ask and I will give more details.