Page 289 - Programming Microcontrollers in C
P. 289
274 Chapter 5 Programming Large 8-Bit Systems
/* range of acceptable PWM_count is 1600 to 3300 */
long limit(long x)
{
if(x<COUNT_MIN)
return COUNT_MIN;
else if(x>COUNT_MAX)
return COUNT_MAX;
else
return x;
}
/* The asynchronous service portion of the program */
@port void IC1_Isr( void) /*the motor speed
measurement*/
{
static int i;
int j;
time2=TIC1;
TFLG1=IC1F; /* reset IC1 interrupt flag */
mparray[i]=time2-time1;
if (++i==COUNT_8)
{
i=0;
measured_period=0;
for(j=0;j<COUNT_8;j++)
measured_period += mparray[j];
measured_period >>= DIVIDE_8_SHIFT;
}
time1=time2;
TOC2=time2+5*measured_period/8; /*debounce time
5/8 revolution */
TMSK1.IC1I=OFF; /* disable IC1 interrupt */
TMSK1.OC2I=ON; /* enable OC2 interrupt */
}
@port void OC2_Isr(void) /* the debounce isr */
{
TFLG1=IC1F|OC2F; /* reset interrupt flags */