unsigned char iter=0;
unsigned char potval; // this will hold a value between 0 and 225
unsigned int cval=0;
#pragma vector = TIMER1_CAPT_vect
__interrupt void TIMER1_CAPT_ISR(void)
{
unsigned int tempval;
cval+=ICR1;
TCNT1=0x0000;
if (iter==0)
{
iter=1;
cval=0;
return;
}
iter++;
if (iter>9) // have we obtained 8 samples?
{
iter=1;
// value of 1 sample is around 3450-3900, but we will
// however, lets assume 3425-3925
// so the 4 sample value will be 13700-15700
// this is a span of 2000
if (cval>29400)
{
LED_ON;
}
else
{
LED_OFF;
}
tempval=cval-27400; // remove the offset
cval=0; // reset the sampled value
// we need a value of between 0 and 200
// if we divide by 8, this is a span of 250 which is good
tempval=tempval>>4;
potval=(unsigned char)tempval;
}
}
void
init(void)
{
// set up timer1
TCNT1=0x0000;
TCCR1B=0x01; // trigger on falling edge, and set prescaler to 1
}
int main( void )
{
unsigned char oldpotval=0;
unsigned int pllval;
init();
TCNT1=0x0000;
TIMSK1=0x20; // enable input capture interrupt
__enable_interrupt();
while(1)
{
// pot value is between 0 and 250
if (potval>125)
{
LED_ON;
}
else
{
LED_OFF;
}
} // end while
return 0; // expect a warning on this line, because we never return
}