Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
Pulsein Scratch , Pind , 4 , 1
// Estimate pulse width using timer2void pinChangeIntrSetup(void){ short index; // Set up pin change monitor for pins 17(FLIP), 20(RIGHT), 21(LEFT) PCMSK2 = 0; PCMSK2 |= (1<<PCINT17); // Set pin change mask for FLIP PCMSK2 |= (1<<PCINT20); // set pin change mask for RIGHT PCMSK2 |= (1<<PCINT21); // Set pin change mask for LEFT for(index=0; index<3; index++){ pulseWidth[index] = 0; inStatus[index] = 0; } PCICR = (1<<PCIE2); // Enable pin change interrupt}// interrupt for detecting pin state changesISR (PCINT2_vect){ if(FLIP_IS_LOW) // Negative egde transition -- end of pulse inStatus[FLIPIN] = DVALID; else { // Positive edge transition (start of pulse) -- init pulse width inStatus[FLIPIN] = MACTIVE; pulseWidth[FLIPIN] = 0; } // we only care if there was a change from 0 to 1 if(RIGHT_IS_LOW) inStatus[RIGHTIN] = DVALID; else { inStatus[RIGHTIN] = MACTIVE; pulseWidth[RIGHTIN] = 0; } // we only care if there was a change from 0 to 1 if(LEFT_IS_LOW) { inStatus[LEFTIN] = DVALID; } else { inStatus[LEFTIN] = MACTIVE; pulseWidth[LEFTIN] = 0; } // PCIFR &= (1 << PCIF2); // Done automatically when ISR is executed}#define TIMER2_PERIOD (256-78) // Full count yields 128*78 = 9984usec (9.984msec)ISR (TIMER2_OVF_vect){ TCNT2 = TIMER2_PERIOD; // start clock at t=0 if(inStatus[FLIPIN] & MACTIVE) pulseWidth[FLIPIN]++; if(inStatus[RIGHTIN] & MACTIVE) pulseWidth[RIGHTIN]++; if(inStatus[LEFTIN] & MACTIVE) pulseWidth[LEFTIN]++; // TIFR2 &= (1 << TOV2); // Done automatically when ISR is executed}// Setup Timer 2 to generate an interrupt every 32usec#define TIMER2_CONFIG ((1 << CS20) | (1 << CS21) | (1 << CS22)) // Prescalor = 1024 --> resolution = 128usecvoid timer2Setup(void){ TCCR2B = 0; TCNT2 = TIMER2_PERIOD; // Start clock at t=0 TCCR2B |= (1 << CS22); // Prescale clock at 256 TIMSK2 |= (1 << TOIE2); // Enable check for overflow of timer/counter 2}
//Axoncbi(DDRG, PG5); //set G5 as input pinreset_timer_0();//set timer to zerowhile(bit_is_clear(PING, 5)); //wait while pin is lowelapsed_time=timer0GetOverflowCount()*255+TCNT0;//store timer value
//$50 Robot with ATmega168cbi(DDRC, PC5); //set C5 as input pinreset_timer();//set timer to zerowhile(bit_is_clear(PINC, 5)); //wait while pin is lowelapsed_time=timer0GetOverflowCount()*255+TCNT0;//store timer value
//Axoncbi(DDRG, PG5); //set G5 as input pinwhile(PORT_IS_OFF(PING, 5)); //wait till pin goes highreset_timer_0();//set timer to zerowhile(PORT_IS_ON(PING, 5)); //wait till pin goes lowelapsed_time=timer0GetOverflowCount()*255+TCNT0;//store timer value